diff --git a/apps/judicial-system/backend/src/app/modules/case/case.controller.ts b/apps/judicial-system/backend/src/app/modules/case/case.controller.ts index cbab6e505062..63b82483e3a9 100644 --- a/apps/judicial-system/backend/src/app/modules/case/case.controller.ts +++ b/apps/judicial-system/backend/src/app/modules/case/case.controller.ts @@ -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' 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 6f77a6752c3e..c3b89d061cc5 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 @@ -2035,11 +2035,23 @@ export class CaseService { } async createCourtCase(theCase: Case, user: TUser): Promise { + 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, diff --git a/apps/judicial-system/backend/src/app/modules/case/internalCase.service.ts b/apps/judicial-system/backend/src/app/modules/case/internalCase.service.ts index 211931752bfc..79c804678739 100644 --- a/apps/judicial-system/backend/src/app/modules/case/internalCase.service.ts +++ b/apps/judicial-system/backend/src/app/modules/case/internalCase.service.ts @@ -580,6 +580,10 @@ 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( @@ -587,12 +591,8 @@ export class InternalCaseService { 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) => ({ diff --git a/apps/judicial-system/backend/src/app/modules/case/test/caseController/createCourtCase.spec.ts b/apps/judicial-system/backend/src/app/modules/case/test/caseController/createCourtCase.spec.ts index 318a3420af0d..d1818bf7936b 100644 --- a/apps/judicial-system/backend/src/app/modules/case/test/caseController/createCourtCase.spec.ts +++ b/apps/judicial-system/backend/src/app/modules/case/test/caseController/createCourtCase.spec.ts @@ -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 @@ -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 = { @@ -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, @@ -125,6 +190,7 @@ describe('CaseController - Create court case', () => { caseId, courtId, type, + indictmentConfirmedDate, policeCaseNumbers, false, indictmentSubtypes, diff --git a/apps/judicial-system/backend/src/app/modules/case/test/internalCaseController/deliverIndictmentInfoToCourt.spec.ts b/apps/judicial-system/backend/src/app/modules/case/test/internalCaseController/deliverIndictmentInfoToCourt.spec.ts index 4c929c759e7a..3236f05eb4de 100644 --- a/apps/judicial-system/backend/src/app/modules/case/test/internalCaseController/deliverIndictmentInfoToCourt.spec.ts +++ b/apps/judicial-system/backend/src/app/modules/case/test/internalCaseController/deliverIndictmentInfoToCourt.spec.ts @@ -96,7 +96,7 @@ describe('InternalCaseController - Deliver indictment info to court', () => { caseId, courtName, courtCaseNumber, - receivedDate, + indictmentDate, indictmentDate, policeCaseNumber, ['Umferðarlagabrot', 'Hylming', 'Þjófnaður'], diff --git a/apps/judicial-system/backend/src/app/modules/court/court.service.ts b/apps/judicial-system/backend/src/app/modules/court/court.service.ts index 3ff75964c054..d38a2abf3416 100644 --- a/apps/judicial-system/backend/src/app/modules/court/court.service.ts +++ b/apps/judicial-system/backend/src/app/modules/court/court.service.ts @@ -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' @@ -324,6 +323,7 @@ export class CourtService { caseId: string, courtId = '', type: CaseType, + receivalDate: Date, policeCaseNumbers: string[], isExtension: boolean, indictmentSubtypes?: IndictmentSubtypeMap, @@ -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] : '', diff --git a/apps/judicial-system/backend/src/app/modules/court/test/createCourtCase.spec.ts b/apps/judicial-system/backend/src/app/modules/court/test/createCourtCase.spec.ts index d1be752555d1..a32798d80b4e 100644 --- a/apps/judicial-system/backend/src/app/modules/court/test/createCourtCase.spec.ts +++ b/apps/judicial-system/backend/src/app/modules/court/test/createCourtCase.spec.ts @@ -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 @@ -30,13 +27,14 @@ type GivenWhenThen = ( caseId: string, courtId: string, type: CaseType, + receivalDate: Date, policeCaseNumbers: string[], isExtension: boolean, indictmentSubtypes?: IndictmentSubtypeMap, ) => Promise describe('CourtService - Create court case', () => { - const date = randomDate() + const receivalDate = randomDate() let mockCourtClientService: CourtClientService let givenWhenThen: GivenWhenThen @@ -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, @@ -66,6 +62,7 @@ describe('CourtService - Create court case', () => { caseId, courtId, type, + receivalDate, policeCaseNumbers, isExtension, indictmentSubtypes, @@ -93,6 +90,7 @@ describe('CourtService - Create court case', () => { caseId, courtId, type, + receivalDate, policeCaseNumbers, isExtension, ) @@ -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], }, @@ -132,6 +130,7 @@ describe('CourtService - Create court case', () => { caseId, courtId, type, + receivalDate, policeCaseNumbers, isExtension, indictmentSubtypes, @@ -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], }, @@ -171,6 +170,7 @@ describe('CourtService - Create court case', () => { caseId, courtId, type, + receivalDate, policeCaseNumbers, isExtension, ) @@ -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], }) @@ -205,6 +205,7 @@ describe('CourtService - Create court case', () => { caseId, courtId, type, + receivalDate, policeCaseNumbers, isExtension, ) @@ -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], }) @@ -248,6 +249,7 @@ describe('CourtService - Create court case', () => { caseId, courtId, type, + receivalDate, policeCaseNumbers, isExtension, indictmentSubtypes, @@ -284,6 +286,7 @@ describe('CourtService - Create court case', () => { caseId, courtId, type, + receivalDate, policeCaseNumbers, isExtension, indictmentSubtypes, diff --git a/apps/judicial-system/backend/src/app/modules/file/file.controller.ts b/apps/judicial-system/backend/src/app/modules/file/file.controller.ts index 9982d5c839c3..8e0a7940bc95 100644 --- a/apps/judicial-system/backend/src/app/modules/file/file.controller.ts +++ b/apps/judicial-system/backend/src/app/modules/file/file.controller.ts @@ -34,7 +34,6 @@ import { districtCourtAssistantRule, districtCourtJudgeRule, districtCourtRegistrarRule, - prisonSystemStaffRule, prosecutorRepresentativeRule, prosecutorRule, publicProsecutorStaffRule, diff --git a/apps/judicial-system/backend/src/app/modules/notification/test/internalNotificationController/sendAdvocateAssignedNotifications.spec.ts b/apps/judicial-system/backend/src/app/modules/notification/test/internalNotificationController/sendAdvocateAssignedNotifications.spec.ts index eff36227b407..0898f603b32f 100644 --- a/apps/judicial-system/backend/src/app/modules/notification/test/internalNotificationController/sendAdvocateAssignedNotifications.spec.ts +++ b/apps/judicial-system/backend/src/app/modules/notification/test/internalNotificationController/sendAdvocateAssignedNotifications.spec.ts @@ -19,7 +19,6 @@ import { createTestingNotificationModule } from '../createTestingNotificationMod import { Case } from '../../../case' import { CaseNotificationDto } from '../../dto/caseNotification.dto' import { DeliverResponse } from '../../models/deliver.response' -import { Notification } from '../../models/notification.model' import { notificationModuleConfig } from '../../notification.config' jest.mock('../../../../factories') @@ -41,17 +40,12 @@ describe('InternalNotificationController - Send defender assigned notifications' let mockEmailService: EmailService let mockConfig: ConfigType - let mockNotificationModel: typeof Notification let givenWhenThen: GivenWhenThen let notificationDTO: CaseNotificationDto beforeEach(async () => { - const { - emailService, - notificationConfig, - notificationModel, - internalNotificationController, - } = await createTestingNotificationModule() + const { emailService, notificationConfig, internalNotificationController } = + await createTestingNotificationModule() notificationDTO = { user: { id: userId } as User, @@ -60,7 +54,6 @@ describe('InternalNotificationController - Send defender assigned notifications' mockEmailService = emailService mockConfig = notificationConfig - mockNotificationModel = notificationModel givenWhenThen = async ( caseId: string, diff --git a/apps/judicial-system/backend/src/app/modules/subpoena/subpoena.controller.ts b/apps/judicial-system/backend/src/app/modules/subpoena/subpoena.controller.ts index da84d9c66c59..da139a356906 100644 --- a/apps/judicial-system/backend/src/app/modules/subpoena/subpoena.controller.ts +++ b/apps/judicial-system/backend/src/app/modules/subpoena/subpoena.controller.ts @@ -5,7 +5,6 @@ import { Get, Header, Inject, - InternalServerErrorException, Param, Query, Res, diff --git a/apps/judicial-system/backend/src/app/modules/subpoena/test/internalSubpoenaController/deliverSubpoenaToPolice.spec.ts b/apps/judicial-system/backend/src/app/modules/subpoena/test/internalSubpoenaController/deliverSubpoenaToPolice.spec.ts index c2f9ff68a082..43255bd30aa1 100644 --- a/apps/judicial-system/backend/src/app/modules/subpoena/test/internalSubpoenaController/deliverSubpoenaToPolice.spec.ts +++ b/apps/judicial-system/backend/src/app/modules/subpoena/test/internalSubpoenaController/deliverSubpoenaToPolice.spec.ts @@ -4,6 +4,7 @@ import { createTestingSubpoenaModule } from '../createTestingSubpoenaModule' import { Case } from '../../../case' import { Defendant } from '../../../defendant' +import { DeliverDto } from '../../dto/deliver.dto' import { DeliverResponse } from '../../models/deliver.response' import { Subpoena } from '../../models/subpoena.model' import { SubpoenaService } from '../../subpoena.service' @@ -22,7 +23,7 @@ describe('InternalSubpoenaController - Deliver subpoena to police', () => { const subpoena = { id: subpoenaId } as Subpoena const defendant = { id: defendantId, subpoenas: [subpoena] } as Defendant const theCase = { id: caseId } as Case - const user = { user: { id: uuid() } } as any + const user = { user: { id: uuid() } } as DeliverDto const delivered = { delivered: true } as DeliverResponse let mockSubpoenaService: SubpoenaService