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(); }
public async Task GetAllAatfSentOnDataCsvHandler_GivenAreaParameter_FileNameIsCorrect() { SystemTime.Freeze(new DateTime(2019, 8, 27, 11, 30, 1)); var panArea = fixture.Create <PanArea>(); var request = new GetAllAatfSentOnDataCsv(ComplianceYear, null, null, panArea.Id); A.CallTo(() => commonDataAccess.FetchLookup <PanArea>(panArea.Id)).Returns(panArea); var data = await handler.HandleAsync(request); data.FileName.Should().Be($"2019_{panArea.Name}_AATF WEEE sent on for treatment_27082019_1130.csv"); }
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); }
public async Task HandleAsync_GivenAatfAeMandatoryParametersAndPanArea_FileNameShouldBeCorrect() { GetAatfAeDetailsCsv request = new GetAatfAeDetailsCsv(fixture.Create <int>(), fixture.Create <ReportFacilityType>(), null, fixture.Create <Guid>(), null, false); PanArea panArea = fixture.Create <PanArea>(); A.CallTo(() => commonDataAccess.FetchLookup <PanArea>(request.PanArea.Value)).Returns(panArea); DateTime date = new DateTime(2019, 05, 18, 11, 12, 0); SystemTime.Freeze(date); CSVFileData data = await handler.HandleAsync(request); data.FileName.Should().Be($"{request.ComplianceYear}_{panArea.Name}_{request.FacilityType.ToString().ToUpper()}_AATF-AE-PCS-organisation details_{date:ddMMyyyy_HHmm}.csv"); SystemTime.Unfreeze(); }
public async Task HandleAsync_GivenMandatoryParametersAndPanArea_FileNameShouldBeCorrect(string expectedText, bool includeResubmissions) { var request = new GetAatfAeReturnDataCsv(fixture.Create <int>(), fixture.Create <int>(), fixture.Create <FacilityType>(), null, null, fixture.Create <Guid>(), null, fixture.Create <string>(), includeResubmissions); var panArea = fixture.Create <PanArea>(); A.CallTo(() => commonDataAccess.FetchLookup <PanArea>(request.PanArea.Value)).Returns(panArea); var date = new DateTime(2019, 05, 18, 11, 12, 0); SystemTime.Freeze(date); var data = await handler.HandleAsync(request); data.FileName.Should().Be($"{request.ComplianceYear}_Q{request.Quarter}_{expectedText}_{request.FacilityType.ToString().ToUpper()}_{panArea.Name}_Summary of AATF-AE returns to date_{date:ddMMyyyy_HHmm}.csv"); SystemTime.Unfreeze(); }
public async Task <CSVFileData> HandleAsync(GetAllAatfReuseSitesCsv request) { authorization.EnsureCanAccessInternalArea(); if (request.ComplianceYear == 0) { var message = $"Compliance year cannot be \"{request.ComplianceYear}\"."; throw new ArgumentException(message); } var reuseSitesData = await weeContext.StoredProcedures.GetAllAatfReuseSitesCsvData(request.ComplianceYear, request.AuthorityId, request.PanArea); var csvWriter = csvWriterFactory.Create <AatfReuseSitesData>(); csvWriter.DefineColumn(@"Appropriate authority", i => i.Abbreviation); csvWriter.DefineColumn(@"WROS Pan Area Team", i => i.PanName); csvWriter.DefineColumn(@"EA Area", i => i.LaName); csvWriter.DefineColumn(@"Compliance year", i => i.ComplianceYear); csvWriter.DefineColumn(@"Quarter", i => i.Quarter); csvWriter.DefineColumn(@"Submitted by", i => i.SubmittedBy); csvWriter.DefineColumn(@"Date submitted (GMT)", i => i.SubmittedDate); csvWriter.DefineColumn(@"Name of AATF", i => i.Name); csvWriter.DefineColumn(@"Approval number", i => i.ApprovalNumber); csvWriter.DefineColumn(@"Organisation name", i => i.OrgName); csvWriter.DefineColumn(@"Reuse site name", i => i.SiteName); csvWriter.DefineColumn(@"Reuse site address", i => i.SiteAddress); var fileContent = csvWriter.Write(reuseSitesData); var fileName = $"{request.ComplianceYear}"; if (request.AuthorityId.HasValue) { var authority = await commonDataAccess.FetchCompetentAuthorityById(request.AuthorityId.Value); fileName += "_" + authority.Abbreviation; } if (request.PanArea.HasValue) { var panArea = await commonDataAccess.FetchLookup <PanArea>(request.PanArea.Value); fileName += "_" + panArea.Name; } fileName += $"_AATF using reuse sites_{SystemTime.UtcNow:ddMMyyyy_HHmm}.csv"; return(new CSVFileData { FileContent = fileContent, FileName = fileName }); }
public async Task HandleAsync_WithNoLocalArea_LocalAreaIsNull(Core.AatfReturn.FacilityType facilityType) { 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>(), null) { FacilityType = facilityType }; var aatfId = Guid.NewGuid(); var request = new AddAatf() { Aatf = aatf, AatfContact = A.Dummy <AatfContactData>(), OrganisationId = Guid.NewGuid() }; var result = await handler.HandleAsync(request); A.CallTo(() => commonDataAccess.FetchLookup <LocalArea>(A <Guid> ._)).MustNotHaveHappened(); }
public async Task <CSVFileData> HandleAsync(GetAatfAeDetailsCsv request) { authorization.EnsureCanAccessInternalArea(); if (request.ComplianceYear == 0) { var message = $"Compliance year cannot be \"{request.ComplianceYear}\"."; throw new ArgumentException(message); } var facilityType = request.FacilityType != null ? (int)request.FacilityType : 4; List <AatfAeDetailsData> items = await context.StoredProcedures.GetAatfAeDetailsCsvData( request.ComplianceYear, facilityType, request.AuthorityId, request.LocalArea, request.PanArea); string type = request.FacilityType.ToString().ToUpper(); var csvWriter = csvWriterFactory.Create <AatfAeDetailsData>(); csvWriter.DefineColumn($"Compliance year", i => i.ComplianceYear); csvWriter.DefineColumn($"Appropriate authority", i => i.AppropriateAuthorityAbbr); if (!request.IsPublicRegister) { csvWriter.DefineColumn($"WROS Pan Area Team", i => i.PanAreaTeam); csvWriter.DefineColumn($"EA Area", i => i.EaArea); csvWriter.DefineColumn($"AATF, AE or PCS?", i => i.RecordType); csvWriter.DefineColumn($"Name", i => i.Name); csvWriter.DefineColumn($"Approval number", i => i.ApprovalNumber); csvWriter.DefineColumn($"Status", i => i.Status); csvWriter.DefineColumn($"AATF / AE address1", i => i.Address1); csvWriter.DefineColumn($"AATF / AE address2", i => i.Address2); csvWriter.DefineColumn($"AATF / AE town or city", i => i.TownCity); csvWriter.DefineColumn($"AATF / AE county or region", i => i.CountyRegion); csvWriter.DefineColumn($"AATF / AE postcode", i => i.PostCode); csvWriter.DefineColumn($"AATF / AE country", i => i.Country); csvWriter.DefineColumn($"PCS billing reference", i => i.IbisCustomerReference); csvWriter.DefineColumn($"PCS obligation type", i => i.ObligationType); csvWriter.DefineColumn($"AATF / AE date of approval", i => i.ApprovalDateString); csvWriter.DefineColumn($"AATF / AE size", i => i.Size); csvWriter.DefineColumn($"Contact first name", i => i.FirstName); csvWriter.DefineColumn($"Contact last name", i => i.LastName); csvWriter.DefineColumn($"Contact position", i => i.ContactPosition); csvWriter.DefineColumn($"Contact address1", i => i.ContactAddress1); csvWriter.DefineColumn($"Contact address2", i => i.ContactAddress2); csvWriter.DefineColumn($"Contact town or city", i => i.ContactTownCity); csvWriter.DefineColumn($"Contact county or region", i => i.ContactCountyRegion); csvWriter.DefineColumn($"Contact postcode", i => i.ContactPostcode); csvWriter.DefineColumn($"Contact country", i => i.ContactCountry); csvWriter.DefineColumn($"Contact phone number", i => i.ContactPhone); csvWriter.DefineColumn($"Contact email", i => i.ContactEmail); csvWriter.DefineColumn($"Organisation type", i => i.OrganisationTypeString); csvWriter.DefineColumn($"Organisation name", i => i.OperatorName); csvWriter.DefineColumn($"Organisation business trading name", i => i.OperatorTradingName); csvWriter.DefineColumn($"Organisation company registration number", i => i.CompanyRegistrationNumber); csvWriter.DefineColumn($"Organisation address1", i => i.OrganisationAddress1); csvWriter.DefineColumn($"Organisation address2", i => i.OrganisationAddress2); csvWriter.DefineColumn($"Organisation town or city", i => i.OrganisationTownCity); csvWriter.DefineColumn($"Organisation county or region", i => i.OrganisationCountyRegion); csvWriter.DefineColumn($"Organisation postcode", i => i.OrganisationPostcode); csvWriter.DefineColumn($"Organisation country", i => i.OrganisationCountry); csvWriter.DefineColumn($"Organisation telephone", i => i.OrganisationTelephone); csvWriter.DefineColumn($"Organisation email", i => i.OrganisationEmail); } else { csvWriter.DefineColumn($"Name of {type}", i => i.Name); csvWriter.DefineColumn($"{type} address", i => i.AatfAddress); csvWriter.DefineColumn($"{type} postcode", i => i.PostCode); csvWriter.DefineColumn($"{type} country", i => i.Country); csvWriter.DefineColumn($"EA Area for the {type}", i => i.EaArea); csvWriter.DefineColumn($"{type} approval number", i => i.ApprovalNumber); csvWriter.DefineColumn($"Date of approval", i => i.ApprovalDateString); csvWriter.DefineColumn($"{type} size", i => i.Size); csvWriter.DefineColumn($"{type} status", i => i.Status); csvWriter.DefineColumn($"Name of operator", i => i.OperatorName); csvWriter.DefineColumn($"Business trading name of operator", i => i.OperatorTradingName); csvWriter.DefineColumn($"Operator address ", i => i.OperatorAddress); csvWriter.DefineColumn($"Operator postcode", i => i.OrganisationPostcode); csvWriter.DefineColumn($"Operator country", i => i.OrganisationCountry); } var fileContent = csvWriter.Write(items); var additionalParameters = string.Empty; var additionalText = string.Empty; var facilityText = string.Empty; if (request.FacilityType.HasValue) { facilityText = $"_{request.FacilityType.ToString().ToUpper()}"; } if (request.AuthorityId.HasValue) { additionalParameters += $"_{(await commonDataAccess.FetchCompetentAuthorityById(request.AuthorityId.Value)).Abbreviation}"; } if (request.PanArea.HasValue) { additionalParameters += $"_{(await commonDataAccess.FetchLookup<PanArea>(request.PanArea.Value)).Name}"; } if (request.IsPublicRegister) { additionalText = " public register"; } else { additionalText = "_AATF-AE-PCS-organisation details"; } var fileName = $"{request.ComplianceYear}{additionalParameters}{facilityText}{additionalText}_{SystemTime.UtcNow:ddMMyyyy_HHmm}.csv"; return(new CSVFileData { FileContent = fileContent, FileName = fileName }); }
public async Task <CSVFileData> HandleAsync(GetAatfAeReturnDataCsv request) { authorization.EnsureCanAccessInternalArea(); if (request.ComplianceYear == 0) { var message = $"Compliance year cannot be \"{request.ComplianceYear}\"."; throw new ArgumentException(message); } var items = await context.StoredProcedures.GetAatfAeReturnDataCsvData( request.ComplianceYear, request.Quarter, (int)request.FacilityType, request.ReturnStatus.HasValue?(int)request.ReturnStatus : (int?)null, request.AuthorityId, request.LocalArea, request.PanArea, request.IncludeReSubmissions); foreach (var item in items) { item.AatfDataUrl = $@" =HYPERLINK(""""{request.AatfDataUrl}{item.AatfId}#data"""", """"View AATF / AE data"""")"; } var csvWriter = csvWriterFactory.Create <AatfAeReturnData>(); csvWriter.DefineColumn(@"Name of AATF / AE", i => i.Name); csvWriter.DefineColumn(@"Approval number", i => i.ApprovalNumber); csvWriter.DefineColumn(@"Organisation name", i => i.OrganisationName); csvWriter.DefineColumn(@"Submission status", i => i.ReturnStatus); csvWriter.DefineColumn(@"Date created (GMT)", i => i.CreatedDate); csvWriter.DefineColumn(@"Date submitted (GMT)", i => i.SubmittedDate); csvWriter.DefineColumn(@"Submitted by", i => i.SubmittedBy); csvWriter.DefineColumn(@"Appropriate authority", i => i.CompetentAuthorityAbbr); csvWriter.DefineColumn(@"First submission / resubmission", i => i.ReSubmission); csvWriter.DefineColumn(@" ", i => i.AatfDataUrl); var fileContent = csvWriter.Write(items); //Trim the space before equals in =Hyperlink fileContent = fileContent.Replace(" =HYPERLINK", "=HYPERLINK"); var excludeResubmissions = request.IncludeReSubmissions ? "Include resubmissions" : "Exclude resubmissions"; var additionalParameters = string.Empty; if (request.ReturnStatus.HasValue) { additionalParameters = $"_{EnumHelper.GetDisplayName(request.ReturnStatus.Value)}"; } if (request.AuthorityId.HasValue) { additionalParameters += $"_{(await commonDataAccess.FetchCompetentAuthorityById(request.AuthorityId.Value)).Abbreviation}"; } if (request.PanArea.HasValue) { additionalParameters += $"_{(await commonDataAccess.FetchLookup<PanArea>(request.PanArea.Value)).Name}"; } var fileName = $"{request.ComplianceYear}_Q{request.Quarter}_{excludeResubmissions}_{request.FacilityType.ToString().ToUpper()}{additionalParameters}_Summary of AATF-AE returns to date_{SystemTime.UtcNow:ddMMyyyy_HHmm}.csv"; return(new CSVFileData { FileContent = fileContent, FileName = fileName }); }
public async Task <CSVFileData> HandleAsync(GetAllAatfSentOnDataCsv request) { authorization.EnsureCanAccessInternalArea(); if (request.ComplianceYear == 0) { var message = $"Compliance year cannot be \"{request.ComplianceYear}\"."; throw new ArgumentException(message); } var obligatedData = await weeContext.StoredProcedures.GetAllAatfSentOnDataCsv(request.ComplianceYear, request.ObligationType, request.AuthorityId, request.PanArea); //Remove the Id columns if (obligatedData.Tables.Count > 0 && obligatedData.Tables[0] != null) { if (obligatedData.Tables[0].Columns.Contains("AatfId")) { obligatedData.Tables[0].Columns.Remove("AatfId"); } if (obligatedData.Tables[0].Columns.Contains("ReturnId")) { obligatedData.Tables[0].Columns.Remove("ReturnId"); } if (obligatedData.Tables[0].Columns.Contains("Q")) { obligatedData.Tables[0].Columns.Remove("Q"); } if (obligatedData.Tables[0].Columns.Contains("CategoryId")) { obligatedData.Tables[0].Columns.Remove("CategoryId"); } if (obligatedData.Tables[0].Columns.Contains("TonnageType")) { obligatedData.Tables[0].Columns.Remove("TonnageType"); } } var fileName = $"{request.ComplianceYear}"; if (request.AuthorityId.HasValue) { var authority = await commonDataAccess.FetchCompetentAuthorityById(request.AuthorityId.Value); fileName += "_" + authority.Abbreviation; } if (request.PanArea.HasValue) { var panArea = await commonDataAccess.FetchLookup <PanArea>(request.PanArea.Value); fileName += "_" + panArea.Name; } if (!string.IsNullOrEmpty(request.ObligationType)) { fileName += "_" + request.ObligationType; } fileName += $"_AATF WEEE sent on for treatment_{SystemTime.UtcNow:ddMMyyyy_HHmm}.csv"; var fileContent = string.Empty; if (obligatedData.Tables.Count > 0) { fileContent = obligatedData.Tables[0].DataSetSentOnToCsv(obligatedData.Tables[1]); } obligatedData.Dispose(); return(new CSVFileData { FileContent = fileContent, FileName = fileName }); }
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); }