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 6, 2024
2 parents 5779c62 + a2ad59a commit 45bc9f9
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DatePrintedFooter } from '@shared/business/utilities/pdfGenerator/compo
import {
PreviousTerm,
TrialLocationData,
} from '@web-api/business/useCases/trialSessions/runTrialSessionPlanningReportInteractor';
} from '@web-api/business/useCases/trialSessions/getTrialSessionPlanningReportDataInteractor';
import { ReportsMetaHeader } from '@shared/business/utilities/pdfGenerator/components/ReportsMetaHeader';
import { ServerApplicationContext } from '@web-api/applicationContext';
import { TrialSessionPlanningReport } from '@shared/business/utilities/pdfGenerator/documentTemplates/TrialSessionPlanningReport';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,10 @@ import {
ROLE_PERMISSIONS,
isAuthorized,
} from '@shared/authorization/authorizationClientService';
import { RawTrialSession } from '@shared/business/entities/trialSessions/TrialSession';
import {
SESSION_TYPES,
TRIAL_CITIES,
US_STATES,
} from '@shared/business/entities/EntityConstants';
import { ServerApplicationContext } from '@web-api/applicationContext';
import { UnauthorizedError } from '@web-api/errors/errors';
import { UnknownAuthUser } from '@shared/business/entities/authUser/AuthUser';
import { capitalize, invert } from 'lodash';

export type PreviousTerm = {
term: string;
termDisplay: string;
year: string;
};

export type TrialLocationData = {
allCaseCount: number;
previousTermsData: string[][];
regularCaseCount: number;
smallCaseCount: number;
stateAbbreviation: string;
trialCityState: string;
};
import { capitalize } from 'lodash';

export const runTrialSessionPlanningReportInteractor = async (
applicationContext: ServerApplicationContext,
Expand All @@ -40,11 +19,13 @@ export const runTrialSessionPlanningReportInteractor = async (
throw new UnauthorizedError('Unauthorized');
}

const reportData = await getTrialSessionPlanningReportData({
applicationContext,
term,
year,
});
const reportData = await applicationContext
.getUseCases()
.getTrialSessionPlanningReportDataInteractor(
applicationContext,
{ term, year: Number(year) },
authorizedUser,
);

const trialSessionPlanningReport = await applicationContext
.getDocumentGenerators()
Expand All @@ -63,155 +44,3 @@ export const runTrialSessionPlanningReportInteractor = async (
useTempBucket: true,
});
};

const getTrialSessionPlanningReportData = async ({
applicationContext,
term,
year,
}: {
applicationContext: ServerApplicationContext;
term: string;
year: string;
}): Promise<{
previousTerms: PreviousTerm[];
trialLocationData: TrialLocationData[];
}> => {
const previousTerms: PreviousTerm[] = [];
let currentTerm: string = term;
let currentYear: string = year;
for (let i = 0; i < 3; i++) {
const previous = getPreviousTerm(currentTerm, currentYear);
previousTerms.push(previous);
currentTerm = previous.term;
currentYear = previous.year;
}

const trialCities = TRIAL_CITIES.ALL.sort((a, b) => {
return applicationContext.getUtilities().compareStrings(a.city, b.city);
});

const allTrialSessions = (
await applicationContext
.getPersistenceGateway()
.getTrialSessions({ applicationContext })
).filter(session =>
['Regular', 'Small', 'Hybrid', 'Hybrid-S'].includes(session.sessionType),
);

const trialLocationData: TrialLocationData[] = await Promise.all(
trialCities.map(trialLocation =>
getTrialLocation(applicationContext, {
allTrialSessions,
previousTerms,
trialLocation,
}),
),
);

return { previousTerms, trialLocationData };
};

const getPreviousTerm = (
currentTerm: string,
currentYear: string,
): PreviousTerm => {
const terms = [
`fall ${+currentYear - 1}`,
`winter ${currentYear}`,
`spring ${currentYear}`,
`fall ${currentYear}`,
];
const termsReversed = terms.reverse();
const termI = terms.findIndex(t => `${currentTerm} ${currentYear}` === t);
const [term, year] = termsReversed[termI + 1].split(' ');

const termDisplay = `${capitalize(term)} ${year}`;
return {
term,
termDisplay,
year,
};
};

const getTrialLocation = async (
applicationContext: ServerApplicationContext,
{
allTrialSessions,
previousTerms,
trialLocation,
}: {
trialLocation: { city: string; state: string };
previousTerms: PreviousTerm[];
allTrialSessions: RawTrialSession[];
},
): Promise<TrialLocationData> => {
const trialCityState = `${trialLocation.city}, ${trialLocation.state}`;
const trialCityStateStripped = trialCityState.replace(/[\s.,]/g, '');
const stateAbbreviation = invert(US_STATES)[trialLocation.state];

const eligibleCasesSmall = await applicationContext
.getPersistenceGateway()
.getEligibleCasesForTrialCity({
applicationContext,
procedureType: 'Small',
trialCity: trialCityStateStripped,
});
const eligibleCasesRegular = await applicationContext
.getPersistenceGateway()
.getEligibleCasesForTrialCity({
applicationContext,
procedureType: 'Regular',
trialCity: trialCityStateStripped,
});

const smallCaseCount = eligibleCasesSmall.length;
const regularCaseCount = eligibleCasesRegular.length;
const allCaseCount = smallCaseCount + regularCaseCount;

const previousTermsData: string[][] = [];
previousTerms.forEach(previousTerm => {
const previousTermSessions = allTrialSessions.filter(
trialSession =>
trialSession.term.toLowerCase() === previousTerm.term.toLowerCase() &&
trialSession.termYear === previousTerm.year &&
trialSession.trialLocation === trialCityState,
);

previousTermSessions.sort((a, b) => {
return applicationContext
.getUtilities()
.compareISODateStrings(a.startDate, b.startDate);
});

const previousTermSessionList: string[] = [];
previousTermSessions.forEach(previousTermSession => {
if (
previousTermSession &&
previousTermSession.sessionType &&
previousTermSession.judge
) {
const sessionTypeChar =
previousTermSession.sessionType === SESSION_TYPES.hybridSmall
? 'HS'
: previousTermSession.sessionType.charAt(0);
const strippedJudgeName = previousTermSession.judge.name.replace(
'Judge ',
'',
);
previousTermSessionList.push(
`(${sessionTypeChar}) ${strippedJudgeName}`,
);
}
});
previousTermsData.push(previousTermSessionList);
});

return {
allCaseCount,
previousTermsData,
regularCaseCount,
smallCaseCount,
stateAbbreviation,
trialCityState,
};
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { state } from '@web-client/presenter/app.cerebral';

/**
* get the pdf file and pdf blob url from the passed in htmlString
* @param {object} providers the providers object
Expand All @@ -8,9 +6,9 @@ import { state } from '@web-client/presenter/app.cerebral';
*/
export const runTrialSessionPlanningReportAction = async ({
applicationContext,
get,
props,
}: ActionProps) => {
const { term, year } = get(state.modal);
const { term, year } = props;

const { url } = await applicationContext
.getUseCases()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { state } from '@web-client/presenter/app.cerebral';

/**
* validate the trial session planning modal
* @param {object} providers the providers object
Expand All @@ -8,10 +6,10 @@ import { state } from '@web-client/presenter/app.cerebral';
* @returns {object} the next path based on if validation was successful or error
*/
export const validateTrialSessionPlanningAction = ({
get,
path,
}: ActionProps) => {
const { term, year } = get(state.modal);
props,
}: ActionProps<{ term: string; year: number }>) => {
const { term, year } = props;

let errors = null;
if (!term || !year) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ export type TrialLocationDataFormatted = TrialLocationData & {
type TrialSessionPlanningReportViewHelperResults = {
citiesNotCalendaredInTwoPreviousTerms: string[][];
trialSessionPlanningReportHeader: string;
previousTermsFormatted: { termDisplayFormatted }[];
previousTermsFormatted: { termDisplayFormatted: string }[];
trialLocationDataFormatted: TrialLocationDataFormatted[];
trialTerm: string;
trialYear: number;
};

function formatCities(allCities: string[]): string[][] {
Expand Down Expand Up @@ -95,8 +97,11 @@ export const trialSessionPlanningReportViewHelper = (
previousTermsFormatted,
trialLocationDataFormatted,
trialSessionPlanningReportHeader,
trialTerm,
trialYear,
};
};

function formatLastVisitedDate(lastVisitedDate: string | undefined): string {
if (!lastVisitedDate) return 'Never visited.';
const formattedSessionWeekStartDate = createDateAtStartOfWeekEST(
Expand Down
3 changes: 1 addition & 2 deletions web-client/src/presenter/presenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1194,8 +1194,7 @@ export const presenterSequences = {
reviewCaseAssociationRequestSequence as unknown as Function,
reviewExternalDocumentInformationSequence:
reviewExternalDocumentInformationSequence as unknown as Function,
runTrialSessionPlanningReportSequence:
runTrialSessionPlanningReportSequence as unknown as Function,
runTrialSessionPlanningReportSequence,
saveCourtIssuedDocketEntrySequence:
saveCourtIssuedDocketEntrySequence as unknown as Function,
saveDocketEntryForLaterCompleteSequence:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ export const runTrialSessionPlanningReportSequence = [
navigateToTrialSessionPlanningReportAction,
]),
},
];
] as unknown as (props: { term: string; year: number }) => void;
Loading

0 comments on commit 45bc9f9

Please sign in to comment.