diff --git a/src/EPR.Calculator.API.UnitTests/UtilTest.cs b/src/EPR.Calculator.API.UnitTests/UtilTest.cs
index c7bd104..27d8cb2 100644
--- a/src/EPR.Calculator.API.UnitTests/UtilTest.cs
+++ b/src/EPR.Calculator.API.UnitTests/UtilTest.cs
@@ -58,5 +58,78 @@ public void GetParameterValueTest_For_Percent()
Assert.IsNotNull(parameterValue);
Assert.AreEqual(parameterValue, "100");
}
+
+ ///
+ /// Tests that GetFinancialYearAsYYYY returns the first year from a valid financial year string.
+ ///
+ /// The financial year string to parse.
+ /// The expected first year as a string.
+ [TestMethod]
+ [DataRow("2024-25", "2024")]
+ [DataRow("2023-24", "2023")]
+ [DataRow("2022-23", "2022")]
+ public void GetFinancialYearAsYYYY_ValidString_ShouldReturnFirstYear(string financialYear, string expectedFinancialYear)
+ {
+ var result = Util.GetFinancialYearAsYYYY(financialYear);
+ Assert.AreEqual(expectedFinancialYear, result);
+ }
+
+ ///
+ /// Tests that GetFinancialYearAsYYYY throws a FormatException for an invalid string.
+ ///
+ /// The invalid financial year string to parse.
+ [TestMethod]
+ [DataRow("2024")]
+ [DataRow("24-25")]
+ [DataRow("2024-2025")]
+ [DataRow("abcd-efgh")]
+ public void GetFinancialYearAsYYYY_InvalidString_ShouldThrowFormatException(string financialYear)
+ {
+ var exception = Assert.ThrowsException(() => Util.GetFinancialYearAsYYYY(financialYear));
+ Assert.AreEqual("Financial year format is invalid. Expected format is 'YYYY-YY'.", exception.Message);
+ }
+
+ ///
+ /// Tests that GetFinancialYearAsYYYY throws a ArgumentException for a null or empty string.
+ ///
+ /// The null or empty financial year string to convert.
+ [TestMethod]
+ [DataRow(null)]
+ [DataRow("")]
+ [DataRow(" ")]
+ public void GetFinancialYearAsYYYY_NullOrEmptyString_ShouldThrowArgumentException(string financialYear)
+ {
+ var exception = Assert.ThrowsException(() => Util.GetFinancialYearAsYYYY(financialYear));
+ Assert.AreEqual("Financial year cannot be null or empty (Parameter 'value')", exception.Message);
+ }
+
+ ///
+ /// Tests that GetCalendarYear returns the previous year as a string for a valid financial year string.
+ ///
+ /// The financial year string to convert.
+ /// The expected previous calendar year as a string.
+ [TestMethod]
+ [DataRow("2024-25", "2023")]
+ [DataRow("2023-24", "2022")]
+ [DataRow("2022-23", "2021")]
+ public void GetCalendarYear_ValidString_ShouldReturnPreviousYearAsString(string financialYear, string expectedCalendarYear)
+ {
+ var result = Util.GetCalendarYear(financialYear);
+ Assert.AreEqual(expectedCalendarYear, result);
+ }
+
+ ///
+ /// Tests that GetCalendarYear throws an ArgumentException for a null or empty string.
+ ///
+ /// The null or empty financial year string to convert.
+ [TestMethod]
+ [DataRow(null)]
+ [DataRow("")]
+ [DataRow(" ")]
+ public void GetCalendarYear_NullOrEmptyString_ShouldThrowArgumentException(string financialYear)
+ {
+ var exception = Assert.ThrowsException(() => Util.GetCalendarYear(financialYear));
+ Assert.AreEqual("Financial year cannot be null or empty (Parameter 'financialYear')", exception.Message);
+ }
}
}
diff --git a/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs b/src/EPR.Calculator.API/Controllers/CalculatorInternalController.cs
index 4547855..5f17409 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.Utils;
using EPR.Calculator.API.Validators;
using EPR.Calculator.API.Wrapper;
using Microsoft.AspNetCore.Mvc;
@@ -68,7 +69,7 @@ public IActionResult UpdateRpdStatus([FromBody] UpdateRpdStatus request)
var stagingOrganisationData = this.wrapper.GetOrganisationData();
var calcOrganisationMaster = new CalculatorRunOrganisationDataMaster
{
- CalendarYear = "2023", //Take the financial year from Calc Run table and Derive the Calendar year
+ CalendarYear = Util.GetCalendarYear("2024-25"), //Take the financial year from Calc Run table and Derive the Calendar year
CreatedAt = DateTime.Now,
CreatedBy = request.UpdatedBy,
EffectiveFrom = DateTime.Now,
diff --git a/src/EPR.Calculator.API/Utils/Util.cs b/src/EPR.Calculator.API/Utils/Util.cs
index 3fba1ef..edb0a50 100644
--- a/src/EPR.Calculator.API/Utils/Util.cs
+++ b/src/EPR.Calculator.API/Utils/Util.cs
@@ -1,6 +1,7 @@
using EPR.Calculator.API.Data.DataModels;
using EPR.Calculator.API.Dtos;
using System.Text;
+using System.Text.RegularExpressions;
namespace EPR.Calculator.API.Utils
{
@@ -195,5 +196,46 @@ public static CreateLapcapDataErrorDto CreateLapcapDataErrorDto(string country,
UniqueReference = uniqueReference
};
}
+
+ ///
+ /// Extracts the first year from a financial year string in the format "YYYY-YY".
+ ///
+ /// The financial year string to parse, in the format "YYYY-YY".
+ /// The first year as a string.
+ /// Thrown when the format is invalid.
+ public static string GetFinancialYearAsYYYY(string value)
+ {
+ if (string.IsNullOrWhiteSpace(value))
+ {
+ throw new ArgumentException("Financial year cannot be null or empty", nameof(value));
+ }
+
+ string pattern = @"^\d{4}-\d{2}$";
+ TimeSpan regexTimeout = TimeSpan.FromSeconds(1);
+ if (!Regex.IsMatch(value, pattern, RegexOptions.None, regexTimeout))
+ {
+ throw new FormatException("Financial year format is invalid. Expected format is 'YYYY-YY'.");
+ }
+
+ var years = value.Split('-');
+ return years[0];
+ }
+
+ ///
+ /// Converts a financial year string to the previous calendar year as a string.
+ ///
+ /// The financial year string to convert, in the format "YYYY-YY".
+ /// The previous calendar year as a string.
+ /// Thrown when the financial year string is null or empty.
+ public static string GetCalendarYear(string financialYear)
+ {
+ if (string.IsNullOrWhiteSpace(financialYear))
+ {
+ throw new ArgumentException("Financial year cannot be null or empty", nameof(financialYear));
+ }
+
+ int year = int.Parse(GetFinancialYearAsYYYY(financialYear));
+ return (year - 1).ToString();
+ }
}
}