Skip to content

Commit

Permalink
feat(j-s): Confirmed court records (#15921)
Browse files Browse the repository at this point in the history
* Checkpoint

* Cleanup

* Rename

* Refactor

* Remove unused code

* Fix lint

* Create confirmed ruling

* Create confirmed ruling

* Make confirmation smaller

* Make confirmation smaller

* Use correct date

* Merge

* Refactor

* Refactor

* Cleanup

* Create court record pdf confirmation

* Cleanup

* Fix typos

* Merge

* Fix typo

* Fix shouldGetConfirmedDocument function

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
oddsson and kodiakhq[bot] authored Sep 12, 2024
1 parent d298945 commit 8cce1bb
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 52 deletions.
148 changes: 131 additions & 17 deletions apps/judicial-system/backend/src/app/formatters/confirmedPdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import {
} from './pdfHelpers'
import { PDFKitCoatOfArms } from './PDFKitCoatOfArms'

type ConfirmableCaseFileCategories =
| CaseFileCategory.INDICTMENT
| CaseFileCategory.RULING
| CaseFileCategory.COURT_RECORD

// Colors
const lightGray = rgb(0.9804, 0.9804, 0.9804)
const darkGray = rgb(0.7961, 0.7961, 0.7961)
Expand Down Expand Up @@ -201,28 +206,28 @@ const createRulingConfirmation = async (
const doc = pages[0]

const { height } = doc.getSize()
const shaddowHeight = calculatePt(70)
const shadowHeight = calculatePt(70)
const institutionWidth = calculatePt(160)
const confirmedByWidth = institutionWidth + calculatePt(48)
const shaddowWidth = institutionWidth + confirmedByWidth + coatOfArmsWidth
const shadowWidth = institutionWidth + confirmedByWidth + coatOfArmsWidth
const titleHeight = calculatePt(24)
const titleWidth = institutionWidth + confirmedByWidth

// Draw the shadow
doc.drawRectangle({
x: pageMargin,
y: height - shaddowHeight - pageMargin,
width: shaddowWidth,
height: shaddowHeight,
y: height - shadowHeight - pageMargin,
width: shadowWidth,
height: shadowHeight,
color: lightGray,
})

// Draw the box around the coat of arms
doc.drawRectangle({
x: coatOfArmsX,
y: height - shaddowHeight - pageMargin + calculatePt(8),
y: height - shadowHeight - pageMargin + calculatePt(8),
width: coatOfArmsWidth,
height: shaddowHeight,
height: shadowHeight,
color: rgb(1, 1, 1),
borderColor: darkGray,
borderWidth: 1,
Expand Down Expand Up @@ -259,7 +264,7 @@ const createRulingConfirmation = async (
})

doc.drawText(formatDate(confirmation.date) || '', {
x: shaddowWidth - calculatePt(24),
x: shadowWidth - calculatePt(24),
y: height - pageMargin - titleHeight + calculatePt(16),
size: calculatePt(smallFontSize),
font: timesRomanFont,
Expand All @@ -270,7 +275,7 @@ const createRulingConfirmation = async (
x: coatOfArmsX + coatOfArmsWidth,
y: height - pageMargin - titleHeight - confirmedByHeight + calculatePt(12),
width: institutionWidth,
height: shaddowHeight - titleHeight,
height: shadowHeight - titleHeight,
color: white,
borderColor: darkGray,
borderWidth: 1,
Expand All @@ -297,7 +302,7 @@ const createRulingConfirmation = async (
x: coatOfArmsX + coatOfArmsWidth + institutionWidth,
y: height - pageMargin - titleHeight - confirmedByHeight + calculatePt(12),
width: confirmedByWidth,
height: shaddowHeight - titleHeight,
height: shadowHeight - titleHeight,
color: white,
borderColor: darkGray,
borderWidth: 1,
Expand Down Expand Up @@ -330,19 +335,128 @@ const createRulingConfirmation = async (
}
}

const createCourtRecordConfirmation = async (
confirmation: Confirmation,
pdfDoc: PDFDocument,
) => {
const pages = pdfDoc.getPages()
const doc = pages[0]

const { height } = doc.getSize()
const shadowHeight = calculatePt(70)
const institutionWidth = calculatePt(160)
const confirmedByWidth = institutionWidth + calculatePt(48)
const shadowWidth = institutionWidth + confirmedByWidth + coatOfArmsWidth
const titleHeight = calculatePt(24)
const titleWidth = institutionWidth + confirmedByWidth

// Draw the shadow
doc.drawRectangle({
x: pageMargin,
y: height - shadowHeight - pageMargin,
width: shadowWidth,
height: shadowHeight,
color: lightGray,
})

// Draw the box around the coat of arms
doc.drawRectangle({
x: coatOfArmsX,
y: height - shadowHeight - pageMargin + calculatePt(8),
width: coatOfArmsWidth,
height: shadowHeight,
color: rgb(1, 1, 1),
borderColor: darkGray,
borderWidth: 1,
})

PDFKitCoatOfArms(doc, height + 8)

doc.drawRectangle({
x: coatOfArmsX + coatOfArmsWidth,
y: height - pageMargin - titleHeight + calculatePt(8),
width: titleWidth,
height: titleHeight,
color: lightGray,
borderColor: darkGray,
borderWidth: 1,
})

const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman)
const timesRomanBoldFont = await pdfDoc.embedFont(
StandardFonts.TimesRomanBold,
)
doc.drawText('Réttarvörslugátt', {
x: titleX,
y: height - pageMargin - titleHeight + calculatePt(16),
size: calculatePt(smallFontSize),
font: timesRomanBoldFont,
})

doc.drawText('Rafræn staðfesting', {
x: 158,
y: height - pageMargin - titleHeight + calculatePt(16),
size: calculatePt(smallFontSize),
font: timesRomanFont,
})

doc.drawText(formatDate(confirmation.date) || '', {
x: shadowWidth - calculatePt(24),
y: height - pageMargin - titleHeight + calculatePt(16),
size: calculatePt(smallFontSize),
font: timesRomanFont,
})

// Draw the "Institution" box
doc.drawRectangle({
x: coatOfArmsX + coatOfArmsWidth,
y: height - pageMargin - titleHeight - confirmedByHeight + calculatePt(12),
width: institutionWidth + confirmedByWidth,
height: shadowHeight - titleHeight,
color: white,
borderColor: darkGray,
borderWidth: 1,
})

doc.drawText('Dómstóll', {
x: titleX,
y: height - pageMargin - titleHeight - calculatePt(10),
size: calculatePt(smallFontSize),
font: timesRomanBoldFont,
})

if (confirmation?.institution) {
doc.drawText(confirmation.institution, {
x: titleX,
y: height - pageMargin - titleHeight - calculatePt(22),
font: timesRomanFont,
size: calculatePt(smallFontSize),
})
}
}

export const createConfirmedPdf = async (
confirmation: Confirmation,
pdf: Buffer,
fileType: CaseFileCategory.INDICTMENT | CaseFileCategory.RULING,
fileType: ConfirmableCaseFileCategories,
) => {
const pdfDoc = await PDFDocument.load(pdf)

if (fileType === CaseFileCategory.INDICTMENT) {
await createIndictmentConfirmation(confirmation, pdfDoc)
}

if (fileType === CaseFileCategory.RULING) {
await createRulingConfirmation(confirmation, pdfDoc)
switch (fileType) {
case CaseFileCategory.INDICTMENT:
await createIndictmentConfirmation(confirmation, pdfDoc)
break
case CaseFileCategory.RULING: {
await createRulingConfirmation(confirmation, pdfDoc)
break
}
case CaseFileCategory.COURT_RECORD: {
await createCourtRecordConfirmation(confirmation, pdfDoc)
break
}
default: {
throw new Error('CaseFileCategory not supported')
}
}

const pdfBytes = await pdfDoc.save()
Expand Down
74 changes: 39 additions & 35 deletions apps/judicial-system/backend/src/app/modules/file/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ export class FileService {
}

if (
file.category === CaseFileCategory.RULING &&
(file.category === CaseFileCategory.RULING ||
file.category === CaseFileCategory.COURT_RECORD) &&
isCompletedCase(theCase.state) &&
theCase.rulingDate
) {
Expand All @@ -209,7 +210,7 @@ export class FileService {
date: theCase.rulingDate,
},
pdf,
CaseFileCategory.RULING,
file.category,
)
}
})
Expand All @@ -236,33 +237,42 @@ export class FileService {
})
}

private shouldGetConfirmedDocument = (file: CaseFile, theCase: Case) => {
// Only case files in indictment cases can be confirmed
if (!isIndictmentCase(theCase.type)) {
return false
}

// Only indictments that have been submitted to court can be confirmed
if (
file.category === CaseFileCategory.INDICTMENT &&
hasIndictmentCaseBeenSubmittedToCourt(theCase.state)
) {
return true
}

// Rulings and court records are only confirmed when a case is completed
if (
(file.category === CaseFileCategory.RULING ||
file.category === CaseFileCategory.COURT_RECORD) &&
isCompletedCase(theCase.state)
) {
return true
}

// Don't get confirmed document for any other file categories
return false
}

async getCaseFileFromS3(theCase: Case, file: CaseFile): Promise<Buffer> {
if (isIndictmentCase(theCase.type)) {
if (
file.category === CaseFileCategory.INDICTMENT &&
hasIndictmentCaseBeenSubmittedToCourt(theCase.state)
) {
return this.awsS3Service.getConfirmedIndictmentCaseObject(
theCase.type,
file.key,
!file.hash,
(content: Buffer) =>
this.confirmIndictmentCaseFile(theCase, file, content),
)
}

if (
file.category === CaseFileCategory.RULING &&
isCompletedCase(theCase.state)
) {
return this.awsS3Service.getConfirmedIndictmentCaseObject(
theCase.type,
file.key,
!file.hash,
(content: Buffer) =>
this.confirmIndictmentCaseFile(theCase, file, content),
)
}
if (this.shouldGetConfirmedDocument(file, theCase)) {
return this.awsS3Service.getConfirmedIndictmentCaseObject(
theCase.type,
file.key,
!file.hash,
(content: Buffer) =>
this.confirmIndictmentCaseFile(theCase, file, content),
)
}

return this.awsS3Service.getObject(theCase.type, file.key)
Expand Down Expand Up @@ -396,13 +406,7 @@ export class FileService {
file: CaseFile,
timeToLive?: number,
): Promise<string> {
if (
isIndictmentCase(theCase.type) &&
((file.category === CaseFileCategory.INDICTMENT &&
hasIndictmentCaseBeenSubmittedToCourt(theCase.state)) ||
(file.category === CaseFileCategory.RULING &&
isCompletedCase(theCase.state)))
) {
if (this.shouldGetConfirmedDocument(file, theCase)) {
return this.awsS3Service.getConfirmedIndictmentCaseSignedUrl(
theCase.type,
file.key,
Expand Down

0 comments on commit 8cce1bb

Please sign in to comment.