Skip to content

Commit

Permalink
10434: reuse report data for printable report; get term and year from…
Browse files Browse the repository at this point in the history
… state;
  • Loading branch information
John Cruz committed Dec 6, 2024
1 parent 345c727 commit a2ad59a
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 196 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/src/authorization/authorizationClientService';
import { RawTrialSession } from '@shared/business/entities/trialSessions/TrialSession';
import {
SESSION_TYPES,
TRIAL_CITIES,
US_STATES,
} from '../../../../../shared/src/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 @@ -15,6 +15,8 @@ type TrialSessionPlanningReportViewHelperResults = {
trialSessionPlanningReportHeader: string;
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 @@ -1198,8 +1198,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;
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ import { BigHeader } from '@web-client/views/BigHeader';
import { Button } from '@web-client/ustc-ui/Button/Button';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { TrialLocationDataFormatted } from '@web-client/presenter/computeds/trialSessionPlanningReportViewHelper';
import { state as cerebralState } from '@web-client/presenter/app.cerebral';
import {
state as cerebralState,
sequences,
} from '@web-client/presenter/app.cerebral';
import { connect } from '@web-client/presenter/shared.cerebral';
import React from 'react';

export const TrialSessionPlanningReportView = connect(
{
trialSessionPlanningReportData:
cerebralState.trialSessionPlanningReportData,
runTrialSessionPlanningReportSequence:
sequences.runTrialSessionPlanningReportSequence,
trialSessionPlanningReportViewHelper:
cerebralState.trialSessionPlanningReportViewHelper,
},
function TrialSessionPlanningReportView({
runTrialSessionPlanningReportSequence,
trialSessionPlanningReportViewHelper,
}) {
const {
Expand All @@ -35,7 +39,10 @@ export const TrialSessionPlanningReportView = connect(
</Button>
</div>
<TrialSessionPlanningReportHeader
printSequence={runTrialSessionPlanningReportSequence}
term={trialSessionPlanningReportViewHelper.trialTerm}
trialSessionPlanningReportHeader={trialSessionPlanningReportHeader}
year={trialSessionPlanningReportViewHelper.trialYear}
/>

<CitiesNotCalendaredInPastTwoTerms
Expand All @@ -59,10 +66,16 @@ TrialSessionPlanningReportView.displayName = 'TrialSessionPlanningReport';

type TrialSessionPlanningReportHeaderParams = {
trialSessionPlanningReportHeader: string;
term: string;
year: number;
printSequence: typeof sequences.runTrialSessionPlanningReportSequence;
};

function TrialSessionPlanningReportHeader({
printSequence,
term,
trialSessionPlanningReportHeader,
year,
}: TrialSessionPlanningReportHeaderParams) {
return (
<div className="grid-container display-flex height-6">
Expand All @@ -79,8 +92,13 @@ function TrialSessionPlanningReportHeader({
<Button
link
className="margin-bottom-3"
href="/trial-sessions"
icon="print"
onClick={() => {
printSequence({
term,
year,
});
}}
>
Print
</Button>
Expand Down

0 comments on commit a2ad59a

Please sign in to comment.