From ff2e71f7d6dd523c1a4bac959f99cfc06e20ded9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0j=C3=B3n=20Gu=C3=B0j=C3=B3nsson?= Date: Wed, 25 Sep 2024 14:41:40 +0200 Subject: [PATCH 1/3] Locks subpoena fields when arraignment date has been set --- .../routes/Court/Indictments/Subpoena/Subpoena.tsx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/apps/judicial-system/web/src/routes/Court/Indictments/Subpoena/Subpoena.tsx b/apps/judicial-system/web/src/routes/Court/Indictments/Subpoena/Subpoena.tsx index 92455ec72667..4d13fb753f1c 100644 --- a/apps/judicial-system/web/src/routes/Court/Indictments/Subpoena/Subpoena.tsx +++ b/apps/judicial-system/web/src/routes/Court/Indictments/Subpoena/Subpoena.tsx @@ -46,11 +46,11 @@ const Subpoena: FC = () => { } = useCourtArrangements(workingCase, setWorkingCase, 'arraignmentDate') const { sendNotification } = useCase() - const isArraignmentDone = Boolean(workingCase.indictmentDecision) + const isArraignmentScheduled = Boolean(workingCase.arraignmentDate) const handleNavigationTo = useCallback( async (destination: keyof stepValidationsType) => { - if (isArraignmentDone) { + if (isArraignmentScheduled) { router.push(`${destination}/${workingCase.id}`) return } @@ -89,7 +89,7 @@ const Subpoena: FC = () => { router.push(`${destination}/${workingCase.id}`) }, [ - isArraignmentDone, + isArraignmentScheduled, sendCourtDateToServer, workingCase.defendants, workingCase.notifications, @@ -134,8 +134,8 @@ const Subpoena: FC = () => { handleCourtDateChange={handleCourtDateChange} handleCourtRoomChange={handleCourtRoomChange} courtDate={workingCase.arraignmentDate} - dateTimeDisabled={isArraignmentDone} - courtRoomDisabled={isArraignmentDone} + dateTimeDisabled={isArraignmentScheduled} + courtRoomDisabled={isArraignmentScheduled} courtRoomRequired /> @@ -169,14 +169,14 @@ const Subpoena: FC = () => { previousUrl={`${constants.INDICTMENTS_RECEPTION_AND_ASSIGNMENT_ROUTE}/${workingCase.id}`} nextIsLoading={isLoadingWorkingCase} onNextButtonClick={() => { - if (isArraignmentDone) { + if (isArraignmentScheduled) { router.push( `${constants.INDICTMENTS_DEFENDER_ROUTE}/${workingCase.id}`, ) } else setNavigateTo(constants.INDICTMENTS_DEFENDER_ROUTE) }} nextButtonText={ - isArraignmentDone + isArraignmentScheduled ? undefined : formatMessage(strings.nextButtonText) } From cf631db4b8d9115243fffc8e56b5c4d1ce9d5380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0j=C3=B3n=20Gu=C3=B0j=C3=B3nsson?= Date: Wed, 25 Sep 2024 15:49:34 +0200 Subject: [PATCH 2/3] Move arraignment date message handling to the server side --- .../src/app/modules/case/case.service.ts | 44 ++++++++++++++++--- .../notification/notification.service.ts | 9 ---- .../app/modules/subpoena/subpoena.service.ts | 1 + .../Court/Indictments/Subpoena/Subpoena.tsx | 24 +--------- 4 files changed, 39 insertions(+), 39 deletions(-) diff --git a/apps/judicial-system/backend/src/app/modules/case/case.service.ts b/apps/judicial-system/backend/src/app/modules/case/case.service.ts index 1d4043abd85d..2a21877fa13f 100644 --- a/apps/judicial-system/backend/src/app/modules/case/case.service.ts +++ b/apps/judicial-system/backend/src/app/modules/case/case.service.ts @@ -1143,15 +1143,29 @@ export class CaseService { private addMessagesForNewCourtDateToQueue( theCase: Case, user: TUser, + arraignmentDateChanged: boolean, ): Promise { - return this.messageService.sendMessagesToQueue([ + const messages: Message[] = [ { type: MessageType.NOTIFICATION, user, caseId: theCase.id, body: { type: NotificationType.COURT_DATE }, }, - ]) + ] + + if (arraignmentDateChanged) { + theCase.defendants?.forEach((defendant) => { + messages.push({ + type: MessageType.DELIVERY_TO_POLICE_SUBPOENA, + user, + caseId: theCase.id, + elementId: defendant.id, + }) + }) + } + + return this.messageService.sendMessagesToQueue(messages) } private async addMessagesForUpdatedCaseToQueue( @@ -1310,11 +1324,27 @@ export class CaseService { } // This only applies to indictments - const courtDate = DateLog.courtDate(theCase.dateLogs) - const updatedCourtDate = DateLog.courtDate(updatedCase.dateLogs) - if (updatedCourtDate && updatedCourtDate.date !== courtDate?.date) { - // New court date - await this.addMessagesForNewCourtDateToQueue(updatedCase, user) + if (isIndictment) { + const arraignmentDate = DateLog.arraignmentDate(theCase.dateLogs) + const updatedArraignmentDate = DateLog.arraignmentDate( + updatedCase.dateLogs, + ) + const arraignmentDateChanged = + updatedArraignmentDate && + updatedArraignmentDate.date !== arraignmentDate?.date + const courtDate = DateLog.courtDate(theCase.dateLogs) + const updatedCourtDate = DateLog.courtDate(updatedCase.dateLogs) + const courtDateChanged = + updatedCourtDate && updatedCourtDate.date !== courtDate?.date + + if (arraignmentDateChanged || courtDateChanged) { + // New arraignment date or new court date + await this.addMessagesForNewCourtDateToQueue( + updatedCase, + user, + Boolean(arraignmentDateChanged), + ) + } } } diff --git a/apps/judicial-system/backend/src/app/modules/notification/notification.service.ts b/apps/judicial-system/backend/src/app/modules/notification/notification.service.ts index e4ffc288777d..8103b1106f14 100644 --- a/apps/judicial-system/backend/src/app/modules/notification/notification.service.ts +++ b/apps/judicial-system/backend/src/app/modules/notification/notification.service.ts @@ -69,15 +69,6 @@ export class NotificationService { ] } else { messages = [this.getNotificationMessage(type, user, theCase)] - theCase.defendants?.forEach((defendant) => { - // TODO: move this elsewhere when we know exactly where the trigger should be - messages.push({ - type: MessageType.DELIVERY_TO_POLICE_SUBPOENA, - user, - caseId: theCase.id, - elementId: defendant.id, - }) - }) } break case NotificationType.HEADS_UP: diff --git a/apps/judicial-system/backend/src/app/modules/subpoena/subpoena.service.ts b/apps/judicial-system/backend/src/app/modules/subpoena/subpoena.service.ts index 62d0355fd57c..c28cf4ec4875 100644 --- a/apps/judicial-system/backend/src/app/modules/subpoena/subpoena.service.ts +++ b/apps/judicial-system/backend/src/app/modules/subpoena/subpoena.service.ts @@ -126,6 +126,7 @@ export class SubpoenaService { return { delivered: false } } + // TODO: Improve error handling by checking how many rows were affected and posting error event await this.subpoenaModel.update( { subpoenaId: createdSubpoena.subpoenaId }, { where: { id: subpoena.id } }, diff --git a/apps/judicial-system/web/src/routes/Court/Indictments/Subpoena/Subpoena.tsx b/apps/judicial-system/web/src/routes/Court/Indictments/Subpoena/Subpoena.tsx index 4d13fb753f1c..11c767d7008f 100644 --- a/apps/judicial-system/web/src/routes/Court/Indictments/Subpoena/Subpoena.tsx +++ b/apps/judicial-system/web/src/routes/Court/Indictments/Subpoena/Subpoena.tsx @@ -19,14 +19,9 @@ import { SectionHeading, useCourtArrangements, } from '@island.is/judicial-system-web/src/components' -import { NotificationType } from '@island.is/judicial-system-web/src/graphql/schema' import { SubpoenaType } from '@island.is/judicial-system-web/src/routes/Court/components' import type { stepValidationsType } from '@island.is/judicial-system-web/src/utils/formHelper' -import { - useCase, - useDefendants, -} from '@island.is/judicial-system-web/src/utils/hooks' -import { hasSentNotification } from '@island.is/judicial-system-web/src/utils/stepHelper' +import { useDefendants } from '@island.is/judicial-system-web/src/utils/hooks' import { isSubpoenaStepValid } from '@island.is/judicial-system-web/src/utils/validate' import { subpoena as strings } from './Subpoena.strings' @@ -39,12 +34,10 @@ const Subpoena: FC = () => { const { formatMessage } = useIntl() const { courtDate, - courtDateHasChanged, handleCourtDateChange, handleCourtRoomChange, sendCourtDateToServer, } = useCourtArrangements(workingCase, setWorkingCase, 'arraignmentDate') - const { sendNotification } = useCase() const isArraignmentScheduled = Boolean(workingCase.arraignmentDate) @@ -69,18 +62,6 @@ const Subpoena: FC = () => { }) } - if ( - !hasSentNotification( - NotificationType.COURT_DATE, - workingCase.notifications, - ).hasSent || - courtDateHasChanged - ) { - promises.push( - sendNotification(workingCase.id, NotificationType.COURT_DATE), - ) - } - const allDataSentToServer = await Promise.all(promises) if (!allDataSentToServer.every((result) => result)) { return @@ -92,11 +73,8 @@ const Subpoena: FC = () => { isArraignmentScheduled, sendCourtDateToServer, workingCase.defendants, - workingCase.notifications, workingCase.id, - courtDateHasChanged, updateDefendant, - sendNotification, ], ) From 9192246cd32f31de15dff4b58985ff6dabc2796d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0j=C3=B3n=20Gu=C3=B0j=C3=B3nsson?= Date: Wed, 25 Sep 2024 16:22:36 +0200 Subject: [PATCH 3/3] Updates tests and fixes date comparison --- .../src/app/modules/case/case.service.ts | 6 ++- .../case/test/caseController/update.spec.ts | 48 ++++++++++++++++++- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/apps/judicial-system/backend/src/app/modules/case/case.service.ts b/apps/judicial-system/backend/src/app/modules/case/case.service.ts index 2a21877fa13f..9f3973a8f152 100644 --- a/apps/judicial-system/backend/src/app/modules/case/case.service.ts +++ b/apps/judicial-system/backend/src/app/modules/case/case.service.ts @@ -1331,11 +1331,13 @@ export class CaseService { ) const arraignmentDateChanged = updatedArraignmentDate && - updatedArraignmentDate.date !== arraignmentDate?.date + updatedArraignmentDate.date.getTime() !== + arraignmentDate?.date.getTime() const courtDate = DateLog.courtDate(theCase.dateLogs) const updatedCourtDate = DateLog.courtDate(updatedCase.dateLogs) const courtDateChanged = - updatedCourtDate && updatedCourtDate.date !== courtDate?.date + updatedCourtDate && + updatedCourtDate.date.getTime() !== courtDate?.date.getTime() if (arraignmentDateChanged || courtDateChanged) { // New arraignment date or new court date diff --git a/apps/judicial-system/backend/src/app/modules/case/test/caseController/update.spec.ts b/apps/judicial-system/backend/src/app/modules/case/test/caseController/update.spec.ts index 0ffde6ad16ca..932319cac89e 100644 --- a/apps/judicial-system/backend/src/app/modules/case/test/caseController/update.spec.ts +++ b/apps/judicial-system/backend/src/app/modules/case/test/caseController/update.spec.ts @@ -872,11 +872,56 @@ describe('CaseController - Update', () => { }) }) - describe('court date updated', () => { + describe('indictment arraignment date updated', () => { + const arraignmentDate = { date: new Date(), location: uuid() } + const caseToUpdate = { arraignmentDate } + const updatedCase = { + ...theCase, + type: CaseType.INDICTMENT, + dateLogs: [{ dateType: DateType.ARRAIGNMENT_DATE, ...arraignmentDate }], + } + + beforeEach(async () => { + const mockFindOne = mockCaseModel.findOne as jest.Mock + mockFindOne.mockResolvedValueOnce(updatedCase) + + await givenWhenThen(caseId, user, theCase, caseToUpdate) + }) + + it('should update case', () => { + expect(mockDateLogModel.create).toHaveBeenCalledWith( + { dateType: DateType.ARRAIGNMENT_DATE, caseId, ...arraignmentDate }, + { transaction }, + ) + expect(mockMessageService.sendMessagesToQueue).toHaveBeenCalledWith([ + { + type: MessageType.NOTIFICATION, + user, + caseId, + body: { type: NotificationType.COURT_DATE }, + }, + { + type: MessageType.DELIVERY_TO_POLICE_SUBPOENA, + user, + caseId: theCase.id, + elementId: defendantId1, + }, + { + type: MessageType.DELIVERY_TO_POLICE_SUBPOENA, + user, + caseId: theCase.id, + elementId: defendantId2, + }, + ]) + }) + }) + + describe('indictment court date updated', () => { const courtDate = { date: new Date(), location: uuid() } const caseToUpdate = { courtDate } const updatedCase = { ...theCase, + type: CaseType.INDICTMENT, dateLogs: [{ dateType: DateType.COURT_DATE, ...courtDate }], } @@ -892,7 +937,6 @@ describe('CaseController - Update', () => { { dateType: DateType.COURT_DATE, caseId, ...courtDate }, { transaction }, ) - expect(mockMessageService.sendMessagesToQueue).toHaveBeenCalledWith([ { type: MessageType.NOTIFICATION,