public async Task HandleAsync_WithNoRecordForRegistrationNumberAndComplianceYear_ThrowsArgumentException()
        {
            // Arrange
            IGetProducerDetailsDataAccess dataAccess = A.Fake <IGetProducerDetailsDataAccess>();

            A.CallTo(() => dataAccess.Fetch("WEE/AA1111AA", 2016))
            .Returns(new List <ProducerSubmission>());

            IWeeeAuthorization authorization = AuthorizationBuilder.CreateUserWithAllRights();
            IMapper            mapper        = A.Fake <IMapper>();

            GetProducerDetailsHandler handler = new GetProducerDetailsHandler(dataAccess, authorization, mapper);

            Requests.Admin.GetProducerDetails request = new Requests.Admin.GetProducerDetails()
            {
                RegistrationNumber = "WEE/AA1111AA",
                ComplianceYear     = 2016
            };

            // Act
            Func <Task <ProducerDetails> > action = async() => await handler.HandleAsync(request);

            // Assert
            await Assert.ThrowsAsync <ArgumentException>(action);
        }
Exemple #2
0
        public async Task <ProducerDetails> HandleAsync(Requests.Admin.GetProducerDetails request)
        {
            authorization.EnsureCanAccessInternalArea();

            List <ProducerSubmission> producers = await dataAccess.Fetch(request.RegistrationNumber, request.ComplianceYear);

            if (producers.Count == 0)
            {
                string message = string.Format(
                    "No producer has been registered with the registration number \"{0}\" in the {1} compliance year.",
                    request.RegistrationNumber, request.ComplianceYear);

                throw new ArgumentException(message);
            }

            ProducerDetails producerDetails = new ProducerDetails();

            producerDetails.CanRemoveProducer = authorization.CheckUserInRole(Roles.InternalAdmin);
            producerDetails.Schemes           = new List <ProducerDetailsScheme>();

            var schemeGroups = producers
                               .GroupBy(p => new { p.MemberUpload.Scheme.Id, p.MemberUpload.Scheme.SchemeName })
                               .OrderBy(p => p.Key.SchemeName);

            foreach (var schemeGroup in schemeGroups)
            {
                DateTime registrationDate = schemeGroup
                                            .OrderBy(p => p.UpdatedDate)
                                            .First()
                                            .UpdatedDate;

                ProducerSubmission latestDetails = schemeGroup
                                                   .OrderBy(p => p.UpdatedDate)
                                                   .Last();

                string companyNumber = null;
                if (latestDetails.ProducerBusiness.CompanyDetails != null)
                {
                    companyNumber = latestDetails.ProducerBusiness.CompanyDetails.CompanyNumber;
                }

                var isAuthorisedRepresentative = "No";

                if (latestDetails.AuthorisedRepresentativeId != null &&
                    latestDetails.AuthorisedRepresentative.OverseasContactId != null &&
                    latestDetails.AuthorisedRepresentative.OverseasContact != null &&
                    !string.IsNullOrEmpty(latestDetails.AuthorisedRepresentative.OverseasContact.Email))
                {
                    isAuthorisedRepresentative = "Yes";
                }

                ProducerContact producerBusinessContact = null;
                bool            isCompany = false;
                if (latestDetails.ProducerBusiness.CompanyDetails != null)
                {
                    producerBusinessContact = latestDetails.ProducerBusiness.CompanyDetails.RegisteredOfficeContact;
                    isCompany = true;
                }
                else if (latestDetails.ProducerBusiness.Partnership != null)
                {
                    producerBusinessContact = latestDetails.ProducerBusiness.Partnership.PrincipalPlaceOfBusiness;
                }
                else
                {
                    throw new MissingFieldException(
                              string.Format("No producer business details could be found for producer:{0}", latestDetails.Id));
                }

                ProducerContactDetails producerBusinessContactDetails = new ProducerContactDetails
                {
                    ContactName = producerBusinessContact.ContactName,
                    Email       = producerBusinessContact.Email,
                    Mobile      = producerBusinessContact.Mobile,
                    Telephone   = producerBusinessContact.Telephone,
                    Address     = producerBusinessContact.Address.ToString()
                };

                ProducerDetailsScheme producerSchemeDetails = new ProducerDetailsScheme()
                {
                    RegisteredProducerId       = latestDetails.RegisteredProducer.Id,
                    SchemeName                 = schemeGroup.Key.SchemeName,
                    ProducerName               = latestDetails.OrganisationName,
                    TradingName                = latestDetails.TradingName,
                    RegistrationDate           = registrationDate,
                    CompanyNumber              = companyNumber,
                    ObligationType             = (ObligationType)latestDetails.ObligationType,
                    ChargeBandType             = (ChargeBandType)latestDetails.ChargeBandAmount.ChargeBand,
                    CeasedToExist              = latestDetails.CeaseToExist,
                    IsAuthorisedRepresentative = isAuthorisedRepresentative,
                    ProducerBusinessContact    = producerBusinessContactDetails,
                    IsCompany          = isCompany,
                    ProducerEeeDetails =
                        mapper.Map <IEnumerable <ProducerEeeByQuarter>, ProducerEeeDetails>(
                            (await
                             dataAccess.EeeOutputBySchemeAndComplianceYear(request.RegistrationNumber, request.ComplianceYear,
                                                                           schemeGroup.Key.Id)))
                };

                producerDetails.Schemes.Add(producerSchemeDetails);
            }

            return(producerDetails);
        }
        public async Task HandleAsync_ProducerRegisteredTwiceIn2017ForSameScheme_ReturnsLatestProducerDetailsWithFirstRegistrationDate()
        {
            // Arrange
            var scheme = A.Fake <EA.Weee.Domain.Scheme.Scheme>();

            A.CallTo(() => scheme.SchemeName).Returns("Scheme Name");

            var memberUpload1 = A.Fake <EA.Weee.Domain.Scheme.MemberUpload>();

            A.CallTo(() => memberUpload1.ComplianceYear).Returns(2017);
            A.CallTo(() => memberUpload1.Scheme).Returns(scheme);

            var producer1 = new EA.Weee.Domain.Producer.ProducerSubmission(
                new Domain.Producer.RegisteredProducer("WEE/AA1111AA", 2017, scheme),
                memberUpload1,
                new EA.Weee.Domain.Producer.ProducerBusiness(),
                null,
                new DateTime(2015, 1, 1),
                0,
                false,
                null,
                "Trading Name 1",
                EEEPlacedOnMarketBandType.Lessthan5TEEEplacedonmarket,
                SellingTechniqueType.Both,
                Domain.Obligation.ObligationType.Both,
                AnnualTurnOverBandType.Greaterthanonemillionpounds,
                new List <Domain.Producer.BrandName>(),
                new List <Domain.Producer.SICCode>(),
                A.Dummy <ChargeBandAmount>(),
                0,
                A.Dummy <StatusType>());

            var memberUpload2 = A.Fake <EA.Weee.Domain.Scheme.MemberUpload>();

            A.CallTo(() => memberUpload2.ComplianceYear).Returns(2017);
            A.CallTo(() => memberUpload2.Scheme).Returns(scheme);

            var producer2 = new EA.Weee.Domain.Producer.ProducerSubmission(
                new Domain.Producer.RegisteredProducer("WEE/AA1111AA", 2017, scheme),
                memberUpload2,
                CreatePartnershipBusiness(),
                null,
                new DateTime(2015, 1, 2),
                0,
                false,
                null,
                "Trading Name 2",
                EEEPlacedOnMarketBandType.Lessthan5TEEEplacedonmarket,
                SellingTechniqueType.Both,
                Domain.Obligation.ObligationType.Both,
                AnnualTurnOverBandType.Greaterthanonemillionpounds,
                new List <Domain.Producer.BrandName>(),
                new List <Domain.Producer.SICCode>(),
                A.Dummy <ChargeBandAmount>(),
                0,
                A.Dummy <StatusType>());

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

            A.CallTo(() => dataAccess.Fetch("WEE/AA1111AA", 2017)).Returns(new List <Domain.Producer.ProducerSubmission>()
            {
                producer1,
                producer2
            });

            IWeeeAuthorization authorization = AuthorizationBuilder.CreateUserWithAllRights();

            IMapper mapper = A.Fake <IMapper>();

            GetProducerDetailsHandler handler = new GetProducerDetailsHandler(dataAccess, authorization, mapper);

            Requests.Admin.GetProducerDetails request = new Requests.Admin.GetProducerDetails()
            {
                RegistrationNumber = "WEE/AA1111AA",
                ComplianceYear     = 2017
            };

            // Act
            ProducerDetails result = await handler.HandleAsync(request);

            // Assert
            Assert.NotNull(result);
            Assert.Equal("Trading Name 2", result.Schemes[0].TradingName);
            Assert.Equal(new DateTime(2015, 1, 1), result.Schemes[0].RegistrationDate);
        }
        public async Task HandleAsync_ForUnknownBusinessType_ThrowsException()
        {
            // Arrange
            Scheme scheme = new Scheme(A.Dummy <Guid>());

            MemberUpload memberUpload = new MemberUpload(
                A.Dummy <Guid>(),
                A.Dummy <string>(),
                A.Dummy <List <MemberUploadError> >(),
                A.Dummy <decimal>(),
                2017,
                scheme,
                A.Dummy <string>(),
                A.Dummy <string>());

            RegisteredProducer registeredProducer = new RegisteredProducer(
                "WEE/AA1111AA",
                2017,
                scheme);

            var producer = new ProducerSubmission(
                registeredProducer,
                memberUpload,
                new ProducerBusiness(),
                null,
                new DateTime(2015, 1, 1),
                0,
                false,
                null,
                "Trading Name 1",
                EEEPlacedOnMarketBandType.Lessthan5TEEEplacedonmarket,
                SellingTechniqueType.Both,
                Domain.Obligation.ObligationType.Both,
                AnnualTurnOverBandType.Greaterthanonemillionpounds,
                new List <BrandName>(),
                new List <SICCode>(),
                A.Dummy <ChargeBandAmount>(),
                0,
                A.Dummy <StatusType>());

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

            A.CallTo(() => dataAccess.Fetch(A <string> ._, A <int> ._))
            .Returns(new List <ProducerSubmission> {
                producer
            });

            IWeeeAuthorization authorization = A.Fake <IWeeeAuthorization>();

            A.CallTo(() => authorization.CheckUserInRole(Roles.InternalAdmin))
            .Returns(false);

            IMapper mapper = A.Fake <IMapper>();

            GetProducerDetailsHandler handler = new GetProducerDetailsHandler(dataAccess, authorization, mapper);

            // Act
            Func <Task> action = async() => await handler.HandleAsync(A.Dummy <GetProducerDetails>());

            // Assert
            await Assert.ThrowsAsync <MissingFieldException>(action);
        }
        public async Task HandleAsync_ForPartnershipProducer_ReturnsPartnershipContactDetails_AndIsCompanyValueFalse()
        {
            // Arrange
            Scheme scheme = new Scheme(A.Dummy <Guid>());

            MemberUpload memberUpload = new MemberUpload(
                A.Dummy <Guid>(),
                A.Dummy <string>(),
                A.Dummy <List <MemberUploadError> >(),
                A.Dummy <decimal>(),
                2017,
                scheme,
                A.Dummy <string>(),
                A.Dummy <string>());

            RegisteredProducer registeredProducer = new RegisteredProducer(
                "WEE/AA1111AA",
                2017,
                scheme);

            var producerAddress = new ProducerAddress("PrimaryName", "SecondaryName", "Street", "Town", "Locality", "AdministrativeArea",
                                                      new Country(A.Dummy <Guid>(), "TestCountry"), "PostCode");

            var producerContact = new ProducerContact(
                "Title", "Forename", "Surname", "123456", "1235467", "12345678", "[email protected]", producerAddress);

            var producerBusiness = new ProducerBusiness(
                partnership: new Partnership("TestPartnership", producerContact, new List <Partner> {
            }));

            var producer = new ProducerSubmission(
                registeredProducer,
                memberUpload,
                producerBusiness,
                null,
                new DateTime(2015, 1, 1),
                0,
                false,
                null,
                "Trading Name 1",
                EEEPlacedOnMarketBandType.Lessthan5TEEEplacedonmarket,
                SellingTechniqueType.Both,
                Domain.Obligation.ObligationType.Both,
                AnnualTurnOverBandType.Greaterthanonemillionpounds,
                new List <BrandName>(),
                new List <SICCode>(),
                A.Dummy <ChargeBandAmount>(),
                0,
                A.Dummy <StatusType>());

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

            A.CallTo(() => dataAccess.Fetch(A <string> ._, A <int> ._))
            .Returns(new List <ProducerSubmission> {
                producer
            });

            IWeeeAuthorization authorization = A.Fake <IWeeeAuthorization>();

            A.CallTo(() => authorization.CheckUserInRole(Roles.InternalAdmin))
            .Returns(false);

            IMapper mapper = A.Fake <IMapper>();

            GetProducerDetailsHandler handler = new GetProducerDetailsHandler(dataAccess, authorization, mapper);

            // Act
            var result = await handler.HandleAsync(A.Dummy <GetProducerDetails>());

            // Assert
            Assert.NotNull(result);
            Assert.Single(result.Schemes);

            ProducerContactDetails businessContact = result.Schemes[0].ProducerBusinessContact;

            Assert.Equal("PrimaryName, SecondaryName, Street, Town, Locality, AdministrativeArea, PostCode, TestCountry", businessContact.Address);
            Assert.Equal("Title Forename Surname", businessContact.ContactName);
            Assert.Equal("123456", businessContact.Telephone);
            Assert.Equal("1235467", businessContact.Mobile);
            Assert.Equal("[email protected]", businessContact.Email);
            Assert.False(result.Schemes[0].IsCompany);
        }
        public async Task HandleAync_ReturnsFalseForCanRemoveProducer_WhenCurrentUserIsNotInternalAdmin()
        {
            Scheme scheme = new Scheme(A.Dummy <Guid>());

            MemberUpload memberUpload = new MemberUpload(
                A.Dummy <Guid>(),
                A.Dummy <string>(),
                A.Dummy <List <MemberUploadError> >(),
                A.Dummy <decimal>(),
                2017,
                scheme,
                A.Dummy <string>(),
                A.Dummy <string>());

            RegisteredProducer registeredProducer = new RegisteredProducer(
                "WEE/AA1111AA",
                2017,
                scheme);

            var producer = new ProducerSubmission(
                registeredProducer,
                memberUpload,
                CreatePartnershipBusiness(),
                null,
                new DateTime(2015, 1, 1),
                0,
                false,
                null,
                "Trading Name 1",
                EEEPlacedOnMarketBandType.Lessthan5TEEEplacedonmarket,
                SellingTechniqueType.Both,
                Domain.Obligation.ObligationType.Both,
                AnnualTurnOverBandType.Greaterthanonemillionpounds,
                new List <BrandName>(),
                new List <SICCode>(),
                A.Dummy <ChargeBandAmount>(),
                0,
                A.Dummy <StatusType>());

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

            A.CallTo(() => dataAccess.Fetch(A <string> ._, A <int> ._))
            .Returns(new List <ProducerSubmission> {
                producer
            });

            IWeeeAuthorization authorization = A.Fake <IWeeeAuthorization>();

            A.CallTo(() => authorization.CheckUserInRole(Roles.InternalAdmin))
            .Returns(false);

            IMapper mapper = A.Fake <IMapper>();

            GetProducerDetailsHandler handler = new GetProducerDetailsHandler(dataAccess, authorization, mapper);

            var result = await handler.HandleAsync(A.Dummy <GetProducerDetails>());

            Assert.False(result.CanRemoveProducer);
            A.CallTo(() => authorization.CheckUserInRole(Roles.InternalAdmin))
            .MustHaveHappened();
        }