From a2ad59a79de86e668fa0f5dfba003d630d85df92 Mon Sep 17 00:00:00 2001 From: John Cruz Date: Fri, 6 Dec 2024 10:05:35 -0700 Subject: [PATCH] 10434: reuse report data for printable report; get term and year from state; --- .../trialSessionPlanningReport.ts | 2 +- ...runTrialSessionPlanningReportInteractor.ts | 187 +----------------- .../runTrialSessionPlanningReportAction.ts | 6 +- .../validateTrialSessionPlanningAction.ts | 8 +- .../trialSessionPlanningReportViewHelper.ts | 5 + web-client/src/presenter/presenter.ts | 3 +- .../runTrialSessionPlanningReportSequence.ts | 2 +- .../TrialSessionPlanningReportView.tsx | 26 ++- 8 files changed, 43 insertions(+), 196 deletions(-) diff --git a/shared/src/business/utilities/documentGenerators/trialSessionPlanningReport.ts b/shared/src/business/utilities/documentGenerators/trialSessionPlanningReport.ts index 57192b348ca..49dda1ae977 100644 --- a/shared/src/business/utilities/documentGenerators/trialSessionPlanningReport.ts +++ b/shared/src/business/utilities/documentGenerators/trialSessionPlanningReport.ts @@ -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'; diff --git a/web-api/src/business/useCases/trialSessions/runTrialSessionPlanningReportInteractor.ts b/web-api/src/business/useCases/trialSessions/runTrialSessionPlanningReportInteractor.ts index 272fd2764dc..dc0279d5624 100644 --- a/web-api/src/business/useCases/trialSessions/runTrialSessionPlanningReportInteractor.ts +++ b/web-api/src/business/useCases/trialSessions/runTrialSessionPlanningReportInteractor.ts @@ -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, @@ -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() @@ -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 => { - 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, - }; -}; diff --git a/web-client/src/presenter/actions/TrialSession/runTrialSessionPlanningReportAction.ts b/web-client/src/presenter/actions/TrialSession/runTrialSessionPlanningReportAction.ts index f7fa6d75576..e16475e9550 100644 --- a/web-client/src/presenter/actions/TrialSession/runTrialSessionPlanningReportAction.ts +++ b/web-client/src/presenter/actions/TrialSession/runTrialSessionPlanningReportAction.ts @@ -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 @@ -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() diff --git a/web-client/src/presenter/actions/validateTrialSessionPlanningAction.ts b/web-client/src/presenter/actions/validateTrialSessionPlanningAction.ts index 408add2af2d..b2513c7e3dd 100644 --- a/web-client/src/presenter/actions/validateTrialSessionPlanningAction.ts +++ b/web-client/src/presenter/actions/validateTrialSessionPlanningAction.ts @@ -1,5 +1,3 @@ -import { state } from '@web-client/presenter/app.cerebral'; - /** * validate the trial session planning modal * @param {object} providers the providers object @@ -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) { diff --git a/web-client/src/presenter/computeds/trialSessionPlanningReportViewHelper.ts b/web-client/src/presenter/computeds/trialSessionPlanningReportViewHelper.ts index 6753e0042c5..8ee76cfb3ba 100644 --- a/web-client/src/presenter/computeds/trialSessionPlanningReportViewHelper.ts +++ b/web-client/src/presenter/computeds/trialSessionPlanningReportViewHelper.ts @@ -15,6 +15,8 @@ type TrialSessionPlanningReportViewHelperResults = { trialSessionPlanningReportHeader: string; previousTermsFormatted: { termDisplayFormatted: string }[]; trialLocationDataFormatted: TrialLocationDataFormatted[]; + trialTerm: string; + trialYear: number; }; function formatCities(allCities: string[]): string[][] { @@ -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( diff --git a/web-client/src/presenter/presenter.ts b/web-client/src/presenter/presenter.ts index 79e0a1363e7..2d3df2f3392 100644 --- a/web-client/src/presenter/presenter.ts +++ b/web-client/src/presenter/presenter.ts @@ -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: diff --git a/web-client/src/presenter/sequences/runTrialSessionPlanningReportSequence.ts b/web-client/src/presenter/sequences/runTrialSessionPlanningReportSequence.ts index b3174af481a..ba9208caebf 100644 --- a/web-client/src/presenter/sequences/runTrialSessionPlanningReportSequence.ts +++ b/web-client/src/presenter/sequences/runTrialSessionPlanningReportSequence.ts @@ -21,4 +21,4 @@ export const runTrialSessionPlanningReportSequence = [ navigateToTrialSessionPlanningReportAction, ]), }, -]; +] as unknown as (props: { term: string; year: number }) => void; diff --git a/web-client/src/views/TrialSessions/TrialSessionPlanningReportView.tsx b/web-client/src/views/TrialSessions/TrialSessionPlanningReportView.tsx index 6c1a6d1f9dc..c94cfa7e94d 100644 --- a/web-client/src/views/TrialSessions/TrialSessionPlanningReportView.tsx +++ b/web-client/src/views/TrialSessions/TrialSessionPlanningReportView.tsx @@ -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 { @@ -35,7 +39,10 @@ export const TrialSessionPlanningReportView = connect( @@ -79,8 +92,13 @@ function TrialSessionPlanningReportHeader({