diff --git a/web-api/src/business/useCases/trialSessions/getTrialSessionPlanningReportDataInteractor.test.ts b/web-api/src/business/useCases/trialSessions/getTrialSessionPlanningReportDataInteractor.test.ts new file mode 100644 index 00000000000..c2d692da86e --- /dev/null +++ b/web-api/src/business/useCases/trialSessions/getTrialSessionPlanningReportDataInteractor.test.ts @@ -0,0 +1,223 @@ +import { RawTrialSession } from '@shared/business/entities/trialSessions/TrialSession'; +import { SESSION_TYPES } from '@shared/business/entities/EntityConstants'; +import { UnauthorizedError } from '@web-api/errors/errors'; +import { applicationContext } from '@shared/business/test/createTestApplicationContext'; +import { getTrialSessionPlanningReportDataInteractor } from '@web-api/business/useCases/trialSessions/getTrialSessionPlanningReportDataInteractor'; +import { + mockPetitionerUser, + mockPetitionsClerkUser, +} from '@shared/test/mockAuthUsers'; + +describe('getTrialSessionPlanningReportDataInteractor', () => { + const ALL_TRIAL_SESSIONS_MOCK: RawTrialSession[] = [ + { + isCalendared: true, + judge: { + name: 'TEST_JUDGE_4', + userId: 'TEST_JUDGE_ID', + }, + sessionType: 'Hybrid', + startDate: '2099-03-01T00:00:00.000Z', + term: 'winter', + termYear: '2023', + trialLocation: 'Denver, Colorado', + } as RawTrialSession, + { + isCalendared: true, + judge: { + name: 'TEST_JUDGE_3', + userId: 'TEST_JUDGE_ID', + }, + sessionType: SESSION_TYPES.hybridSmall, + startDate: '2098-03-01T00:00:00.000Z', + term: 'winter', + termYear: '2023', + trialLocation: 'Denver, Colorado', + } as RawTrialSession, + { + isCalendared: true, + judge: { + name: 'TEST_JUDGE_2', + userId: 'TEST_JUDGE_ID', + }, + sessionType: 'Hybrid', + startDate: '2099-03-01T00:00:00.000Z', + term: 'spring', + termYear: '2023', + trialLocation: 'Denver, Colorado', + } as RawTrialSession, + { + isCalendared: true, + judge: { + name: 'TEST_JUDGE_1', + userId: 'TEST_JUDGE_ID', + }, + sessionType: 'Hybrid', + startDate: '2099-03-01T00:00:00.000Z', + term: 'fall', + termYear: '2023', + trialLocation: 'Denver, Colorado', + } as RawTrialSession, + { + isCalendared: true, + sessionType: 'Special', + startDate: '2024-03-01T00:00:00.000Z', + term: 'winter', + termYear: '2024', + trialLocation: 'Denver, Colorado', + } as RawTrialSession, + { + isCalendared: true, + sessionType: 'Hybrid', + startDate: '2024-03-01T00:00:00.000Z', + term: 'winter', + termYear: '2024', + trialLocation: 'Denver, Colorado', + } as RawTrialSession, + { + isCalendared: false, + sessionType: 'Special', + startDate: '2024-03-01T00:00:00.000Z', + term: 'winter', + termYear: '2024', + trialLocation: 'Denver, Colorado', + } as RawTrialSession, + { + isCalendared: true, + sessionType: 'Special', + startDate: '1998-03-01T00:00:00.000Z', + term: 'winter', + termYear: '2024', + trialLocation: 'Fresno, California', + } as RawTrialSession, + { + isCalendared: true, + sessionType: 'Special', + startDate: '1999-03-01T00:00:00.000Z', + term: 'winter', + termYear: '2024', + trialLocation: 'Fresno, California', + } as RawTrialSession, + { + isCalendared: true, + sessionType: 'Special', + startDate: '1997-03-01T00:00:00.000Z', + term: 'winter', + termYear: '2024', + } as RawTrialSession, + { + isCalendared: true, + sessionType: 'Special', + startDate: '2024-03-01T00:00:00.000Z', + term: 'winter', + termYear: '2024', + trialLocation: 'Denver, Colorado', + } as RawTrialSession, + { + isCalendared: true, + sessionType: 'Hybrid', + startDate: '2019-03-01T00:00:00.000Z', + term: 'winter', + termYear: '2024', + trialLocation: 'Denver, Colorado', + } as RawTrialSession, + ]; + const SMALL_ELIGIBLE_CASES_FOR_TRIAL_CITY_MOCK = [{}]; + const REGULAR_ELIGIBLE_CASES_FOR_TRIAL_CITY_MOCK = [{}, {}]; + const BLOCKED_CASES_MOCK = [{}, {}, {}]; + + beforeEach(() => { + applicationContext + .getPersistenceGateway() + .getTrialSessions.mockResolvedValue(ALL_TRIAL_SESSIONS_MOCK); + + applicationContext + .getPersistenceGateway() + .getEligibleCasesForTrialCity.mockImplementation(({ procedureType }) => { + if (procedureType === 'Small') + return SMALL_ELIGIBLE_CASES_FOR_TRIAL_CITY_MOCK; + return REGULAR_ELIGIBLE_CASES_FOR_TRIAL_CITY_MOCK; + }); + + applicationContext + .getPersistenceGateway() + .getBlockedCases.mockResolvedValue(BLOCKED_CASES_MOCK); + }); + + it('should throw error if the user is "Unauthorized"', async () => { + applicationContext + .getPersistenceGateway() + .getTrialSessionById.mockReturnValue({}); + + await expect( + getTrialSessionPlanningReportDataInteractor( + applicationContext, + { + term: 'winter', + year: 2024, + }, + mockPetitionerUser, + ), + ).rejects.toThrow(UnauthorizedError); + }); + + it('should return the correct Trial Session Planning Data for the correct term', async () => { + const { previousTerms, trialLocationData } = + await getTrialSessionPlanningReportDataInteractor( + applicationContext, + { + term: 'winter', + year: 2024, + }, + mockPetitionsClerkUser, + ); + + const DENVER_TRIAL_SESSION = trialLocationData.find( + ts => ts.trialCityState === 'Denver, Colorado', + ); + + expect(DENVER_TRIAL_SESSION).toEqual({ + allCaseCount: 3, + blockedCaseCount: 3, + lastVisitedDate: '2099-03-01T00:00:00.000Z', + previousTermsData: [ + ['(H) TEST_JUDGE_1'], + ['(H) TEST_JUDGE_2'], + ['(HS) TEST_JUDGE_3', '(H) TEST_JUDGE_4'], + ], + regularCaseCount: 2, + smallCaseCount: 1, + specialCaseCount: 2, + stateAbbreviation: 'CO', + trialCityState: 'Denver, Colorado', + }); + + expect(previousTerms).toEqual([ + { term: 'fall', termDisplay: 'Fall 2023', year: 2023 }, + { term: 'spring', termDisplay: 'Spring 2023', year: 2023 }, + { term: 'winter', termDisplay: 'Winter 2023', year: 2023 }, + ]); + }); + + it('should return the correct last visited date when there is no scheduled trial session in past 3 terms', async () => { + const { trialLocationData } = + await getTrialSessionPlanningReportDataInteractor( + applicationContext, + { + term: 'winter', + year: 2024, + }, + mockPetitionsClerkUser, + ); + + const FRESNO_TRIAL_SESSION = trialLocationData.find( + ts => ts.trialCityState === 'Fresno, California', + ); + + expect(FRESNO_TRIAL_SESSION).toMatchObject({ + lastVisitedDate: '1999-03-01T00:00:00.000Z', + specialCaseCount: 2, + trialCityState: 'Fresno, California', + }); + }); +}); diff --git a/web-api/src/business/useCases/trialSessions/getTrialSessionPlanningReportDataInteractor.ts b/web-api/src/business/useCases/trialSessions/getTrialSessionPlanningReportDataInteractor.ts index a309c5cee44..b6cefd6a5a9 100644 --- a/web-api/src/business/useCases/trialSessions/getTrialSessionPlanningReportDataInteractor.ts +++ b/web-api/src/business/useCases/trialSessions/getTrialSessionPlanningReportDataInteractor.ts @@ -43,15 +43,16 @@ export const getTrialSessionPlanningReportDataInteractor = async ( throw new UnauthorizedError('Unauthorized'); } - const reportData = await getTrialSessionPlanningReportData({ - applicationContext, - term, - year, - }); + const { previousTerms, trialLocationData } = + await getTrialSessionPlanningReportData({ + applicationContext, + term, + year, + }); return { - previousTerms: reportData.previousTerms, - trialLocationData: reportData.trialLocationData, + previousTerms, + trialLocationData, }; }; @@ -87,7 +88,7 @@ const getTrialSessionPlanningReportData = async ({ .getTrialSessions({ applicationContext }) ).filter(ts => ts.isCalendared); - const tempTrialSessions = allTrialSessions.filter(session => + const filteredTrialSessions = allTrialSessions.filter(session => ['Regular', 'Small', 'Hybrid', 'Hybrid-S'].includes(session.sessionType), ); @@ -112,9 +113,9 @@ const getTrialSessionPlanningReportData = async ({ trialCities.map(trialLocation => getTrialLocation(applicationContext, { allTrialSessions, + filteredTrialSessions, previousTerms, specialTrialSessionsCounts, - tempTrialSessions, trialLocation, }), ), @@ -145,20 +146,19 @@ const getPreviousTerm = ( }; }; -// TODO: rename tempTrialSessions; const getTrialLocation = async ( applicationContext: ServerApplicationContext, { allTrialSessions, + filteredTrialSessions, previousTerms, specialTrialSessionsCounts, - tempTrialSessions, trialLocation, }: { allTrialSessions: RawTrialSession[]; trialLocation: { city: string; state: string }; previousTerms: PreviousTerm[]; - tempTrialSessions: RawTrialSession[]; + filteredTrialSessions: RawTrialSession[]; specialTrialSessionsCounts: { [key: string]: number }; }, ): Promise => { @@ -195,10 +195,9 @@ const getTrialLocation = async ( const blockedCaseCount = blockedCasesResult.length; const specialCaseCount = specialTrialSessionsCounts[trialCityState] || 0; - const previousTermsDataTemp: RawTrialSession[][] = []; - + const previousTermsDataInformation: RawTrialSession[][] = []; previousTerms.forEach(previousTerm => { - const previousTermSessions = tempTrialSessions.filter( + const previousTermSessions = filteredTrialSessions.filter( trialSession => trialSession.term.toLowerCase() === previousTerm.term.toLowerCase() && Number(trialSession.termYear) === previousTerm.year && @@ -221,18 +220,18 @@ const getTrialLocation = async ( previousTermSessionList.push(previousTermSession); } }); - previousTermsDataTemp.push(previousTermSessionList); + previousTermsDataInformation.push(previousTermSessionList); }); const lastVisitedDate: string | undefined = - getLatestDateFromPreviousTerm(previousTermsDataTemp[2]) || + getLatestDateFromPreviousTerm(previousTermsDataInformation[2]) || getLatestDateForTrialSessionLocation({ allTrialSessions, applicationContext, trialCityState, }); - const previousTermsData = previousTermsDataTemp.map(prevTermArray => { + const previousTermsData = previousTermsDataInformation.map(prevTermArray => { return prevTermArray.map(prevTerm => { const sessionTypeChar = prevTerm.sessionType === SESSION_TYPES.hybridSmall @@ -270,8 +269,8 @@ function getLatestDateForTrialSessionLocation({ applicationContext, trialCityState, }: { - applicationContext: ServerApplicationContext; allTrialSessions: RawTrialSession[]; + applicationContext: ServerApplicationContext; trialCityState: string; }): string | undefined { const allTrialSessionsForLocation = allTrialSessions.filter( diff --git a/web-api/src/business/useCases/trialSessions/runTrialSessionPlanningReportInteractor.test.ts b/web-api/src/business/useCases/trialSessions/runTrialSessionPlanningReportInteractor.test.ts index adfc8a7a057..30ef9150ece 100644 --- a/web-api/src/business/useCases/trialSessions/runTrialSessionPlanningReportInteractor.test.ts +++ b/web-api/src/business/useCases/trialSessions/runTrialSessionPlanningReportInteractor.test.ts @@ -1,7 +1,7 @@ import { - SESSION_TYPES, - TRIAL_CITIES, -} from '@shared/business/entities/EntityConstants'; + PreviousTerm, + TrialLocationData, +} from '@web-api/business/useCases/trialSessions/getTrialSessionPlanningReportDataInteractor'; import { applicationContext } from '@shared/business/test/createTestApplicationContext'; import { mockPetitionerUser, @@ -12,19 +12,48 @@ import { runTrialSessionPlanningReportInteractor } from './runTrialSessionPlanni describe('run trial session planning report', () => { const mockPdfUrl = 'www.example.com'; + const PREVIOUS_TERMS_MOCK: PreviousTerm[] = [ + { term: 'winter', termDisplay: 'Winter 2020', year: 2020 }, + { term: 'fall', termDisplay: 'Fall 2019', year: 2019 }, + { term: 'spring', termDisplay: 'Spring 2019', year: 2019 }, + ]; + + const TRIAL_LOCATION_DATA_MOCK: TrialLocationData[] = [ + { + allCaseCount: 1, + blockedCaseCount: 5, + lastVisitedDate: 'lastVisitedDate', + previousTermsData: [], + regularCaseCount: 2, + smallCaseCount: 3, + specialCaseCount: 4, + stateAbbreviation: 'stateAbbreviation', + trialCityState: 'trialCityState', + }, + ]; + + const getTrialSessionPlanningReportDataInteractorResults: { + previousTerms: PreviousTerm[]; + trialLocationData: TrialLocationData[]; + } = { + previousTerms: PREVIOUS_TERMS_MOCK, + trialLocationData: TRIAL_LOCATION_DATA_MOCK, + }; + beforeEach(() => { + applicationContext + .getUseCases() + .getTrialSessionPlanningReportDataInteractor.mockReturnValue( + getTrialSessionPlanningReportDataInteractorResults, + ); + applicationContext .getUseCaseHelpers() .saveFileAndGenerateUrl.mockReturnValue(mockPdfUrl); + applicationContext - .getPersistenceGateway() - .getEligibleCasesForTrialCity.mockResolvedValue([ - { docketNumber: '123-20' }, - { docketNumber: '234-20' }, - ]); - applicationContext - .getPersistenceGateway() - .getTrialSessions.mockResolvedValue([]); + .getDocumentGenerators() + .trialSessionPlanningReport.mockReturnValue('MOCKED_FILE'); }); it('throws error if user is unauthorized', async () => { @@ -44,68 +73,8 @@ describe('run trial session planning report', () => { ).rejects.toThrow(); }); - it('returns previous terms and the trial locations and case counts', async () => { - let mockTrialSessions = [ - { - judge: { name: 'Judge Colvin' }, - sessionType: SESSION_TYPES.regular, - startDate: '2019-05-01T21:40:46.415Z', - term: 'spring', - termYear: '2019', - trialLocation: 'Aberdeen, South Dakota', - trialSessionId: '123', - }, - { - judge: { name: 'Judge Buch' }, - sessionType: SESSION_TYPES.small, - startDate: '2019-04-01T21:40:46.415Z', - term: 'spring', - termYear: '2019', - trialLocation: 'Aberdeen, South Dakota', - trialSessionId: '234', - }, - { - //judge is missing, so this one should not show up in the list - sessionType: SESSION_TYPES.small, - startDate: '2019-06-01T21:40:46.415Z', - term: 'spring', - termYear: '2019', - trialLocation: 'Aberdeen, South Dakota', - trialSessionId: '234', - }, - { - judge: { name: 'Judge Ashford' }, - sessionType: SESSION_TYPES.small, - startDate: '2019-09-01T21:40:46.415Z', - term: 'fall', - termYear: '2019', - trialLocation: 'Aberdeen, South Dakota', - trialSessionId: '345', - }, - { - judge: { name: 'Judge Ashford' }, - sessionType: SESSION_TYPES.special, - startDate: '2019-10-01T21:40:46.415Z', - term: 'fall', - termYear: '2019', - trialLocation: 'Aberdeen, South Dakota', - trialSessionId: '456', - }, - { - judge: { name: 'Judge Ashford' }, - sessionType: SESSION_TYPES.hybridSmall, - startDate: '2019-11-11T21:40:40.415Z', - term: 'fall', - termYear: '2019', - trialLocation: 'Albany, New York', - trialSessionId: '888', - }, - ]; - applicationContext - .getPersistenceGateway() - .getTrialSessions.mockResolvedValue(mockTrialSessions); - - const result = await runTrialSessionPlanningReportInteractor( + it('should save and generate PDF url with correct planning report data', async () => { + const results = await runTrialSessionPlanningReportInteractor( applicationContext, { term: 'winter', @@ -114,122 +83,34 @@ describe('run trial session planning report', () => { mockPetitionsClerkUser, ); - expect( - applicationContext.getDocumentGenerators().trialSessionPlanningReport.mock - .calls[0][0].data.locationData.length, - ).toEqual(TRIAL_CITIES.ALL.length); - expect( - applicationContext.getDocumentGenerators().trialSessionPlanningReport.mock - .calls[0][0].data.locationData[0], - ).toMatchObject({ - allCaseCount: 4, - previousTermsData: [['(S) Ashford'], ['(S) Buch', '(R) Colvin'], []], - regularCaseCount: 2, - smallCaseCount: 2, - stateAbbreviation: 'SD', - trialCityState: 'Aberdeen, South Dakota', - }); - expect( - applicationContext.getDocumentGenerators().trialSessionPlanningReport.mock - .calls[0][0].data.locationData[1], - ).toMatchObject({ - allCaseCount: 4, - previousTermsData: [['(HS) Ashford'], [], []], - regularCaseCount: 2, - smallCaseCount: 2, - stateAbbreviation: 'NY', - trialCityState: 'Albany, New York', - }); - expect(result).toBe(mockPdfUrl); - expect( - applicationContext.getUseCaseHelpers().saveFileAndGenerateUrl, - ).toHaveBeenCalled(); - }); + const getTrialSessionPlanningReportDataInteractorCalls = + applicationContext.getUseCases() + .getTrialSessionPlanningReportDataInteractor.mock.calls; - it('sorts trial locations by city', async () => { - await runTrialSessionPlanningReportInteractor( - applicationContext, - { - term: 'winter', - year: '2020', - }, - mockPetitionsClerkUser, - ); + expect(getTrialSessionPlanningReportDataInteractorCalls.length).toEqual(1); + expect(getTrialSessionPlanningReportDataInteractorCalls[0][1]).toEqual({ + term: 'winter', + year: 2020, + }); - const actualtrialLocationData = + const trialSessionPlanningReportCalls = applicationContext.getDocumentGenerators().trialSessionPlanningReport.mock - .calls[0][0].data.locationData; - expect(actualtrialLocationData[0].trialCityState).toEqual( - 'Aberdeen, South Dakota', - ); - expect(actualtrialLocationData[1].trialCityState).toEqual( - 'Albany, New York', - ); - expect( - actualtrialLocationData[actualtrialLocationData.length - 1] - .trialCityState, - ).toEqual('Winston-Salem, North Carolina'); - }); - - describe('previous terms', () => { - it('returns previous terms when the term is winter', async () => { - await runTrialSessionPlanningReportInteractor( - applicationContext, - { - term: 'winter', - year: '2020', - }, - mockPetitionsClerkUser, - ); - - expect( - applicationContext.getDocumentGenerators().trialSessionPlanningReport - .mock.calls[0][0].data.previousTerms, - ).toEqual([ - { term: 'fall', termDisplay: 'Fall 2019', year: '2019' }, - { term: 'spring', termDisplay: 'Spring 2019', year: '2019' }, - { term: 'winter', termDisplay: 'Winter 2019', year: '2019' }, - ]); + .calls; + expect(trialSessionPlanningReportCalls.length).toEqual(1); + expect(trialSessionPlanningReportCalls[0][0].data).toEqual({ + locationData: TRIAL_LOCATION_DATA_MOCK, + previousTerms: PREVIOUS_TERMS_MOCK, + term: 'Winter 2020', }); - it('returns previous terms when the term is fall', async () => { - await runTrialSessionPlanningReportInteractor( - applicationContext, - { - term: 'fall', - year: '2020', - }, - mockPetitionsClerkUser, - ); - - expect( - applicationContext.getDocumentGenerators().trialSessionPlanningReport - .mock.calls[0][0].data.previousTerms, - ).toEqual([ - { term: 'spring', termDisplay: 'Spring 2020', year: '2020' }, - { term: 'winter', termDisplay: 'Winter 2020', year: '2020' }, - { term: 'fall', termDisplay: 'Fall 2019', year: '2019' }, - ]); + const saveFileAndGenerateUrlCalls = + applicationContext.getUseCaseHelpers().saveFileAndGenerateUrl.mock.calls; + expect(saveFileAndGenerateUrlCalls.length).toEqual(1); + expect(saveFileAndGenerateUrlCalls[0][0]).toMatchObject({ + file: 'MOCKED_FILE', + useTempBucket: true, }); - it('returns previous terms when the term is spring', async () => { - await runTrialSessionPlanningReportInteractor( - applicationContext, - { - term: 'spring', - year: '2020', - }, - mockPetitionsClerkUser, - ); - - expect( - applicationContext.getDocumentGenerators().trialSessionPlanningReport - .mock.calls[0][0].data.previousTerms, - ).toEqual([ - { term: 'winter', termDisplay: 'Winter 2020', year: '2020' }, - { term: 'fall', termDisplay: 'Fall 2019', year: '2019' }, - { term: 'spring', termDisplay: 'Spring 2019', year: '2019' }, - ]); - }); + expect(results).toEqual(mockPdfUrl); }); });