diff --git a/portal/server/controllers/utilisation-report-service/utilisation-report-upload/utilisation-report-validator.js b/portal/server/controllers/utilisation-report-service/utilisation-report-upload/utilisation-report-validator.js index 23c7dbfd4a..4e16667d49 100644 --- a/portal/server/controllers/utilisation-report-service/utilisation-report-upload/utilisation-report-validator.js +++ b/portal/server/controllers/utilisation-report-service/utilisation-report-upload/utilisation-report-validator.js @@ -106,14 +106,20 @@ const validateFilenameContainsReportPeriod = (filename, dueReportPeriod) => { return { regex, regexWithExactYear }; }); - const firstMatchingRegex = regexPatterns.filter(({ regex }) => regex.test(filename)).at(0); - if (!firstMatchingRegex) { + const allMatchingRegex = regexPatterns.filter(({ regex }) => regex.test(filename)); + if (allMatchingRegex.length === 0) { const filenameError = `The selected file must contain the reporting period as part of its name, for example '${expectedFilenameReportPeriod}'`; return { filenameError }; } - const { regexWithExactYear } = firstMatchingRegex; - if (regexWithExactYear.test(expectedFilenameReportPeriod)) { + const specificReportPeriodRegex = allMatchingRegex.filter(({ regex }) => regex.test(expectedFilenameReportPeriod)).at(0); + if (!specificReportPeriodRegex) { + const filenameError = `The selected file must be the ${dueReportPeriod} report`; + return { filenameError }; + } + + const { regexWithExactYear } = specificReportPeriodRegex; + if (regexWithExactYear.test(filename)) { return {}; } diff --git a/portal/server/controllers/utilisation-report-service/utilisation-report-upload/utilisation-report-validator.test.js b/portal/server/controllers/utilisation-report-service/utilisation-report-upload/utilisation-report-validator.test.js index 2c5945f626..5f4242934b 100644 --- a/portal/server/controllers/utilisation-report-service/utilisation-report-upload/utilisation-report-validator.test.js +++ b/portal/server/controllers/utilisation-report-service/utilisation-report-upload/utilisation-report-validator.test.js @@ -163,7 +163,7 @@ describe('utilisation-report-validator', () => { expect(filenameError).toBeUndefined(); }); - it('should return specific error text when the filename contains the incorrect reporting period', () => { + it('should return specific error text when the filename contains the incorrect reporting period month', () => { const reportPeriod = 'December 2023'; const filename = 'Bank_November_2023.xlsx'; @@ -172,6 +172,15 @@ describe('utilisation-report-validator', () => { expect(filenameError).toEqual(`The selected file must be the ${reportPeriod} report`); }); + it('should return specific error text when the filename contains the incorrect reporting period year', () => { + const reportPeriod = 'December 2023'; + const filename = 'Bank_December_2022.xlsx'; + + const { filenameError } = validateFilenameContainsReportPeriod(filename, reportPeriod); + + expect(filenameError).toEqual(`The selected file must be the ${reportPeriod} report`); + }); + it('should return specific error text when the filename contains no reporting period', () => { const reportPeriod = 'December 2023'; const filename = 'Bank_paid_this_much.xlsx';