Skip to content

Commit

Permalink
ustc #4372: allow full ISO date + end date for reconciliation report
Browse files Browse the repository at this point in the history
  • Loading branch information
jtdevos committed Dec 13, 2023
1 parent d6f1c72 commit 97ae0f6
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { DateTime } from 'luxon';
import {
FORMATS,
USTC_TZ,
createEndOfDayISO,
createStartOfDayISO,
formatNow,
Expand Down Expand Up @@ -163,4 +165,46 @@ describe('getReconciliationReportInteractor', () => {
.calls[0][0].docketNumbers,
).toEqual(['135-20']);
});

//Given date may contain ISO time component
it('should accept ISO dates with a time component', async () => {
const startDate = '2020-01-01T01:00';
await expect(
getReconciliationReportInteractor(applicationContext, {
reconciliationDate: startDate,
}),
).resolves.not.toThrow();
});

//Caller may provide two date arguments
it('should accept two arguments representing a date range', async () => {
const startDate = '2021-01-05';
const endDate = '2021-01-05T09:00';
const isoEndDate = DateTime.fromISO(endDate, { zone: USTC_TZ })
.toUTC()
.toISO();
const docketEntries = [
{
docketEntryId: '3d27e02e-6954-4595-8b3f-0e91bbc1b51e',
docketNumber: '135-20',
documentTitle: 'Petition',
eventCode: 'P',
filedBy: 'Petr. Kaitlin Chaney',
filingDate: '2021-01-05T21:14:09.031Z',
servedAt: '2021-01-05T21:14:09.031Z',
},
] as any;

applicationContext
.getPersistenceGateway()
.getReconciliationReport.mockReturnValue(docketEntries);

const result = await getReconciliationReportInteractor(applicationContext, {
reconciliationDate: startDate,
reconciliationDateEnd: endDate,
});
expect(result.reconciliationDate).toBe(startDate);
expect(result.reconciliationDateEnd).toBe(isoEndDate);
});
//TODO: If given two future dates, system should throw error
});
47 changes: 36 additions & 11 deletions shared/src/business/useCases/getReconciliationReportInteractor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { DateTime } from 'luxon';
import { DocketEntryDynamoRecord } from '../../../../web-api/src/persistence/dynamo/dynamoTypes';
import {
FORMATS,
PATTERNS,
createEndOfDayISO,
createStartOfDayISO,
USTC_TZ,
formatNow,
isValidISODate,
} from '../../business/utilities/DateHandler';

import {
ROLE_PERMISSIONS,
isAuthorized,
Expand All @@ -14,7 +15,7 @@ import { ReconciliationReportEntry } from '../entities/ReconciliationReportEntry
import { UnauthorizedError } from '@web-api/errors/errors';

const isValidDate = dateString => {
const dateInputValid = PATTERNS.YYYYMMDD.test(dateString);
const dateInputValid = isValidISODate(dateString);
const todayDate = formatNow(FORMATS.YYYYMMDD);
const dateLessthanOrEqualToToday = dateString <= todayDate;
return dateInputValid && dateLessthanOrEqualToToday;
Expand All @@ -30,7 +31,10 @@ const isValidDate = dateString => {
*/
export const getReconciliationReportInteractor = async (
applicationContext: IApplicationContext,
{ reconciliationDate }: { reconciliationDate: string },
{
reconciliationDate,
reconciliationDateEnd,
}: { reconciliationDate: string; reconciliationDateEnd?: string },
) => {
const authorizedUser = applicationContext.getCurrentUser();

Expand All @@ -43,15 +47,35 @@ export const getReconciliationReportInteractor = async (
} else {
const dateInputValid = isValidDate(reconciliationDate);
if (!dateInputValid) {
throw new Error(
'Date must be formatted as YYYY-MM-DD and not later than today',
);
throw new Error('Date must be formatted as ISO and not later than today');
}
}

const [year, month, day] = reconciliationDate.split('-');
const reconciliationDateStart = createStartOfDayISO({ day, month, year });
const reconciliationDateEnd = createEndOfDayISO({ day, month, year });
const reconciliationDateStart = DateTime.fromISO(reconciliationDate, {
zone: USTC_TZ,
})
.toUTC()
.toISO()!;

//If no end date specified, set it to end of the same day as start date
if (!reconciliationDateEnd) {
reconciliationDateEnd = DateTime.fromISO(reconciliationDate, {
zone: USTC_TZ,
})
.endOf('day')
.toUTC()
.toISO()!;
} else {
reconciliationDateEnd = DateTime.fromISO(reconciliationDateEnd, {
zone: USTC_TZ,
})
.toUTC()
.toISO()!;
}

if (!DateTime.fromISO(reconciliationDateEnd).isValid) {
throw new Error('End date must be formatted as ISO');
}

const docketEntries = await applicationContext
.getPersistenceGateway()
Expand All @@ -69,6 +93,7 @@ export const getReconciliationReportInteractor = async (
{ applicationContext },
),
reconciliationDate,
reconciliationDateEnd,
reportTitle: 'Reconciliation Report',
totalDocketEntries: docketEntries.length,
};
Expand Down
19 changes: 11 additions & 8 deletions shared/src/business/utilities/DateHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ export const prepareDateFromString = (
dateString?: string,
inputFormat?: TimeFormats,
): DateTime => {
const dateToFormat: string = dateString || createISODateString();
let dateToFormat: string = dateString || createISODateString();
let result: DateTime;

if (inputFormat === FORMATS.ISO) {
result = DateTime.fromISO(dateToFormat, { zone: 'utc' });
} else if (inputFormat) {
Expand Down Expand Up @@ -184,9 +183,9 @@ export const createDateAtStartOfWeekEST = (
};

export const createEndOfDayISO = (params?: {
day: string;
month: string;
year: string;
day: string | number;
month: string | number;
year: string | number;
}): string => {
const dateObject = params
? DateTime.fromObject(
Expand All @@ -203,9 +202,9 @@ export const createEndOfDayISO = (params?: {
};

export const createStartOfDayISO = (params?: {
day: string;
month: string;
year: string;
day: string | number;
month: string | number;
year: string | number;
}): string => {
const dateObject = params
? DateTime.fromObject(
Expand Down Expand Up @@ -269,6 +268,10 @@ export const formatNow = (formatStr?: TimeFormats | TimeFormatNames) => {
return formatDateString(now, formatStr);
};

export function isValidISODate(isodate: string): boolean {
return DateTime.fromISO(isodate).isValid;
}

/**
* @param {string} date1 the first date to be compared
* @param {string} date2 the second date to be compared
Expand Down
2 changes: 1 addition & 1 deletion web-api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ app.get(
lambdaWrapper(v2GetDocumentDownloadUrlLambda),
);
app.get(
'/v2/reconciliation-report/:reconciliationDate',
'/v2/reconciliation-report/:reconciliationDate/:reconciliationDateEnd',
lambdaWrapper(v2GetReconciliationReportLambda),
);
}
Expand Down

0 comments on commit 97ae0f6

Please sign in to comment.