Beispiel #1
0
        public async Task <ManagePendingCharges> HandleAsync(Requests.Charges.FetchPendingCharges message)
        {
            authorization.EnsureCanAccessInternalArea(true);

            UKCompetentAuthority authority = await dataAccess.FetchCompetentAuthority(message.Authority);

            IReadOnlyList <MemberUpload> memberUploads = await dataAccess.FetchSubmittedNonInvoicedMemberUploadsAsync(authority);

            var groups = memberUploads.GroupBy(mu => new { mu.Scheme, mu.ComplianceYear });

            List <PendingCharge> pendingCharges = new List <PendingCharge>();

            foreach (var group in groups)
            {
                PendingCharge pendingCharge = new PendingCharge()
                {
                    SchemeName           = group.Key.Scheme.SchemeName,
                    SchemeApprovalNumber = group.Key.Scheme.ApprovalNumber,
                    ComplianceYear       = group.Key.ComplianceYear.Value,
                    TotalGBP             = group.Sum(p => p.TotalCharges),
                    SubmittedDate        = group.FirstOrDefault().SubmittedDate.Value
                };

                pendingCharges.Add(pendingCharge);
            }

            var managePendingCharges = new ManagePendingCharges
            {
                PendingCharges      = pendingCharges,
                CanUserIssueCharges = authorization.CheckUserInRole(Roles.InternalAdmin)
            };

            return(managePendingCharges);
        }
Beispiel #2
0
        public async Task HandleAsync_GivenMessage_DetailsAreUpdatedCorrectly()
        {
            var data                     = CreateAatfData(out var competentAuthority);
            var updateRequest            = fixture.Build <EditAatfDetails>().With(e => e.Data, data).Create();
            var siteAddress              = fixture.Create <AatfAddress>();
            var aatf                     = A.Fake <Aatf>();
            var competentAuthorityDomain = A.Fake <UKCompetentAuthority>();
            var localAreaDomain          = A.Fake <LocalArea>();
            var panAreaDomain            = A.Fake <PanArea>();

            A.CallTo(() => addressMapper.Map(data.SiteAddress)).Returns(siteAddress);
            A.CallTo(() => aatf.ComplianceYear).Returns((Int16)2019);
            A.CallTo(() => genericDataAccess.GetById <Aatf>(updateRequest.Data.Id)).Returns(aatf);
            A.CallTo(() => commonDataAccess.FetchCompetentAuthority(updateRequest.Data.CompetentAuthority.Abbreviation)).Returns(competentAuthorityDomain);
            A.CallTo(() => commonDataAccess.FetchLookup <LocalArea>(updateRequest.Data.LocalAreaDataId.Value)).Returns(localAreaDomain);
            A.CallTo(() => commonDataAccess.FetchLookup <PanArea>(updateRequest.Data.PanAreaDataId.Value)).Returns(panAreaDomain);

            var result = await handler.HandleAsync(updateRequest);

            A.CallTo(() => aatfDataAccess.UpdateDetails(aatf, A <Aatf> .That.Matches(a => a.Name == data.Name &&
                                                                                     a.CompetentAuthority.Equals(competentAuthorityDomain) &&
                                                                                     a.LocalArea.Equals(localAreaDomain) &&
                                                                                     a.PanArea.Equals(panAreaDomain) &&
                                                                                     a.ApprovalNumber == data.ApprovalNumber &&
                                                                                     a.AatfStatus == Domain.AatfReturn.AatfStatus.Approved &&
                                                                                     a.Size == Domain.AatfReturn.AatfSize.Large &&
                                                                                     a.ApprovalDate == data.ApprovalDate.GetValueOrDefault() &&
                                                                                     a.ComplianceYear == aatf.ComplianceYear))).MustHaveHappenedOnceExactly();
        }
Beispiel #3
0
        public async Task HandleAsync_ValidInput_AddsAatf(Core.AatfReturn.FacilityType facilityType)
        {
            var competentAuthority = A.Fake <UKCompetentAuthority>();
            var localarea          = A.Fake <LocalArea>();
            var panarea            = A.Fake <PanArea>();

            var aatf = new AatfData(Guid.NewGuid(), "name", "approval number", 2019, A.Dummy <Core.Shared.UKCompetentAuthorityData>(),
                                    Core.AatfReturn.AatfStatus.Approved, A.Dummy <AatfAddressData>(), Core.AatfReturn.AatfSize.Large, DateTime.Now,
                                    A.Dummy <Core.Shared.PanAreaData>(), A.Dummy <Core.Admin.LocalAreaData>())
            {
                FacilityType = facilityType
            };

            var aatfId = Guid.NewGuid();

            var request = new AddAatf()
            {
                Aatf           = aatf,
                AatfContact    = A.Dummy <AatfContactData>(),
                OrganisationId = Guid.NewGuid()
            };

            var expectedFacilityType = facilityType.ToDomainEnumeration <Domain.AatfReturn.FacilityType>();

            A.CallTo(() => commonDataAccess.FetchCompetentAuthority(aatf.CompetentAuthority.Abbreviation)).Returns(competentAuthority);
            A.CallTo(() => commonDataAccess.FetchLookup <LocalArea>(aatf.LocalAreaData.Id)).Returns(localarea);
            A.CallTo(() => commonDataAccess.FetchLookup <PanArea>(aatf.PanAreaData.Id)).Returns(panarea);

            var result = await handler.HandleAsync(request);

            A.CallTo(() => dataAccess.Add <Domain.AatfReturn.Aatf>(A <Domain.AatfReturn.Aatf> .That.Matches(
                                                                       c => c.Name == aatf.Name &&
                                                                       c.ApprovalNumber == aatf.ApprovalNumber &&
                                                                       c.CompetentAuthority.Equals(competentAuthority) &&
                                                                       c.LocalArea.Equals(localarea) &&
                                                                       c.PanArea.Equals(panarea) &&
                                                                       c.Name == aatf.Name &&
                                                                       c.SiteAddress.Id == aatf.SiteAddress.Id &&
                                                                       Enumeration.FromValue <Domain.AatfReturn.AatfSize>(c.Size.Value) == Enumeration.FromValue <Domain.AatfReturn.AatfSize>(aatf.Size.Value) &&
                                                                       Enumeration.FromValue <Domain.AatfReturn.AatfStatus>(c.AatfStatus.Value) == Enumeration.FromValue <Domain.AatfReturn.AatfStatus>(aatf.AatfStatus.Value) &&
                                                                       c.ApprovalDate == aatf.ApprovalDate &&
                                                                       c.ComplianceYear == aatf.ComplianceYear &&
                                                                       c.FacilityType == expectedFacilityType))).MustHaveHappened(Repeated.Exactly.Once);

            result.Should().Be(true);
        }
        public async Task <IReadOnlyList <SchemeData> > HandleAsync(Requests.Charges.FetchSchemesWithInvoices message)
        {
            authorization.EnsureCanAccessInternalArea();

            UKCompetentAuthority authority = await dataAccess.FetchCompetentAuthority(message.Authority);

            IEnumerable <MemberUpload> invoicedMemberUploads = await dataAccess.FetchInvoicedMemberUploadsAsync(authority);

            return(invoicedMemberUploads
                   .GroupBy(s => new { s.Scheme.Id, s.Scheme.SchemeName, s.Scheme })
                   .Select(s => schemeMap.Map(s.Key.Scheme))
                   .OrderBy(x => x.SchemeName).ToList());
        }
        public async Task <IReadOnlyList <int> > HandleAsync(Requests.Charges.FetchComplianceYearsWithInvoices message)
        {
            authorization.EnsureCanAccessInternalArea();

            UKCompetentAuthority authority = await dataAccess.FetchCompetentAuthority(message.Authority);

            IEnumerable <MemberUpload> invoicedMemberUploads = await dataAccess.FetchInvoicedMemberUploadsAsync(authority);

            return(invoicedMemberUploads
                   .Select(mu => mu.ComplianceYear.Value)
                   .Distinct()
                   .OrderByDescending(x => x)
                   .ToList());
        }
        public async Task <bool> HandleAsync(AddAatf message)
        {
            authorization.EnsureCanAccessInternalArea();
            authorization.EnsureUserInRole(Roles.InternalAdmin);

            var siteAddress = addressMapper.Map(message.Aatf.SiteAddress);

            var organisation = await dataAccess.GetById <Organisation>(message.OrganisationId);

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

            var contact = contactMapper.Map(message.AatfContact);

            LocalArea localArea = null;

            PanArea panArea = null;

            if (message.Aatf.LocalAreaData != null)
            {
                localArea = await commonDataAccess.FetchLookup <LocalArea>(message.Aatf.LocalAreaData.Id);
            }

            if (message.Aatf.PanAreaData != null)
            {
                panArea = await commonDataAccess.FetchLookup <PanArea>(message.Aatf.PanAreaData.Id);
            }

            var aatf = new Domain.AatfReturn.Aatf(
                message.Aatf.Name,
                competentAuthority,
                message.Aatf.ApprovalNumber,
                Enumeration.FromValue <Domain.AatfReturn.AatfStatus>(message.Aatf.AatfStatus.Value),
                organisation,
                siteAddress,
                Enumeration.FromValue <Domain.AatfReturn.AatfSize>(message.Aatf.Size.Value),
                message.Aatf.ApprovalDate.GetValueOrDefault(),
                contact,
                message.Aatf.FacilityType.ToDomainEnumeration <Domain.AatfReturn.FacilityType>(),
                message.Aatf.ComplianceYear,
                localArea,
                panArea, message.AatfId);

            await dataAccess.Add <Domain.AatfReturn.Aatf>(aatf);

            return(true);
        }
Beispiel #7
0
        public async Task GetAllComplianceYearsHandler_ReturnsYearsInDescendingOrder()
        {
            // Arrange
            UKCompetentAuthority competentAuthority =
                new UKCompetentAuthority(Guid.NewGuid(), "Environment Agency", "EA", new Country(Guid.NewGuid(), "UK - England"), "*****@*****.**", 0);
            var scheme1 = A.Fake <Scheme>();

            A.CallTo(() => scheme1.SchemeName).Returns("Test1");
            A.CallTo(() => scheme1.CompetentAuthority).Returns(competentAuthority);
            var scheme2 = A.Fake <Scheme>();

            A.CallTo(() => scheme2.SchemeName).Returns("Test2");
            A.CallTo(() => scheme2.CompetentAuthority).Returns(competentAuthority);
            var scheme3 = A.Fake <Scheme>();

            A.CallTo(() => scheme3.SchemeName).Returns("Test3");
            A.CallTo(() => scheme3.CompetentAuthority).Returns(competentAuthority);

            InvoiceRun invoice = A.Fake <InvoiceRun>();

            var memberUpload1 = A.Fake <MemberUpload>();

            A.CallTo(() => memberUpload1.ComplianceYear).Returns(2015);
            A.CallTo(() => memberUpload1.Scheme).Returns(scheme1);
            A.CallTo(() => memberUpload1.InvoiceRun).Returns(invoice);

            var memberUpload2 = A.Fake <MemberUpload>();

            A.CallTo(() => memberUpload2.ComplianceYear).Returns(2017);
            A.CallTo(() => memberUpload2.Scheme).Returns(scheme2);
            A.CallTo(() => memberUpload2.InvoiceRun).Returns(invoice);

            var memberUpload3 = A.Fake <MemberUpload>();

            A.CallTo(() => memberUpload3.ComplianceYear).Returns(2016);
            A.CallTo(() => memberUpload3.Scheme).Returns(scheme3);
            A.CallTo(() => memberUpload3.InvoiceRun).Returns(invoice);

            ICommonDataAccess dataAccess = A.Fake <ICommonDataAccess>();

            A.CallTo(() => dataAccess.FetchInvoicedMemberUploadsAsync(competentAuthority))
            .Returns(new List <MemberUpload>()
            {
                memberUpload1,
                memberUpload2,
                memberUpload3
            });
            IWeeeAuthorization authorization = AuthorizationBuilder.CreateUserWithAllRights();

            FetchComplianceYearsWithInvoicesHandler handler = new FetchComplianceYearsWithInvoicesHandler(authorization, dataAccess);

            Requests.Charges.FetchComplianceYearsWithInvoices request = new Requests.Charges.FetchComplianceYearsWithInvoices(CompetentAuthority.England);
            A.CallTo(() => dataAccess.FetchCompetentAuthority(CompetentAuthority.England)).Returns(competentAuthority);

            // Act
            var yearsList = await handler.HandleAsync(request);

            Assert.NotNull(yearsList);
            Assert.Equal(3, yearsList.Count);
            Assert.Collection(yearsList,
                              r1 => Assert.Equal("2017", r1.ToString()),
                              r2 => Assert.Equal("2016", r2.ToString()),
                              r3 => Assert.Equal("2015", r3.ToString()));
        }
        public async Task GetAllInvoicedSchemesHandler_ReturnsSchemes()
        {
            // Arrange
            UKCompetentAuthority competentAuthority =
                new UKCompetentAuthority(Guid.NewGuid(), "Environment Agency", "EA", new Country(Guid.NewGuid(), "UK - England"), "*****@*****.**", 0);
            var scheme1 = A.Fake <Scheme>();

            A.CallTo(() => scheme1.CompetentAuthority).Returns(competentAuthority);
            var scheme2 = A.Fake <Scheme>();

            A.CallTo(() => scheme2.CompetentAuthority).Returns(competentAuthority);
            var scheme3 = A.Fake <Scheme>();

            A.CallTo(() => scheme3.CompetentAuthority).Returns(competentAuthority);

            InvoiceRun invoice = A.Fake <InvoiceRun>();

            var memberUpload1 = A.Fake <MemberUpload>();

            A.CallTo(() => memberUpload1.ComplianceYear).Returns(2015);
            A.CallTo(() => memberUpload1.Scheme).Returns(scheme1);
            A.CallTo(() => memberUpload1.InvoiceRun).Returns(invoice);

            var memberUpload2 = A.Fake <MemberUpload>();

            A.CallTo(() => memberUpload2.ComplianceYear).Returns(2017);
            A.CallTo(() => memberUpload2.Scheme).Returns(scheme2);
            A.CallTo(() => memberUpload2.InvoiceRun).Returns(invoice);

            var memberUpload3 = A.Fake <MemberUpload>();

            A.CallTo(() => memberUpload3.ComplianceYear).Returns(2016);
            A.CallTo(() => memberUpload3.Scheme).Returns(scheme3);
            A.CallTo(() => memberUpload3.InvoiceRun).Returns(invoice);

            var schemeData1 = A.Fake <SchemeData>();
            var schemeData2 = A.Fake <SchemeData>();
            var schemeData3 = A.Fake <SchemeData>();

            ICommonDataAccess dataAccess = A.Fake <ICommonDataAccess>();

            A.CallTo(() => dataAccess.FetchInvoicedMemberUploadsAsync(competentAuthority))
            .Returns(new List <MemberUpload>()
            {
                memberUpload1,
                memberUpload2,
                memberUpload3
            });

            IWeeeAuthorization authorization = AuthorizationBuilder.CreateUserWithAllRights();
            var schemeMap = A.Fake <IMap <Scheme, SchemeData> >();

            A.CallTo(() => schemeMap.Map(memberUpload1.Scheme)).Returns(schemeData1);
            A.CallTo(() => schemeMap.Map(memberUpload2.Scheme)).Returns(schemeData2);
            A.CallTo(() => schemeMap.Map(memberUpload3.Scheme)).Returns(schemeData3);

            FetchSchemesWithInvoicesHandler handler = new FetchSchemesWithInvoicesHandler(
                authorization,
                dataAccess,
                schemeMap);

            FetchSchemesWithInvoices request = new FetchSchemesWithInvoices(CompetentAuthority.England);

            A.CallTo(() => dataAccess.FetchCompetentAuthority(CompetentAuthority.England)).Returns(competentAuthority);
            // Act
            var schemesList = await handler.HandleAsync(request);

            //Assert
            Assert.NotNull(schemesList);
            Assert.Equal(3, schemesList.Count);
            Assert.Collection(schemesList,
                              r1 => Assert.Equal(r1, schemeData1),
                              r2 => Assert.Equal(r2, schemeData2),
                              r3 => Assert.Equal(r3, schemeData3));
        }
        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);
        }