Skip to content

Commit

Permalink
Merge branch 'main' into feature/web-pension-calculator-new-year-option
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Dec 16, 2024
2 parents 9226769 + 42c998d commit 7ed1866
Show file tree
Hide file tree
Showing 186 changed files with 3,651 additions and 1,978 deletions.
780 changes: 394 additions & 386 deletions .github/CODEOWNERS

Large diffs are not rendered by default.

21 changes: 21 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 @@ -45,6 +45,7 @@ import {
eventTypes,
isCompletedCase,
isIndictmentCase,
isInvestigationCase,
isRequestCase,
isTrafficViolationCase,
notificationTypes,
Expand Down Expand Up @@ -845,7 +846,25 @@ export class CaseService {
this.getDeliverProsecutorToCourtMessages(theCase, user),
)
}
private addMessagesForSignedCourtRecordToQueue(
theCase: Case,
user: TUser,
): Promise<void> {
const messages = []

if (
theCase.origin === CaseOrigin.LOKE &&
isInvestigationCase(theCase.type)
) {
messages.push({
type: MessageType.DELIVERY_TO_POLICE_SIGNED_COURT_RECORD,
user,
caseId: theCase.id,
})
}

return this.messageService.sendMessagesToQueue(messages)
}
private addMessagesForSignedRulingToQueue(
theCase: Case,
user: TUser,
Expand Down Expand Up @@ -1899,6 +1918,8 @@ export class CaseService {
false,
)

await this.addMessagesForSignedCourtRecordToQueue(theCase, user)

return { documentSigned: true }
} catch (error) {
this.eventService.postErrorEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,35 @@ export class InternalCaseController {
)
}

@UseGuards(
CaseExistsGuard,
new CaseTypeGuard([...restrictionCases, ...investigationCases]),
CaseCompletedGuard,
)
@Post(
`case/:caseId/${
messageEndpoint[MessageType.DELIVERY_TO_POLICE_SIGNED_COURT_RECORD]
}`,
)
@ApiOkResponse({
type: DeliverResponse,
description: 'Delivers a signed court record to police',
})
deliverSignedCourtRecordToPolice(
@Param('caseId') caseId: string,
@CurrentCase() theCase: Case,
@Body() deliverDto: DeliverDto,
): Promise<DeliverResponse> {
this.logger.debug(
`Delivering the signed court record for case ${caseId} to police`,
)

return this.internalCaseService.deliverSignedCourtRecordToPolice(
theCase,
deliverDto.user,
)
}

@UseGuards(
CaseExistsGuard,
new CaseTypeGuard([...restrictionCases, ...investigationCases]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ export class InternalCaseService {
)
}

private getSignedCourtRecordPdf(theCase: Case) {
return this.awsS3Service.getGeneratedRequestCaseObject(
theCase.type,
`${theCase.id}/courtRecord.pdf`,
)
}

private async deliverSignedRulingPdfToCourt(
theCase: Case,
user: TUser,
Expand Down Expand Up @@ -606,6 +613,7 @@ export class InternalCaseService {
? {
name: theCase.prosecutor.name,
nationalId: theCase.prosecutor.nationalId,
email: theCase.prosecutor.email,
}
: undefined,
)
Expand Down Expand Up @@ -1099,6 +1107,32 @@ export class InternalCaseService {
return { delivered }
}

async deliverSignedCourtRecordToPolice(
theCase: Case,
user: TUser,
): Promise<DeliverResponse> {
const delivered = await this.getSignedCourtRecordPdf(theCase)
.then((pdf) =>
this.deliverCaseToPoliceWithFiles(theCase, user, [
{
type: PoliceDocumentType.RVTB,
courtDocument: Base64.btoa(pdf.toString('binary')),
},
]),
)
.catch((reason) => {
// Tolerate failure, but log error
this.logger.error(
`Failed to deliver signed court record for case ${theCase.id} to police`,
{ reason },
)

return false
})

return { delivered }
}

async deliverSignedRulingToPolice(
theCase: Case,
user: TUser,
Expand All @@ -1115,7 +1149,7 @@ export class InternalCaseService {
.catch((reason) => {
// Tolerate failure, but log error
this.logger.error(
`Failed to deliver sigend ruling for case ${theCase.id} to police`,
`Failed to deliver signed ruling for case ${theCase.id} to police`,
{ reason },
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ describe('InternalCaseController - Deliver indictment info to court', () => {
{ eventType: EventType.INDICTMENT_CONFIRMED, created: indictmentDate },
],
defendants: [{ name: 'Test Ákærði', nationalId: '1234567890' }],
prosecutor: { name: 'Test Sækjandi', nationalId: '0101010101' },
prosecutor: {
name: 'Test Sækjandi',
nationalId: '0101010101',
email: '[email protected]',
},
} as Case

let mockCourtService: CourtService
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { Base64 } from 'js-base64'
import { uuid } from 'uuidv4'

import {
CaseOrigin,
CaseState,
CaseType,
User,
} from '@island.is/judicial-system/types'

import { createTestingCaseModule } from '../createTestingCaseModule'

import { nowFactory } from '../../../../factories'
import { randomDate } from '../../../../test'
import { AwsS3Service } from '../../../aws-s3'
import { PoliceDocumentType, PoliceService } from '../../../police'
import { Case } from '../../models/case.model'
import { DeliverResponse } from '../../models/deliver.response'

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

interface Then {
result: DeliverResponse
error: Error
}

type GivenWhenThen = (caseId: string, theCase: Case) => Promise<Then>

describe('InternalCaseController - Deliver signed court record to police', () => {
const userId = uuid()
const user = { id: userId } as User

let mockAwsS3Service: AwsS3Service
let mockPoliceService: PoliceService
let givenWhenThen: GivenWhenThen

beforeEach(async () => {
const { awsS3Service, policeService, internalCaseController } =
await createTestingCaseModule()

mockAwsS3Service = awsS3Service
mockPoliceService = policeService

const mockGetGeneratedObject =
awsS3Service.getGeneratedRequestCaseObject as jest.Mock
mockGetGeneratedObject.mockRejectedValue(new Error('Some error'))
const mockUpdatePoliceCase = mockPoliceService.updatePoliceCase as jest.Mock
mockUpdatePoliceCase.mockRejectedValue(new Error('Some error'))

givenWhenThen = async (caseId: string, theCase: Case) => {
const then = {} as Then

await internalCaseController
.deliverSignedCourtRecordToPolice(caseId, theCase, { user })
.then((result) => (then.result = result))
.catch((error) => (then.error = error))

return then
}
})

describe('deliver case to police', () => {
const caseId = uuid()
const caseType = CaseType.PHONE_TAPPING
const caseState = CaseState.ACCEPTED
const policeCaseNumber = uuid()
const courtCaseNumber = uuid()
const defendantNationalId = '0123456789'
const validToDate = randomDate()
const caseConclusion = 'test conclusion'
const theCase = {
id: caseId,
origin: CaseOrigin.LOKE,
type: caseType,
state: caseState,
policeCaseNumbers: [policeCaseNumber],
courtCaseNumber,
defendants: [{ nationalId: defendantNationalId }],
validToDate,
conclusion: caseConclusion,
} as Case
const courtRecordPdf = 'test court record'

let then: Then

beforeEach(async () => {
const mockToday = nowFactory as jest.Mock
mockToday.mockReturnValueOnce(validToDate)

const mockGetGeneratedObject =
mockAwsS3Service.getGeneratedRequestCaseObject as jest.Mock
mockGetGeneratedObject.mockResolvedValueOnce(courtRecordPdf)
const mockUpdatePoliceCase =
mockPoliceService.updatePoliceCase as jest.Mock
mockUpdatePoliceCase.mockResolvedValueOnce(true)

then = await givenWhenThen(caseId, theCase)
})

it('should update the police case with a signed court record', async () => {
expect(
mockAwsS3Service.getGeneratedRequestCaseObject,
).toHaveBeenCalledWith(caseType, `${caseId}/courtRecord.pdf`)
expect(mockPoliceService.updatePoliceCase).toHaveBeenCalledWith(
user,
caseId,
caseType,
caseState,
policeCaseNumber,
courtCaseNumber,
defendantNationalId,
validToDate,
caseConclusion,
[
{
type: PoliceDocumentType.RVTB,
courtDocument: Base64.btoa(courtRecordPdf),
},
],
)
expect(then.result.delivered).toEqual(true)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { CanActivate } from '@nestjs/common'

import {
investigationCases,
restrictionCases,
} from '@island.is/judicial-system/types'

import { CaseCompletedGuard } from '../../guards/caseCompleted.guard'
import { CaseExistsGuard } from '../../guards/caseExists.guard'
import { CaseTypeGuard } from '../../guards/caseType.guard'
import { InternalCaseController } from '../../internalCase.controller'

describe('InternalCaseController - Deliver signed court record to police guards', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let guards: any[]

beforeEach(() => {
guards = Reflect.getMetadata(
'__guards__',
InternalCaseController.prototype.deliverSignedCourtRecordToPolice,
)
})

it('should have three guards', () => {
expect(guards).toHaveLength(3)
})

describe('CaseExistsGuard', () => {
let guard: CanActivate

beforeEach(() => {
guard = new guards[0]()
})

it('should have CaseExistsGuard as guard 1', () => {
expect(guard).toBeInstanceOf(CaseExistsGuard)
})
})

describe('CaseTypeGuard', () => {
let guard: CanActivate

beforeEach(() => {
guard = guards[1]
})

it('should have CaseTypeGuard as guard 2', () => {
expect(guard).toBeInstanceOf(CaseTypeGuard)
expect(guard).toEqual({
allowedCaseTypes: [...restrictionCases, ...investigationCases],
})
})
})

describe('CaseCompletedGuard', () => {
let guard: CanActivate

beforeEach(() => {
guard = new guards[2]()
})

it('should have CaseCompletedGuard as guard 3', () => {
expect(guard).toBeInstanceOf(CaseCompletedGuard)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ export class CourtService {
)

const isIndictment = isIndictmentCase(type)
const policeCaseNumber = policeCaseNumbers[0]
? policeCaseNumbers[0].replace(/-/g, '')
: ''

return await this.courtClientService.createCase(courtId, {
caseType: isIndictment ? 'S - Ákærumál' : 'R - Rannsóknarmál',
Expand All @@ -344,7 +347,7 @@ export class CourtService {
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] : '',
sourceNumber: policeCaseNumber,
})
} catch (reason) {
if (reason instanceof ServiceUnavailableException) {
Expand Down Expand Up @@ -569,10 +572,13 @@ export class CourtService {
policeCaseNumber?: string,
subtypes?: string[],
defendants?: { name?: string; nationalId?: string }[],
prosecutor?: { name?: string; nationalId?: string },
prosecutor?: { name?: string; nationalId?: string; email?: string },
): Promise<unknown> {
try {
const subject = `${courtName} - ${courtCaseNumber} - upplýsingar`

policeCaseNumber = policeCaseNumber?.replace(/-/g, '')

const content = JSON.stringify({
receivedByCourtDate,
indictmentDate,
Expand Down
Loading

0 comments on commit 7ed1866

Please sign in to comment.