From 7dbbce8149a5388a400e9a8faf497d37b4853b88 Mon Sep 17 00:00:00 2001 From: udaydefra Date: Wed, 30 Oct 2024 14:02:39 +0000 Subject: [PATCH 01/29] Adding CalculationResultModels - UD --- src/EPR.Calculator.API/Models/CalcResult.cs | 11 +++++++++++ src/EPR.Calculator.API/Models/CalcResultDetail.cs | 14 ++++++++++++++ .../Models/CalcResultLapcapData.cs | 7 +++++++ .../Models/CalcResultLapcapDataDetail.cs | 11 +++++++++++ .../Models/CalcResultLateReportingTonnage.cs | 7 +++++++ .../Models/CalcResultLateReportingTonnageDetail.cs | 8 ++++++++ 6 files changed, 58 insertions(+) create mode 100644 src/EPR.Calculator.API/Models/CalcResult.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultDetail.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultLapcapData.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultLapcapDataDetail.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultLateReportingTonnage.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultLateReportingTonnageDetail.cs diff --git a/src/EPR.Calculator.API/Models/CalcResult.cs b/src/EPR.Calculator.API/Models/CalcResult.cs new file mode 100644 index 0000000..4703d57 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResult.cs @@ -0,0 +1,11 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResult + { + public required CalcResultDetail CalcResultDetail { get; set; } + + public required CalcResultLapcapData CalcResultLapcapData { get; set; } + + public required CalcResultLateReportingTonnage CalcResultLateReportingTonnageDetail { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultDetail.cs b/src/EPR.Calculator.API/Models/CalcResultDetail.cs new file mode 100644 index 0000000..e870b5f --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultDetail.cs @@ -0,0 +1,14 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultDetail + { + public required string RunName { get; set; } + public int RunId { get; set; } + public DateTime RunDdate { get; set; } + public required string RunBy { get; set; } + public required string FinancialYear { get; set; } + public required string RpdFile { get; set; } + public required string LapcapFile { get; set; } + public required string ParametersFile { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs b/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs new file mode 100644 index 0000000..9d77b11 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs @@ -0,0 +1,7 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultLapcapData + { + public required IEnumerable CalcResultLapcapDataDetails { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultLapcapDataDetail.cs b/src/EPR.Calculator.API/Models/CalcResultLapcapDataDetail.cs new file mode 100644 index 0000000..0353be0 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultLapcapDataDetail.cs @@ -0,0 +1,11 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultLapcapDataDetail + { + public required string Material { get; set; } + + public required string Country { get; set; } + + public required decimal MaterialCost { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnage.cs b/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnage.cs new file mode 100644 index 0000000..cfeeed6 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnage.cs @@ -0,0 +1,7 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultLateReportingTonnage + { + public required IEnumerable CalcResultLateReportingTonnageDetails { get; set; } = new List(); + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnageDetail.cs b/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnageDetail.cs new file mode 100644 index 0000000..6182a21 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnageDetail.cs @@ -0,0 +1,8 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultLateReportingTonnageDetail + { + public required string Material { get; set; } + public required decimal MaterialTonnage { get; set; } + } +} From 5da5e89e7dc38bdc9cbca4eb38efefaf5e7e4ef2 Mon Sep 17 00:00:00 2001 From: udaydefra Date: Wed, 30 Oct 2024 15:38:06 +0000 Subject: [PATCH 02/29] Adding CalcResultParameterCommunicationCost - 463833 --- src/EPR.Calculator.API/Models/CalcResult.cs | 2 ++ .../Models/CalcResultParameterCommunicationCost.cs | 7 +++++++ .../CalcResultParameterCommunicationCostDetail.cs | 11 +++++++++++ 3 files changed, 20 insertions(+) create mode 100644 src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCost.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail.cs diff --git a/src/EPR.Calculator.API/Models/CalcResult.cs b/src/EPR.Calculator.API/Models/CalcResult.cs index 4703d57..75ff90f 100644 --- a/src/EPR.Calculator.API/Models/CalcResult.cs +++ b/src/EPR.Calculator.API/Models/CalcResult.cs @@ -7,5 +7,7 @@ public class CalcResult public required CalcResultLapcapData CalcResultLapcapData { get; set; } public required CalcResultLateReportingTonnage CalcResultLateReportingTonnageDetail { get; set; } + + public required CalcResultParameterCommunicationCost CalcResultParameterCommunicationCost { get; set; } } } diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCost.cs b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCost.cs new file mode 100644 index 0000000..55a1929 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCost.cs @@ -0,0 +1,7 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultParameterCommunicationCost + { + public IEnumerable CalcResultParameterCommunicationCostDetails { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail.cs b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail.cs new file mode 100644 index 0000000..be1a834 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail.cs @@ -0,0 +1,11 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultParameterCommunicationCostDetail + { + public required string Material { get; set; } + public required decimal Cost { get; set; } + public required string Country { get; set; } + public bool IsApportionment { get; set; } + public bool IsPercentage { get; set; } + } +} From 22a5b46b0a650aa724b39d3b70f538efde8fe5c8 Mon Sep 17 00:00:00 2001 From: udaydefra Date: Wed, 30 Oct 2024 16:10:27 +0000 Subject: [PATCH 03/29] Results --- src/EPR.Calculator.API/Models/CalcResult.cs | 2 ++ .../Models/CalcResult1Plus4Apportionment.cs | 8 ++++++++ .../Models/CalcResultFormatterType.cs | 9 +++++++++ .../Models/CalcResultLapcapData.cs | 3 ++- .../Models/CalcResultLapcapDataDetail.cs | 11 ----------- .../Models/CalcResultParameterCommunicationCost.cs | 3 ++- .../Models/CalcResultParameterCostDetail.cs | 14 ++++++++++++++ .../Models/CalcResultParameterOtherCost.cs | 8 ++++++++ 8 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 src/EPR.Calculator.API/Models/CalcResult1Plus4Apportionment.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultFormatterType.cs delete mode 100644 src/EPR.Calculator.API/Models/CalcResultLapcapDataDetail.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultParameterOtherCost.cs diff --git a/src/EPR.Calculator.API/Models/CalcResult.cs b/src/EPR.Calculator.API/Models/CalcResult.cs index 75ff90f..ba3d9d6 100644 --- a/src/EPR.Calculator.API/Models/CalcResult.cs +++ b/src/EPR.Calculator.API/Models/CalcResult.cs @@ -9,5 +9,7 @@ public class CalcResult public required CalcResultLateReportingTonnage CalcResultLateReportingTonnageDetail { get; set; } public required CalcResultParameterCommunicationCost CalcResultParameterCommunicationCost { get; set; } + + public required CalcResultParameterOtherCost CalcResultParameterOtherCost { get; set; } } } diff --git a/src/EPR.Calculator.API/Models/CalcResult1Plus4Apportionment.cs b/src/EPR.Calculator.API/Models/CalcResult1Plus4Apportionment.cs new file mode 100644 index 0000000..61377c4 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResult1Plus4Apportionment.cs @@ -0,0 +1,8 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResult1Plus4Apportionment + { + public IEnumerable CalcResultParameterCommunicationCostDetails { get; set; } = + new List(); + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultFormatterType.cs b/src/EPR.Calculator.API/Models/CalcResultFormatterType.cs new file mode 100644 index 0000000..4dfb2fa --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultFormatterType.cs @@ -0,0 +1,9 @@ +namespace EPR.Calculator.API.Models +{ + public enum CalcResultFormatterType + { + None = 1, + Currency, + Percentage + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs b/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs index 9d77b11..3d1857c 100644 --- a/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs +++ b/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs @@ -2,6 +2,7 @@ { public class CalcResultLapcapData { - public required IEnumerable CalcResultLapcapDataDetails { get; set; } + public required IEnumerable CalcResultLapcapDataDetails { get; set; } = + new List(); } } diff --git a/src/EPR.Calculator.API/Models/CalcResultLapcapDataDetail.cs b/src/EPR.Calculator.API/Models/CalcResultLapcapDataDetail.cs deleted file mode 100644 index 0353be0..0000000 --- a/src/EPR.Calculator.API/Models/CalcResultLapcapDataDetail.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace EPR.Calculator.API.Models -{ - public class CalcResultLapcapDataDetail - { - public required string Material { get; set; } - - public required string Country { get; set; } - - public required decimal MaterialCost { get; set; } - } -} diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCost.cs b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCost.cs index 55a1929..431c528 100644 --- a/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCost.cs +++ b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCost.cs @@ -2,6 +2,7 @@ { public class CalcResultParameterCommunicationCost { - public IEnumerable CalcResultParameterCommunicationCostDetails { get; set; } + public IEnumerable CalcResultParameterCommunicationCostDetails { get; set; } = + new List(); } } diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs b/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs new file mode 100644 index 0000000..faaf1fe --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs @@ -0,0 +1,14 @@ +using System.Runtime.Serialization.Formatters; + +namespace EPR.Calculator.API.Models +{ + + public class CalcResultParameterCostDetail + { + public required string Material { get; set; } + public required decimal Cost { get; set; } + public required string Country { get; set; } + public CalcResultFormatterType CalcResultFormatterType { get; set; } + public int OrderId { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterOtherCost.cs b/src/EPR.Calculator.API/Models/CalcResultParameterOtherCost.cs new file mode 100644 index 0000000..0402265 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultParameterOtherCost.cs @@ -0,0 +1,8 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultParameterOtherCost + { + public IEnumerable CalcResultParameterCommunicationCostDetails { get; set; } = + new List(); + } +} From 451461c5e238f0b526899bd133450baebf330696 Mon Sep 17 00:00:00 2001 From: udaydefra Date: Wed, 30 Oct 2024 19:13:11 +0000 Subject: [PATCH 04/29] Results structure --- .../BaseControllerTest.cs | 12 +++++++++- .../CalculatorInternalControllerTest.cs | 13 ++++++++-- .../Builder/CalcResultBuilder.cs | 24 +++++++++++++++++++ .../Builder/CalcResultDetailBuilder.cs | 20 ++++++++++++++++ .../Builder/ICalcResultBuilder.cs | 10 ++++++++ .../Builder/ICalcResultDetailBuilder.cs | 10 ++++++++ .../CalculatorInternalController.cs | 19 +++++++++++++-- .../Dtos/CalcResultsRequestDto.cs | 7 ++++++ .../Exporter/CalcResultsExporter.cs | 12 ++++++++++ .../Exporter/ICalcResultsExporter.cs | 7 ++++++ src/EPR.Calculator.API/Models/CalcResult.cs | 10 ++++---- .../Models/CalcResultDetail.cs | 12 +++++----- .../Models/CalcResultLapcapData.cs | 3 +-- .../Models/CalcResultLateReportingTonnage.cs | 2 +- .../Models/CalcResultParameterCostDetail.cs | 6 ++--- 15 files changed, 145 insertions(+), 22 deletions(-) create mode 100644 src/EPR.Calculator.API/Builder/CalcResultBuilder.cs create mode 100644 src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs create mode 100644 src/EPR.Calculator.API/Builder/ICalcResultBuilder.cs create mode 100644 src/EPR.Calculator.API/Builder/ICalcResultDetailBuilder.cs create mode 100644 src/EPR.Calculator.API/Dtos/CalcResultsRequestDto.cs create mode 100644 src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs create mode 100644 src/EPR.Calculator.API/Exporter/ICalcResultsExporter.cs diff --git a/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs b/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs index 365b59a..41c8301 100644 --- a/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs +++ b/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs @@ -11,6 +11,9 @@ using Microsoft.Extensions.Azure; using EPR.Calculator.API.Enums; using EPR.Calculator.API.Wrapper; +using EPR.Calculator.API.Builder; +using EPR.Calculator.API.Models; +using EPR.Calculator.API.Exporter; namespace EPR.Calculator.API.Tests.Controllers { @@ -44,8 +47,15 @@ public void SetUp() ILapcapDataValidator lapcapDataValidator = new LapcapDataValidator(dbContext); lapcapDataController = new LapcapDataController(dbContext, lapcapDataValidator); + wrapper = new Mock().Object; - calculatorInternalController = new CalculatorInternalController(dbContext, new RpdStatusDataValidator(wrapper), wrapper); + calculatorInternalController = new CalculatorInternalController( + dbContext, + new RpdStatusDataValidator(wrapper), + wrapper, + new Mock().Object, + new Mock>().Object + ); var mockFactory = new Mock>(); var mockClient = new Mock(); diff --git a/src/EPR.Calculator.API.UnitTests/CalculatorInternalControllerTest.cs b/src/EPR.Calculator.API.UnitTests/CalculatorInternalControllerTest.cs index de633ed..bf42e73 100644 --- a/src/EPR.Calculator.API.UnitTests/CalculatorInternalControllerTest.cs +++ b/src/EPR.Calculator.API.UnitTests/CalculatorInternalControllerTest.cs @@ -1,5 +1,8 @@ -using EPR.Calculator.API.Controllers; +using EPR.Calculator.API.Builder; +using EPR.Calculator.API.Controllers; using EPR.Calculator.API.Data.DataModels; +using EPR.Calculator.API.Exporter; +using EPR.Calculator.API.Models; using EPR.Calculator.API.Tests.Controllers; using EPR.Calculator.API.Validators; using EPR.Calculator.API.Wrapper; @@ -171,7 +174,13 @@ public void UpdateRpdStatus_With_RunId_When_Successful() if (dbContext != null) { - var controller = new CalculatorInternalController(dbContext, new RpdStatusDataValidator(mock.Object), mock.Object); + var controller = new CalculatorInternalController( + dbContext, + new RpdStatusDataValidator(mock.Object), + mock.Object, + new Mock().Object, + new Mock>().Object + ); var request = new Dtos.UpdateRpdStatus { isSuccessful = true, RunId = 1, UpdatedBy = "User1" }; var result = controller?.UpdateRpdStatus(request); diff --git a/src/EPR.Calculator.API/Builder/CalcResultBuilder.cs b/src/EPR.Calculator.API/Builder/CalcResultBuilder.cs new file mode 100644 index 0000000..05992b3 --- /dev/null +++ b/src/EPR.Calculator.API/Builder/CalcResultBuilder.cs @@ -0,0 +1,24 @@ +using EPR.Calculator.API.Data; +using EPR.Calculator.API.Dtos; +using EPR.Calculator.API.Models; + +namespace EPR.Calculator.API.Builder +{ + public class CalcResultBuilder : ICalcResultBuilder + { + private readonly ApplicationDBContext context; + private readonly ICalcResultDetailBuilder calcResultDetailBuilder; + public CalcResultBuilder(ApplicationDBContext context, ICalcResultDetailBuilder calcResultDetailBuilder) + { + this.context = context; + this.calcResultDetailBuilder = calcResultDetailBuilder; + } + + public CalcResult Build(CalcResultsRequestDto resultsRequestDto) + { + var calcResult = new CalcResult(); + calcResult.CalcResultDetail = this.calcResultDetailBuilder.Construct(resultsRequestDto); + return calcResult; + } + } +} diff --git a/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs b/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs new file mode 100644 index 0000000..7f52650 --- /dev/null +++ b/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs @@ -0,0 +1,20 @@ +using EPR.Calculator.API.Data; +using EPR.Calculator.API.Dtos; +using EPR.Calculator.API.Models; + +namespace EPR.Calculator.API.Builder +{ + public class CalcResultDetailBuilder : ICalcResultDetailBuilder + { + private readonly ApplicationDBContext context; + public CalcResultDetailBuilder(ApplicationDBContext context) + { + this.context = context; + } + + public CalcResultDetail Construct(CalcResultsRequestDto resultsRequestDto) + { + return new CalcResultDetail(); + } + } +} \ No newline at end of file diff --git a/src/EPR.Calculator.API/Builder/ICalcResultBuilder.cs b/src/EPR.Calculator.API/Builder/ICalcResultBuilder.cs new file mode 100644 index 0000000..68cd157 --- /dev/null +++ b/src/EPR.Calculator.API/Builder/ICalcResultBuilder.cs @@ -0,0 +1,10 @@ +using EPR.Calculator.API.Dtos; +using EPR.Calculator.API.Models; + +namespace EPR.Calculator.API.Builder +{ + public interface ICalcResultBuilder + { + public CalcResult Build(CalcResultsRequestDto resultsRequestDto); + } +} diff --git a/src/EPR.Calculator.API/Builder/ICalcResultDetailBuilder.cs b/src/EPR.Calculator.API/Builder/ICalcResultDetailBuilder.cs new file mode 100644 index 0000000..0b9e476 --- /dev/null +++ b/src/EPR.Calculator.API/Builder/ICalcResultDetailBuilder.cs @@ -0,0 +1,10 @@ +using EPR.Calculator.API.Dtos; +using EPR.Calculator.API.Models; + +namespace EPR.Calculator.API.Builder +{ + public interface ICalcResultDetailBuilder + { + public CalcResultDetail Construct(CalcResultsRequestDto resultsRequestDto); + } +} \ No newline at end of file diff --git a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs index fb98913..81bbbf1 100644 --- a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs +++ b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs @@ -1,7 +1,9 @@ -using EPR.Calculator.API.Data; +using EPR.Calculator.API.Builder; +using EPR.Calculator.API.Data; using EPR.Calculator.API.Data.DataModels; using EPR.Calculator.API.Dtos; using EPR.Calculator.API.Enums; +using EPR.Calculator.API.Exporter; using EPR.Calculator.API.Models; using EPR.Calculator.API.Validators; using EPR.Calculator.API.Wrapper; @@ -16,10 +18,14 @@ public class CalculatorInternalController : ControllerBase private readonly ApplicationDBContext context; private readonly IRpdStatusDataValidator rpdStatusDataValidator; private readonly IOrgAndPomWrapper wrapper; + private readonly ICalcResultBuilder builder; + private readonly ICalcResultsExporter exporter; public CalculatorInternalController(ApplicationDBContext context, IRpdStatusDataValidator rpdStatusDataValidator, - IOrgAndPomWrapper wrapper) + IOrgAndPomWrapper wrapper, + ICalcResultBuilder builder, + ICalcResultsExporter exporter) { this.context = context; this.rpdStatusDataValidator = rpdStatusDataValidator; @@ -133,5 +139,14 @@ public IActionResult UpdateRpdStatus([FromBody] UpdateRpdStatus request) } } } + + [HttpPost] + [Route("writeResults")] + public IActionResult WriteResultsToFile([FromBody] CalcResultsRequestDto resultsRequestDto) + { + var results = this.builder.Build(resultsRequestDto); + this.exporter.Export(results); + return new ObjectResult(null) { StatusCode = StatusCodes.Status201Created }; + } } } diff --git a/src/EPR.Calculator.API/Dtos/CalcResultsRequestDto.cs b/src/EPR.Calculator.API/Dtos/CalcResultsRequestDto.cs new file mode 100644 index 0000000..7ddd425 --- /dev/null +++ b/src/EPR.Calculator.API/Dtos/CalcResultsRequestDto.cs @@ -0,0 +1,7 @@ +namespace EPR.Calculator.API.Dtos +{ + public class CalcResultsRequestDto + { + public int RunId { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs b/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs new file mode 100644 index 0000000..090a859 --- /dev/null +++ b/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs @@ -0,0 +1,12 @@ +using EPR.Calculator.API.Models; + +namespace EPR.Calculator.API.Exporter +{ + public class CalcResultsExporter : ICalcResultsExporter + { + public void Export(CalcResult results) + { + // Code to export the File to Csv + } + } +} diff --git a/src/EPR.Calculator.API/Exporter/ICalcResultsExporter.cs b/src/EPR.Calculator.API/Exporter/ICalcResultsExporter.cs new file mode 100644 index 0000000..895d147 --- /dev/null +++ b/src/EPR.Calculator.API/Exporter/ICalcResultsExporter.cs @@ -0,0 +1,7 @@ +namespace EPR.Calculator.API.Exporter +{ + public interface ICalcResultsExporter + { + public void Export(T results); + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResult.cs b/src/EPR.Calculator.API/Models/CalcResult.cs index ba3d9d6..e26254f 100644 --- a/src/EPR.Calculator.API/Models/CalcResult.cs +++ b/src/EPR.Calculator.API/Models/CalcResult.cs @@ -2,14 +2,14 @@ { public class CalcResult { - public required CalcResultDetail CalcResultDetail { get; set; } + public CalcResultDetail CalcResultDetail { get; set; } - public required CalcResultLapcapData CalcResultLapcapData { get; set; } + public CalcResultLapcapData CalcResultLapcapData { get; set; } - public required CalcResultLateReportingTonnage CalcResultLateReportingTonnageDetail { get; set; } + public CalcResultLateReportingTonnage CalcResultLateReportingTonnageDetail { get; set; } - public required CalcResultParameterCommunicationCost CalcResultParameterCommunicationCost { get; set; } + public CalcResultParameterCommunicationCost CalcResultParameterCommunicationCost { get; set; } - public required CalcResultParameterOtherCost CalcResultParameterOtherCost { get; set; } + public CalcResultParameterOtherCost CalcResultParameterOtherCost { get; set; } } } diff --git a/src/EPR.Calculator.API/Models/CalcResultDetail.cs b/src/EPR.Calculator.API/Models/CalcResultDetail.cs index e870b5f..021119f 100644 --- a/src/EPR.Calculator.API/Models/CalcResultDetail.cs +++ b/src/EPR.Calculator.API/Models/CalcResultDetail.cs @@ -2,13 +2,13 @@ { public class CalcResultDetail { - public required string RunName { get; set; } + public string RunName { get; set; } = string.Empty; public int RunId { get; set; } public DateTime RunDdate { get; set; } - public required string RunBy { get; set; } - public required string FinancialYear { get; set; } - public required string RpdFile { get; set; } - public required string LapcapFile { get; set; } - public required string ParametersFile { get; set; } + public string RunBy { get; set; } = string.Empty; + public string FinancialYear { get; set; } = string.Empty; + public string RpdFile { get; set; } = string.Empty; + public string LapcapFile { get; set; } = string.Empty; + public string ParametersFile { get; set; } = string.Empty; } } diff --git a/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs b/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs index 3d1857c..8d1f462 100644 --- a/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs +++ b/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs @@ -2,7 +2,6 @@ { public class CalcResultLapcapData { - public required IEnumerable CalcResultLapcapDataDetails { get; set; } = - new List(); + public required IEnumerable? CalcResultLapcapDataDetails { get; set; } } } diff --git a/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnage.cs b/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnage.cs index cfeeed6..8ebe36d 100644 --- a/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnage.cs +++ b/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnage.cs @@ -2,6 +2,6 @@ { public class CalcResultLateReportingTonnage { - public required IEnumerable CalcResultLateReportingTonnageDetails { get; set; } = new List(); + public required IEnumerable? CalcResultLateReportingTonnageDetails { get; set; } = new List(); } } diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs b/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs index faaf1fe..ffd5e12 100644 --- a/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs +++ b/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs @@ -5,9 +5,9 @@ namespace EPR.Calculator.API.Models public class CalcResultParameterCostDetail { - public required string Material { get; set; } - public required decimal Cost { get; set; } - public required string Country { get; set; } + public string Material { get; set; } = string.Empty; + public decimal Cost { get; set; } + public string Country { get; set; } = string.Empty; public CalcResultFormatterType CalcResultFormatterType { get; set; } public int OrderId { get; set; } } From 4c077283c684ab04f97f681ddc38877eaf05313e Mon Sep 17 00:00:00 2001 From: udaydefra Date: Thu, 31 Oct 2024 09:12:48 +0000 Subject: [PATCH 05/29] Registering Builder / Exporter --- .../Controllers/CalculatorInternalController.cs | 2 ++ src/EPR.Calculator.API/Program.cs | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs index 81bbbf1..dd83ed6 100644 --- a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs +++ b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs @@ -30,6 +30,8 @@ public CalculatorInternalController(ApplicationDBContext context, this.context = context; this.rpdStatusDataValidator = rpdStatusDataValidator; this.wrapper = wrapper; + this.builder = builder; + this.exporter = exporter; } [HttpPost] diff --git a/src/EPR.Calculator.API/Program.cs b/src/EPR.Calculator.API/Program.cs index e4b9d19..27a13e1 100644 --- a/src/EPR.Calculator.API/Program.cs +++ b/src/EPR.Calculator.API/Program.cs @@ -1,6 +1,9 @@ using Azure.Messaging.ServiceBus; +using EPR.Calculator.API.Builder; using EPR.Calculator.API.Data; using EPR.Calculator.API.Exceptions; +using EPR.Calculator.API.Exporter; +using EPR.Calculator.API.Models; using EPR.Calculator.API.Validators; using EPR.Calculator.API.Wrapper; using FluentValidation; @@ -22,6 +25,9 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped, CalcResultsExporter>(); builder.Services.AddValidatorsFromAssemblyContaining(); builder.Services.AddDbContext(options => From 68573544eb8d8b588b657013c85f384ee29050a7 Mon Sep 17 00:00:00 2001 From: udaydefra Date: Thu, 31 Oct 2024 09:56:09 +0000 Subject: [PATCH 06/29] Removing DBContext and renaming action method --- src/EPR.Calculator.API/Builder/CalcResultBuilder.cs | 5 ++--- .../Controllers/CalculatorInternalController.cs | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/EPR.Calculator.API/Builder/CalcResultBuilder.cs b/src/EPR.Calculator.API/Builder/CalcResultBuilder.cs index 05992b3..3183ba4 100644 --- a/src/EPR.Calculator.API/Builder/CalcResultBuilder.cs +++ b/src/EPR.Calculator.API/Builder/CalcResultBuilder.cs @@ -6,11 +6,9 @@ namespace EPR.Calculator.API.Builder { public class CalcResultBuilder : ICalcResultBuilder { - private readonly ApplicationDBContext context; private readonly ICalcResultDetailBuilder calcResultDetailBuilder; - public CalcResultBuilder(ApplicationDBContext context, ICalcResultDetailBuilder calcResultDetailBuilder) + public CalcResultBuilder(ICalcResultDetailBuilder calcResultDetailBuilder) { - this.context = context; this.calcResultDetailBuilder = calcResultDetailBuilder; } @@ -18,6 +16,7 @@ public CalcResult Build(CalcResultsRequestDto resultsRequestDto) { var calcResult = new CalcResult(); calcResult.CalcResultDetail = this.calcResultDetailBuilder.Construct(resultsRequestDto); + return calcResult; } } diff --git a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs index dd83ed6..4547855 100644 --- a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs +++ b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs @@ -143,8 +143,8 @@ public IActionResult UpdateRpdStatus([FromBody] UpdateRpdStatus request) } [HttpPost] - [Route("writeResults")] - public IActionResult WriteResultsToFile([FromBody] CalcResultsRequestDto resultsRequestDto) + [Route("prepareCalcResults")] + public IActionResult PrepareCalcResults([FromBody] CalcResultsRequestDto resultsRequestDto) { var results = this.builder.Build(resultsRequestDto); this.exporter.Export(results); From 18db0783d3b401da56ddd327fa257f4d702fa3c3 Mon Sep 17 00:00:00 2001 From: udaydefra Date: Thu, 31 Oct 2024 12:01:41 +0000 Subject: [PATCH 07/29] Adding CalcResult - 463833 --- src/EPR.Calculator.API/Models/CalcResult.cs | 6 ++++++ .../Models/CalcResultLaDisposalCostData.cs | 7 +++++++ .../Models/CalcResultOnePlusFourApportionment.cs | 8 ++++++++ .../Models/CalcResultParameterCostDetail.cs | 10 ++++++++-- src/EPR.Calculator.API/Models/CalcResultSummary.cs | 7 +++++++ .../Models/CalcResultSummaryDetail.cs | 13 +++++++++++++ 6 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/EPR.Calculator.API/Models/CalcResultLaDisposalCostData.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultOnePlusFourApportionment.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultSummary.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultSummaryDetail.cs diff --git a/src/EPR.Calculator.API/Models/CalcResult.cs b/src/EPR.Calculator.API/Models/CalcResult.cs index e26254f..f7d6fad 100644 --- a/src/EPR.Calculator.API/Models/CalcResult.cs +++ b/src/EPR.Calculator.API/Models/CalcResult.cs @@ -11,5 +11,11 @@ public class CalcResult public CalcResultParameterCommunicationCost CalcResultParameterCommunicationCost { get; set; } public CalcResultParameterOtherCost CalcResultParameterOtherCost { get; set; } + + public CalcResultOnePlusFourApportionment CalcResultOnePlusFourApportionment { get; set; } + + public CalcResultLaDisposalCostData CalcResultLaDisposalCostData { get; set; } + + public CalcResultSummary CalcResultSummary { get; set; } } } diff --git a/src/EPR.Calculator.API/Models/CalcResultLaDisposalCostData.cs b/src/EPR.Calculator.API/Models/CalcResultLaDisposalCostData.cs new file mode 100644 index 0000000..42b6e7b --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultLaDisposalCostData.cs @@ -0,0 +1,7 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultLaDisposalCostData + { + public IEnumerable CalcResultLaDisposalCostDetails { get; set; } = new List(); + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultOnePlusFourApportionment.cs b/src/EPR.Calculator.API/Models/CalcResultOnePlusFourApportionment.cs new file mode 100644 index 0000000..8a8514f --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultOnePlusFourApportionment.cs @@ -0,0 +1,8 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultOnePlusFourApportionment + { + public IEnumerable CalcResultOnePlusFourApportionmentDetails { get; set; } = new List(); + } + +} diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs b/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs index ffd5e12..8b67cc7 100644 --- a/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs +++ b/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs @@ -5,9 +5,15 @@ namespace EPR.Calculator.API.Models public class CalcResultParameterCostDetail { - public string Material { get; set; } = string.Empty; + /// + /// KeyName can be a Material name or Cost name + /// + public string KeyName { get; set; } = string.Empty; public decimal Cost { get; set; } - public string Country { get; set; } = string.Empty; + /// + /// Category Name can be the Country name + /// + public string CategoryName { get; set; } = string.Empty; public CalcResultFormatterType CalcResultFormatterType { get; set; } public int OrderId { get; set; } } diff --git a/src/EPR.Calculator.API/Models/CalcResultSummary.cs b/src/EPR.Calculator.API/Models/CalcResultSummary.cs new file mode 100644 index 0000000..f2b3551 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultSummary.cs @@ -0,0 +1,7 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultSummary + { + public IEnumerable CalcResultSummaryDetails { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultSummaryDetail.cs b/src/EPR.Calculator.API/Models/CalcResultSummaryDetail.cs new file mode 100644 index 0000000..a4a364f --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultSummaryDetail.cs @@ -0,0 +1,13 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultSummaryDetail + { + public int ProducerId { get; set; } + public string SubsidaryId { get; set; } + public string SubsidaryName { get; set; } + public decimal ReportedHouseholdPackagingWasteTonnage { get; set; } + public decimal ReportedSelfManagedConsumerWasteTonnage { get; set; } + public decimal NetReportedTonnage { get; set; } + public decimal PricePerTonne { get; set; } + } +} From cde4a5ae2a4988b3e9f78d3c70367508c8deb958 Mon Sep 17 00:00:00 2001 From: udaydefra Date: Thu, 31 Oct 2024 14:40:53 +0000 Subject: [PATCH 08/29] Calculation Results Model --- src/EPR.Calculator.API/Models/CalcResult.cs | 2 -- .../Models/CalcResultLaDisposalCostData.cs | 1 + src/EPR.Calculator.API/Models/CalcResultLapcapData.cs | 1 + .../Models/CalcResultLateReportingTonnage.cs | 1 + .../Models/CalcResultOnePlusFourApportionment.cs | 1 + .../Models/CalcResultParameterCommunicationCost.cs | 1 + .../Models/CalcResultParameterCostDetail.cs | 2 ++ .../Models/CalcResultParameterOtherCost.cs | 1 + src/EPR.Calculator.API/Models/CalcResultSummaryDetail.cs | 7 +++---- 9 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/EPR.Calculator.API/Models/CalcResult.cs b/src/EPR.Calculator.API/Models/CalcResult.cs index f7d6fad..4b797b6 100644 --- a/src/EPR.Calculator.API/Models/CalcResult.cs +++ b/src/EPR.Calculator.API/Models/CalcResult.cs @@ -15,7 +15,5 @@ public class CalcResult public CalcResultOnePlusFourApportionment CalcResultOnePlusFourApportionment { get; set; } public CalcResultLaDisposalCostData CalcResultLaDisposalCostData { get; set; } - - public CalcResultSummary CalcResultSummary { get; set; } } } diff --git a/src/EPR.Calculator.API/Models/CalcResultLaDisposalCostData.cs b/src/EPR.Calculator.API/Models/CalcResultLaDisposalCostData.cs index 42b6e7b..3382482 100644 --- a/src/EPR.Calculator.API/Models/CalcResultLaDisposalCostData.cs +++ b/src/EPR.Calculator.API/Models/CalcResultLaDisposalCostData.cs @@ -2,6 +2,7 @@ { public class CalcResultLaDisposalCostData { + public string Name { get; set; } public IEnumerable CalcResultLaDisposalCostDetails { get; set; } = new List(); } } diff --git a/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs b/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs index 8d1f462..91b5fd9 100644 --- a/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs +++ b/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs @@ -2,6 +2,7 @@ { public class CalcResultLapcapData { + public string Name { get; set; } public required IEnumerable? CalcResultLapcapDataDetails { get; set; } } } diff --git a/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnage.cs b/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnage.cs index 8ebe36d..d959c32 100644 --- a/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnage.cs +++ b/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnage.cs @@ -2,6 +2,7 @@ { public class CalcResultLateReportingTonnage { + public string Name { get; set; } public required IEnumerable? CalcResultLateReportingTonnageDetails { get; set; } = new List(); } } diff --git a/src/EPR.Calculator.API/Models/CalcResultOnePlusFourApportionment.cs b/src/EPR.Calculator.API/Models/CalcResultOnePlusFourApportionment.cs index 8a8514f..77873b3 100644 --- a/src/EPR.Calculator.API/Models/CalcResultOnePlusFourApportionment.cs +++ b/src/EPR.Calculator.API/Models/CalcResultOnePlusFourApportionment.cs @@ -2,6 +2,7 @@ { public class CalcResultOnePlusFourApportionment { + public string Name { get; set; } public IEnumerable CalcResultOnePlusFourApportionmentDetails { get; set; } = new List(); } diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCost.cs b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCost.cs index 431c528..df08ce4 100644 --- a/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCost.cs +++ b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCost.cs @@ -2,6 +2,7 @@ { public class CalcResultParameterCommunicationCost { + public string Name { get; set; } public IEnumerable CalcResultParameterCommunicationCostDetails { get; set; } = new List(); } diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs b/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs index 8b67cc7..616ce38 100644 --- a/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs +++ b/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs @@ -16,5 +16,7 @@ public class CalcResultParameterCostDetail public string CategoryName { get; set; } = string.Empty; public CalcResultFormatterType CalcResultFormatterType { get; set; } public int OrderId { get; set; } + + public int Precision { get; set; } } } diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterOtherCost.cs b/src/EPR.Calculator.API/Models/CalcResultParameterOtherCost.cs index 0402265..40ddf5b 100644 --- a/src/EPR.Calculator.API/Models/CalcResultParameterOtherCost.cs +++ b/src/EPR.Calculator.API/Models/CalcResultParameterOtherCost.cs @@ -2,6 +2,7 @@ { public class CalcResultParameterOtherCost { + public string Name { get; set; } public IEnumerable CalcResultParameterCommunicationCostDetails { get; set; } = new List(); } diff --git a/src/EPR.Calculator.API/Models/CalcResultSummaryDetail.cs b/src/EPR.Calculator.API/Models/CalcResultSummaryDetail.cs index a4a364f..62fb69d 100644 --- a/src/EPR.Calculator.API/Models/CalcResultSummaryDetail.cs +++ b/src/EPR.Calculator.API/Models/CalcResultSummaryDetail.cs @@ -5,9 +5,8 @@ public class CalcResultSummaryDetail public int ProducerId { get; set; } public string SubsidaryId { get; set; } public string SubsidaryName { get; set; } - public decimal ReportedHouseholdPackagingWasteTonnage { get; set; } - public decimal ReportedSelfManagedConsumerWasteTonnage { get; set; } - public decimal NetReportedTonnage { get; set; } - public decimal PricePerTonne { get; set; } + public decimal HouseholdPackagingWasteTonnage { get; set; } + public decimal SelfManagedConsumerWasteTonnage { get; set; } + } } From 3374b7b00468d0dad6ff61893daad1f2b25bb793 Mon Sep 17 00:00:00 2001 From: johnbenjamin-defra Date: Thu, 31 Oct 2024 21:53:19 +0000 Subject: [PATCH 09/29] 459419 Transpose pom and org data --- .../CalculatorInternalController.cs | 16 +++- .../Services/TransposePomAndOrgDataService.cs | 79 +++++++++++++++++++ 2 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs diff --git a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs index 81bbbf1..6fd9c68 100644 --- a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs +++ b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs @@ -5,6 +5,7 @@ using EPR.Calculator.API.Enums; using EPR.Calculator.API.Exporter; using EPR.Calculator.API.Models; +using EPR.Calculator.API.Services; using EPR.Calculator.API.Validators; using EPR.Calculator.API.Wrapper; using Microsoft.AspNetCore.Mvc; @@ -144,9 +145,18 @@ public IActionResult UpdateRpdStatus([FromBody] UpdateRpdStatus request) [Route("writeResults")] public IActionResult WriteResultsToFile([FromBody] CalcResultsRequestDto resultsRequestDto) { - var results = this.builder.Build(resultsRequestDto); - this.exporter.Export(results); - return new ObjectResult(null) { StatusCode = StatusCodes.Status201Created }; + try + { + TransposePomAndOrgDataService.Transpose(context, resultsRequestDto.RunId); + + var results = this.builder.Build(resultsRequestDto); + this.exporter.Export(results); + return new ObjectResult(null) { StatusCode = StatusCodes.Status201Created }; + } + catch(Exception exception) + { + return StatusCode(StatusCodes.Status500InternalServerError, exception); + } } } } diff --git a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs new file mode 100644 index 0000000..88bda77 --- /dev/null +++ b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs @@ -0,0 +1,79 @@ +using EPR.Calculator.API.Data; +using EPR.Calculator.API.Data.DataModels; + +namespace EPR.Calculator.API.Services +{ + public static class TransposePomAndOrgDataService + { + public static void Transpose(ApplicationDBContext context, int runId) + { + var materials = context.Material.ToList(); + + var pomDataMaster = context.CalculatorRunPomDataMaster.Single(pdm => pdm.Id == runId); + + var pomDataDetails = context.CalculatorRunPomDataDetails.Where(pdd => pdd.CalculatorRunPomDataMasterId == pomDataMaster.Id).ToList(); + + var calculatorRun = context.CalculatorRuns.Single(cr => cr.Id == runId); + + var producerDetails = new List(); + + var producerReportedMaterials = new List(); + + using (var transaction = context.Database.BeginTransaction()) + { + try + { + foreach (var pom in pomDataDetails) + { + if (pom.OrganisationId != null) + { + var organisationDataMaster = context.CalculatorRunOrganisationDataMaster.Single(odm => odm.Id == runId); + + var organisationDataDetails = context.CalculatorRunOrganisationDataDetails.Where(odd => odd.CalculatorRunOrganisationDataMasterId == organisationDataMaster.Id).ToList(); + + var producerName = organisationDataDetails.Single(odd => odd.OrganisationId == pom.OrganisationId).OrganisationName; + + var producerDetail = new ProducerDetail + { + CalculatorRunId = runId, + ProducerId = pom.OrganisationId, + SubsidiaryId = pom.SubsidaryId, + ProducerName = producerName, + CalculatorRun = calculatorRun + }; + + producerDetails.Add(producerDetail); + + context.ProducerDetail.Add(producerDetail); + context.SaveChanges(); + + var material = materials.Single(m => m.Code == pom.PackagingMaterial); + + var producerReportedMaterial = new ProducerReportedMaterial + { + MaterialId = material.Id, + Material = material, + ProducerDetailId = producerDetail.Id, + ProducerDetail = producerDetail, + PackagingType = pom.PackagingType, + PackagingTonnage = (decimal)pom.PackagingMaterialWeight / 1000, + }; + + producerReportedMaterials.Add(producerReportedMaterial); + } + } + + context.ProducerReportedMaterial.AddRange(producerReportedMaterials); + context.SaveChanges(); + + transaction.Commit(); + } + catch (Exception exception) + { + // Error, rollback transaction + transaction.Rollback(); + } + } + } + } +} From d8f8a85704bdc353c395cba656ba5f497585aeac Mon Sep 17 00:00:00 2001 From: udaydefra Date: Mon, 4 Nov 2024 09:37:45 +0000 Subject: [PATCH 10/29] Calculator Results Model structure Changes - 463833 --- .../BaseControllerTest.cs | 12 +- .../Builder/CalcResultBuilderTests.cs | 33 ++ .../Builder/CalcResultDetailBuilderTests.cs | 32 ++ .../CalcResultLapcapDataBuilderTests.cs | 33 ++ .../CalculatorInternalControllerTest.cs | 13 +- .../CalculatorInternalControllerTests.cs | 424 ++++++++++++++++++ .../Builder/CalcResultBuilder.cs | 26 ++ .../Builder/CalcResultDetailBuilder.cs | 20 + .../Builder/CalcResultLapcapDataBuilder.cs | 21 + .../Builder/ICalcResultBuilder.cs | 10 + .../Builder/ICalcResultDetailBuilder.cs | 10 + .../Builder/ICalcResultLapcapDataBuilder.cs | 10 + .../CalculatorInternalController.cs | 21 +- .../Dtos/CalcResultsRequestDto.cs | 7 + .../Exporter/CalcResultsExporter.cs | 12 + .../Exporter/ICalcResultsExporter.cs | 7 + src/EPR.Calculator.API/Models/CalcResult.cs | 19 + .../Models/CalcResult1Plus4Apportionment.cs | 8 + .../Models/CalcResultDetail.cs | 14 + .../Models/CalcResultFormatterType.cs | 9 + .../Models/CalcResultLaDisposalCostData.cs | 8 + .../CalcResultLaDisposalCostDataDetail.cs | 21 + .../Models/CalcResultLapcapData.cs | 8 + .../Models/CalcResultLapcapDataDetails.cs | 19 + .../Models/CalcResultLateReportingTonnage.cs | 8 + .../CalcResultLateReportingTonnageDetail.cs | 8 + .../CalcResultOnePlusFourApportionment.cs | 8 + ...alcResultOnePlusFourApportionmentDetail.cs | 14 + .../CalcResultParameterCommunicationCost.cs | 15 + ...cResultParameterCommunicationCostDetail.cs | 11 + ...ResultParameterCommunicationCostDetail1.cs | 13 + ...ResultParameterCommunicationCostDetail2.cs | 18 + ...ResultParameterCommunicationCostDetail3.cs | 12 + .../Models/CalcResultParameterCostDetail.cs | 22 + .../Models/CalcResultParameterOtherCost.cs | 19 + .../CalcResultParameterOtherCostDetail.cs | 13 + .../CalcResultParameterOtherCostDetail5.cs | 9 + .../Models/CalcResultSummary.cs | 7 + .../Models/CalcResultSummaryDetail.cs | 12 + src/EPR.Calculator.API/Program.cs | 7 + 40 files changed, 988 insertions(+), 5 deletions(-) create mode 100644 src/EPR.Calculator.API.UnitTests/Builder/CalcResultBuilderTests.cs create mode 100644 src/EPR.Calculator.API.UnitTests/Builder/CalcResultDetailBuilderTests.cs create mode 100644 src/EPR.Calculator.API.UnitTests/Builder/CalcResultLapcapDataBuilderTests.cs create mode 100644 src/EPR.Calculator.API.UnitTests/Controllers/CalculatorInternalControllerTests.cs create mode 100644 src/EPR.Calculator.API/Builder/CalcResultBuilder.cs create mode 100644 src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs create mode 100644 src/EPR.Calculator.API/Builder/CalcResultLapcapDataBuilder.cs create mode 100644 src/EPR.Calculator.API/Builder/ICalcResultBuilder.cs create mode 100644 src/EPR.Calculator.API/Builder/ICalcResultDetailBuilder.cs create mode 100644 src/EPR.Calculator.API/Builder/ICalcResultLapcapDataBuilder.cs create mode 100644 src/EPR.Calculator.API/Dtos/CalcResultsRequestDto.cs create mode 100644 src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs create mode 100644 src/EPR.Calculator.API/Exporter/ICalcResultsExporter.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResult.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResult1Plus4Apportionment.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultDetail.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultFormatterType.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultLaDisposalCostData.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultLaDisposalCostDataDetail.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultLapcapData.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultLapcapDataDetails.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultLateReportingTonnage.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultLateReportingTonnageDetail.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultOnePlusFourApportionment.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultOnePlusFourApportionmentDetail.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCost.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail1.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail2.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail3.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultParameterOtherCost.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultParameterOtherCostDetail.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultParameterOtherCostDetail5.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultSummary.cs create mode 100644 src/EPR.Calculator.API/Models/CalcResultSummaryDetail.cs diff --git a/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs b/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs index 365b59a..41c8301 100644 --- a/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs +++ b/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs @@ -11,6 +11,9 @@ using Microsoft.Extensions.Azure; using EPR.Calculator.API.Enums; using EPR.Calculator.API.Wrapper; +using EPR.Calculator.API.Builder; +using EPR.Calculator.API.Models; +using EPR.Calculator.API.Exporter; namespace EPR.Calculator.API.Tests.Controllers { @@ -44,8 +47,15 @@ public void SetUp() ILapcapDataValidator lapcapDataValidator = new LapcapDataValidator(dbContext); lapcapDataController = new LapcapDataController(dbContext, lapcapDataValidator); + wrapper = new Mock().Object; - calculatorInternalController = new CalculatorInternalController(dbContext, new RpdStatusDataValidator(wrapper), wrapper); + calculatorInternalController = new CalculatorInternalController( + dbContext, + new RpdStatusDataValidator(wrapper), + wrapper, + new Mock().Object, + new Mock>().Object + ); var mockFactory = new Mock>(); var mockClient = new Mock(); diff --git a/src/EPR.Calculator.API.UnitTests/Builder/CalcResultBuilderTests.cs b/src/EPR.Calculator.API.UnitTests/Builder/CalcResultBuilderTests.cs new file mode 100644 index 0000000..93a6508 --- /dev/null +++ b/src/EPR.Calculator.API.UnitTests/Builder/CalcResultBuilderTests.cs @@ -0,0 +1,33 @@ +namespace EPR.Calculator.API.UnitTests.Builder +{ + using System; + using EPR.Calculator.API.Builder; + using Microsoft.VisualStudio.TestTools.UnitTesting; + using Moq; + + [TestClass] + public class CalcResultBuilderTests + { + private CalcResultBuilder testClass; + private Mock calcResultDetailBuilder; + private Mock lapcapBuilder; + + [TestInitialize] + public void SetUp() + { + this.calcResultDetailBuilder = new Mock(); + this.lapcapBuilder = new Mock(); + this.testClass = new CalcResultBuilder(calcResultDetailBuilder.Object, lapcapBuilder.Object); + } + + [TestMethod] + public void CanConstruct() + { + // Act + var instance = new CalcResultBuilder(calcResultDetailBuilder.Object, lapcapBuilder.Object); + + // Assert + Assert.IsNotNull(instance); + } + } +} \ No newline at end of file diff --git a/src/EPR.Calculator.API.UnitTests/Builder/CalcResultDetailBuilderTests.cs b/src/EPR.Calculator.API.UnitTests/Builder/CalcResultDetailBuilderTests.cs new file mode 100644 index 0000000..3cefcbf --- /dev/null +++ b/src/EPR.Calculator.API.UnitTests/Builder/CalcResultDetailBuilderTests.cs @@ -0,0 +1,32 @@ +namespace EPR.Calculator.API.UnitTests.Builder +{ + using System; + using EPR.Calculator.API.Builder; + using EPR.Calculator.API.Data; + using EPR.Calculator.API.Dtos; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class CalcResultDetailBuilderTests + { + private CalcResultDetailBuilder _testClass; + private ApplicationDBContext _context; + + [TestInitialize] + public void SetUp() + { + _context = new ApplicationDBContext(); + _testClass = new CalcResultDetailBuilder(_context); + } + + [TestMethod] + public void CanConstruct() + { + // Act + var instance = new CalcResultDetailBuilder(_context); + + // Assert + Assert.IsNotNull(instance); + } + } +} \ No newline at end of file diff --git a/src/EPR.Calculator.API.UnitTests/Builder/CalcResultLapcapDataBuilderTests.cs b/src/EPR.Calculator.API.UnitTests/Builder/CalcResultLapcapDataBuilderTests.cs new file mode 100644 index 0000000..aa25e5f --- /dev/null +++ b/src/EPR.Calculator.API.UnitTests/Builder/CalcResultLapcapDataBuilderTests.cs @@ -0,0 +1,33 @@ +namespace EPR.Calculator.API.UnitTests.Builder +{ + using System; + using EPR.Calculator.API.Builder; + using EPR.Calculator.API.Data; + using EPR.Calculator.API.Dtos; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class CalcResultLapcapDataBuilderTests + { + private CalcResultLapcapDataBuilder _testClass; + private ApplicationDBContext _context; + + [TestInitialize] + public void SetUp() + { + _context = new ApplicationDBContext(); + _testClass = new CalcResultLapcapDataBuilder(_context); + } + + [TestMethod] + public void CanConstruct() + { + // Act + var instance = new CalcResultLapcapDataBuilder(_context); + + // Assert + Assert.IsNotNull(instance); + } + + } +} \ No newline at end of file diff --git a/src/EPR.Calculator.API.UnitTests/CalculatorInternalControllerTest.cs b/src/EPR.Calculator.API.UnitTests/CalculatorInternalControllerTest.cs index de633ed..bf42e73 100644 --- a/src/EPR.Calculator.API.UnitTests/CalculatorInternalControllerTest.cs +++ b/src/EPR.Calculator.API.UnitTests/CalculatorInternalControllerTest.cs @@ -1,5 +1,8 @@ -using EPR.Calculator.API.Controllers; +using EPR.Calculator.API.Builder; +using EPR.Calculator.API.Controllers; using EPR.Calculator.API.Data.DataModels; +using EPR.Calculator.API.Exporter; +using EPR.Calculator.API.Models; using EPR.Calculator.API.Tests.Controllers; using EPR.Calculator.API.Validators; using EPR.Calculator.API.Wrapper; @@ -171,7 +174,13 @@ public void UpdateRpdStatus_With_RunId_When_Successful() if (dbContext != null) { - var controller = new CalculatorInternalController(dbContext, new RpdStatusDataValidator(mock.Object), mock.Object); + var controller = new CalculatorInternalController( + dbContext, + new RpdStatusDataValidator(mock.Object), + mock.Object, + new Mock().Object, + new Mock>().Object + ); var request = new Dtos.UpdateRpdStatus { isSuccessful = true, RunId = 1, UpdatedBy = "User1" }; var result = controller?.UpdateRpdStatus(request); diff --git a/src/EPR.Calculator.API.UnitTests/Controllers/CalculatorInternalControllerTests.cs b/src/EPR.Calculator.API.UnitTests/Controllers/CalculatorInternalControllerTests.cs new file mode 100644 index 0000000..bdfb4f3 --- /dev/null +++ b/src/EPR.Calculator.API.UnitTests/Controllers/CalculatorInternalControllerTests.cs @@ -0,0 +1,424 @@ +namespace EPR.Calculator.API.UnitTests.Controllers +{ + using System; + using System.Collections.Generic; + using EPR.Calculator.API.Builder; + using EPR.Calculator.API.Controllers; + using EPR.Calculator.API.Data; + using EPR.Calculator.API.Dtos; + using EPR.Calculator.API.Exporter; + using EPR.Calculator.API.Models; + using EPR.Calculator.API.Validators; + using EPR.Calculator.API.Wrapper; + using Microsoft.VisualStudio.TestTools.UnitTesting; + using Moq; + + [TestClass] + public class CalculatorInternalControllerTests + { + private CalculatorInternalController _testClass; + private ApplicationDBContext _context; + private Mock _rpdStatusDataValidator; + private Mock _wrapper; + private Mock _builder; + private Mock> _exporter; + + [TestInitialize] + public void SetUp() + { + _context = new ApplicationDBContext(); + _rpdStatusDataValidator = new Mock(); + _wrapper = new Mock(); + _builder = new Mock(); + _exporter = new Mock>(); + _testClass = new CalculatorInternalController(_context, _rpdStatusDataValidator.Object, _wrapper.Object, _builder.Object, _exporter.Object); + } + + [TestMethod] + public void CanCallPrepareCalcResults() + { + // Arrange + var resultsRequestDto = new CalcResultsRequestDto { RunId = 1706708422 }; + + _builder.Setup(mock => mock.Build(It.IsAny())).Returns(new CalcResult + { + CalcResultDetail = new CalcResultDetail + { + RunName = "TestValue26600268", + RunId = 1555710394, + RunDate = DateTime.UtcNow, + RunBy = "TestValue887677417", + FinancialYear = "TestValue2028236729", + RpdFile = "TestValue1468463827", + LapcapFile = "TestValue1811770456", + ParametersFile = "TestValue1028165412" + }, + CalcResultLapcapData = new CalcResultLapcapData + { + Name = "TestValue1390335580", + CalcResultLapcapDataDetails = new[] { + new CalcResultLapcapDataDetails + { + Name = "TestValue1226159960", + EnglandDisposalCost = "TestValue441421652", + WalesDisposalCost = "TestValue1552381969", + ScotlandDisposalCost = "TestValue1978928873", + NorthernIrelandDisposalCost = "TestValue566927844", + TotalDisposalCost = "TestValue1764326788", + OrderId = 704773396 + }, + new CalcResultLapcapDataDetails + { + Name = "TestValue1531493392", + EnglandDisposalCost = "TestValue813688780", + WalesDisposalCost = "TestValue1343449936", + ScotlandDisposalCost = "TestValue871919063", + NorthernIrelandDisposalCost = "TestValue1649127366", + TotalDisposalCost = "TestValue346141130", + OrderId = 781377830 + }, + new CalcResultLapcapDataDetails + { + Name = "TestValue111688060", + EnglandDisposalCost = "TestValue48524705", + WalesDisposalCost = "TestValue1281709861", + ScotlandDisposalCost = "TestValue2028925117", + NorthernIrelandDisposalCost = "TestValue413187689", + TotalDisposalCost = "TestValue436582086", + OrderId = 1776367784 + } + } + }, + CalcResultLateReportingTonnageDetail = new CalcResultLateReportingTonnage + { + Name = "TestValue2008053382", + CalcResultLateReportingTonnageDetails = new[] { + new CalcResultLateReportingTonnageDetail + { + Name = "TestValue2143215974", + TotalLateReportingTonnage = 1142363418.57M + }, + new CalcResultLateReportingTonnageDetail + { + Name = "TestValue950828146", + TotalLateReportingTonnage = 2103732562.14M + }, + new CalcResultLateReportingTonnageDetail + { + Name = "TestValue1995738811", + TotalLateReportingTonnage = 940670239.41M + } + } + }, + CalcResultParameterCommunicationCost = new CalcResultParameterCommunicationCost + { + Name = "TestValue384507152", + CalcResultParameterCommunicationCostDetails = new[] { + new CalcResultParameterCommunicationCostDetail1 + { + Name = "TestValue163477184", + England = "TestValue39207367", + Wales = "TestValue423871176", + Scotland = "TestValue891242292", + NorthernIreland = "TestValue1925406460", + Total = "TestValue1292599773", + OrderId = 1141710207 + }, + new CalcResultParameterCommunicationCostDetail1 + { + Name = "TestValue1567817807", + England = "TestValue1670419790", + Wales = "TestValue809681337", + Scotland = "TestValue623341535", + NorthernIreland = "TestValue1396596403", + Total = "TestValue188883285", + OrderId = 1822882885 + }, + new CalcResultParameterCommunicationCostDetail1 + { + Name = "TestValue1650299495", + England = "TestValue1426377447", + Wales = "TestValue1298148344", + Scotland = "TestValue323444817", + NorthernIreland = "TestValue1392706085", + Total = "TestValue1444022334", + OrderId = 839152712 + } + }, + CalcResultParameterCommunicationCostDetails2 = new[] { + new CalcResultParameterCommunicationCostDetail2 + { + Name = "TestValue746526868", + England = "TestValue1587022357", + Wales = "TestValue1398582767", + Scotland = "TestValue591583326", + NorthernIreland = "TestValue1761451443", + Total = "TestValue1365590225", + OrderId = 445689325, + ProducerReportedHouseholdPackagingWasteTonnage = "TestValue398479526", + LateReportingTonnage = "TestValue1028100727", + ProducerReportedHouseholdTonnagePlusLateReportingTonnage = "TestValue589429105", + CommsCostByMaterialPricePerTonne = "TestValue1916071624" + }, + new CalcResultParameterCommunicationCostDetail2 + { + Name = "TestValue1902085084", + England = "TestValue1146986240", + Wales = "TestValue650211596", + Scotland = "TestValue1304242568", + NorthernIreland = "TestValue448047887", + Total = "TestValue757885074", + OrderId = 568218317, + ProducerReportedHouseholdPackagingWasteTonnage = "TestValue1636261704", + LateReportingTonnage = "TestValue1117461500", + ProducerReportedHouseholdTonnagePlusLateReportingTonnage = "TestValue628190545", + CommsCostByMaterialPricePerTonne = "TestValue1758773718" + }, + new CalcResultParameterCommunicationCostDetail2 + { + Name = "TestValue1219243780", + England = "TestValue1937329801", + Wales = "TestValue1187469330", + Scotland = "TestValue1587484499", + NorthernIreland = "TestValue185170497", + Total = "TestValue492916479", + OrderId = 1875008643, + ProducerReportedHouseholdPackagingWasteTonnage = "TestValue1800163449", + LateReportingTonnage = "TestValue1801537569", + ProducerReportedHouseholdTonnagePlusLateReportingTonnage = "TestValue603032726", + CommsCostByMaterialPricePerTonne = "TestValue1653633272" + } + }, + CalcResultParameterCommunicationCostDetails3 = new[] { + new CalcResultParameterCommunicationCostDetail3 + { + Name = "TestValue1692838087", + England = "TestValue1564433963", + Wales = "TestValue599668658", + Scotland = "TestValue1172772056", + NorthernIreland = "TestValue299658966", + Total = "TestValue163955017" + }, + new CalcResultParameterCommunicationCostDetail3 + { + Name = "TestValue2074493195", + England = "TestValue696020882", + Wales = "TestValue1169338281", + Scotland = "TestValue642319063", + NorthernIreland = "TestValue220252232", + Total = "TestValue854753558" + }, + new CalcResultParameterCommunicationCostDetail3 + { + Name = "TestValue1641994966", + England = "TestValue1189380692", + Wales = "TestValue1172671289", + Scotland = "TestValue346056927", + NorthernIreland = "TestValue978329560", + Total = "TestValue540263270" + } + } + }, + CalcResultParameterOtherCost = new CalcResultParameterOtherCost + { + Name = "TestValue1902710147", + CalcResultParameterCommunicationCostDetails1 = new[] { + new CalcResultParameterOtherCostDetail + { + Name = "TestValue1903928028", + England = "TestValue326560834", + Wales = "TestValue210962167", + Scotland = "TestValue1135236797", + NorthernIreland = "TestValue367778577", + Total = "TestValue1608736613", + OrderId = 1551597643 + }, + new CalcResultParameterOtherCostDetail + { + Name = "TestValue173966944", + England = "TestValue1180419728", + Wales = "TestValue2100256493", + Scotland = "TestValue670954530", + NorthernIreland = "TestValue1375420925", + Total = "TestValue935375183", + OrderId = 1313038898 + }, + new CalcResultParameterOtherCostDetail + { + Name = "TestValue248451812", + England = "TestValue1601631800", + Wales = "TestValue1233261280", + Scotland = "TestValue704573910", + NorthernIreland = "TestValue1422759478", + Total = "TestValue458226108", + OrderId = 404263300 + } + }, + CalcResultParameterCommunicationCostDetails2 = new[] { + new CalcResultParameterOtherCostDetail + { + Name = "TestValue1709252156", + England = "TestValue421440618", + Wales = "TestValue1606335911", + Scotland = "TestValue1116454655", + NorthernIreland = "TestValue998991548", + Total = "TestValue1082576243", + OrderId = 1692958011 + }, + new CalcResultParameterOtherCostDetail + { + Name = "TestValue398103807", + England = "TestValue1159982855", + Wales = "TestValue146859551", + Scotland = "TestValue896679083", + NorthernIreland = "TestValue1259900179", + Total = "TestValue2046311069", + OrderId = 846588345 + }, + new CalcResultParameterOtherCostDetail + { + Name = "TestValue1327072438", + England = "TestValue1481875920", + Wales = "TestValue429197166", + Scotland = "TestValue231890081", + NorthernIreland = "TestValue1711519090", + Total = "TestValue895544970", + OrderId = 2137471720 + } + }, + CalcResultParameterCommunicationCostDetails3 = new[] { + new CalcResultParameterOtherCostDetail + { + Name = "TestValue894073467", + England = "TestValue743248036", + Wales = "TestValue498383751", + Scotland = "TestValue1414381785", + NorthernIreland = "TestValue1432360696", + Total = "TestValue1689198307", + OrderId = 1393543154 + }, + new CalcResultParameterOtherCostDetail + { + Name = "TestValue1735232539", + England = "TestValue2086893656", + Wales = "TestValue1788966664", + Scotland = "TestValue1703053729", + NorthernIreland = "TestValue82037162", + Total = "TestValue1498446847", + OrderId = 194675871 + }, + new CalcResultParameterOtherCostDetail + { + Name = "TestValue2052603863", + England = "TestValue752109903", + Wales = "TestValue18782142", + Scotland = "TestValue1516270676", + NorthernIreland = "TestValue526160370", + Total = "TestValue2006123279", + OrderId = 1923346784 + } + }, + CalcResultParameterCommunicationCostDetails4 = new KeyValuePair(), + CalcResultParameterCommunicationCostDetails5 = new[] { + new CalcResultParameterOtherCostDetail5 + { + SevenMateriality = "TestValue20436873", + Amount = "TestValue1953396941", + Percentage = "TestValue1921759094" + }, + new CalcResultParameterOtherCostDetail5 + { + SevenMateriality = "TestValue115520746", + Amount = "TestValue1036547761", + Percentage = "TestValue466450553" + }, + new CalcResultParameterOtherCostDetail5 + { + SevenMateriality = "TestValue1068863021", + Amount = "TestValue119755880", + Percentage = "TestValue804064114" + } + } + }, + CalcResultOnePlusFourApportionment = new CalcResultOnePlusFourApportionment + { + Name = "TestValue472683829", + CalcResultOnePlusFourApportionmentDetails = new[] { + new CalcResultOnePlusFourApportionmentDetail + { + Name = "TestValue1858724035", + Total = "TestValue1710164785", + EnglandDisposalTotal = "TestValue414275227", + WalesDisposalTotal = "TestValue815178689", + ScotlandDisposalTotal = "TestValue1825676229", + NorthernIrelandDisposalTotal = "TestValue1107952160", + TotalDisposalTotal = "TestValue1849556517", + OrderId = 1714114499 + }, + new CalcResultOnePlusFourApportionmentDetail + { + Name = "TestValue1540861583", + Total = "TestValue299414857", + EnglandDisposalTotal = "TestValue810354915", + WalesDisposalTotal = "TestValue1220572846", + ScotlandDisposalTotal = "TestValue505376535", + NorthernIrelandDisposalTotal = "TestValue1341109001", + TotalDisposalTotal = "TestValue1177863017", + OrderId = 547864222 + }, + new CalcResultOnePlusFourApportionmentDetail + { + Name = "TestValue651912473", + Total = "TestValue1421952222", + EnglandDisposalTotal = "TestValue729766017", + WalesDisposalTotal = "TestValue410415024", + ScotlandDisposalTotal = "TestValue863103052", + NorthernIrelandDisposalTotal = "TestValue1185358720", + TotalDisposalTotal = "TestValue1036905338", + OrderId = 214124936 + } + } + }, + CalcResultLaDisposalCostData = new CalcResultLaDisposalCostData + { + Name = "TestValue873636594", + CalcResultLaDisposalCostDetails = new[] { + new CalcResultParameterCostDetail + { + KeyName = "TestValue937334742", + Cost = 716867220.96M, + CategoryName = "TestValue1298861039", + CalcResultFormatterType = CalcResultFormatterType.None, + OrderId = 1222747754, + Precision = 324680133 + }, + new CalcResultParameterCostDetail + { + KeyName = "TestValue1615476659", + Cost = 1270001246.58M, + CategoryName = "TestValue1316282835", + CalcResultFormatterType = CalcResultFormatterType.Percentage, + OrderId = 519356024, + Precision = 1084171620 + }, + new CalcResultParameterCostDetail + { + KeyName = "TestValue1526980830", + Cost = 224658357.66M, + CategoryName = "TestValue1791641390", + CalcResultFormatterType = CalcResultFormatterType.None, + OrderId = 1949639824, + Precision = 1132782434 + } + } + } + }); + + // Act + var result = _testClass.PrepareCalcResults(resultsRequestDto); + + // Assert + _builder.Verify(mock => mock.Build(It.IsAny())); + } + } +} \ No newline at end of file diff --git a/src/EPR.Calculator.API/Builder/CalcResultBuilder.cs b/src/EPR.Calculator.API/Builder/CalcResultBuilder.cs new file mode 100644 index 0000000..40d86a1 --- /dev/null +++ b/src/EPR.Calculator.API/Builder/CalcResultBuilder.cs @@ -0,0 +1,26 @@ +using EPR.Calculator.API.Data; +using EPR.Calculator.API.Dtos; +using EPR.Calculator.API.Models; + +namespace EPR.Calculator.API.Builder +{ + public class CalcResultBuilder : ICalcResultBuilder + { + private readonly ICalcResultDetailBuilder calcResultDetailBuilder; + private readonly ICalcResultLapcapDataBuilder lapcapBuilder; + public CalcResultBuilder(ICalcResultDetailBuilder calcResultDetailBuilder, ICalcResultLapcapDataBuilder lapcapBuilder) + { + this.calcResultDetailBuilder = calcResultDetailBuilder; + this.lapcapBuilder = lapcapBuilder; + } + + public CalcResult Build(CalcResultsRequestDto resultsRequestDto) + { + var calcResult = new CalcResult(); + calcResult.CalcResultDetail = this.calcResultDetailBuilder.Construct(resultsRequestDto); + calcResult.CalcResultLapcapData = this.lapcapBuilder.Construct(resultsRequestDto); + + return calcResult; + } + } +} diff --git a/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs b/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs new file mode 100644 index 0000000..9af75ac --- /dev/null +++ b/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs @@ -0,0 +1,20 @@ +using EPR.Calculator.API.Data; +using EPR.Calculator.API.Dtos; +using EPR.Calculator.API.Models; + +namespace EPR.Calculator.API.Builder +{ + public class CalcResultDetailBuilder : ICalcResultDetailBuilder + { + private readonly ApplicationDBContext context; + public CalcResultDetailBuilder(ApplicationDBContext context) + { + this.context = context; + } + + public CalcResultDetail Construct(CalcResultsRequestDto resultsRequestDto) + { + return new CalcResultDetail(); + } + } +} \ No newline at end of file diff --git a/src/EPR.Calculator.API/Builder/CalcResultLapcapDataBuilder.cs b/src/EPR.Calculator.API/Builder/CalcResultLapcapDataBuilder.cs new file mode 100644 index 0000000..ddc3b02 --- /dev/null +++ b/src/EPR.Calculator.API/Builder/CalcResultLapcapDataBuilder.cs @@ -0,0 +1,21 @@ +using EPR.Calculator.API.Data; +using EPR.Calculator.API.Dtos; +using EPR.Calculator.API.Models; + +namespace EPR.Calculator.API.Builder +{ + public class CalcResultLapcapDataBuilder : ICalcResultLapcapDataBuilder + { + private readonly ApplicationDBContext context; + public CalcResultLapcapDataBuilder(ApplicationDBContext context) + { + this.context = context; + } + + public CalcResultLapcapData Construct(CalcResultsRequestDto resultsRequestDto) + { + var data = new List(); + return new CalcResultLapcapData { Name = "LAPCAP Data", CalcResultLapcapDataDetails = data }; + } + } +} diff --git a/src/EPR.Calculator.API/Builder/ICalcResultBuilder.cs b/src/EPR.Calculator.API/Builder/ICalcResultBuilder.cs new file mode 100644 index 0000000..68cd157 --- /dev/null +++ b/src/EPR.Calculator.API/Builder/ICalcResultBuilder.cs @@ -0,0 +1,10 @@ +using EPR.Calculator.API.Dtos; +using EPR.Calculator.API.Models; + +namespace EPR.Calculator.API.Builder +{ + public interface ICalcResultBuilder + { + public CalcResult Build(CalcResultsRequestDto resultsRequestDto); + } +} diff --git a/src/EPR.Calculator.API/Builder/ICalcResultDetailBuilder.cs b/src/EPR.Calculator.API/Builder/ICalcResultDetailBuilder.cs new file mode 100644 index 0000000..0b9e476 --- /dev/null +++ b/src/EPR.Calculator.API/Builder/ICalcResultDetailBuilder.cs @@ -0,0 +1,10 @@ +using EPR.Calculator.API.Dtos; +using EPR.Calculator.API.Models; + +namespace EPR.Calculator.API.Builder +{ + public interface ICalcResultDetailBuilder + { + public CalcResultDetail Construct(CalcResultsRequestDto resultsRequestDto); + } +} \ No newline at end of file diff --git a/src/EPR.Calculator.API/Builder/ICalcResultLapcapDataBuilder.cs b/src/EPR.Calculator.API/Builder/ICalcResultLapcapDataBuilder.cs new file mode 100644 index 0000000..73ce7a0 --- /dev/null +++ b/src/EPR.Calculator.API/Builder/ICalcResultLapcapDataBuilder.cs @@ -0,0 +1,10 @@ +using EPR.Calculator.API.Dtos; +using EPR.Calculator.API.Models; + +namespace EPR.Calculator.API.Builder +{ + public interface ICalcResultLapcapDataBuilder + { + public CalcResultLapcapData Construct(CalcResultsRequestDto resultsRequestDto); + } +} diff --git a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs index fb98913..4547855 100644 --- a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs +++ b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs @@ -1,7 +1,9 @@ -using EPR.Calculator.API.Data; +using EPR.Calculator.API.Builder; +using EPR.Calculator.API.Data; using EPR.Calculator.API.Data.DataModels; using EPR.Calculator.API.Dtos; using EPR.Calculator.API.Enums; +using EPR.Calculator.API.Exporter; using EPR.Calculator.API.Models; using EPR.Calculator.API.Validators; using EPR.Calculator.API.Wrapper; @@ -16,14 +18,20 @@ public class CalculatorInternalController : ControllerBase private readonly ApplicationDBContext context; private readonly IRpdStatusDataValidator rpdStatusDataValidator; private readonly IOrgAndPomWrapper wrapper; + private readonly ICalcResultBuilder builder; + private readonly ICalcResultsExporter exporter; public CalculatorInternalController(ApplicationDBContext context, IRpdStatusDataValidator rpdStatusDataValidator, - IOrgAndPomWrapper wrapper) + IOrgAndPomWrapper wrapper, + ICalcResultBuilder builder, + ICalcResultsExporter exporter) { this.context = context; this.rpdStatusDataValidator = rpdStatusDataValidator; this.wrapper = wrapper; + this.builder = builder; + this.exporter = exporter; } [HttpPost] @@ -133,5 +141,14 @@ public IActionResult UpdateRpdStatus([FromBody] UpdateRpdStatus request) } } } + + [HttpPost] + [Route("prepareCalcResults")] + public IActionResult PrepareCalcResults([FromBody] CalcResultsRequestDto resultsRequestDto) + { + var results = this.builder.Build(resultsRequestDto); + this.exporter.Export(results); + return new ObjectResult(null) { StatusCode = StatusCodes.Status201Created }; + } } } diff --git a/src/EPR.Calculator.API/Dtos/CalcResultsRequestDto.cs b/src/EPR.Calculator.API/Dtos/CalcResultsRequestDto.cs new file mode 100644 index 0000000..7ddd425 --- /dev/null +++ b/src/EPR.Calculator.API/Dtos/CalcResultsRequestDto.cs @@ -0,0 +1,7 @@ +namespace EPR.Calculator.API.Dtos +{ + public class CalcResultsRequestDto + { + public int RunId { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs b/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs new file mode 100644 index 0000000..090a859 --- /dev/null +++ b/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs @@ -0,0 +1,12 @@ +using EPR.Calculator.API.Models; + +namespace EPR.Calculator.API.Exporter +{ + public class CalcResultsExporter : ICalcResultsExporter + { + public void Export(CalcResult results) + { + // Code to export the File to Csv + } + } +} diff --git a/src/EPR.Calculator.API/Exporter/ICalcResultsExporter.cs b/src/EPR.Calculator.API/Exporter/ICalcResultsExporter.cs new file mode 100644 index 0000000..895d147 --- /dev/null +++ b/src/EPR.Calculator.API/Exporter/ICalcResultsExporter.cs @@ -0,0 +1,7 @@ +namespace EPR.Calculator.API.Exporter +{ + public interface ICalcResultsExporter + { + public void Export(T results); + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResult.cs b/src/EPR.Calculator.API/Models/CalcResult.cs new file mode 100644 index 0000000..4b797b6 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResult.cs @@ -0,0 +1,19 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResult + { + public CalcResultDetail CalcResultDetail { get; set; } + + public CalcResultLapcapData CalcResultLapcapData { get; set; } + + public CalcResultLateReportingTonnage CalcResultLateReportingTonnageDetail { get; set; } + + public CalcResultParameterCommunicationCost CalcResultParameterCommunicationCost { get; set; } + + public CalcResultParameterOtherCost CalcResultParameterOtherCost { get; set; } + + public CalcResultOnePlusFourApportionment CalcResultOnePlusFourApportionment { get; set; } + + public CalcResultLaDisposalCostData CalcResultLaDisposalCostData { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResult1Plus4Apportionment.cs b/src/EPR.Calculator.API/Models/CalcResult1Plus4Apportionment.cs new file mode 100644 index 0000000..61377c4 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResult1Plus4Apportionment.cs @@ -0,0 +1,8 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResult1Plus4Apportionment + { + public IEnumerable CalcResultParameterCommunicationCostDetails { get; set; } = + new List(); + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultDetail.cs b/src/EPR.Calculator.API/Models/CalcResultDetail.cs new file mode 100644 index 0000000..acdd8dd --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultDetail.cs @@ -0,0 +1,14 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultDetail + { + public string RunName { get; set; } = string.Empty; + public int RunId { get; set; } + public DateTime RunDate { get; set; } + public string RunBy { get; set; } = string.Empty; + public string FinancialYear { get; set; } = string.Empty; + public string RpdFile { get; set; } = string.Empty; + public string LapcapFile { get; set; } = string.Empty; + public string ParametersFile { get; set; } = string.Empty; + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultFormatterType.cs b/src/EPR.Calculator.API/Models/CalcResultFormatterType.cs new file mode 100644 index 0000000..4dfb2fa --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultFormatterType.cs @@ -0,0 +1,9 @@ +namespace EPR.Calculator.API.Models +{ + public enum CalcResultFormatterType + { + None = 1, + Currency, + Percentage + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultLaDisposalCostData.cs b/src/EPR.Calculator.API/Models/CalcResultLaDisposalCostData.cs new file mode 100644 index 0000000..3382482 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultLaDisposalCostData.cs @@ -0,0 +1,8 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultLaDisposalCostData + { + public string Name { get; set; } + public IEnumerable CalcResultLaDisposalCostDetails { get; set; } = new List(); + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultLaDisposalCostDataDetail.cs b/src/EPR.Calculator.API/Models/CalcResultLaDisposalCostDataDetail.cs new file mode 100644 index 0000000..f882b5b --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultLaDisposalCostDataDetail.cs @@ -0,0 +1,21 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultLaDisposalCostDataDetail + { + public string Name { get; set; } + public string Material { get; set; } + public string England { get; set; } + public string Wales { get; set; } + public string Scotland { get; set; } + public string NorthernIreland { get; set; } + public string Total { get; set; } + + public string ProducerReportedHouseholdPackagingWasteTonnage { get; set; } + + public string LateReportingTonnage { get; set; } + + public string ProducerReportedHouseholdTonnagePlusLateReportingTonnage { get; set; } + + public string DisposalCostPricePerTonne { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs b/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs new file mode 100644 index 0000000..4ee4339 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultLapcapData.cs @@ -0,0 +1,8 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultLapcapData + { + public string Name { get; set; } = string.Empty; + public required IEnumerable? CalcResultLapcapDataDetails { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultLapcapDataDetails.cs b/src/EPR.Calculator.API/Models/CalcResultLapcapDataDetails.cs new file mode 100644 index 0000000..034f131 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultLapcapDataDetails.cs @@ -0,0 +1,19 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultLapcapDataDetails + { + public string Name { get; set; } + + public string EnglandDisposalCost { get; set; } + + public string WalesDisposalCost { get; set; } + + public string ScotlandDisposalCost { get; set; } + + public string NorthernIrelandDisposalCost { get; set; } + + public string TotalDisposalCost { get; set; } + + public int OrderId { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnage.cs b/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnage.cs new file mode 100644 index 0000000..d959c32 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnage.cs @@ -0,0 +1,8 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultLateReportingTonnage + { + public string Name { get; set; } + public required IEnumerable? CalcResultLateReportingTonnageDetails { get; set; } = new List(); + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnageDetail.cs b/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnageDetail.cs new file mode 100644 index 0000000..a0605ec --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultLateReportingTonnageDetail.cs @@ -0,0 +1,8 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultLateReportingTonnageDetail + { + public required string Name { get; set; } + public required decimal TotalLateReportingTonnage { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultOnePlusFourApportionment.cs b/src/EPR.Calculator.API/Models/CalcResultOnePlusFourApportionment.cs new file mode 100644 index 0000000..2d979c9 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultOnePlusFourApportionment.cs @@ -0,0 +1,8 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultOnePlusFourApportionment + { + public string Name { get; set; } + public IEnumerable CalcResultOnePlusFourApportionmentDetails { get; set; } = new List(); + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultOnePlusFourApportionmentDetail.cs b/src/EPR.Calculator.API/Models/CalcResultOnePlusFourApportionmentDetail.cs new file mode 100644 index 0000000..131e197 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultOnePlusFourApportionmentDetail.cs @@ -0,0 +1,14 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultOnePlusFourApportionmentDetail + { + public string Name { get; set; } + public string Total { get; set; } + public string EnglandDisposalTotal { get; set; } + public string WalesDisposalTotal { get; set; } + public string ScotlandDisposalTotal { get; set; } + public string NorthernIrelandDisposalTotal { get; set; } + public string TotalDisposalTotal { get; set; } + public int OrderId { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCost.cs b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCost.cs new file mode 100644 index 0000000..86b3aba --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCost.cs @@ -0,0 +1,15 @@ +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace EPR.Calculator.API.Models +{ + public class CalcResultParameterCommunicationCost + { + public string Name { get; set; } + public IEnumerable CalcResultParameterCommunicationCostDetails { get; set; } = + new List(); + public IEnumerable CalcResultParameterCommunicationCostDetails2 { get; set; } = + new List(); + public IEnumerable CalcResultParameterCommunicationCostDetails3 { get; set; } = + new List(); + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail.cs b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail.cs new file mode 100644 index 0000000..be1a834 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail.cs @@ -0,0 +1,11 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultParameterCommunicationCostDetail + { + public required string Material { get; set; } + public required decimal Cost { get; set; } + public required string Country { get; set; } + public bool IsApportionment { get; set; } + public bool IsPercentage { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail1.cs b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail1.cs new file mode 100644 index 0000000..633a862 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail1.cs @@ -0,0 +1,13 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultParameterCommunicationCostDetail1 + { + public string Name { get; set; } + public string England { get; set; } + public string Wales { get; set; } + public string Scotland { get; set; } + public string NorthernIreland { get; set; } + public string Total { get; set; } + public int OrderId { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail2.cs b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail2.cs new file mode 100644 index 0000000..326ec5a --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail2.cs @@ -0,0 +1,18 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultParameterCommunicationCostDetail2 + { + public string Name { get; set; } + public string England { get; set; } + public string Wales { get; set; } + public string Scotland { get; set; } + public string NorthernIreland { get; set; } + public string Total { get; set; } + public int OrderId { get; set; } + public string ProducerReportedHouseholdPackagingWasteTonnage { get; set; } + public string LateReportingTonnage { get; set; } + public string ProducerReportedHouseholdTonnagePlusLateReportingTonnage { get; set; } + public string CommsCostByMaterialPricePerTonne { get; set; } + + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail3.cs b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail3.cs new file mode 100644 index 0000000..dc0f36f --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultParameterCommunicationCostDetail3.cs @@ -0,0 +1,12 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultParameterCommunicationCostDetail3 + { + public string Name { get; set; } + public string England { get; set; } + public string Wales { get; set; } + public string Scotland { get; set; } + public string NorthernIreland { get; set; } + public string Total { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs b/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs new file mode 100644 index 0000000..616ce38 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultParameterCostDetail.cs @@ -0,0 +1,22 @@ +using System.Runtime.Serialization.Formatters; + +namespace EPR.Calculator.API.Models +{ + + public class CalcResultParameterCostDetail + { + /// + /// KeyName can be a Material name or Cost name + /// + public string KeyName { get; set; } = string.Empty; + public decimal Cost { get; set; } + /// + /// Category Name can be the Country name + /// + public string CategoryName { get; set; } = string.Empty; + public CalcResultFormatterType CalcResultFormatterType { get; set; } + public int OrderId { get; set; } + + public int Precision { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterOtherCost.cs b/src/EPR.Calculator.API/Models/CalcResultParameterOtherCost.cs new file mode 100644 index 0000000..f3c0ff6 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultParameterOtherCost.cs @@ -0,0 +1,19 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultParameterOtherCost + { + public string Name { get; set; } + public IEnumerable CalcResultParameterCommunicationCostDetails1 { get; set; } = + new List(); + public IEnumerable CalcResultParameterCommunicationCostDetails2 { get; set; } = + new List(); + + public IEnumerable CalcResultParameterCommunicationCostDetails3 { get; set; } = + new List(); + + public KeyValuePair CalcResultParameterCommunicationCostDetails4 { get; set; } = new KeyValuePair(); + + public IEnumerable CalcResultParameterCommunicationCostDetails5 { get; set; } = + new List(); + } +} \ No newline at end of file diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterOtherCostDetail.cs b/src/EPR.Calculator.API/Models/CalcResultParameterOtherCostDetail.cs new file mode 100644 index 0000000..fb76df1 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultParameterOtherCostDetail.cs @@ -0,0 +1,13 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultParameterOtherCostDetail + { + public string Name { get; set; } + public string England { get; set; } + public string Wales { get; set; } + public string Scotland { get; set; } + public string NorthernIreland { get; set; } + public string Total { get; set; } + public int OrderId { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultParameterOtherCostDetail5.cs b/src/EPR.Calculator.API/Models/CalcResultParameterOtherCostDetail5.cs new file mode 100644 index 0000000..b1c6070 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultParameterOtherCostDetail5.cs @@ -0,0 +1,9 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultParameterOtherCostDetail5 + { + public string SevenMateriality { get; set; } + public string Amount { get; set; } + public string Percentage { get; set; } + } +} \ No newline at end of file diff --git a/src/EPR.Calculator.API/Models/CalcResultSummary.cs b/src/EPR.Calculator.API/Models/CalcResultSummary.cs new file mode 100644 index 0000000..f2b3551 --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultSummary.cs @@ -0,0 +1,7 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultSummary + { + public IEnumerable CalcResultSummaryDetails { get; set; } + } +} diff --git a/src/EPR.Calculator.API/Models/CalcResultSummaryDetail.cs b/src/EPR.Calculator.API/Models/CalcResultSummaryDetail.cs new file mode 100644 index 0000000..62fb69d --- /dev/null +++ b/src/EPR.Calculator.API/Models/CalcResultSummaryDetail.cs @@ -0,0 +1,12 @@ +namespace EPR.Calculator.API.Models +{ + public class CalcResultSummaryDetail + { + public int ProducerId { get; set; } + public string SubsidaryId { get; set; } + public string SubsidaryName { get; set; } + public decimal HouseholdPackagingWasteTonnage { get; set; } + public decimal SelfManagedConsumerWasteTonnage { get; set; } + + } +} diff --git a/src/EPR.Calculator.API/Program.cs b/src/EPR.Calculator.API/Program.cs index e4b9d19..3833a81 100644 --- a/src/EPR.Calculator.API/Program.cs +++ b/src/EPR.Calculator.API/Program.cs @@ -1,6 +1,9 @@ using Azure.Messaging.ServiceBus; +using EPR.Calculator.API.Builder; using EPR.Calculator.API.Data; using EPR.Calculator.API.Exceptions; +using EPR.Calculator.API.Exporter; +using EPR.Calculator.API.Models; using EPR.Calculator.API.Validators; using EPR.Calculator.API.Wrapper; using FluentValidation; @@ -22,6 +25,10 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped, CalcResultsExporter>(); +builder.Services.AddScoped(); builder.Services.AddValidatorsFromAssemblyContaining(); builder.Services.AddDbContext(options => From 729d5cfe1254d2326b9af647c2f39feada2cc820 Mon Sep 17 00:00:00 2001 From: johnbenjamin-defra Date: Mon, 4 Nov 2024 16:16:43 +0000 Subject: [PATCH 11/29] 459419 Transpose pom and org data --- .../Controllers/CalculatorController.cs | 2 + .../Services/DataService.cs | 13 +++ .../Services/TransposePomAndOrgDataService.cs | 110 +++++++++--------- 3 files changed, 73 insertions(+), 52 deletions(-) create mode 100644 src/EPR.Calculator.API/Services/DataService.cs diff --git a/src/EPR.Calculator.API/Controllers/CalculatorController.cs b/src/EPR.Calculator.API/Controllers/CalculatorController.cs index c62efe5..3f1802b 100644 --- a/src/EPR.Calculator.API/Controllers/CalculatorController.cs +++ b/src/EPR.Calculator.API/Controllers/CalculatorController.cs @@ -4,6 +4,7 @@ using EPR.Calculator.API.Dtos; using EPR.Calculator.API.Enums; using EPR.Calculator.API.Models; +using EPR.Calculator.API.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Azure; @@ -173,6 +174,7 @@ public IActionResult GetCalculatorRuns([FromBody] CalculatorRunsParamsDto reques [Route("calculatorRuns/{id}")] public IActionResult GetCalculatorRun() { + TransposePomAndOrgDataService.Transpose(context, 58); // TODO: Return the details of a particular run return new OkResult(); } diff --git a/src/EPR.Calculator.API/Services/DataService.cs b/src/EPR.Calculator.API/Services/DataService.cs new file mode 100644 index 0000000..312770e --- /dev/null +++ b/src/EPR.Calculator.API/Services/DataService.cs @@ -0,0 +1,13 @@ +using EPR.Calculator.API.Data; +using EPR.Calculator.API.Data.DataModels; + +namespace EPR.Calculator.API.Services +{ + public static class DataService + { + public static IEnumerable GetPomDataDetails(ApplicationDBContext context, int pomDataMasterId) + { + return context.CalculatorRunPomDataDetails.Where(pdd => pdd.CalculatorRunPomDataMasterId == pomDataMasterId).ToList(); + } + } +} diff --git a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs index 88bda77..ca89cec 100644 --- a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs +++ b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs @@ -9,69 +9,75 @@ public static void Transpose(ApplicationDBContext context, int runId) { var materials = context.Material.ToList(); - var pomDataMaster = context.CalculatorRunPomDataMaster.Single(pdm => pdm.Id == runId); - - var pomDataDetails = context.CalculatorRunPomDataDetails.Where(pdd => pdd.CalculatorRunPomDataMasterId == pomDataMaster.Id).ToList(); - var calculatorRun = context.CalculatorRuns.Single(cr => cr.Id == runId); - var producerDetails = new List(); - - var producerReportedMaterials = new List(); - - using (var transaction = context.Database.BeginTransaction()) + if (calculatorRun.CalculatorRunPomDataMasterId != null) { - try + using (var transaction = context.Database.BeginTransaction()) { - foreach (var pom in pomDataDetails) + try { - if (pom.OrganisationId != null) - { - var organisationDataMaster = context.CalculatorRunOrganisationDataMaster.Single(odm => odm.Id == runId); - - var organisationDataDetails = context.CalculatorRunOrganisationDataDetails.Where(odd => odd.CalculatorRunOrganisationDataMasterId == organisationDataMaster.Id).ToList(); - - var producerName = organisationDataDetails.Single(odd => odd.OrganisationId == pom.OrganisationId).OrganisationName; - - var producerDetail = new ProducerDetail - { - CalculatorRunId = runId, - ProducerId = pom.OrganisationId, - SubsidiaryId = pom.SubsidaryId, - ProducerName = producerName, - CalculatorRun = calculatorRun - }; - - producerDetails.Add(producerDetail); + var producerDetails = new List(); + var producerReportedMaterials = new List(); - context.ProducerDetail.Add(producerDetail); - context.SaveChanges(); + var pomDataDetails = DataService.GetPomDataDetails(context, (int)calculatorRun.CalculatorRunPomDataMasterId); - var material = materials.Single(m => m.Code == pom.PackagingMaterial); - - var producerReportedMaterial = new ProducerReportedMaterial + foreach (var pom in pomDataDetails) + { + if (pom.OrganisationId.HasValue) { - MaterialId = material.Id, - Material = material, - ProducerDetailId = producerDetail.Id, - ProducerDetail = producerDetail, - PackagingType = pom.PackagingType, - PackagingTonnage = (decimal)pom.PackagingMaterialWeight / 1000, - }; - - producerReportedMaterials.Add(producerReportedMaterial); + var organisationDataMaster = context.CalculatorRunOrganisationDataMaster.Single(odm => odm.Id == calculatorRun.CalculatorRunOrganisationDataMasterId); + + var organisationDataDetails = context.CalculatorRunOrganisationDataDetails.Where + ( + odd => odd.CalculatorRunOrganisationDataMasterId == organisationDataMaster.Id + ).ToList(); + + var producerName = organisationDataDetails.Single(odd => odd.OrganisationId == pom.OrganisationId)?.OrganisationName; + + if (producerName != null) + { + var producerDetail = new ProducerDetail + { + CalculatorRunId = runId, + ProducerId = pom.OrganisationId.Value, + SubsidiaryId = pom.SubsidaryId, + ProducerName = producerName, + CalculatorRun = calculatorRun + }; + + producerDetails.Add(producerDetail); + + context.ProducerDetail.Add(producerDetail); + context.SaveChanges(); + + var material = materials.Single(m => m.Code == pom.PackagingMaterial); + + var producerReportedMaterial = new ProducerReportedMaterial + { + MaterialId = material.Id, + Material = material, + ProducerDetailId = producerDetail.Id, + ProducerDetail = producerDetail, + PackagingType = pom.PackagingType, + PackagingTonnage = (decimal)pom.PackagingMaterialWeight / 1000, + }; + + producerReportedMaterials.Add(producerReportedMaterial); + } + } } - } - context.ProducerReportedMaterial.AddRange(producerReportedMaterials); - context.SaveChanges(); + context.ProducerReportedMaterial.AddRange(producerReportedMaterials); + context.SaveChanges(); - transaction.Commit(); - } - catch (Exception exception) - { - // Error, rollback transaction - transaction.Rollback(); + transaction.Commit(); + } + catch (Exception exception) + { + // Error, rollback transaction + transaction.Rollback(); + } } } } From 6adae67bc39f25114394cdc904166a9d50f7114d Mon Sep 17 00:00:00 2001 From: johnbenjamin-defra Date: Mon, 4 Nov 2024 19:34:45 +0000 Subject: [PATCH 12/29] 459419 Transpose pom and org data --- .../BaseControllerTest.cs | 106 +++++++++++++++++- .../TransposePomAndOrgDataServiceTests.cs | 63 +++++++++++ .../Services/TransposePomAndOrgDataService.cs | 1 - 3 files changed, 167 insertions(+), 3 deletions(-) create mode 100644 src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs diff --git a/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs b/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs index 41c8301..3a3321d 100644 --- a/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs +++ b/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs @@ -65,11 +65,18 @@ public void SetUp() mockFactory.Setup(m => m.CreateClient(It.IsAny())).Returns(mockClient.Object); - - dbContext.CalculatorRuns.AddRange(GetCalculatorRuns()); dbContext.SaveChanges(); calculatorController = new CalculatorController(dbContext, ConfigurationItems.GetConfigurationValues(), mockFactory.Object); + + dbContext.Material.AddRange(GetMaterials()); + dbContext.SaveChanges(); + + dbContext.PomData.AddRange(GetPomData()); + dbContext.SaveChanges(); + + dbContext.OrganisationData.AddRange(GetOrganisationData()); + dbContext.SaveChanges(); } public void CheckDbContext() @@ -711,5 +718,100 @@ protected static IEnumerable GetCalculatorRuns() }); return list; } + + protected static IEnumerable GetMaterials() + { + var list = new List(); + list.Add(new Material + { + Id = 1, + Code = "AL", + Name = "Aluminium", + Description = "Aluminium" + }); + list.Add(new Material + { + Id = 2, + Code = "FC", + Name = "Fibre composite", + Description = "Fibre composite" + }); + list.Add(new Material + { + Id = 3, + Code = "GL", + Name = "Glass", + Description = "Glass" + }); + list.Add(new Material + { + Id = 4, + Code = "PC", + Name = "Paper or card", + Description = "Paper or card" + }); + list.Add(new Material + { + Id = 5, + Code = "PL", + Name = "Plastic", + Description = "Plastic" + }); + list.Add(new Material + { + Id = 6, + Code = "ST", + Name = "Steel", + Description = "Steel" + }); + list.Add(new Material + { + Id = 7, + Code = "WD", + Name = "Wood", + Description = "Wood" + }); + list.Add(new Material + { + Id = 8, + Code = "OT", + Name = "Other materials", + Description = "Other materials" + }); + return list; + } + + protected static IEnumerable GetPomData() + { + var list = new List(); + list.Add(new PomData + { + OrganisationId = 1, + SubsidaryId = "SUBSID1", + SubmissionPeriod = "2023-P3", + PackagingActivity = null, + PackagingType = "CW", + PackagingClass = "O1", + PackagingMaterial = "PC", + PackagingMaterialWeight = 1000, + LoadTimeStamp = DateTime.Now, + SubmissionPeriodDesc = "July to December 2023" + }); + return list; + } + + protected static IEnumerable GetOrganisationData() + { + var list = new List(); + list.Add(new OrganisationData + { + OrganisationId = 1, + SubsidaryId = "SUBSID1", + OrganisationName = "UPU LIMITED", + LoadTimestamp = DateTime.Now, + SubmissionPeriodDesc = "January to June 2023" + }); + return list; + } } } diff --git a/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs b/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs new file mode 100644 index 0000000..f66fb58 --- /dev/null +++ b/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs @@ -0,0 +1,63 @@ +using EPR.Calculator.API.Data.DataModels; +using EPR.Calculator.API.Services; +using EPR.Calculator.API.Tests.Controllers; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace EPR.Calculator.API.UnitTests.Services +{ + [TestClass] + public class TransposePomAndOrgDataServiceTests : BaseControllerTest + { + [TestMethod] + public void Transpose_Should_Return_Correct_Producer_Detail() + { + var expectedResult = new ProducerDetail + { + Id = 1, + ProducerId = 1, + SubsidiaryId = "SUBSID1", + ProducerName = "UPU LIMITED", + CalculatorRunId = 1, + CalculatorRun = new CalculatorRun() + }; + + TransposePomAndOrgDataService.Transpose(dbContext, 1); + + var producerDetail = dbContext.ProducerDetail.FirstOrDefault(); + Assert.AreEqual(expectedResult, producerDetail); + } + + [TestMethod] + public void Transpose_Should_Return_Correct_Producer_Reported_Material() + { + var expectedResult = new ProducerReportedMaterial + { + Id = 1, + MaterialId = 4, + ProducerDetailId = 1, + PackagingType = "CW", + PackagingTonnage = 1, + Material = new Material{ + Id = 1, + Code = "AL", + Name = "Aluminium", + Description = "Aluminium" + }, + ProducerDetail = new ProducerDetail + { + Id = 1, + ProducerId = 1, + SubsidiaryId = "SUBSID1", + ProducerName = "UPU LIMITED", + CalculatorRunId = 1, + CalculatorRun = new CalculatorRun() + } + }; + + TransposePomAndOrgDataService.Transpose(dbContext, 1); + + var producerReportedMaterial = dbContext.ProducerReportedMaterial.FirstOrDefault(); + Assert.AreEqual(expectedResult, producerReportedMaterial); + } + } +} diff --git a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs index ca89cec..5f31b0c 100644 --- a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs +++ b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs @@ -49,7 +49,6 @@ public static void Transpose(ApplicationDBContext context, int runId) producerDetails.Add(producerDetail); context.ProducerDetail.Add(producerDetail); - context.SaveChanges(); var material = materials.Single(m => m.Code == pom.PackagingMaterial); From 37b8d2777548a323f8a34bb6479a38f3efdf0082 Mon Sep 17 00:00:00 2001 From: johnbenjamin-defra Date: Tue, 5 Nov 2024 10:35:00 +0000 Subject: [PATCH 13/29] Update CalculatorInternalController.cs --- .../Controllers/CalculatorInternalController.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs index 4154d63..c7c8934 100644 --- a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs +++ b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs @@ -147,6 +147,7 @@ public IActionResult UpdateRpdStatus([FromBody] UpdateRpdStatus request) [Route("prepareCalcResults")] public IActionResult PrepareCalcResults([FromBody] CalcResultsRequestDto resultsRequestDto) { + TransposePomAndOrgDataService.Transpose(context, resultsRequestDto.RunId); var results = this.builder.Build(resultsRequestDto); this.exporter.Export(results); return new ObjectResult(null) { StatusCode = StatusCodes.Status201Created }; From 52162601018ab1578facabdc57507b2a44e74e70 Mon Sep 17 00:00:00 2001 From: udaydefra Date: Tue, 5 Nov 2024 10:50:41 +0000 Subject: [PATCH 14/29] Moving TransposePomAndOrgDataService to a concrete instance and Some unit tests --- .../TransposePomAndOrgDataServiceTests.cs | 28 ++++++++++++++++++- .../CalculatorInternalController.cs | 7 +++-- src/EPR.Calculator.API/Program.cs | 2 ++ .../ITransposePomAndOrgDataService.cs | 7 +++++ .../Services/TransposePomAndOrgDataService.cs | 26 +++++++++++++---- 5 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 src/EPR.Calculator.API/Services/ITransposePomAndOrgDataService.cs diff --git a/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs b/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs index f66fb58..f756fba 100644 --- a/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs +++ b/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs @@ -1,7 +1,9 @@ using EPR.Calculator.API.Data.DataModels; using EPR.Calculator.API.Services; using EPR.Calculator.API.Tests.Controllers; +using EPR.Calculator.API.Wrapper; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; namespace EPR.Calculator.API.UnitTests.Services { @@ -21,7 +23,31 @@ public void Transpose_Should_Return_Correct_Producer_Detail() CalculatorRun = new CalculatorRun() }; - TransposePomAndOrgDataService.Transpose(dbContext, 1); + var pomDataList = new List(); + pomDataList.Add(new PomData + { + OrganisationId = 1, + SubsidaryId = "SUBSID1", + SubmissionPeriod = "2023-P3", + PackagingActivity = null, + PackagingType = "CW", + PackagingClass = "O1", + PackagingMaterial = "PC", + PackagingMaterialWeight = 1000, + LoadTimeStamp = DateTime.Now, + SubmissionPeriodDesc = "July to December 2023" + }); + +# + var mockWrapper = new Mock(); + + mockWrapper.Setup(x => x.GetPomData()).Returns(pomDataList); + + var service = new TransposePomAndOrgDataService(dbContext, mockWrapper.Object); + + service.Transpose(1); + + var producerDetail = dbContext.ProducerDetail.FirstOrDefault(); Assert.AreEqual(expectedResult, producerDetail); diff --git a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs index c7c8934..ccc17a4 100644 --- a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs +++ b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs @@ -21,18 +21,21 @@ public class CalculatorInternalController : ControllerBase private readonly IOrgAndPomWrapper wrapper; private readonly ICalcResultBuilder builder; private readonly ICalcResultsExporter exporter; + private readonly ITransposePomAndOrgDataService transposePomAndOrgDataService; public CalculatorInternalController(ApplicationDBContext context, IRpdStatusDataValidator rpdStatusDataValidator, IOrgAndPomWrapper wrapper, ICalcResultBuilder builder, - ICalcResultsExporter exporter) + ICalcResultsExporter exporter, + ITransposePomAndOrgDataService transposePomAndOrgDataService) { this.context = context; this.rpdStatusDataValidator = rpdStatusDataValidator; this.wrapper = wrapper; this.builder = builder; this.exporter = exporter; + this.transposePomAndOrgDataService = transposePomAndOrgDataService; } [HttpPost] @@ -147,7 +150,7 @@ public IActionResult UpdateRpdStatus([FromBody] UpdateRpdStatus request) [Route("prepareCalcResults")] public IActionResult PrepareCalcResults([FromBody] CalcResultsRequestDto resultsRequestDto) { - TransposePomAndOrgDataService.Transpose(context, resultsRequestDto.RunId); + this.transposePomAndOrgDataService.Transpose(resultsRequestDto.RunId); var results = this.builder.Build(resultsRequestDto); this.exporter.Export(results); return new ObjectResult(null) { StatusCode = StatusCodes.Status201Created }; diff --git a/src/EPR.Calculator.API/Program.cs b/src/EPR.Calculator.API/Program.cs index 3833a81..3438944 100644 --- a/src/EPR.Calculator.API/Program.cs +++ b/src/EPR.Calculator.API/Program.cs @@ -4,6 +4,7 @@ using EPR.Calculator.API.Exceptions; using EPR.Calculator.API.Exporter; using EPR.Calculator.API.Models; +using EPR.Calculator.API.Services; using EPR.Calculator.API.Validators; using EPR.Calculator.API.Wrapper; using FluentValidation; @@ -29,6 +30,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped, CalcResultsExporter>(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddValidatorsFromAssemblyContaining(); builder.Services.AddDbContext(options => diff --git a/src/EPR.Calculator.API/Services/ITransposePomAndOrgDataService.cs b/src/EPR.Calculator.API/Services/ITransposePomAndOrgDataService.cs new file mode 100644 index 0000000..7fe9a71 --- /dev/null +++ b/src/EPR.Calculator.API/Services/ITransposePomAndOrgDataService.cs @@ -0,0 +1,7 @@ +namespace EPR.Calculator.API.Services +{ + public interface ITransposePomAndOrgDataService + { + public void Transpose(int runId); + } +} diff --git a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs index 5f31b0c..05ba150 100644 --- a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs +++ b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs @@ -1,26 +1,40 @@ using EPR.Calculator.API.Data; using EPR.Calculator.API.Data.DataModels; +using EPR.Calculator.API.Wrapper; namespace EPR.Calculator.API.Services { - public static class TransposePomAndOrgDataService + + public class TransposePomAndOrgDataService : ITransposePomAndOrgDataService { - public static void Transpose(ApplicationDBContext context, int runId) + private readonly ApplicationDBContext context; + private readonly IOrgAndPomWrapper wrapper; + + public TransposePomAndOrgDataService(ApplicationDBContext context, IOrgAndPomWrapper wrapper) + { + this.context = context; + this.wrapper = wrapper; + } + + public void Transpose(int runId) { - var materials = context.Material.ToList(); + var materials = this.context.Material.ToList(); - var calculatorRun = context.CalculatorRuns.Single(cr => cr.Id == runId); + var calculatorRun = this.context.CalculatorRuns.Single(cr => cr.Id == runId); if (calculatorRun.CalculatorRunPomDataMasterId != null) { - using (var transaction = context.Database.BeginTransaction()) + using (var transaction = this.context.Database.BeginTransaction()) { try { var producerDetails = new List(); var producerReportedMaterials = new List(); - var pomDataDetails = DataService.GetPomDataDetails(context, (int)calculatorRun.CalculatorRunPomDataMasterId); + var allPomDataDetails = this.wrapper.GetPomData(); + + //John To add + var pomDataDetails = allPomDataDetails; foreach (var pom in pomDataDetails) { From 48a3bf677f3fefbea76cb5097f091114645feb79 Mon Sep 17 00:00:00 2001 From: Mazar Shaik Date: Tue, 5 Nov 2024 12:37:50 +0000 Subject: [PATCH 15/29] Checkin for Part 3 Write initial results file - Run information - 2 --- .../CalResultsTests.cs | 79 ++++++++++ .../CalcResultsExporterTests.cs | 144 ++++++++++++++++++ .../CalculatorInternalControllerTest.cs | 84 ++++++++++ .../Builder/CalcResultDetailBuilder.cs | 28 +++- .../Constants/BlobStorageSettings.cs | 9 ++ .../EPR.Calculator.API.csproj | 1 + .../Exporter/CalcResultsExporter.cs | 48 +++++- .../Models/CalcResultDetail.cs | 5 +- src/EPR.Calculator.API/Program.cs | 18 +++ .../Services/BlobStorageService.cs | 53 +++++++ .../Services/IBlobStorageService.cs | 10 ++ src/EPR.Calculator.API/Utils/CsvSanitiser.cs | 25 +++ .../appsettings.Development.json | 5 + 13 files changed, 505 insertions(+), 4 deletions(-) create mode 100644 src/EPR.Calculator.API.UnitTests/CalResultsTests.cs create mode 100644 src/EPR.Calculator.API.UnitTests/CalcResultsExporterTests.cs create mode 100644 src/EPR.Calculator.API/Constants/BlobStorageSettings.cs create mode 100644 src/EPR.Calculator.API/Services/BlobStorageService.cs create mode 100644 src/EPR.Calculator.API/Services/IBlobStorageService.cs create mode 100644 src/EPR.Calculator.API/Utils/CsvSanitiser.cs diff --git a/src/EPR.Calculator.API.UnitTests/CalResultsTests.cs b/src/EPR.Calculator.API.UnitTests/CalResultsTests.cs new file mode 100644 index 0000000..0e08361 --- /dev/null +++ b/src/EPR.Calculator.API.UnitTests/CalResultsTests.cs @@ -0,0 +1,79 @@ +using EPR.Calculator.API.Builder; +using EPR.Calculator.API.Controllers; +using EPR.Calculator.API.Data; +using EPR.Calculator.API.Dtos; +using EPR.Calculator.API.Exporter; +using EPR.Calculator.API.Models; +using EPR.Calculator.API.Validators; +using EPR.Calculator.API.Wrapper; +using Microsoft.AspNetCore.Mvc; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; + +namespace EPR.Calculator.API.UnitTests +{ + [TestClass] + public class CalcResultServiceTests + { + private Mock mockCalcResultBuilder; + private Mock> mockExporter; + private Mock mockDetailBuilder; + private Mock mockContext; + private CalculatorInternalController controller; + private CalcResultBuilder calcResultBuilder; + private CalcResultDetailBuilder detailBuilder; + private CalcResultsExporter exporter; + protected ApplicationDBContext? dbContext; + protected IOrgAndPomWrapper? wrapper; + + [TestInitialize] + public void Setup() + { + mockCalcResultBuilder = new Mock(); + mockExporter = new Mock>(); + wrapper = new Mock().Object; + controller = new CalculatorInternalController( + dbContext, + new RpdStatusDataValidator(wrapper), + wrapper, + mockCalcResultBuilder.Object, + mockExporter.Object + ); + + mockDetailBuilder = new Mock(); + calcResultBuilder = new CalcResultBuilder(mockDetailBuilder.Object); + + mockContext = new Mock(); + detailBuilder = new CalcResultDetailBuilder(mockContext.Object); + + } + + [TestMethod] + public void PrepareCalcResults_ShouldReturnCreatedStatus() + { + var requestDto = new CalcResultsRequestDto(); + var calcResult = new CalcResult(); + mockCalcResultBuilder.Setup(b => b.Build(requestDto)).Returns(calcResult); + + var result = controller.PrepareCalcResults(requestDto) as ObjectResult; + + Assert.IsNotNull(result); + Assert.AreEqual(201, result.StatusCode); + mockExporter.Verify(e => e.Export(calcResult), Times.Once); + } + + [TestMethod] + public void Build_ShouldReturnCalcResultWithDetail() + { + var requestDto = new CalcResultsRequestDto(); + var detail = new CalcResultDetail(); + mockDetailBuilder.Setup(d => d.Construct(requestDto)).Returns(detail); + + var result = calcResultBuilder.Build(requestDto); + + Assert.IsNotNull(result); + Assert.AreEqual(detail, result.CalcResultDetail); + } + } +} + diff --git a/src/EPR.Calculator.API.UnitTests/CalcResultsExporterTests.cs b/src/EPR.Calculator.API.UnitTests/CalcResultsExporterTests.cs new file mode 100644 index 0000000..e0b7561 --- /dev/null +++ b/src/EPR.Calculator.API.UnitTests/CalcResultsExporterTests.cs @@ -0,0 +1,144 @@ +using EPR.Calculator.API.Exporter; +using EPR.Calculator.API.Models; +using EPR.Calculator.API.Services; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using System.Text; + +namespace EPR.Calculator.API.UnitTests +{ + [TestClass] + public class CalcResultsExporterTests + { + private CalcResultsExporter _calcResultsExporter; + private Mock _blobStorageServiceMock; + + [TestInitialize] + public void Setup() + { + _blobStorageServiceMock = new Mock(); + _calcResultsExporter = new CalcResultsExporter(_blobStorageServiceMock.Object); + } + + [TestMethod] + public void Export_ShouldUploadCorrectCsvContent() + { + var calcResult = new CalcResult + { + CalcResultDetail = new CalcResultDetail + { + RunName = "Test Run", + RunId = 123, + RunDate = new DateTime(2023, 10, 10, 14, 30, 0), + RunBy = "Tester", + FinancialYear = "2023-24", + LapcapFile = "Lapcap.csv,2023-10-01,John Doe", + ParametersFile = "Params.csv,2023-10-02,Jane Doe" + } + }; + + var expectedCsvContent = new StringBuilder(); + expectedCsvContent.AppendLine("Run Name,Test Run"); + expectedCsvContent.AppendLine("Run Id,123"); + expectedCsvContent.AppendLine("Run Date,10/10/2023 14:30"); + expectedCsvContent.AppendLine("Run by,Tester"); + expectedCsvContent.AppendLine("Financial Year,2023-24"); + expectedCsvContent.AppendLine("Lapcap File,Lapcap.csv,2023-10-01,John Doe"); + expectedCsvContent.AppendLine("Parameters File,Params.csv,2023-10-02,Jane Doe"); + + _calcResultsExporter.Export(calcResult); + + _blobStorageServiceMock.Verify(service => service.UploadResultFileContentAsync( + $"{calcResult.CalcResultDetail.RunId}-{DateTime.Now:yyyy-MM-dd-HHmm}", + It.Is(content => content.ToString() == expectedCsvContent.ToString()) + ), Times.Once); + } + + [TestMethod] + public void Export_ShouldHandleIOExceptionGracefully() + { + var calcResult = new CalcResult + { + CalcResultDetail = new CalcResultDetail + { + RunName = "Test Run", + RunId = 123, + RunDate = DateTime.Now, + RunBy = "Tester", + FinancialYear = "2023-24", + LapcapFile = "Lapcap.csv,2023-10-01,John Doe", + ParametersFile = "Params.csv,2023-10-02,Jane Doe" + } + }; + + _blobStorageServiceMock + .Setup(service => service.UploadResultFileContentAsync(It.IsAny(), It.IsAny())) + .Throws(new IOException("Simulated IO error")); + + using (var consoleOutput = new StringWriter()) + { + Console.SetOut(consoleOutput); + + _calcResultsExporter.Export(calcResult); + + StringAssert.Contains(consoleOutput.ToString(), "Error writing file: Simulated IO error"); + } + } + + [TestMethod] + public void AppendFileInfo_ShouldNotAppendIfFilePartsAreInvalid() + { + var calcResult = new CalcResult + { + CalcResultDetail = new CalcResultDetail + { + RunName = "Test Run", + RunId = 123, + RunDate = DateTime.Now, + RunBy = "Tester", + FinancialYear = "2023-24", + LapcapFile = "InvalidFileInfo", + ParametersFile = "InvalidFileInfo" + } + }; + + var expectedCsvContent = new StringBuilder(); + expectedCsvContent.AppendLine("Run Name,Test Run"); + expectedCsvContent.AppendLine("Run Id,123"); + expectedCsvContent.AppendLine("Run Date," + calcResult.CalcResultDetail.RunDate.ToString("dd/MM/yyyy HH:mm")); + expectedCsvContent.AppendLine("Run by,Tester"); + expectedCsvContent.AppendLine("Financial Year,2023-24"); + + _calcResultsExporter.Export(calcResult); + + _blobStorageServiceMock.Verify(service => service.UploadResultFileContentAsync( + $"{calcResult.CalcResultDetail.RunId}-{DateTime.Now:yyyy-MM-dd-HHmm}", + It.Is(content => content.ToString() == expectedCsvContent.ToString()) + ), Times.Once); + } + + [TestMethod] + public void Export_CreatesCorrectCsvContent() + { + var calcResult = new CalcResult + { + CalcResultDetail = new CalcResultDetail + { + RunName = "Test Run", + RunId = 123, + RunDate = DateTime.Now, + RunBy = "Tester", + FinancialYear = "2024", + LapcapFile = "lapcap.csv,2024-11-01,Tester", + ParametersFile = "params.csv,2024-11-01,Tester" + } + }; + + _calcResultsExporter.Export(calcResult); + + _blobStorageServiceMock.Verify(x => x.UploadResultFileContentAsync(It.IsAny(), It.IsAny()), Times.Once); + var expectedFileName = $"{calcResult.CalcResultDetail.RunId}-{DateTime.Now:yyyy-MM-dd-HHmm}"; + _blobStorageServiceMock.Verify(x => x.UploadResultFileContentAsync(expectedFileName, It.IsAny()), Times.Once); + } + } +} diff --git a/src/EPR.Calculator.API.UnitTests/CalculatorInternalControllerTest.cs b/src/EPR.Calculator.API.UnitTests/CalculatorInternalControllerTest.cs index bf42e73..d199c7c 100644 --- a/src/EPR.Calculator.API.UnitTests/CalculatorInternalControllerTest.cs +++ b/src/EPR.Calculator.API.UnitTests/CalculatorInternalControllerTest.cs @@ -1,8 +1,10 @@ using EPR.Calculator.API.Builder; using EPR.Calculator.API.Controllers; using EPR.Calculator.API.Data.DataModels; +using EPR.Calculator.API.Dtos; using EPR.Calculator.API.Exporter; using EPR.Calculator.API.Models; +using EPR.Calculator.API.Services; using EPR.Calculator.API.Tests.Controllers; using EPR.Calculator.API.Validators; using EPR.Calculator.API.Wrapper; @@ -195,5 +197,87 @@ public void UpdateRpdStatus_With_RunId_When_Successful() } } + + [TestMethod] + public void Construct_ShouldReturnCalcResultDetail() + { + var mock = new Mock(); + dbContext.LapcapDataMaster.RemoveRange(dbContext.LapcapDataMaster); + dbContext.SaveChanges(); + dbContext.LapcapDataMaster.AddRange(GetLapcapMasterData().ToList()); + dbContext.SaveChanges(); + + dbContext.DefaultParameterSettings.RemoveRange(dbContext.DefaultParameterSettings); + dbContext.SaveChanges(); + dbContext.DefaultParameterSettings.AddRange(GetDefaultParameterSettingsMasterData().ToList()); + dbContext.SaveChanges(); + + var mockBlobStorageService = new Mock(); + var CalcResultsExporter = new CalcResultsExporter(mockBlobStorageService.Object); + var CalcResultDetailBuilder = new CalcResultDetailBuilder(dbContext); + var calcResultBuilder = new CalcResultBuilder(CalcResultDetailBuilder); + if (dbContext != null) + { + var controller = new CalculatorInternalController( + dbContext, + new RpdStatusDataValidator(mock.Object), + mock.Object, + calcResultBuilder, + CalcResultsExporter + ); + var calResult = controller.PrepareCalcResults(new CalcResultsRequestDto() { RunId = 1 }); + var objResult = calResult as ObjectResult; + Assert.IsNotNull(calResult); + Assert.AreEqual(201, objResult?.StatusCode); + } + } + + protected static IEnumerable GetDefaultParameterSettingsMasterData() + { + var list = new List + { + new() { + CreatedAt = DateTime.Now, + CreatedBy = "Test User1", + EffectiveFrom = DateTime.Now, + EffectiveTo = DateTime.Now, + Id = 1, + }, + new() { + CreatedAt = DateTime.Now, + CreatedBy = "Test User2", + EffectiveFrom = DateTime.Now, + EffectiveTo = DateTime.Now, + Id = 2, + } + }; + + return list; + } + + protected static IEnumerable GetLapcapMasterData() + { + var list = new List + { + new() { + CreatedAt = DateTime.Now, + CreatedBy = "Test User1", + EffectiveFrom = DateTime.Now, + EffectiveTo = DateTime.Now, + Id = 1, + ProjectionYear = "2024-25", + }, + new() { + CreatedAt = DateTime.Now, + CreatedBy = "Test User2", + EffectiveFrom = DateTime.Now, + EffectiveTo = DateTime.Now, + Id = 2, + ProjectionYear = "2024-25", + } + }; + + return list; + } } } diff --git a/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs b/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs index 7f52650..0929752 100644 --- a/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs +++ b/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs @@ -1,6 +1,7 @@ using EPR.Calculator.API.Data; using EPR.Calculator.API.Dtos; using EPR.Calculator.API.Models; +using Microsoft.EntityFrameworkCore; namespace EPR.Calculator.API.Builder { @@ -14,7 +15,32 @@ public CalcResultDetailBuilder(ApplicationDBContext context) public CalcResultDetail Construct(CalcResultsRequestDto resultsRequestDto) { - return new CalcResultDetail(); + var calcResultDetail = context.CalculatorRuns + .Include(o => o.CalculatorRunOrganisationDataMaster) + .Include(o => o.CalculatorRunPomDataMaster) + .Include(o => o.DefaultParameterSettingMaster) + .Include(x => x.LapcapDataMaster) + .ToListAsync(); + + var results = new CalcResultDetail(); + + foreach (var item in calcResultDetail.Result) + { + results.RunId = item.Id; + results.RunName = item.Name; + results.RunBy = item.CreatedBy; + results.RunDate = item.CreatedAt; + results.FinancialYear = item.Financial_Year; + if (item.CalculatorRunOrganisationDataMaster != null) + results.RpdFileORG = item.CalculatorRunOrganisationDataMaster.CreatedAt.ToString("dd/MM/yyyy HH:mm"); + if (item.CalculatorRunPomDataMaster != null) + results.RpdFilePOM = item.CalculatorRunPomDataMaster.CreatedAt.ToString("dd/MM/yyyy HH:mm"); + if (item.LapcapDataMaster != null) + results.LapcapFile = "TestFileName1" + "," + item.LapcapDataMaster.CreatedAt.ToString("dd/MM/yyyy HH:mm") + "," + item.LapcapDataMaster.CreatedBy; + if (item.DefaultParameterSettingMaster != null) + results.ParametersFile = item.DefaultParameterSettingMaster.ParameterFileName + "," + item.DefaultParameterSettingMaster.CreatedAt.ToString("dd/MM/yyyy HH:mm") + "," + item.DefaultParameterSettingMaster.CreatedBy; + } + return results; } } } \ No newline at end of file diff --git a/src/EPR.Calculator.API/Constants/BlobStorageSettings.cs b/src/EPR.Calculator.API/Constants/BlobStorageSettings.cs new file mode 100644 index 0000000..5dc9a85 --- /dev/null +++ b/src/EPR.Calculator.API/Constants/BlobStorageSettings.cs @@ -0,0 +1,9 @@ +namespace EPR.Calculator.API.Constants +{ + public class BlobStorageSettings + { + public string ConnectionString { get; set; } = string.Empty; + public string ContainerName { get; set; } = string.Empty; + public string CsvFileName { get; set; } = string.Empty; + } +} \ No newline at end of file diff --git a/src/EPR.Calculator.API/EPR.Calculator.API.csproj b/src/EPR.Calculator.API/EPR.Calculator.API.csproj index ecf76a4..478961b 100644 --- a/src/EPR.Calculator.API/EPR.Calculator.API.csproj +++ b/src/EPR.Calculator.API/EPR.Calculator.API.csproj @@ -18,6 +18,7 @@ + diff --git a/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs b/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs index 090a859..05d1cdf 100644 --- a/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs +++ b/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs @@ -1,12 +1,58 @@ using EPR.Calculator.API.Models; +using EPR.Calculator.API.Services; +using EPR.Calculator.API.Utils; +using System; +using System.Text; namespace EPR.Calculator.API.Exporter { public class CalcResultsExporter : ICalcResultsExporter { + private readonly IBlobStorageService _blobStorageService; + public CalcResultsExporter(IBlobStorageService blobStorageService) + { + _blobStorageService = blobStorageService; + } public void Export(CalcResult results) { - // Code to export the File to Csv + var csvContent = new StringBuilder(); + AppendCsvLine(csvContent, "Run Name", results.CalcResultDetail.RunName); + AppendCsvLine(csvContent, "Run Id", results.CalcResultDetail.RunId.ToString()); + AppendCsvLine(csvContent, "Run Date", results.CalcResultDetail.RunDate.ToString("dd/MM/yyyy HH:mm")); + AppendCsvLine(csvContent, "Run by", results.CalcResultDetail.RunBy); + AppendCsvLine(csvContent, "Financial Year", results.CalcResultDetail.FinancialYear); + AppendFileInfo(csvContent, "Lapcap File", results.CalcResultDetail.LapcapFile); + AppendFileInfo(csvContent, "Parameters File", results.CalcResultDetail.ParametersFile); + var fileName = GetResultFileName(results.CalcResultDetail.RunId); + try + { + _blobStorageService.UploadResultFileContentAsync(fileName, csvContent); + } + catch (IOException ex) + { + Console.WriteLine($"Error writing file: {ex.Message}"); + } + } + + private static void AppendFileInfo(StringBuilder csvContent, string label, string filePath) + { + var fileParts = filePath.Split(','); + if (fileParts.Length >= 3) + { + string fileName = CsvSanitiser.SanitiseData(fileParts[0]); + string date = CsvSanitiser.SanitiseData(fileParts[1]); + string user = CsvSanitiser.SanitiseData(fileParts[2]); + csvContent.AppendLine($"{label},{fileName},{date},{user}"); + } + } + + private static void AppendCsvLine(StringBuilder csvContent, string label, string value) + { + csvContent.AppendLine($"{label},{CsvSanitiser.SanitiseData(value)}"); + } + private static string GetResultFileName(int runId) + { + return $"{runId}-{DateTime.Now:yyyy-MM-dd-HHmm}"; } } } diff --git a/src/EPR.Calculator.API/Models/CalcResultDetail.cs b/src/EPR.Calculator.API/Models/CalcResultDetail.cs index 021119f..894199a 100644 --- a/src/EPR.Calculator.API/Models/CalcResultDetail.cs +++ b/src/EPR.Calculator.API/Models/CalcResultDetail.cs @@ -4,10 +4,11 @@ public class CalcResultDetail { public string RunName { get; set; } = string.Empty; public int RunId { get; set; } - public DateTime RunDdate { get; set; } + public DateTime RunDate { get; set; } public string RunBy { get; set; } = string.Empty; public string FinancialYear { get; set; } = string.Empty; - public string RpdFile { get; set; } = string.Empty; + public string RpdFileORG { get; set; } = string.Empty; + public string RpdFilePOM { get; set; } = string.Empty; public string LapcapFile { get; set; } = string.Empty; public string ParametersFile { get; set; } = string.Empty; } diff --git a/src/EPR.Calculator.API/Program.cs b/src/EPR.Calculator.API/Program.cs index 27a13e1..182e5ee 100644 --- a/src/EPR.Calculator.API/Program.cs +++ b/src/EPR.Calculator.API/Program.cs @@ -1,9 +1,12 @@ using Azure.Messaging.ServiceBus; +using Azure.Storage.Blobs; using EPR.Calculator.API.Builder; +using EPR.Calculator.API.Constants; using EPR.Calculator.API.Data; using EPR.Calculator.API.Exceptions; using EPR.Calculator.API.Exporter; using EPR.Calculator.API.Models; +using EPR.Calculator.API.Services; using EPR.Calculator.API.Validators; using EPR.Calculator.API.Wrapper; using FluentValidation; @@ -28,6 +31,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped, CalcResultsExporter>(); +builder.Services.AddScoped(); builder.Services.AddValidatorsFromAssemblyContaining(); builder.Services.AddDbContext(options => @@ -35,6 +39,20 @@ options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")); }); +builder.Services.Configure( + builder.Configuration.GetSection("AzureBlobStorage")); + +builder.Services.AddSingleton(provider => +{ + var configuration = provider.GetRequiredService(); + var connectionString = configuration.GetSection("AzureBlobStorage:ConnectionString").Value; + if (string.IsNullOrEmpty(connectionString)) + { + throw new ArgumentNullException("AzureBlobStorage:ConnectionString", "Azure Blob Storage connection string is not configured."); + } + return new BlobServiceClient(connectionString); +}); + var serviceBusConnectionString = builder.Configuration.GetSection("ServiceBus").GetSection("ConnectionString"); var serviceBusQueueName = builder.Configuration.GetSection("ServiceBus").GetSection("QueueName").Value; #pragma warning disable CS8604 // Possible null reference argument. diff --git a/src/EPR.Calculator.API/Services/BlobStorageService.cs b/src/EPR.Calculator.API/Services/BlobStorageService.cs new file mode 100644 index 0000000..7be4430 --- /dev/null +++ b/src/EPR.Calculator.API/Services/BlobStorageService.cs @@ -0,0 +1,53 @@ +using Azure.Storage.Blobs; +using EPR.Calculator.API.Constants; +using System.Text; +namespace EPR.Calculator.API.Services +{ + public class BlobStorageService: IBlobStorageService + { + private readonly BlobContainerClient _containerClient; + private readonly BlobClient _blobClient; + + public BlobStorageService(BlobServiceClient blobServiceClient, IConfiguration configuration) + { + var settings = configuration.GetSection("AzureBlobStorage").Get() ?? throw new ArgumentNullException("AzureBlobStorage settings are missing in configuration."); + _containerClient = blobServiceClient.GetBlobContainerClient(settings.ContainerName ?? throw new ArgumentNullException("Container name is missing in configuration.")); + _blobClient = _containerClient.GetBlobClient(settings.CsvFileName ?? throw new ArgumentNullException("CSV file name is missing in configuration.")); + } + + public async Task GetResultFileContentAsync() + { + try + { + if (await _blobClient.ExistsAsync()) + { + var downloadInfo = await _blobClient.DownloadAsync(); + using var streamReader = new StreamReader(downloadInfo.Value.Content); + return await streamReader.ReadToEndAsync(); + } + return null; + } + catch (Exception ex) + { + Console.WriteLine($"Error occurred while retrieving blob content: {ex.Message}"); + return null; + } + } + + public void UploadResultFileContentAsync(string fileName, StringBuilder csvContent) + { + try + { + File.WriteAllText(fileName, csvContent.ToString()); + var blobClient = _containerClient.GetBlobClient(fileName); + using var fileStream = File.OpenRead(fileName); + blobClient.UploadAsync(fileStream, true); + fileStream.Close(); + } + catch (Exception ex) + { + Console.WriteLine($"Error occurred while saving blob content: {ex.Message}"); + } + } + } +} \ No newline at end of file diff --git a/src/EPR.Calculator.API/Services/IBlobStorageService.cs b/src/EPR.Calculator.API/Services/IBlobStorageService.cs new file mode 100644 index 0000000..8adc6b4 --- /dev/null +++ b/src/EPR.Calculator.API/Services/IBlobStorageService.cs @@ -0,0 +1,10 @@ +using System.Text; + +namespace EPR.Calculator.API.Services +{ + public interface IBlobStorageService + { + void UploadResultFileContentAsync(string fileName, StringBuilder content); + Task GetResultFileContentAsync(); + } +} diff --git a/src/EPR.Calculator.API/Utils/CsvSanitiser.cs b/src/EPR.Calculator.API/Utils/CsvSanitiser.cs new file mode 100644 index 0000000..e85d6e9 --- /dev/null +++ b/src/EPR.Calculator.API/Utils/CsvSanitiser.cs @@ -0,0 +1,25 @@ +using Newtonsoft.Json; + +namespace EPR.Calculator.API.Utils +{ + public static class CsvSanitiser + { + public static string SanitiseData(T value) + { + if (value == null) return string.Empty; + + // If the value is a string, use it directly; otherwise, serialize the object to JSON. + var stringToSanitise = value is string + ? value.ToString() + : JsonConvert.SerializeObject(value); + + // Remove newline, carriage returns, and commas, then trim + stringToSanitise = stringToSanitise.Replace("\r", string.Empty) + .Replace("\n", string.Empty) + .Replace(",", string.Empty) + .Trim(); + + return stringToSanitise; + } + } +} diff --git a/src/EPR.Calculator.API/appsettings.Development.json b/src/EPR.Calculator.API/appsettings.Development.json index b8bdb7e..3bbc394 100644 --- a/src/EPR.Calculator.API/appsettings.Development.json +++ b/src/EPR.Calculator.API/appsettings.Development.json @@ -17,5 +17,10 @@ "QueueName": "Queue1", "PostMessageRetryCount": 3, "PostMessageRetryPeriod": 2 + }, + "AzureBlobStorage": { + "ConnectionString": "ConnectionString", + "ContainerName": "ContainerName", + "CsvFileName": "CsvFileName" } } From 9952ca486daa508144a592079e2a862a8174a618 Mon Sep 17 00:00:00 2001 From: johnbenjamin-defra Date: Tue, 5 Nov 2024 12:41:18 +0000 Subject: [PATCH 16/29] 459419 Transpose pom and org data --- .../BaseControllerTest.cs | 85 ++++++++++++++++--- .../CalculatorInternalControllerTest.cs | 4 +- .../CalculatorInternalControllerTests.cs | 5 +- .../TransposePomAndOrgDataServiceTests.cs | 61 ++++++------- .../Controllers/CalculatorController.cs | 1 - .../CalculatorInternalController.cs | 2 +- .../Services/TransposePomAndOrgDataService.cs | 9 +- 7 files changed, 109 insertions(+), 58 deletions(-) diff --git a/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs b/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs index 3a3321d..b0a638f 100644 --- a/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs +++ b/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs @@ -14,6 +14,7 @@ using EPR.Calculator.API.Builder; using EPR.Calculator.API.Models; using EPR.Calculator.API.Exporter; +using EPR.Calculator.API.Services; namespace EPR.Calculator.API.Tests.Controllers { @@ -54,7 +55,8 @@ public void SetUp() new RpdStatusDataValidator(wrapper), wrapper, new Mock().Object, - new Mock>().Object + new Mock>().Object, + new Mock().Object ); var mockFactory = new Mock>(); @@ -72,11 +74,11 @@ public void SetUp() dbContext.Material.AddRange(GetMaterials()); dbContext.SaveChanges(); - dbContext.PomData.AddRange(GetPomData()); - dbContext.SaveChanges(); + //dbContext.CalculatorRunPomDataDetails.AddRange(GetCalculatorRunPomDataDetails()); + //dbContext.SaveChanges(); - dbContext.OrganisationData.AddRange(GetOrganisationData()); - dbContext.SaveChanges(); + //dbContext.CalculatorRunOrganisationDataDetails.AddRange(GetCalculatorRunOrganisationDataDetails()); + //dbContext.SaveChanges(); } public void CheckDbContext() @@ -716,6 +718,28 @@ protected static IEnumerable GetCalculatorRuns() CreatedAt = new DateTime(2024, 8, 21, 14, 16, 27, DateTimeKind.Utc), CreatedBy = "Test User" }); + list.Add(new CalculatorRun + { + Id = 3, + CalculatorRunClassificationId = (int)RunClassification.RUNNING, + Name = "Test Run", + Financial_Year = "2024-25", + CreatedAt = new DateTime(2024, 8, 28, 10, 12, 30, DateTimeKind.Utc), + CreatedBy = "Test User", + CalculatorRunOrganisationDataMasterId = 1, + CalculatorRunPomDataMasterId = 1, + }); + list.Add(new CalculatorRun + { + Id = 4, + CalculatorRunClassificationId = (int)RunClassification.RUNNING, + Name = "Test Calculated Result", + Financial_Year = "2024-25", + CreatedAt = new DateTime(2024, 8, 21, 14, 16, 27, DateTimeKind.Utc), + CreatedBy = "Test User", + CalculatorRunOrganisationDataMasterId = 2, + CalculatorRunPomDataMasterId = 2, + }); return list; } @@ -781,11 +805,26 @@ protected static IEnumerable GetMaterials() return list; } - protected static IEnumerable GetPomData() + protected static IEnumerable GetCalculatorRunPomDataMaster() + { + var list = new List(); + list.Add(new CalculatorRunPomDataMaster + { + Id = 1, + CalendarYear = "2024-25", + EffectiveFrom = DateTime.Now, + CreatedBy = "Test user", + CreatedAt = DateTime.Now + }); + return list; + } + + protected static IEnumerable GetCalculatorRunPomDataDetails() { - var list = new List(); - list.Add(new PomData + var list = new List(); + list.Add(new CalculatorRunPomDataDetail { + Id= 1, OrganisationId = 1, SubsidaryId = "SUBSID1", SubmissionPeriod = "2023-P3", @@ -795,21 +834,39 @@ protected static IEnumerable GetPomData() PackagingMaterial = "PC", PackagingMaterialWeight = 1000, LoadTimeStamp = DateTime.Now, - SubmissionPeriodDesc = "July to December 2023" + CalculatorRunPomDataMasterId = 1, + SubmissionPeriodDesc = "July to December 2023", + CalculatorRunPomDataMaster = BaseControllerTest.GetCalculatorRunPomDataMaster().ToList()[0] + }); + return list; + } + + protected static IEnumerable GetCalculatorRunOrganisationDataMaster() + { + var list = new List(); + list.Add(new CalculatorRunOrganisationDataMaster + { + CalendarYear = "2024-25", + EffectiveFrom = DateTime.Now, + CreatedBy = "Test user", + CreatedAt = DateTime.Now }); return list; } - protected static IEnumerable GetOrganisationData() + protected static IEnumerable GetCalculatorRunOrganisationDataDetails() { - var list = new List(); - list.Add(new OrganisationData + var list = new List(); + list.Add(new CalculatorRunOrganisationDataDetail { + Id = 1, OrganisationId = 1, SubsidaryId = "SUBSID1", OrganisationName = "UPU LIMITED", - LoadTimestamp = DateTime.Now, - SubmissionPeriodDesc = "January to June 2023" + LoadTimeStamp= DateTime.Now, + CalculatorRunOrganisationDataMasterId = 1, + SubmissionPeriodDesc = "January to June 2023", + CalculatorRunOrganisationDataMaster = BaseControllerTest.GetCalculatorRunOrganisationDataMaster().ToList()[0] }); return list; } diff --git a/src/EPR.Calculator.API.UnitTests/CalculatorInternalControllerTest.cs b/src/EPR.Calculator.API.UnitTests/CalculatorInternalControllerTest.cs index bf42e73..0046548 100644 --- a/src/EPR.Calculator.API.UnitTests/CalculatorInternalControllerTest.cs +++ b/src/EPR.Calculator.API.UnitTests/CalculatorInternalControllerTest.cs @@ -3,6 +3,7 @@ using EPR.Calculator.API.Data.DataModels; using EPR.Calculator.API.Exporter; using EPR.Calculator.API.Models; +using EPR.Calculator.API.Services; using EPR.Calculator.API.Tests.Controllers; using EPR.Calculator.API.Validators; using EPR.Calculator.API.Wrapper; @@ -179,7 +180,8 @@ public void UpdateRpdStatus_With_RunId_When_Successful() new RpdStatusDataValidator(mock.Object), mock.Object, new Mock().Object, - new Mock>().Object + new Mock>().Object, + new Mock().Object ); var request = new Dtos.UpdateRpdStatus { isSuccessful = true, RunId = 1, UpdatedBy = "User1" }; diff --git a/src/EPR.Calculator.API.UnitTests/Controllers/CalculatorInternalControllerTests.cs b/src/EPR.Calculator.API.UnitTests/Controllers/CalculatorInternalControllerTests.cs index bdfb4f3..355a803 100644 --- a/src/EPR.Calculator.API.UnitTests/Controllers/CalculatorInternalControllerTests.cs +++ b/src/EPR.Calculator.API.UnitTests/Controllers/CalculatorInternalControllerTests.cs @@ -8,6 +8,7 @@ namespace EPR.Calculator.API.UnitTests.Controllers using EPR.Calculator.API.Dtos; using EPR.Calculator.API.Exporter; using EPR.Calculator.API.Models; + using EPR.Calculator.API.Services; using EPR.Calculator.API.Validators; using EPR.Calculator.API.Wrapper; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -22,6 +23,7 @@ public class CalculatorInternalControllerTests private Mock _wrapper; private Mock _builder; private Mock> _exporter; + private Mock _transposePomAndOrgDataService; [TestInitialize] public void SetUp() @@ -31,7 +33,8 @@ public void SetUp() _wrapper = new Mock(); _builder = new Mock(); _exporter = new Mock>(); - _testClass = new CalculatorInternalController(_context, _rpdStatusDataValidator.Object, _wrapper.Object, _builder.Object, _exporter.Object); + _transposePomAndOrgDataService = new Mock(); + _testClass = new CalculatorInternalController(_context, _rpdStatusDataValidator.Object, _wrapper.Object, _builder.Object, _exporter.Object, _transposePomAndOrgDataService.Object); } [TestMethod] diff --git a/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs b/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs index f756fba..d8d7265 100644 --- a/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs +++ b/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs @@ -10,6 +10,16 @@ namespace EPR.Calculator.API.UnitTests.Services [TestClass] public class TransposePomAndOrgDataServiceTests : BaseControllerTest { + [TestInitialize] + public void Setup() + { + dbContext.CalculatorRunPomDataDetails.AddRange(GetCalculatorRunPomDataDetails()); + dbContext.SaveChanges(); + + dbContext.CalculatorRunOrganisationDataDetails.AddRange(GetCalculatorRunOrganisationDataDetails()); + dbContext.SaveChanges(); + } + [TestMethod] public void Transpose_Should_Return_Correct_Producer_Detail() { @@ -23,34 +33,13 @@ public void Transpose_Should_Return_Correct_Producer_Detail() CalculatorRun = new CalculatorRun() }; - var pomDataList = new List(); - pomDataList.Add(new PomData - { - OrganisationId = 1, - SubsidaryId = "SUBSID1", - SubmissionPeriod = "2023-P3", - PackagingActivity = null, - PackagingType = "CW", - PackagingClass = "O1", - PackagingMaterial = "PC", - PackagingMaterialWeight = 1000, - LoadTimeStamp = DateTime.Now, - SubmissionPeriodDesc = "July to December 2023" - }); - -# - var mockWrapper = new Mock(); - - mockWrapper.Setup(x => x.GetPomData()).Returns(pomDataList); - - var service = new TransposePomAndOrgDataService(dbContext, mockWrapper.Object); - - service.Transpose(1); - - + var service = new TransposePomAndOrgDataService(dbContext); + service.Transpose(3); var producerDetail = dbContext.ProducerDetail.FirstOrDefault(); - Assert.AreEqual(expectedResult, producerDetail); + Assert.AreEqual(expectedResult.ProducerId, producerDetail.ProducerId); + Assert.AreEqual(expectedResult.ProducerName, producerDetail.ProducerName); + Assert.AreEqual(expectedResult.SubsidiaryId, producerDetail.SubsidiaryId); } [TestMethod] @@ -63,11 +52,12 @@ public void Transpose_Should_Return_Correct_Producer_Reported_Material() ProducerDetailId = 1, PackagingType = "CW", PackagingTonnage = 1, - Material = new Material{ - Id = 1, - Code = "AL", - Name = "Aluminium", - Description = "Aluminium" + Material = new Material + { + Id = 4, + Code = "PC", + Name = "Paper or card", + Description = "Paper or card" }, ProducerDetail = new ProducerDetail { @@ -80,10 +70,15 @@ public void Transpose_Should_Return_Correct_Producer_Reported_Material() } }; - TransposePomAndOrgDataService.Transpose(dbContext, 1); + var service = new TransposePomAndOrgDataService(dbContext); + service.Transpose(3); var producerReportedMaterial = dbContext.ProducerReportedMaterial.FirstOrDefault(); - Assert.AreEqual(expectedResult, producerReportedMaterial); + Assert.AreEqual(expectedResult.ProducerDetailId, producerReportedMaterial.ProducerDetailId); + Assert.AreEqual(expectedResult.Material.Code, producerReportedMaterial.Material.Code); + Assert.AreEqual(expectedResult.Material.Name, producerReportedMaterial.Material.Name); + Assert.AreEqual(expectedResult.ProducerDetail.ProducerId, producerReportedMaterial.ProducerDetail.ProducerId); + Assert.AreEqual(expectedResult.ProducerDetail.ProducerName, producerReportedMaterial.ProducerDetail.ProducerName); } } } diff --git a/src/EPR.Calculator.API/Controllers/CalculatorController.cs b/src/EPR.Calculator.API/Controllers/CalculatorController.cs index 3f1802b..4e1cbac 100644 --- a/src/EPR.Calculator.API/Controllers/CalculatorController.cs +++ b/src/EPR.Calculator.API/Controllers/CalculatorController.cs @@ -174,7 +174,6 @@ public IActionResult GetCalculatorRuns([FromBody] CalculatorRunsParamsDto reques [Route("calculatorRuns/{id}")] public IActionResult GetCalculatorRun() { - TransposePomAndOrgDataService.Transpose(context, 58); // TODO: Return the details of a particular run return new OkResult(); } diff --git a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs index ccc17a4..4e27e7e 100644 --- a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs +++ b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs @@ -21,7 +21,7 @@ public class CalculatorInternalController : ControllerBase private readonly IOrgAndPomWrapper wrapper; private readonly ICalcResultBuilder builder; private readonly ICalcResultsExporter exporter; - private readonly ITransposePomAndOrgDataService transposePomAndOrgDataService; + private readonly ITransposePomAndOrgDataService transposePomAndOrgDataService; public CalculatorInternalController(ApplicationDBContext context, IRpdStatusDataValidator rpdStatusDataValidator, diff --git a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs index 05ba150..692e76d 100644 --- a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs +++ b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs @@ -8,12 +8,10 @@ namespace EPR.Calculator.API.Services public class TransposePomAndOrgDataService : ITransposePomAndOrgDataService { private readonly ApplicationDBContext context; - private readonly IOrgAndPomWrapper wrapper; - public TransposePomAndOrgDataService(ApplicationDBContext context, IOrgAndPomWrapper wrapper) + public TransposePomAndOrgDataService(ApplicationDBContext context) { this.context = context; - this.wrapper = wrapper; } public void Transpose(int runId) @@ -31,10 +29,7 @@ public void Transpose(int runId) var producerDetails = new List(); var producerReportedMaterials = new List(); - var allPomDataDetails = this.wrapper.GetPomData(); - - //John To add - var pomDataDetails = allPomDataDetails; + var pomDataDetails = context.CalculatorRunPomDataDetails.Where(pdd => pdd.CalculatorRunPomDataMasterId == (int)calculatorRun.CalculatorRunPomDataMasterId).ToList(); foreach (var pom in pomDataDetails) { From 872c72195aa415dab05d425c688ccc38ef713fe6 Mon Sep 17 00:00:00 2001 From: johnbenjamin-defra Date: Tue, 5 Nov 2024 13:06:57 +0000 Subject: [PATCH 17/29] 459419 Transpose pom and org data --- .../BaseControllerTest.cs | 6 ------ src/EPR.Calculator.API/Services/DataService.cs | 13 ------------- 2 files changed, 19 deletions(-) delete mode 100644 src/EPR.Calculator.API/Services/DataService.cs diff --git a/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs b/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs index b0a638f..0205193 100644 --- a/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs +++ b/src/EPR.Calculator.API.UnitTests/BaseControllerTest.cs @@ -73,12 +73,6 @@ public void SetUp() dbContext.Material.AddRange(GetMaterials()); dbContext.SaveChanges(); - - //dbContext.CalculatorRunPomDataDetails.AddRange(GetCalculatorRunPomDataDetails()); - //dbContext.SaveChanges(); - - //dbContext.CalculatorRunOrganisationDataDetails.AddRange(GetCalculatorRunOrganisationDataDetails()); - //dbContext.SaveChanges(); } public void CheckDbContext() diff --git a/src/EPR.Calculator.API/Services/DataService.cs b/src/EPR.Calculator.API/Services/DataService.cs deleted file mode 100644 index 312770e..0000000 --- a/src/EPR.Calculator.API/Services/DataService.cs +++ /dev/null @@ -1,13 +0,0 @@ -using EPR.Calculator.API.Data; -using EPR.Calculator.API.Data.DataModels; - -namespace EPR.Calculator.API.Services -{ - public static class DataService - { - public static IEnumerable GetPomDataDetails(ApplicationDBContext context, int pomDataMasterId) - { - return context.CalculatorRunPomDataDetails.Where(pdd => pdd.CalculatorRunPomDataMasterId == pomDataMasterId).ToList(); - } - } -} From f6cd2bf1e18ca9992cd274cfa057092b765be106 Mon Sep 17 00:00:00 2001 From: johnbenjamin-defra Date: Tue, 5 Nov 2024 13:08:22 +0000 Subject: [PATCH 18/29] Update CalculatorController.cs --- src/EPR.Calculator.API/Controllers/CalculatorController.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/EPR.Calculator.API/Controllers/CalculatorController.cs b/src/EPR.Calculator.API/Controllers/CalculatorController.cs index 4e1cbac..c62efe5 100644 --- a/src/EPR.Calculator.API/Controllers/CalculatorController.cs +++ b/src/EPR.Calculator.API/Controllers/CalculatorController.cs @@ -4,7 +4,6 @@ using EPR.Calculator.API.Dtos; using EPR.Calculator.API.Enums; using EPR.Calculator.API.Models; -using EPR.Calculator.API.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Azure; From 49a3f2e1d6aa523aad20aee179d558b6caf7ffd2 Mon Sep 17 00:00:00 2001 From: johnbenjamin-defra Date: Tue, 5 Nov 2024 13:21:26 +0000 Subject: [PATCH 19/29] Sonar issues --- .../Services/TransposePomAndOrgDataServiceTests.cs | 8 ++++++++ .../Services/TransposePomAndOrgDataService.cs | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs b/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs index d8d7265..35102b9 100644 --- a/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs +++ b/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs @@ -13,11 +13,13 @@ public class TransposePomAndOrgDataServiceTests : BaseControllerTest [TestInitialize] public void Setup() { +#pragma warning disable CS8602 // Dereference of a possibly null reference. dbContext.CalculatorRunPomDataDetails.AddRange(GetCalculatorRunPomDataDetails()); dbContext.SaveChanges(); dbContext.CalculatorRunOrganisationDataDetails.AddRange(GetCalculatorRunOrganisationDataDetails()); dbContext.SaveChanges(); +#pragma warning restore CS8602 // Dereference of a possibly null reference. } [TestMethod] @@ -33,10 +35,13 @@ public void Transpose_Should_Return_Correct_Producer_Detail() CalculatorRun = new CalculatorRun() }; +#pragma warning disable CS8604 // Possible null reference argument. var service = new TransposePomAndOrgDataService(dbContext); +#pragma warning restore CS8604 // Possible null reference argument. service.Transpose(3); var producerDetail = dbContext.ProducerDetail.FirstOrDefault(); + Assert.IsNotNull(producerDetail); Assert.AreEqual(expectedResult.ProducerId, producerDetail.ProducerId); Assert.AreEqual(expectedResult.ProducerName, producerDetail.ProducerName); Assert.AreEqual(expectedResult.SubsidiaryId, producerDetail.SubsidiaryId); @@ -70,10 +75,13 @@ public void Transpose_Should_Return_Correct_Producer_Reported_Material() } }; +#pragma warning disable CS8604 // Possible null reference argument. var service = new TransposePomAndOrgDataService(dbContext); +#pragma warning restore CS8604 // Possible null reference argument. service.Transpose(3); var producerReportedMaterial = dbContext.ProducerReportedMaterial.FirstOrDefault(); + Assert.IsNotNull(producerReportedMaterial); Assert.AreEqual(expectedResult.ProducerDetailId, producerReportedMaterial.ProducerDetailId); Assert.AreEqual(expectedResult.Material.Code, producerReportedMaterial.Material.Code); Assert.AreEqual(expectedResult.Material.Name, producerReportedMaterial.Material.Name); diff --git a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs index 692e76d..c37d267 100644 --- a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs +++ b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs @@ -67,7 +67,7 @@ public void Transpose(int runId) Material = material, ProducerDetailId = producerDetail.Id, ProducerDetail = producerDetail, - PackagingType = pom.PackagingType, + PackagingType = pom.PackagingType ?? " ", PackagingTonnage = (decimal)pom.PackagingMaterialWeight / 1000, }; From bfb9951d14b58a6cbdfa4cf507c704c94d264c9b Mon Sep 17 00:00:00 2001 From: johnbenjamin-defra Date: Tue, 5 Nov 2024 13:30:47 +0000 Subject: [PATCH 20/29] Update TransposePomAndOrgDataService.cs --- .../Services/TransposePomAndOrgDataService.cs | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs index c37d267..0cee492 100644 --- a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs +++ b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs @@ -61,17 +61,20 @@ public void Transpose(int runId) var material = materials.Single(m => m.Code == pom.PackagingMaterial); - var producerReportedMaterial = new ProducerReportedMaterial + if (pom.PackagingType != null && pom.PackagingMaterialWeight != null) { - MaterialId = material.Id, - Material = material, - ProducerDetailId = producerDetail.Id, - ProducerDetail = producerDetail, - PackagingType = pom.PackagingType ?? " ", - PackagingTonnage = (decimal)pom.PackagingMaterialWeight / 1000, - }; - - producerReportedMaterials.Add(producerReportedMaterial); + var producerReportedMaterial = new ProducerReportedMaterial + { + MaterialId = material.Id, + Material = material, + ProducerDetailId = producerDetail.Id, + ProducerDetail = producerDetail, + PackagingType = pom.PackagingType ?? " ", + PackagingTonnage = (decimal)pom.PackagingMaterialWeight / 1000, + }; + + producerReportedMaterials.Add(producerReportedMaterial); + } } } } @@ -81,10 +84,12 @@ public void Transpose(int runId) transaction.Commit(); } - catch (Exception exception) + catch (Exception) { // Error, rollback transaction transaction.Rollback(); + // TO DO: Decide upon the exception later during the complete integration + throw; } } } From 2cb28fe172bc79da4ada26c5e5ac5cba4e571dec Mon Sep 17 00:00:00 2001 From: Mazar Shaik Date: Tue, 5 Nov 2024 14:20:54 +0000 Subject: [PATCH 21/29] Refactored the code --- .../Builder/CalcResultDetailBuilder.cs | 16 +++++++--- .../Constants/CalculationResults.cs | 7 +++++ .../Exporter/CalcResultsExporter.cs | 31 +++++++++++++------ 3 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 src/EPR.Calculator.API/Constants/CalculationResults.cs diff --git a/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs b/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs index 0929752..1832d3e 100644 --- a/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs +++ b/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs @@ -1,4 +1,5 @@ -using EPR.Calculator.API.Data; +using EPR.Calculator.API.Constants; +using EPR.Calculator.API.Data; using EPR.Calculator.API.Dtos; using EPR.Calculator.API.Models; using Microsoft.EntityFrameworkCore; @@ -32,15 +33,20 @@ public CalcResultDetail Construct(CalcResultsRequestDto resultsRequestDto) results.RunDate = item.CreatedAt; results.FinancialYear = item.Financial_Year; if (item.CalculatorRunOrganisationDataMaster != null) - results.RpdFileORG = item.CalculatorRunOrganisationDataMaster.CreatedAt.ToString("dd/MM/yyyy HH:mm"); + results.RpdFileORG = item.CalculatorRunOrganisationDataMaster.CreatedAt.ToString(CalculationResults.DateFormat); if (item.CalculatorRunPomDataMaster != null) - results.RpdFilePOM = item.CalculatorRunPomDataMaster.CreatedAt.ToString("dd/MM/yyyy HH:mm"); + results.RpdFilePOM = item.CalculatorRunPomDataMaster.CreatedAt.ToString(CalculationResults.DateFormat); if (item.LapcapDataMaster != null) - results.LapcapFile = "TestFileName1" + "," + item.LapcapDataMaster.CreatedAt.ToString("dd/MM/yyyy HH:mm") + "," + item.LapcapDataMaster.CreatedBy; + results.LapcapFile = FormatFileData("TestFileName1", item.LapcapDataMaster.CreatedAt, item.LapcapDataMaster.CreatedBy); if (item.DefaultParameterSettingMaster != null) - results.ParametersFile = item.DefaultParameterSettingMaster.ParameterFileName + "," + item.DefaultParameterSettingMaster.CreatedAt.ToString("dd/MM/yyyy HH:mm") + "," + item.DefaultParameterSettingMaster.CreatedBy; + results.ParametersFile = FormatFileData(item.DefaultParameterSettingMaster.ParameterFileName, item.DefaultParameterSettingMaster.CreatedAt, item.DefaultParameterSettingMaster.CreatedBy); } return results; } + + private static string FormatFileData(string fileName, DateTime createdAt, string createdBy) + { + return $"{fileName},{createdAt.ToString(CalculationResults.DateFormat)},{createdBy}"; + } } } \ No newline at end of file diff --git a/src/EPR.Calculator.API/Constants/CalculationResults.cs b/src/EPR.Calculator.API/Constants/CalculationResults.cs new file mode 100644 index 0000000..d724cc6 --- /dev/null +++ b/src/EPR.Calculator.API/Constants/CalculationResults.cs @@ -0,0 +1,7 @@ +namespace EPR.Calculator.API.Constants +{ + public class CalculationResults + { + public static readonly string DateFormat = "dd/MM/yyyy HH:mm"; + } +} diff --git a/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs b/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs index 05d1cdf..3fb3f28 100644 --- a/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs +++ b/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs @@ -1,7 +1,7 @@ -using EPR.Calculator.API.Models; +using EPR.Calculator.API.Constants; +using EPR.Calculator.API.Models; using EPR.Calculator.API.Services; using EPR.Calculator.API.Utils; -using System; using System.Text; namespace EPR.Calculator.API.Exporter @@ -9,6 +9,14 @@ namespace EPR.Calculator.API.Exporter public class CalcResultsExporter : ICalcResultsExporter { private readonly IBlobStorageService _blobStorageService; + private const string RunName = "Run Name"; + private const string RunId = "Run Id"; + private const string RunDate = "Run Date"; + private const string Runby = "Run by"; + private const string FinancialYear = "Financial Year"; + private const string LapcapFile = "Lapcap File"; + private const string ParametersFile = "Parameters File"; + public CalcResultsExporter(IBlobStorageService blobStorageService) { _blobStorageService = blobStorageService; @@ -16,13 +24,7 @@ public CalcResultsExporter(IBlobStorageService blobStorageService) public void Export(CalcResult results) { var csvContent = new StringBuilder(); - AppendCsvLine(csvContent, "Run Name", results.CalcResultDetail.RunName); - AppendCsvLine(csvContent, "Run Id", results.CalcResultDetail.RunId.ToString()); - AppendCsvLine(csvContent, "Run Date", results.CalcResultDetail.RunDate.ToString("dd/MM/yyyy HH:mm")); - AppendCsvLine(csvContent, "Run by", results.CalcResultDetail.RunBy); - AppendCsvLine(csvContent, "Financial Year", results.CalcResultDetail.FinancialYear); - AppendFileInfo(csvContent, "Lapcap File", results.CalcResultDetail.LapcapFile); - AppendFileInfo(csvContent, "Parameters File", results.CalcResultDetail.ParametersFile); + LoadCalcResultDetail(results, csvContent); var fileName = GetResultFileName(results.CalcResultDetail.RunId); try { @@ -34,6 +36,17 @@ public void Export(CalcResult results) } } + private static void LoadCalcResultDetail(CalcResult results, StringBuilder csvContent) + { + AppendCsvLine(csvContent, RunName, results.CalcResultDetail.RunName); + AppendCsvLine(csvContent, RunId, results.CalcResultDetail.RunId.ToString()); + AppendCsvLine(csvContent, RunDate, results.CalcResultDetail.RunDate.ToString(CalculationResults.DateFormat)); + AppendCsvLine(csvContent, Runby, results.CalcResultDetail.RunBy); + AppendCsvLine(csvContent, FinancialYear, results.CalcResultDetail.FinancialYear); + AppendFileInfo(csvContent, LapcapFile, results.CalcResultDetail.LapcapFile); + AppendFileInfo(csvContent, ParametersFile, results.CalcResultDetail.ParametersFile); + } + private static void AppendFileInfo(StringBuilder csvContent, string label, string filePath) { var fileParts = filePath.Split(','); From 018d322b7134d1223f499fca178561c9dc96820d Mon Sep 17 00:00:00 2001 From: Mazar Shaik Date: Tue, 5 Nov 2024 15:05:48 +0000 Subject: [PATCH 22/29] Refactored code for more readability --- .../CalcResultsExporterTests.cs | 12 +++--------- .../Exporter/CalcResultsExporter.cs | 2 +- .../Services/BlobStorageService.cs | 1 - src/EPR.Calculator.API/Utils/CsvSanitiser.cs | 8 ++++---- 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/EPR.Calculator.API.UnitTests/CalcResultsExporterTests.cs b/src/EPR.Calculator.API.UnitTests/CalcResultsExporterTests.cs index e0b7561..040d3b4 100644 --- a/src/EPR.Calculator.API.UnitTests/CalcResultsExporterTests.cs +++ b/src/EPR.Calculator.API.UnitTests/CalcResultsExporterTests.cs @@ -74,17 +74,11 @@ public void Export_ShouldHandleIOExceptionGracefully() _blobStorageServiceMock .Setup(service => service.UploadResultFileContentAsync(It.IsAny(), It.IsAny())) .Throws(new IOException("Simulated IO error")); - - using (var consoleOutput = new StringWriter()) - { - Console.SetOut(consoleOutput); - - _calcResultsExporter.Export(calcResult); - - StringAssert.Contains(consoleOutput.ToString(), "Error writing file: Simulated IO error"); - } + var exception = Assert.ThrowsException(() => _calcResultsExporter.Export(calcResult)); + Assert.AreEqual("File upload failed: Simulated IO error", exception.Message); } + [TestMethod] public void AppendFileInfo_ShouldNotAppendIfFilePartsAreInvalid() { diff --git a/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs b/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs index 3fb3f28..ba9a103 100644 --- a/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs +++ b/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs @@ -32,7 +32,7 @@ public void Export(CalcResult results) } catch (IOException ex) { - Console.WriteLine($"Error writing file: {ex.Message}"); + throw new IOException($"File upload failed: {ex.Message}", ex); } } diff --git a/src/EPR.Calculator.API/Services/BlobStorageService.cs b/src/EPR.Calculator.API/Services/BlobStorageService.cs index 7be4430..152eb16 100644 --- a/src/EPR.Calculator.API/Services/BlobStorageService.cs +++ b/src/EPR.Calculator.API/Services/BlobStorageService.cs @@ -42,7 +42,6 @@ public void UploadResultFileContentAsync(string fileName, StringBuilder csvConte var blobClient = _containerClient.GetBlobClient(fileName); using var fileStream = File.OpenRead(fileName); blobClient.UploadAsync(fileStream, true); - fileStream.Close(); } catch (Exception ex) { diff --git a/src/EPR.Calculator.API/Utils/CsvSanitiser.cs b/src/EPR.Calculator.API/Utils/CsvSanitiser.cs index e85d6e9..d6cb6ff 100644 --- a/src/EPR.Calculator.API/Utils/CsvSanitiser.cs +++ b/src/EPR.Calculator.API/Utils/CsvSanitiser.cs @@ -14,10 +14,10 @@ public static string SanitiseData(T value) : JsonConvert.SerializeObject(value); // Remove newline, carriage returns, and commas, then trim - stringToSanitise = stringToSanitise.Replace("\r", string.Empty) - .Replace("\n", string.Empty) - .Replace(",", string.Empty) - .Trim(); + stringToSanitise = stringToSanitise.Replace(Environment.NewLine, string.Empty) + .Replace("\t", string.Empty) + .Replace(",", string.Empty) + .Trim(); return stringToSanitise; } From 095d95564a1025622fb16dafcf26abdf6b60969a Mon Sep 17 00:00:00 2001 From: Mazar Shaik Date: Tue, 5 Nov 2024 16:06:53 +0000 Subject: [PATCH 23/29] Used Async and await. --- src/EPR.Calculator.API/Services/BlobStorageService.cs | 6 +++--- src/EPR.Calculator.API/Services/IBlobStorageService.cs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/EPR.Calculator.API/Services/BlobStorageService.cs b/src/EPR.Calculator.API/Services/BlobStorageService.cs index 152eb16..227f0c1 100644 --- a/src/EPR.Calculator.API/Services/BlobStorageService.cs +++ b/src/EPR.Calculator.API/Services/BlobStorageService.cs @@ -34,14 +34,14 @@ public BlobStorageService(BlobServiceClient blobServiceClient, IConfiguration co } } - public void UploadResultFileContentAsync(string fileName, StringBuilder csvContent) + public async Task UploadResultFileContentAsync(string fileName, StringBuilder csvContent) { try { File.WriteAllText(fileName, csvContent.ToString()); - var blobClient = _containerClient.GetBlobClient(fileName); + BlobClient blobClient = _containerClient.GetBlobClient(fileName); using var fileStream = File.OpenRead(fileName); - blobClient.UploadAsync(fileStream, true); + await blobClient.UploadAsync(fileStream, true); } catch (Exception ex) { diff --git a/src/EPR.Calculator.API/Services/IBlobStorageService.cs b/src/EPR.Calculator.API/Services/IBlobStorageService.cs index 8adc6b4..3b7b823 100644 --- a/src/EPR.Calculator.API/Services/IBlobStorageService.cs +++ b/src/EPR.Calculator.API/Services/IBlobStorageService.cs @@ -4,7 +4,7 @@ namespace EPR.Calculator.API.Services { public interface IBlobStorageService { - void UploadResultFileContentAsync(string fileName, StringBuilder content); + Task UploadResultFileContentAsync(string fileName, StringBuilder content); Task GetResultFileContentAsync(); } } From dcd884baf7f75faa2346275ca280767b3098df3d Mon Sep 17 00:00:00 2001 From: Mazar Shaik Date: Tue, 5 Nov 2024 16:14:18 +0000 Subject: [PATCH 24/29] Added file extension as csv --- src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs b/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs index ba9a103..4f7e763 100644 --- a/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs +++ b/src/EPR.Calculator.API/Exporter/CalcResultsExporter.cs @@ -65,7 +65,7 @@ private static void AppendCsvLine(StringBuilder csvContent, string label, string } private static string GetResultFileName(int runId) { - return $"{runId}-{DateTime.Now:yyyy-MM-dd-HHmm}"; + return $"{runId}-{DateTime.Now:yyyy-MM-dd-HHmm}.csv"; } } } From a30d18bd0a72d4ba357b4bca0a279c96cfacc75f Mon Sep 17 00:00:00 2001 From: Mazar Shaik Date: Tue, 5 Nov 2024 16:53:57 +0000 Subject: [PATCH 25/29] Test cases failure checkin --- .../CalcResultsExporterTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/EPR.Calculator.API.UnitTests/CalcResultsExporterTests.cs b/src/EPR.Calculator.API.UnitTests/CalcResultsExporterTests.cs index 040d3b4..4932dcf 100644 --- a/src/EPR.Calculator.API.UnitTests/CalcResultsExporterTests.cs +++ b/src/EPR.Calculator.API.UnitTests/CalcResultsExporterTests.cs @@ -49,7 +49,7 @@ public void Export_ShouldUploadCorrectCsvContent() _calcResultsExporter.Export(calcResult); _blobStorageServiceMock.Verify(service => service.UploadResultFileContentAsync( - $"{calcResult.CalcResultDetail.RunId}-{DateTime.Now:yyyy-MM-dd-HHmm}", + $"{calcResult.CalcResultDetail.RunId}-{DateTime.Now:yyyy-MM-dd-HHmm}.csv", It.Is(content => content.ToString() == expectedCsvContent.ToString()) ), Times.Once); } @@ -106,7 +106,7 @@ public void AppendFileInfo_ShouldNotAppendIfFilePartsAreInvalid() _calcResultsExporter.Export(calcResult); _blobStorageServiceMock.Verify(service => service.UploadResultFileContentAsync( - $"{calcResult.CalcResultDetail.RunId}-{DateTime.Now:yyyy-MM-dd-HHmm}", + $"{calcResult.CalcResultDetail.RunId}-{DateTime.Now:yyyy-MM-dd-HHmm}.csv", It.Is(content => content.ToString() == expectedCsvContent.ToString()) ), Times.Once); } @@ -131,7 +131,7 @@ public void Export_CreatesCorrectCsvContent() _calcResultsExporter.Export(calcResult); _blobStorageServiceMock.Verify(x => x.UploadResultFileContentAsync(It.IsAny(), It.IsAny()), Times.Once); - var expectedFileName = $"{calcResult.CalcResultDetail.RunId}-{DateTime.Now:yyyy-MM-dd-HHmm}"; + var expectedFileName = $"{calcResult.CalcResultDetail.RunId}-{DateTime.Now:yyyy-MM-dd-HHmm}.csv"; _blobStorageServiceMock.Verify(x => x.UploadResultFileContentAsync(expectedFileName, It.IsAny()), Times.Once); } } From 766d5336e93cd2ca563fb9da8519d0e1c5a574de Mon Sep 17 00:00:00 2001 From: johnbenjamin-defra Date: Wed, 6 Nov 2024 11:50:31 +0000 Subject: [PATCH 26/29] 459419 Transpose pom and org data --- .../Services/TransposePomAndOrgDataServiceTests.cs | 9 +++++++-- .../Controllers/CalculatorInternalController.cs | 2 +- .../Services/ITransposePomAndOrgDataService.cs | 6 ++++-- .../Services/TransposePomAndOrgDataService.cs | 8 ++++---- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs b/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs index 35102b9..bf2b449 100644 --- a/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs +++ b/src/EPR.Calculator.API.UnitTests/Services/TransposePomAndOrgDataServiceTests.cs @@ -1,4 +1,5 @@ using EPR.Calculator.API.Data.DataModels; +using EPR.Calculator.API.Dtos; using EPR.Calculator.API.Services; using EPR.Calculator.API.Tests.Controllers; using EPR.Calculator.API.Wrapper; @@ -38,7 +39,9 @@ public void Transpose_Should_Return_Correct_Producer_Detail() #pragma warning disable CS8604 // Possible null reference argument. var service = new TransposePomAndOrgDataService(dbContext); #pragma warning restore CS8604 // Possible null reference argument. - service.Transpose(3); + + var resultsRequestDto = new CalcResultsRequestDto { RunId = 3 }; + service.Transpose(resultsRequestDto); var producerDetail = dbContext.ProducerDetail.FirstOrDefault(); Assert.IsNotNull(producerDetail); @@ -78,7 +81,9 @@ public void Transpose_Should_Return_Correct_Producer_Reported_Material() #pragma warning disable CS8604 // Possible null reference argument. var service = new TransposePomAndOrgDataService(dbContext); #pragma warning restore CS8604 // Possible null reference argument. - service.Transpose(3); + + var resultsRequestDto = new CalcResultsRequestDto { RunId = 3 }; + service.Transpose(resultsRequestDto); var producerReportedMaterial = dbContext.ProducerReportedMaterial.FirstOrDefault(); Assert.IsNotNull(producerReportedMaterial); diff --git a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs index 4e27e7e..a668183 100644 --- a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs +++ b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs @@ -150,7 +150,7 @@ public IActionResult UpdateRpdStatus([FromBody] UpdateRpdStatus request) [Route("prepareCalcResults")] public IActionResult PrepareCalcResults([FromBody] CalcResultsRequestDto resultsRequestDto) { - this.transposePomAndOrgDataService.Transpose(resultsRequestDto.RunId); + this.transposePomAndOrgDataService.Transpose(resultsRequestDto); var results = this.builder.Build(resultsRequestDto); this.exporter.Export(results); return new ObjectResult(null) { StatusCode = StatusCodes.Status201Created }; diff --git a/src/EPR.Calculator.API/Services/ITransposePomAndOrgDataService.cs b/src/EPR.Calculator.API/Services/ITransposePomAndOrgDataService.cs index 7fe9a71..26651c5 100644 --- a/src/EPR.Calculator.API/Services/ITransposePomAndOrgDataService.cs +++ b/src/EPR.Calculator.API/Services/ITransposePomAndOrgDataService.cs @@ -1,7 +1,9 @@ -namespace EPR.Calculator.API.Services +using EPR.Calculator.API.Dtos; + +namespace EPR.Calculator.API.Services { public interface ITransposePomAndOrgDataService { - public void Transpose(int runId); + public void Transpose(CalcResultsRequestDto resultsRequestDto); } } diff --git a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs index 0cee492..a5874f2 100644 --- a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs +++ b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs @@ -1,6 +1,6 @@ using EPR.Calculator.API.Data; using EPR.Calculator.API.Data.DataModels; -using EPR.Calculator.API.Wrapper; +using EPR.Calculator.API.Dtos; namespace EPR.Calculator.API.Services { @@ -14,11 +14,11 @@ public TransposePomAndOrgDataService(ApplicationDBContext context) this.context = context; } - public void Transpose(int runId) + public void Transpose(CalcResultsRequestDto resultsRequestDto) { var materials = this.context.Material.ToList(); - var calculatorRun = this.context.CalculatorRuns.Single(cr => cr.Id == runId); + var calculatorRun = this.context.CalculatorRuns.Single(cr => cr.Id == resultsRequestDto.RunId); if (calculatorRun.CalculatorRunPomDataMasterId != null) { @@ -48,7 +48,7 @@ public void Transpose(int runId) { var producerDetail = new ProducerDetail { - CalculatorRunId = runId, + CalculatorRunId = resultsRequestDto.RunId, ProducerId = pom.OrganisationId.Value, SubsidiaryId = pom.SubsidaryId, ProducerName = producerName, From 31fe93be35921709d37f508ccab1172ed891a188 Mon Sep 17 00:00:00 2001 From: johnbenjamin-defra Date: Wed, 6 Nov 2024 12:25:21 +0000 Subject: [PATCH 27/29] Update TransposePomAndOrgDataService.cs --- .../Services/TransposePomAndOrgDataService.cs | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs index a5874f2..7c54340 100644 --- a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs +++ b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs @@ -26,24 +26,35 @@ public void Transpose(CalcResultsRequestDto resultsRequestDto) { try { - var producerDetails = new List(); + // Initialise the producerReportedMaterials var producerReportedMaterials = new List(); - var pomDataDetails = context.CalculatorRunPomDataDetails.Where(pdd => pdd.CalculatorRunPomDataMasterId == (int)calculatorRun.CalculatorRunPomDataMasterId).ToList(); - - foreach (var pom in pomDataDetails) + // Get the calculator run organisation data master record based on the CalculatorRunOrganisationDataMasterId + // from the calculator run table + var organisationDataMaster = context.CalculatorRunOrganisationDataMaster.Single(odm => odm.Id == calculatorRun.CalculatorRunOrganisationDataMasterId); + + // Get the calculator run organisation data details as we need the organisation name + var organisationDataDetails = context.CalculatorRunOrganisationDataDetails.Where + ( + odd => odd.CalculatorRunOrganisationDataMasterId == organisationDataMaster.Id + ).ToList(); + + // Get the calculator run pom data details related to the calculator run pom data master + var calculatorRunPomDataDetails = context.CalculatorRunPomDataDetails.Where(pdd => pdd.CalculatorRunPomDataMasterId == (int)calculatorRun.CalculatorRunPomDataMasterId).ToList(); + + // Loop through the calculator run pom data details and + // populate the producerDetails and producerReportedMaterials + foreach (var pom in calculatorRunPomDataDetails) { + // Proceed further only if the organisation id has a value + // TO DO: We have to record if the organisation id is null in a separate table post Dec 2024 if (pom.OrganisationId.HasValue) { - var organisationDataMaster = context.CalculatorRunOrganisationDataMaster.Single(odm => odm.Id == calculatorRun.CalculatorRunOrganisationDataMasterId); - - var organisationDataDetails = context.CalculatorRunOrganisationDataDetails.Where - ( - odd => odd.CalculatorRunOrganisationDataMasterId == organisationDataMaster.Id - ).ToList(); - + // Get the producer name var producerName = organisationDataDetails.Single(odd => odd.OrganisationId == pom.OrganisationId)?.OrganisationName; + // Proceed further only if the organisation name has a value + // TO DO: We have to record if the organisation name is null in a separate table post Dec 2024 if (producerName != null) { var producerDetail = new ProducerDetail @@ -55,8 +66,7 @@ public void Transpose(CalcResultsRequestDto resultsRequestDto) CalculatorRun = calculatorRun }; - producerDetails.Add(producerDetail); - + // Add producer detail record to the database context context.ProducerDetail.Add(producerDetail); var material = materials.Single(m => m.Code == pom.PackagingMaterial); @@ -73,15 +83,20 @@ public void Transpose(CalcResultsRequestDto resultsRequestDto) PackagingTonnage = (decimal)pom.PackagingMaterialWeight / 1000, }; + // Populate the producer reported material list producerReportedMaterials.Add(producerReportedMaterial); } } } } + // Add the list of producer reported materials to the database context context.ProducerReportedMaterial.AddRange(producerReportedMaterials); + + // Apply the database changes context.SaveChanges(); + // Success, commit transaction transaction.Commit(); } catch (Exception) From e1a994ed7d49728b1714c2ae50fbdbe5a9d6a091 Mon Sep 17 00:00:00 2001 From: Mazar Shaik Date: Wed, 6 Nov 2024 12:36:12 +0000 Subject: [PATCH 28/29] Updated Lapcapfile name --- src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs b/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs index 1832d3e..89d0e0c 100644 --- a/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs +++ b/src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs @@ -37,7 +37,7 @@ public CalcResultDetail Construct(CalcResultsRequestDto resultsRequestDto) if (item.CalculatorRunPomDataMaster != null) results.RpdFilePOM = item.CalculatorRunPomDataMaster.CreatedAt.ToString(CalculationResults.DateFormat); if (item.LapcapDataMaster != null) - results.LapcapFile = FormatFileData("TestFileName1", item.LapcapDataMaster.CreatedAt, item.LapcapDataMaster.CreatedBy); + results.LapcapFile = FormatFileData(item.LapcapDataMaster.LapcapFileName, item.LapcapDataMaster.CreatedAt, item.LapcapDataMaster.CreatedBy); if (item.DefaultParameterSettingMaster != null) results.ParametersFile = FormatFileData(item.DefaultParameterSettingMaster.ParameterFileName, item.DefaultParameterSettingMaster.CreatedAt, item.DefaultParameterSettingMaster.CreatedBy); } From 9140ff175fb2acc1d72341e298e9de3a728e5547 Mon Sep 17 00:00:00 2001 From: johnbenjamin-defra Date: Wed, 6 Nov 2024 14:30:05 +0000 Subject: [PATCH 29/29] Update TransposePomAndOrgDataService.cs --- .../Services/TransposePomAndOrgDataService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs index 7c54340..4f09786 100644 --- a/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs +++ b/src/EPR.Calculator.API/Services/TransposePomAndOrgDataService.cs @@ -55,7 +55,7 @@ public void Transpose(CalcResultsRequestDto resultsRequestDto) // Proceed further only if the organisation name has a value // TO DO: We have to record if the organisation name is null in a separate table post Dec 2024 - if (producerName != null) + if (!string.IsNullOrWhiteSpace(producerName)) { var producerDetail = new ProducerDetail {