Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(j-s): Subpoena PDF #16098

Merged
merged 5 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Header,
Inject,
Param,
Query,
Req,
Res,
UseGuards,
Expand All @@ -19,7 +20,7 @@ import {
CurrentHttpUser,
JwtInjectBearerAuthGuard,
} from '@island.is/judicial-system/auth'
import type { User } from '@island.is/judicial-system/types'
import type { SubpoenaType, User } from '@island.is/judicial-system/types'

import { FileService } from './file.service'

Expand Down Expand Up @@ -162,6 +163,33 @@ export class LimitedAccessFileController {
)
}

@Get('subpoena/:defendantId')
@Header('Content-Type', 'application/pdf')
getSubpoenaPdf(
@Param('id') id: string,
@Param('defendantId') defendantId: string,
@Query('arraignmentDate') arraignmentDate: string,
@Query('location') location: string,
@Query('subpoenaType') subpoenaType: SubpoenaType,
@CurrentHttpUser() user: User,
@Req() req: Request,
@Res() res: Response,
): Promise<Response> {
this.logger.debug(
`Getting the subpoena for defendant ${defendantId} of case ${id} as a pdf document`,
)

return this.fileService.tryGetFile(
user.id,
AuditedAction.GET_SUBPOENA_PDF,
id,
`limitedAccess/defendant/${defendantId}/subpoena?arraignmentDate=${arraignmentDate}&location=${location}&subpoenaType=${subpoenaType}`,
gudjong marked this conversation as resolved.
Show resolved Hide resolved
req,
res,
'pdf',
)
}

@Get('allFiles')
@Header('Content-Type', 'application/zip')
async getAllFiles(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export class LimitedAccessCaseController {
private readonly limitedAccessCaseService: LimitedAccessCaseService,
private readonly eventService: EventService,
private readonly pdfService: PdfService,

@Inject(LOGGER_PROVIDER) private readonly logger: Logger,
) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ export class DefendantController {
DefendantExistsGuard,
)
@RolesRules(
prosecutorRule,
prosecutorRepresentativeRule,
publicProsecutorStaffRule,
districtCourtJudgeRule,
districtCourtRegistrarRule,
districtCourtAssistantRule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { CivilClaimantService } from './civilClaimant.service'
import { DefendantController } from './defendant.controller'
import { DefendantService } from './defendant.service'
import { InternalDefendantController } from './internalDefendant.controller'
import { LimitedAccessDefendantController } from './limitedAccessDefendant.controller'

@Module({
imports: [
Expand All @@ -24,6 +25,7 @@ import { InternalDefendantController } from './internalDefendant.controller'
controllers: [
DefendantController,
InternalDefendantController,
LimitedAccessDefendantController,
CivilClaimantController,
],
providers: [DefendantService, CivilClaimantService],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Response } from 'express'

import {
Controller,
Get,
Header,
Inject,
Param,
Query,
Res,
UseGuards,
} from '@nestjs/common'
import { ApiOkResponse, ApiTags } from '@nestjs/swagger'

import type { Logger } from '@island.is/logging'
import { LOGGER_PROVIDER } from '@island.is/logging'

import {
JwtAuthGuard,
RolesGuard,
RolesRules,
} from '@island.is/judicial-system/auth'
import { indictmentCases, SubpoenaType } from '@island.is/judicial-system/types'

import { defenderRule } from '../../guards'
import {
Case,
CaseExistsGuard,
CaseReadGuard,
CaseTypeGuard,
CurrentCase,
PdfService,
} from '../case'
import { CurrentDefendant } from './guards/defendant.decorator'
import { DefendantExistsGuard } from './guards/defendantExists.guard'
import { Defendant } from './models/defendant.model'

@Controller('api/case/:caseId/limitedAccess/defendant/:defendantId/subpoena')
@UseGuards(
JwtAuthGuard,
RolesGuard,
CaseExistsGuard,
new CaseTypeGuard(indictmentCases),
CaseReadGuard,
DefendantExistsGuard,
)
@ApiTags('limited access defendants')
export class LimitedAccessDefendantController {
constructor(
private readonly pdfService: PdfService,
@Inject(LOGGER_PROVIDER) private readonly logger: Logger,
) {}

@RolesRules(defenderRule)
@Get()
@Header('Content-Type', 'application/pdf')
@ApiOkResponse({
content: { 'application/pdf': {} },
description: 'Gets the subpoena for a given defendant as a pdf document',
})
async getSubpoenaPdf(
@Param('caseId') caseId: string,
@Param('defendantId') defendantId: string,
@CurrentCase() theCase: Case,
@CurrentDefendant() defendant: Defendant,
@Res() res: Response,
@Query('arraignmentDate') arraignmentDate?: Date,
@Query('location') location?: string,
@Query('subpoenaType') subpoenaType?: SubpoenaType,
gudjong marked this conversation as resolved.
Show resolved Hide resolved
): Promise<void> {
this.logger.debug(
`Getting the subpoena for defendant ${defendantId} of case ${caseId} as a pdf document`,
)

const pdf = await this.pdfService.getSubpoenaPdf(
theCase,
defendant,
arraignmentDate,
location,
subpoenaType,
)

res.end(pdf)
gudjong marked this conversation as resolved.
Show resolved Hide resolved
}
gudjong marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { UserService } from '../../user'
import { DefendantController } from '../defendant.controller'
import { DefendantService } from '../defendant.service'
import { InternalDefendantController } from '../internalDefendant.controller'
import { LimitedAccessDefendantController } from '../limitedAccessDefendant.controller'
import { Defendant } from '../models/defendant.model'

jest.mock('@island.is/judicial-system/message')
Expand All @@ -27,7 +28,11 @@ jest.mock('../../case/pdf.service')
export const createTestingDefendantModule = async () => {
const defendantModule = await Test.createTestingModule({
imports: [ConfigModule.forRoot({ load: [sharedAuthModuleConfig] })],
controllers: [DefendantController, InternalDefendantController],
controllers: [
DefendantController,
InternalDefendantController,
LimitedAccessDefendantController,
],
providers: [
SharedAuthModule,
MessageService,
Expand Down Expand Up @@ -81,6 +86,11 @@ export const createTestingDefendantModule = async () => {
InternalDefendantController,
)

const limitedAccessDefendantController =
defendantModule.get<LimitedAccessDefendantController>(
LimitedAccessDefendantController,
)

defendantModule.close()

return {
Expand All @@ -92,5 +102,6 @@ export const createTestingDefendantModule = async () => {
defendantService,
defendantController,
internalDefendantController,
limitedAccessDefendantController,
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { CanActivate } from '@nestjs/common'

import { JwtAuthGuard, RolesGuard } from '@island.is/judicial-system/auth'

import { DefendantController } from '../../defendant.controller'
Expand All @@ -12,31 +10,9 @@ describe('DefendantController - guards', () => {
guards = Reflect.getMetadata('__guards__', DefendantController)
})

it('should have two guards', () => {
it('should have the right guard configuration', () => {
expect(guards).toHaveLength(2)
})

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

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

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

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

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

it('should have RolesGuard as guard 2', () => {
expect(guard).toBeInstanceOf(RolesGuard)
})
expect(new guards[0]()).toBeInstanceOf(JwtAuthGuard)
expect(new guards[1]()).toBeInstanceOf(RolesGuard)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CaseExistsGuard, CaseReadGuard, CaseTypeGuard } from '../../../case'
import { DefendantController } from '../../defendant.controller'
import { DefendantExistsGuard } from '../../guards/defendantExists.guard'

describe('CaseController - Get custody notice pdf guards', () => {
describe('DefendantController - Get custody notice pdf guards', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let guards: any[]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import {
districtCourtAssistantRule,
districtCourtJudgeRule,
districtCourtRegistrarRule,
prosecutorRepresentativeRule,
prosecutorRule,
publicProsecutorStaffRule,
} from '../../../../guards'
import { DefendantController } from '../../defendant.controller'

describe('CaseController - Get custody notice pdf rules', () => {
describe('DefendantController - Get custody notice pdf rules', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let rules: any[]

Expand All @@ -17,7 +20,10 @@ describe('CaseController - Get custody notice pdf rules', () => {
})

it('should give permission to roles', () => {
expect(rules).toHaveLength(3)
expect(rules).toHaveLength(6)
expect(rules).toContain(prosecutorRule)
expect(rules).toContain(prosecutorRepresentativeRule)
expect(rules).toContain(publicProsecutorStaffRule)
expect(rules).toContain(districtCourtJudgeRule)
expect(rules).toContain(districtCourtRegistrarRule)
expect(rules).toContain(districtCourtAssistantRule)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Response } from 'express'
import { uuid } from 'uuidv4'

import { createTestingDefendantModule } from '../createTestingDefendantModule'

import { Case, PdfService } from '../../../case'
import { Defendant } from '../../models/defendant.model'

interface Then {
error: Error
}

type GivenWhenThen = () => Promise<Then>

describe('LimitedAccessDefendantController - Get subpoena pdf', () => {
const caseId = uuid()
const defendantId = uuid()
const defendant = { id: defendantId } as Defendant
const theCase = { id: caseId } as Case
const res = { end: jest.fn() } as unknown as Response
const pdf = Buffer.from(uuid())
let mockPdfService: PdfService
let givenWhenThen: GivenWhenThen

beforeEach(async () => {
const { pdfService, limitedAccessDefendantController } =
await createTestingDefendantModule()

mockPdfService = pdfService
const getSubpoenaPdfMock = mockPdfService.getSubpoenaPdf as jest.Mock
getSubpoenaPdfMock.mockResolvedValueOnce(pdf)

givenWhenThen = async () => {
const then = {} as Then

try {
await limitedAccessDefendantController.getSubpoenaPdf(
caseId,
defendantId,
theCase,
defendant,
res,
)
} catch (error) {
then.error = error as Error
}

return then
}
})

describe('pdf generated', () => {
beforeEach(async () => {
await givenWhenThen()
})

it('should generate pdf', () => {
expect(mockPdfService.getSubpoenaPdf).toHaveBeenCalledWith(
theCase,
defendant,
undefined,
undefined,
undefined,
)
expect(res.end).toHaveBeenCalledWith(pdf)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defenderRule } from '../../../../guards'
import { LimitedAccessDefendantController } from '../../limitedAccessDefendant.controller'

describe('LimitedAccessDefendantController - Get custody notice pdf rules', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let rules: any[]

beforeEach(() => {
rules = Reflect.getMetadata(
'roles-rules',
LimitedAccessDefendantController.prototype.getSubpoenaPdf,
)
})

it('should give permission to roles', () => {
expect(rules).toHaveLength(1)
expect(rules).toContain(defenderRule)
})
})
Loading
Loading