Skip to content

Commit

Permalink
feat(FN-1134): fix validate filename year (#2338)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshBinns2000 authored Nov 22, 2023
1 parent 5f7cc75 commit a1aecf9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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';
Expand Down

0 comments on commit a1aecf9

Please sign in to comment.