Ejemplo n.º 1
0
        public async Task HandleAsync_GivenMessageContainingUpdatedAddress_AddressIsUpdatedCorrectly()
        {
            var data          = CreateAatfData(out var competentAuthority);
            var updateRequest = fixture.Build <EditAatfDetails>().With(e => e.Data, data).Create();
            var siteAddress   = fixture.Create <AatfAddress>();

            A.CallTo(() => addressMapper.Map(data.SiteAddress)).Returns(siteAddress);

            var aatf = A.Fake <Aatf>();

            A.CallTo(() => genericDataAccess.GetById <Aatf>(updateRequest.Data.Id)).Returns(aatf);

            var result = await handler.HandleAsync(updateRequest);

            Assert.True(result);

            A.CallTo(() => aatfDataAccess.UpdateAddress(A <AatfAddress> ._, siteAddress, A <Country> ._)).MustHaveHappenedOnceExactly();
        }
        public async Task <bool> HandleAsync(EditAatfDetails message)
        {
            authorization.EnsureCanAccessInternalArea();
            authorization.EnsureUserInRole(Roles.InternalAdmin);

            using (var transaction = context.BeginTransaction())
            {
                try
                {
                    var updatedAddress = addressMapper.Map(message.Data.SiteAddress);

                    var existingAatf = await genericDataAccess.GetById <Aatf>(message.Data.Id);

                    var competentAuthority = await commonDataAccess.FetchCompetentAuthority(message.Data.CompetentAuthority.Abbreviation);

                    LocalArea localArea = null;
                    PanArea   panArea   = null;

                    if (message.Data.LocalAreaDataId.HasValue)
                    {
                        localArea = await commonDataAccess.FetchLookup <LocalArea>(message.Data.LocalAreaDataId.Value);
                    }

                    if (message.Data.PanAreaDataId.HasValue)
                    {
                        panArea = await commonDataAccess.FetchLookup <PanArea>(message.Data.PanAreaDataId.Value);
                    }

                    var updatedAatf = new Aatf(
                        message.Data.Name,
                        competentAuthority,
                        message.Data.ApprovalNumber,
                        Enumeration.FromValue <Domain.AatfReturn.AatfStatus>(message.Data.AatfStatusValue),
                        existingAatf.Organisation,
                        updatedAddress,
                        Enumeration.FromValue <Domain.AatfReturn.AatfSize>(message.Data.AatfSizeValue),
                        message.Data.ApprovalDate.GetValueOrDefault(),
                        existingAatf.Contact,
                        existingAatf.FacilityType,
                        existingAatf.ComplianceYear,
                        localArea,
                        panArea);

                    var existingAddress = await genericDataAccess.GetById <AatfAddress>(existingAatf.SiteAddress.Id);

                    var country = await organisationDetailsDataAccess.FetchCountryAsync(message.Data.SiteAddress.CountryId);

                    await aatfDataAccess.UpdateAddress(existingAddress, updatedAddress, country);

                    if (message.Data.ApprovalDate.HasValue && existingAatf.ApprovalDate.HasValue)
                    {
                        var flags = await getAatfApprovalDateChangeStatus.Validate(existingAatf, message.Data.ApprovalDate.Value);

                        if (flags.HasFlag(CanApprovalDateBeChangedFlags.DateChanged))
                        {
                            var existingQuarter = await quarterWindowFactory.GetAnnualQuarterForDate(existingAatf.ApprovalDate.Value);

                            var newQuarter = await quarterWindowFactory.GetAnnualQuarterForDate(message.Data.ApprovalDate.Value);

                            var range = Enumerable.Range((int)existingQuarter, (int)newQuarter - 1);

                            await aatfDataAccess.RemoveAatfData(existingAatf, range);
                        }
                    }

                    await aatfDataAccess.UpdateDetails(existingAatf, updatedAatf);

                    context.Commit(transaction);
                }
                catch (Exception ex)
                {
                    transaction.Rollback();

                    if (ex.InnerException != null)
                    {
                        throw ex.InnerException;
                    }
                }
                finally
                {
                    context.Dispose(transaction);
                }
            }

            return(true);
        }