Skip to content

Commit

Permalink
Update unit tests and fixxes some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gudjong committed Nov 12, 2024
1 parent 25fce15 commit 84c232d
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,6 @@ describe('CaseController - Create court case', () => {

it('should post to queue', () => {
expect(mockMessageService.sendMessagesToQueue).toHaveBeenCalledWith([
{
type: MessageType.DELIVERY_TO_COURT_PROSECUTOR,
user,
caseId: theCase.id,
},
{
type: MessageType.DELIVERY_TO_COURT_CASE_FILES_RECORD,
user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,23 +458,6 @@ describe('CaseController - Update', () => {

it('should post to queue', () => {
expect(mockMessageService.sendMessagesToQueue).toHaveBeenCalledWith([
{
type: MessageType.DELIVERY_TO_COURT_PROSECUTOR,
user,
caseId,
},
{
type: MessageType.DELIVERY_TO_COURT_DEFENDANT,
user,
caseId,
elementId: defendantId1,
},
{
type: MessageType.DELIVERY_TO_COURT_DEFENDANT,
user,
caseId,
elementId: defendantId2,
},
{
type: MessageType.DELIVERY_TO_COURT_CASE_FILES_RECORD,
user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,43 +95,49 @@ export class DefendantService {
updatedDefendant: Defendant,
oldDefendant: Defendant,
user: User,
) {
): Promise<void> {
if (!theCase.courtCaseNumber) {
return
}

const messages: Message[] = []

// Handling of updates sent to the court system
// A defendant is updated after the case has been received by the court.
if (updatedDefendant.noNationalId !== oldDefendant.noNationalId) {
// A defendant nationalId is added or removed. Attempt to add the defendant to the court case.
// In case there is no national id, the court will be notified.
await this.messageService.sendMessagesToQueue([
messages.push(
this.getMessageForDeliverDefendantToCourt(updatedDefendant, user),
])
)
} else if (updatedDefendant.nationalId !== oldDefendant.nationalId) {
// A defendant is replaced. Attempt to add the defendant to the court case,
// but also ask the court to verify defendants.
await this.messageService.sendMessagesToQueue([
messages.push(
this.getMessageForSendDefendantsNotUpdatedAtCourtNotification(
theCase,
user,
),
this.getMessageForDeliverDefendantToCourt(updatedDefendant, user),
])
)
}

if (messages.length === 0) {
return
}

return this.messageService.sendMessagesToQueue(messages)
}

private async sendIndictmentCaseUpdateDefendantMessages(
theCase: Case,
updatedDefendant: Defendant,
oldDefendant: Defendant,
) {
): Promise<void> {
if (!theCase.courtCaseNumber) {
return
}

const messages: Message[] = []

if (updatedDefendant.isDefenderChoiceConfirmed) {
if (
updatedDefendant.defenderChoice === DefenderChoice.CHOOSE ||
Expand All @@ -141,30 +147,16 @@ export class DefendantService {

// Defender was just confirmed by judge
if (!oldDefendant.isDefenderChoiceConfirmed) {
messages.push({
type: MessageType.DEFENDANT_NOTIFICATION,
caseId: theCase.id,
body: { type: DefendantNotificationType.DEFENDER_ASSIGNED },
elementId: updatedDefendant.id,
})
await this.messageService.sendMessagesToQueue([
{
type: MessageType.DEFENDANT_NOTIFICATION,
caseId: theCase.id,
body: { type: DefendantNotificationType.DEFENDER_ASSIGNED },
elementId: updatedDefendant.id,
},
])
}
}
} else {
if (
updatedDefendant.defenderChoice === DefenderChoice.CHOOSE &&
(updatedDefendant.defenderChoice !== oldDefendant.defenderChoice ||
updatedDefendant.defenderNationalId !==
oldDefendant.defenderNationalId)
) {
messages.push({
type: MessageType.DEFENDANT_NOTIFICATION,
caseId: theCase.id,
elementId: updatedDefendant.id,
body: {
type: DefendantNotificationType.DEFENDANT_SELECTED_DEFENDER,
},
})
}
}
}

Expand Down Expand Up @@ -246,13 +238,11 @@ export class DefendantService {
theCase: Case,
defendant: Defendant,
update: UpdateDefendantDto,
transaction?: Transaction,
): Promise<Defendant> {
const updatedDefendant = await this.updateDatabaseDefendant(
theCase.id,
defendant.id,
update,
transaction,
)

await this.sendIndictmentCaseUpdateDefendantMessages(
Expand Down Expand Up @@ -290,12 +280,33 @@ export class DefendantService {
// and go through the update method above using the defendantId.
// This is also why we may set the isDefenderChoiceConfirmed to false here - the judge needs to confirm all changes.

return this.updateIndictmentCase(
theCase,
defendant,
const updatedDefendant = await this.updateDatabaseDefendant(
theCase.id,
defendant.id,
{ ...update, isDefenderChoiceConfirmed },
transaction,
)

// Notify the court if the defendant has changed the defender choice
if (
updatedDefendant.isDefenderChoiceConfirmed &&
updatedDefendant.defenderChoice === DefenderChoice.CHOOSE &&
(updatedDefendant.defenderChoice !== defendant.defenderChoice ||
updatedDefendant.defenderNationalId !== defendant.defenderNationalId)
) {
await this.messageService.sendMessagesToQueue([
{
type: MessageType.DEFENDANT_NOTIFICATION,
caseId: theCase.id,
elementId: updatedDefendant.id,
body: {
type: DefendantNotificationType.DEFENDANT_SELECTED_DEFENDER,
},
},
])
}

return updatedDefendant
}

async delete(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface Then {

type GivenWhenThen = (
defendantUpdate: UpdateDefendantDto,
type: CaseType,
courtCaseNumber?: string,
) => Promise<Then>

Expand Down Expand Up @@ -52,6 +53,7 @@ describe('DefendantController - Update', () => {

givenWhenThen = async (
defendantUpdate: UpdateDefendantDto,
type: CaseType,
courtCaseNumber?: string,
) => {
const then = {} as Then
Expand All @@ -61,7 +63,7 @@ describe('DefendantController - Update', () => {
caseId,
defendantId,
user,
{ id: caseId, courtCaseNumber, type: CaseType.INDICTMENT } as Case,
{ id: caseId, courtCaseNumber, type } as Case,
defendant,
defendantUpdate,
)
Expand All @@ -81,7 +83,7 @@ describe('DefendantController - Update', () => {
const mockUpdate = mockDefendantModel.update as jest.Mock
mockUpdate.mockResolvedValueOnce([1, [updatedDefendant]])

then = await givenWhenThen(defendantUpdate)
then = await givenWhenThen(defendantUpdate, CaseType.CUSTODY)
})

it('should update the defendant without queuing', () => {
Expand All @@ -102,7 +104,7 @@ describe('DefendantController - Update', () => {
const mockUpdate = mockDefendantModel.update as jest.Mock
mockUpdate.mockResolvedValueOnce([1, [updatedDefendant]])

await givenWhenThen(defendantUpdate, uuid())
await givenWhenThen(defendantUpdate, CaseType.INDICTMENT, uuid())
})

it('should not queue', () => {
Expand All @@ -118,7 +120,7 @@ describe('DefendantController - Update', () => {
const mockUpdate = mockDefendantModel.update as jest.Mock
mockUpdate.mockResolvedValueOnce([1, [updatedDefendant]])

await givenWhenThen(defendantUpdate, uuid())
await givenWhenThen(defendantUpdate, CaseType.CUSTODY, uuid())
})

it('should queue messages', () => {
Expand All @@ -141,7 +143,7 @@ describe('DefendantController - Update', () => {
const mockUpdate = mockDefendantModel.update as jest.Mock
mockUpdate.mockResolvedValueOnce([1, [updatedDefendant]])

await givenWhenThen(defendantUpdate, uuid())
await givenWhenThen(defendantUpdate, CaseType.TELECOMMUNICATIONS, uuid())
})

it('should queue messages', () => {
Expand All @@ -162,29 +164,6 @@ describe('DefendantController - Update', () => {
})
})

describe(`defendant's defender email changed after case is delivered to court`, () => {
const defendantUpdate = { defenderEmail: uuid() }
const updatedDefendant = { ...defendant, ...defendantUpdate }

beforeEach(async () => {
const mockUpdate = mockDefendantModel.update as jest.Mock
mockUpdate.mockResolvedValueOnce([1, [updatedDefendant]])

await givenWhenThen(defendantUpdate, uuid())
})

it('should queue messages', () => {
expect(mockMessageService.sendMessagesToQueue).toHaveBeenCalledWith([
{
type: MessageType.DELIVERY_TO_COURT_DEFENDANT,
user,
caseId,
elementId: defendantId,
},
])
})
})

describe.each([
{ isDefenderChoiceConfirmed: true, shouldSendEmail: true },
{ isDefenderChoiceConfirmed: false, shouldSendEmail: false },
Expand All @@ -202,7 +181,7 @@ describe('DefendantController - Update', () => {
const mockUpdate = mockDefendantModel.update as jest.Mock
mockUpdate.mockResolvedValueOnce([1, [updatedDefendant]])

await givenWhenThen(defendantUpdate, uuid())
await givenWhenThen(defendantUpdate, CaseType.INDICTMENT, uuid())
})

if (shouldSendEmail) {
Expand All @@ -228,7 +207,7 @@ describe('DefendantController - Update', () => {
let then: Then

beforeEach(async () => {
then = await givenWhenThen({})
then = await givenWhenThen({}, CaseType.CUSTODY)
})

it('should throw Error', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ describe('InternalDefendantController - Update defendant guards', () => {

it('should have the right guard configuration', () => {
expect(guards).toHaveLength(2)
expect(new guards[0]()).toBeInstanceOf(DefendantNationalIdExistsGuard)
expect(guards[1]).toBeInstanceOf(CaseTypeGuard)
expect(guards[1]).toEqual({
expect(guards[0]).toBeInstanceOf(CaseTypeGuard)
expect(guards[0]).toEqual({
allowedCaseTypes: investigationCases,
})
expect(new guards[1]()).toBeInstanceOf(DefendantNationalIdExistsGuard)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { MessageService } from '@island.is/judicial-system/message'

import { CaseService, PdfService } from '../../case'
import { Defendant } from '../../defendant'
import { Defendant, DefendantService } from '../../defendant'
import { EventService } from '../../event'
import { FileService } from '../../file'
import { PoliceService } from '../../police'
Expand All @@ -30,6 +30,7 @@ jest.mock('../../case/pdf.service')
jest.mock('../../police/police.service')
jest.mock('../../file/file.service')
jest.mock('../../event/event.service')
jest.mock('../../defendant/defendant.service')
jest.mock('@island.is/judicial-system/message')

export const createTestingSubpoenaModule = async () => {
Expand All @@ -49,6 +50,7 @@ export const createTestingSubpoenaModule = async () => {
PoliceService,
FileService,
EventService,
DefendantService,
{
provide: LOGGER_PROVIDER,
useValue: {
Expand Down

0 comments on commit 84c232d

Please sign in to comment.