Skip to content

Commit

Permalink
Fixes court receival date for indictment cases
Browse files Browse the repository at this point in the history
  • Loading branch information
gudjong committed Nov 15, 2024
1 parent 9c94bef commit 54adb32
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,10 @@ import {
} from '@island.is/judicial-system/formatters'
import type { User } from '@island.is/judicial-system/types'
import {
CaseAppealRulingDecision,
CaseDecision,
CaseState,
CaseTransition,
CaseType,
indictmentCases,
investigationCases,
isRestrictionCase,
restrictionCases,
UserRole,
} from '@island.is/judicial-system/types'
Expand Down
12 changes: 12 additions & 0 deletions apps/judicial-system/backend/src/app/modules/case/case.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2035,11 +2035,23 @@ export class CaseService {
}

async createCourtCase(theCase: Case, user: TUser): Promise<Case> {
let receivalDate: Date

if (isIndictmentCase(theCase.type)) {
receivalDate =
theCase.eventLogs?.find(
(eventLog) => eventLog.eventType === EventType.INDICTMENT_CONFIRMED,
)?.created ?? nowFactory()
} else {
receivalDate = nowFactory()
}

const courtCaseNumber = await this.courtService.createCourtCase(
user,
theCase.id,
theCase.courtId,
theCase.type,
receivalDate,
theCase.policeCaseNumbers,
Boolean(theCase.parentCaseId),
theCase.indictmentSubtypes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,19 +580,19 @@ export class InternalCaseService {
: []

const mappedSubtypes = subtypeList.flatMap((key) => courtSubtypes[key])
const indictmentIssuedByProsecutorAndReceivedByCourt =
theCase.eventLogs?.find(
(eventLog) => eventLog.eventType === EventType.INDICTMENT_CONFIRMED,
)?.created

return this.courtService
.updateIndictmentCaseWithIndictmentInfo(
user,
theCase.id,
theCase.court?.name,
theCase.courtCaseNumber,
theCase.eventLogs?.find(
(eventLog) => eventLog.eventType === EventType.CASE_RECEIVED_BY_COURT,
)?.created,
theCase.eventLogs?.find(
(eventLog) => eventLog.eventType === EventType.INDICTMENT_CONFIRMED,
)?.created,
indictmentIssuedByProsecutorAndReceivedByCourt,
indictmentIssuedByProsecutorAndReceivedByCourt,
theCase.policeCaseNumbers[0],
mappedSubtypes,
theCase.defendants?.map((defendant) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ import {
CaseFileState,
CaseState,
CaseType,
EventType,
IndictmentSubtype,
investigationCases,
isIndictmentCase,
restrictionCases,
User as TUser,
} from '@island.is/judicial-system/types'

import { createTestingCaseModule } from '../createTestingCaseModule'

import { randomEnum } from '../../../../test'
import { nowFactory } from '../../../../factories'
import { randomDate, randomEnum } from '../../../../test'
import { CourtService } from '../../../court'
import { include } from '../../case.service'
import { Case } from '../../models/case.model'

jest.mock('../../../../factories')

interface Then {
result: Case
error: Error
Expand Down Expand Up @@ -84,16 +87,71 @@ describe('CaseController - Create court case', () => {
}
})

describe('court case created', () => {
describe('request court case created', () => {
const date = randomDate()
const caseId = uuid()
const type = randomEnum(CaseType)
const type = CaseType.CUSTODY
const policeCaseNumber = uuid()
const indictmentSubtype = isIndictmentCase(type)
? randomEnum(IndictmentSubtype)
: undefined
const indictmentSubtypes = isIndictmentCase(type)
? { [policeCaseNumber]: [indictmentSubtype] }
: undefined
const policeCaseNumbers = [policeCaseNumber]
const courtId = uuid()
const theCase = {
id: caseId,
type,
policeCaseNumbers,
courtId,
} as Case
const returnedCase = {
id: caseId,
type,
policeCaseNumbers,
courtId,
courtCaseNumber,
} as Case
let then: Then

beforeEach(async () => {
const mockFindOne = mockCaseModel.findOne as jest.Mock
mockFindOne.mockResolvedValueOnce(returnedCase)

const mockToday = nowFactory as jest.Mock
mockToday.mockReturnValueOnce(date)

then = await givenWhenThen(caseId, user, theCase)
})

it('should create a court case', () => {
expect(mockCourtService.createCourtCase).toHaveBeenCalledWith(
user,
caseId,
courtId,
type,
date,
policeCaseNumbers,
false,
undefined,
)
expect(mockCaseModel.update).toHaveBeenCalledWith(
{ courtCaseNumber },
{ where: { id: caseId }, transaction },
)
expect(mockCaseModel.findOne).toHaveBeenCalledWith({
include,
where: {
id: caseId,
isArchived: false,
},
})
expect(then.result).toBe(returnedCase)
})
})

describe('indictment court case created', () => {
const caseId = uuid()
const type = CaseType.INDICTMENT
const policeCaseNumber = uuid()
const indictmentSubtype = randomEnum(IndictmentSubtype)
const indictmentSubtypes = { [policeCaseNumber]: [indictmentSubtype] }
const indictmentConfirmedDate = randomDate()
const policeCaseNumbers = [policeCaseNumber]
const courtId = uuid()
const theCase = {
Expand All @@ -102,9 +160,16 @@ describe('CaseController - Create court case', () => {
policeCaseNumbers,
indictmentSubtypes,
courtId,
eventLogs: [
{
eventType: EventType.INDICTMENT_CONFIRMED,
created: indictmentConfirmedDate,
},
],
} as Case
const returnedCase = {
id: caseId,
type,
policeCaseNumbers,
indictmentSubtypes,
courtId,
Expand All @@ -125,6 +190,7 @@ describe('CaseController - Create court case', () => {
caseId,
courtId,
type,
indictmentConfirmedDate,
policeCaseNumbers,
false,
indictmentSubtypes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('InternalCaseController - Deliver indictment info to court', () => {
caseId,
courtName,
courtCaseNumber,
receivedDate,
indictmentDate,
indictmentDate,
policeCaseNumber,
['Umferðarlagabrot', 'Hylming', 'Þjófnaður'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
isIndictmentCase,
} from '@island.is/judicial-system/types'

import { nowFactory } from '../../factories'
import { Defendant } from '../defendant'
import { EventService } from '../event'
import { RobotLog } from './models/robotLog.model'
Expand Down Expand Up @@ -324,6 +323,7 @@ export class CourtService {
caseId: string,
courtId = '',
type: CaseType,
receivalDate: Date,
policeCaseNumbers: string[],
isExtension: boolean,
indictmentSubtypes?: IndictmentSubtypeMap,
Expand All @@ -342,7 +342,7 @@ export class CourtService {
caseType: isIndictment ? 'S - Ákærumál' : 'R - Rannsóknarmál',
subtype: courtSubtype as string,
status: 'Skráð',
receivalDate: formatISO(nowFactory(), { representation: 'date' }),
receivalDate: formatISO(receivalDate, { representation: 'date' }),
basedOn: isIndictment ? 'Sakamál' : 'Rannsóknarhagsmunir',
// TODO: pass in all policeCaseNumbers when CourtService supports it
sourceNumber: policeCaseNumbers[0] ? policeCaseNumbers[0] : '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ import {

import { createTestingCourtModule } from './createTestingCourtModule'

import { nowFactory } from '../../../factories'
import { randomBoolean, randomDate, randomEnum } from '../../../test'
import { courtSubtypes, Subtype } from '../court.service'

jest.mock('../../../factories')

interface Then {
result: string
error: Error
Expand All @@ -30,13 +27,14 @@ type GivenWhenThen = (
caseId: string,
courtId: string,
type: CaseType,
receivalDate: Date,
policeCaseNumbers: string[],
isExtension: boolean,
indictmentSubtypes?: IndictmentSubtypeMap,
) => Promise<Then>

describe('CourtService - Create court case', () => {
const date = randomDate()
const receivalDate = randomDate()
let mockCourtClientService: CourtClientService
let givenWhenThen: GivenWhenThen

Expand All @@ -46,14 +44,12 @@ describe('CourtService - Create court case', () => {

mockCourtClientService = courtClientService

const mockToday = nowFactory as jest.Mock
mockToday.mockReturnValueOnce(date)

givenWhenThen = async (
user: User,
caseId: string,
courtId: string,
type: CaseType,
receivalDate: Date,
policeCaseNumbers: string[],
isExtension: boolean,
indictmentSubtypes?: IndictmentSubtypeMap,
Expand All @@ -66,6 +62,7 @@ describe('CourtService - Create court case', () => {
caseId,
courtId,
type,
receivalDate,
policeCaseNumbers,
isExtension,
indictmentSubtypes,
Expand Down Expand Up @@ -93,6 +90,7 @@ describe('CourtService - Create court case', () => {
caseId,
courtId,
type,
receivalDate,
policeCaseNumbers,
isExtension,
)
Expand All @@ -105,7 +103,7 @@ describe('CourtService - Create court case', () => {
caseType: 'R - Rannsóknarmál',
subtype: courtSubtypes[type as Subtype],
status: 'Skráð',
receivalDate: formatISO(date, { representation: 'date' }),
receivalDate: formatISO(receivalDate, { representation: 'date' }),
basedOn: 'Rannsóknarhagsmunir',
sourceNumber: policeCaseNumbers[0],
},
Expand All @@ -132,6 +130,7 @@ describe('CourtService - Create court case', () => {
caseId,
courtId,
type,
receivalDate,
policeCaseNumbers,
isExtension,
indictmentSubtypes,
Expand All @@ -145,7 +144,7 @@ describe('CourtService - Create court case', () => {
caseType: 'S - Ákærumál',
subtype: courtSubtypes[indictmentSubtype],
status: 'Skráð',
receivalDate: formatISO(date, { representation: 'date' }),
receivalDate: formatISO(receivalDate, { representation: 'date' }),
basedOn: 'Sakamál',
sourceNumber: policeCaseNumbers[0],
},
Expand All @@ -171,6 +170,7 @@ describe('CourtService - Create court case', () => {
caseId,
courtId,
type,
receivalDate,
policeCaseNumbers,
isExtension,
)
Expand All @@ -181,7 +181,7 @@ describe('CourtService - Create court case', () => {
caseType: 'R - Rannsóknarmál',
subtype: courtSubtypes[type as Subtype][0],
status: 'Skráð',
receivalDate: formatISO(date, { representation: 'date' }),
receivalDate: formatISO(receivalDate, { representation: 'date' }),
basedOn: 'Rannsóknarhagsmunir',
sourceNumber: policeCaseNumbers[0],
})
Expand All @@ -205,6 +205,7 @@ describe('CourtService - Create court case', () => {
caseId,
courtId,
type,
receivalDate,
policeCaseNumbers,
isExtension,
)
Expand All @@ -215,7 +216,7 @@ describe('CourtService - Create court case', () => {
caseType: 'R - Rannsóknarmál',
subtype: courtSubtypes[type as Subtype][1],
status: 'Skráð',
receivalDate: formatISO(date, { representation: 'date' }),
receivalDate: formatISO(receivalDate, { representation: 'date' }),
basedOn: 'Rannsóknarhagsmunir',
sourceNumber: policeCaseNumbers[0],
})
Expand Down Expand Up @@ -248,6 +249,7 @@ describe('CourtService - Create court case', () => {
caseId,
courtId,
type,
receivalDate,
policeCaseNumbers,
isExtension,
indictmentSubtypes,
Expand Down Expand Up @@ -284,6 +286,7 @@ describe('CourtService - Create court case', () => {
caseId,
courtId,
type,
receivalDate,
policeCaseNumbers,
isExtension,
indictmentSubtypes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import {
districtCourtAssistantRule,
districtCourtJudgeRule,
districtCourtRegistrarRule,
prisonSystemStaffRule,
prosecutorRepresentativeRule,
prosecutorRule,
publicProsecutorStaffRule,
Expand Down
Loading

0 comments on commit 54adb32

Please sign in to comment.