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): Store Subpoena PDF and HASH #16235

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,22 @@
'use strict'

module.exports = {
up(queryInterface, Sequelize) {
return queryInterface.sequelize.transaction((transaction) =>
queryInterface.addColumn(
'subpoena',
'hash',
{ type: Sequelize.STRING, allowNull: true },
{ transaction },
),
)
},

down(queryInterface) {
return queryInterface.sequelize.transaction((transaction) =>
queryInterface.removeColumn('subpoena', 'hash', {
transaction,
}),
)
},
}
13 changes: 5 additions & 8 deletions apps/judicial-system/backend/src/app/formatters/subpoenaPdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
addMediumText,
addNormalRightAlignedText,
addNormalText,
Confirmation,
setTitle,
} from './pdfHelpers'

Expand All @@ -33,6 +34,7 @@ export const createSubpoena = (
arraignmentDate?: Date,
location?: string,
subpoenaType?: SubpoenaType,
confirmation?: Confirmation,
): Promise<Buffer> => {
const doc = new PDFDocument({
size: 'A4',
Expand All @@ -51,7 +53,7 @@ export const createSubpoena = (

setTitle(doc, formatMessage(strings.title))

if (subpoena) {
if (confirmation) {
addEmptyLines(doc, 5)
}

Expand Down Expand Up @@ -154,13 +156,8 @@ export const createSubpoena = (

addFooter(doc)

if (subpoena) {
addConfirmation(doc, {
actor: theCase.judge?.name || '',
title: theCase.judge?.title,
institution: theCase.judge?.institution?.name || '',
date: subpoena.created,
})
if (confirmation) {
addConfirmation(doc, confirmation)
}

doc.end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import CryptoJS from 'crypto-js'

import {
BadRequestException,
forwardRef,
Inject,
Injectable,
InternalServerErrorException,
Expand Down Expand Up @@ -33,7 +34,7 @@ import {
} from '../../formatters'
import { AwsS3Service } from '../aws-s3'
import { Defendant } from '../defendant'
import { Subpoena } from '../subpoena'
import { Subpoena, SubpoenaService } from '../subpoena'
import { UserService } from '../user'
import { Case } from './models/case.model'

Expand All @@ -45,6 +46,8 @@ export class PdfService {
private readonly awsS3Service: AwsS3Service,
private readonly intlService: IntlService,
private readonly userService: UserService,
@Inject(forwardRef(() => SubpoenaService))
gudjong marked this conversation as resolved.
Show resolved Hide resolved
private readonly subpoenaService: SubpoenaService,
@InjectModel(Case) private readonly caseModel: typeof Case,
@Inject(LOGGER_PROVIDER) private readonly logger: Logger,
) {}
Expand Down Expand Up @@ -298,16 +301,58 @@ export class PdfService {
location?: string,
subpoenaType?: SubpoenaType,
): Promise<Buffer> {
let confirmation: Confirmation | undefined = undefined

if (subpoena) {
if (subpoena.hash) {
const existingPdf = await this.tryGetPdfFromS3(
theCase,
`${theCase.id}/subpoena/${subpoena.id}.pdf`,
)

if (existingPdf) {
return existingPdf
}
}

confirmation = {
actor: theCase.judge?.name || '',
title: theCase.judge?.title,
institution: theCase.judge?.institution?.name || '',
date: subpoena.created,
}
gudjong marked this conversation as resolved.
Show resolved Hide resolved
}

await this.refreshFormatMessage()

return createSubpoena(
const generatedPdf = await createSubpoena(
theCase,
defendant,
this.formatMessage,
subpoena,
arraignmentDate,
location,
subpoenaType,
confirmation,
)

if (subpoena) {
const subpoenaHash = CryptoJS.MD5(
generatedPdf.toString('binary'),
).toString(CryptoJS.enc.Hex)

gudjong marked this conversation as resolved.
Show resolved Hide resolved
gudjong marked this conversation as resolved.
Show resolved Hide resolved
// No need to wait for this to finish
this.subpoenaService
.setHash(subpoena.id, subpoenaHash)
.then(() =>
this.tryUploadPdfToS3(
theCase,
`${theCase.id}/subpoena/${subpoena.id}.pdf`,
generatedPdf,
),
)
}
gudjong marked this conversation as resolved.
Show resolved Hide resolved

return generatedPdf
gudjong marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ export class SubpoenaExistsGuard implements CanActivate {
const defendant: Defendant = request.defendant

if (!defendant) {
// subpoenaId is the external police document id
request.subpoena = await this.subpoenaService.findBySubpoenaId(subpoenaId)

return true
}

// subpoenaId is the internal subpoena id
const subpoena = defendant.subpoenas?.find(
(subpoena) => subpoena.id === subpoenaId,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ export class Subpoena extends Model {
@Column({ type: DataType.STRING, allowNull: false })
@ApiProperty({ type: String })
location!: string

@Column({ type: DataType.STRING, allowNull: true })
@ApiPropertyOptional({ type: String })
hash?: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { Base64 } from 'js-base64'
import { Includeable, Sequelize } from 'sequelize'
import { Transaction } from 'sequelize/types'

import { forwardRef, Inject, Injectable } from '@nestjs/common'
import {
forwardRef,
Inject,
Injectable,
InternalServerErrorException,
} from '@nestjs/common'
import { InjectConnection, InjectModel } from '@nestjs/sequelize'

import type { Logger } from '@island.is/logging'
Expand Down Expand Up @@ -52,6 +57,22 @@ export class SubpoenaService {
)
}

async setHash(id: string, hash: string): Promise<void> {
const [numberOfAffectedRows] = await this.subpoenaModel.update(
{ hash },
{ where: { id } },
)

if (numberOfAffectedRows > 1) {
// Tolerate failure, but log error
this.logger.error(
`Unexpected number of rows (${numberOfAffectedRows}) affected when updating subpoena hash for subpoena ${id}`,
)
} else if (numberOfAffectedRows < 1) {
throw new InternalServerErrorException(`Could not update subpoena ${id}`)
}
}

async update(
subpoena: Subpoena,
update: UpdateSubpoenaDto,
Expand Down Expand Up @@ -99,6 +120,7 @@ export class SubpoenaService {
}

const updatedSubpoena = await this.findBySubpoenaId(subpoena.subpoenaId)

return updatedSubpoena
}

Expand All @@ -113,7 +135,7 @@ export class SubpoenaService {
})

if (!subpoena) {
throw new Error(`Subpoena with id ${subpoenaId} not found`)
throw new Error(`Subpoena with subpoena id ${subpoenaId} not found`)
}

return subpoena
Expand Down
Loading