Skip to content

Commit

Permalink
chore(j-s): Indictment Case Arrignment Date (#16156)
Browse files Browse the repository at this point in the history
* Locks subpoena fields when arraignment date has been set

* Move arraignment date message handling to the server side

* Updates tests and fixes date comparison

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and thoreyjona committed Oct 2, 2024
1 parent f3062a8 commit b780bac
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 41 deletions.
46 changes: 39 additions & 7 deletions apps/judicial-system/backend/src/app/modules/case/case.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1146,15 +1146,29 @@ export class CaseService {
private addMessagesForNewCourtDateToQueue(
theCase: Case,
user: TUser,
arraignmentDateChanged: boolean,
): Promise<void> {
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(
Expand Down Expand Up @@ -1313,11 +1327,29 @@ 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.getTime() !==
arraignmentDate?.date.getTime()
const courtDate = DateLog.courtDate(theCase.dateLogs)
const updatedCourtDate = DateLog.courtDate(updatedCase.dateLogs)
const courtDateChanged =
updatedCourtDate &&
updatedCourtDate.date.getTime() !== courtDate?.date.getTime()

if (arraignmentDateChanged || courtDateChanged) {
// New arraignment date or new court date
await this.addMessagesForNewCourtDateToQueue(
updatedCase,
user,
Boolean(arraignmentDateChanged),
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }],
}

Expand All @@ -892,7 +937,6 @@ describe('CaseController - Update', () => {
{ dateType: DateType.COURT_DATE, caseId, ...courtDate },
{ transaction },
)

expect(mockMessageService.sendMessagesToQueue).toHaveBeenCalledWith([
{
type: MessageType.NOTIFICATION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -92,11 +73,8 @@ const Subpoena: FC = () => {
isArraignmentScheduled,
sendCourtDateToServer,
workingCase.defendants,
workingCase.notifications,
workingCase.id,
courtDateHasChanged,
updateDefendant,
sendNotification,
],
)

Expand Down

0 comments on commit b780bac

Please sign in to comment.