Skip to content

Commit

Permalink
feat(j-s): Store Subpoena PDF and HASH (#16235)
Browse files Browse the repository at this point in the history
* Stores has and generated pdf for subpoenas

* Improved undefined handling

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
gudjong and kodiakhq[bot] authored Oct 3, 2024
1 parent 0e26000 commit 9af30c0
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 12 deletions.
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
49 changes: 47 additions & 2 deletions apps/judicial-system/backend/src/app/modules/case/pdf.service.ts
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))
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,
}
}

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)

// 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,
),
)
}

return generatedPdf
}
}
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

0 comments on commit 9af30c0

Please sign in to comment.