Skip to content

Commit

Permalink
Merge branch '10434-story' into 10434-test
Browse files Browse the repository at this point in the history
  • Loading branch information
John Cruz committed Dec 12, 2024
2 parents adda41d + d12ddc4 commit 21bd438
Show file tree
Hide file tree
Showing 3 changed files with 305 additions and 202 deletions.
Original file line number Diff line number Diff line change
@@ -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',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
};

Expand Down Expand Up @@ -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),
);

Expand All @@ -112,9 +113,9 @@ const getTrialSessionPlanningReportData = async ({
trialCities.map(trialLocation =>
getTrialLocation(applicationContext, {
allTrialSessions,
filteredTrialSessions,
previousTerms,
specialTrialSessionsCounts,
tempTrialSessions,
trialLocation,
}),
),
Expand Down Expand Up @@ -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<TrialLocationData> => {
Expand Down Expand Up @@ -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 &&
Expand All @@ -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
Expand Down Expand Up @@ -270,8 +269,8 @@ function getLatestDateForTrialSessionLocation({
applicationContext,
trialCityState,
}: {
applicationContext: ServerApplicationContext;
allTrialSessions: RawTrialSession[];
applicationContext: ServerApplicationContext;
trialCityState: string;
}): string | undefined {
const allTrialSessionsForLocation = allTrialSessions.filter(
Expand Down
Loading

0 comments on commit 21bd438

Please sign in to comment.