Skip to content

Commit

Permalink
Add prisma-json-types-generator to Forms BE (#2137)
Browse files Browse the repository at this point in the history
* Install prisma-json-types-generator

* Create PrismaJson types

* Type existing JSON types in schema.prisma

* Replace all types castings with real types

* Add EmptyFormDataErrorDto

* Add checks for empty form data

* Adjust tests where possible

* Update typescript and related packages to latest version

* Change tsconfig module/moduleResolution
  • Loading branch information
MarekBodingerBA authored Jan 22, 2025
1 parent 5f9d684 commit 51c25b7
Show file tree
Hide file tree
Showing 31 changed files with 356 additions and 103 deletions.
128 changes: 104 additions & 24 deletions nest-forms-backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions nest-forms-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"@types/lodash": "^4.17.0",
"@types/mime-types": "^2.1.4",
"@types/multer": "^1.4.7",
"@types/node": "^20.9.5",
"@types/node": "^22.10.7",
"@types/nodemailer": "^6.4.16",
"@types/passport-http": "^0.3.11",
"@types/passport-http-bearer": "^1.0.41",
Expand Down Expand Up @@ -126,11 +126,12 @@
"path": "^0.12.7",
"prettier": "^3.3.2",
"prisma": "^5.21.1",
"prisma-json-types-generator": "^3.2.2",
"ts-jest": "^29.1.2",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.1.2",
"typescript": "^4.9.4"
"tsconfig-paths": "^4.2.0",
"typescript": "^5.7.3"
},
"jest": {
"moduleFileExtensions": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Prisma, PrismaClient } from '@prisma/client'
import { GenericObjectType } from '@rjsf/utils'
import { getFormDefinitionBySlug } from 'forms-shared/definitions/getFormDefinitionBySlug'
import { createSingleUseValidatorRegistry } from 'forms-shared/form-utils/validatorRegistry'
import { getFormSummary } from 'forms-shared/summary/summary'
Expand Down Expand Up @@ -40,18 +39,22 @@ async function main() {
`Definition not found for slug ${form.formDefinitionSlug}`,
)

if (!form.formDataJson) {
throw new Error(`Form ${form.id} has no data`)
}

console.log(`Generating summary for form ${form.id}...`)
const formSummary = getFormSummary(
formDefinition,
form.formDataJson as GenericObjectType,
form.formDataJson,
validatorRegistry,
)

console.log(`Updating form ${form.id} with summary...`)
await tx.forms.update({
where: { id: form.id },
data: {
formSummary: formSummary as unknown as Prisma.JsonObject,
formSummary,
},
})
console.log(`Successfully processed form ${form.id}`)
Expand Down
6 changes: 6 additions & 0 deletions nest-forms-backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ generator client {
binaryTargets = ["native", "linux-musl", "linux-arm64-openssl-1.1.x", "darwin-arm64", "linux-musl-openssl-3.0.x"]
}

generator json {
provider = "prisma-json-types-generator"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
Expand All @@ -30,6 +34,7 @@ model Forms {
error FormError @default(NONE)
// either the version taken from schema at the time of form creation, or at the time of successful send where applicable (TODO: change when implementing versioning)
jsonVersion String
/// [FormDataJson]
formDataJson Json?
formDataGinis String?
formDataBase64 String?
Expand All @@ -42,6 +47,7 @@ model Forms {
// kedy to bolo podane
finishSubmission DateTime?
formDefinitionSlug String
/// [FormSummary]
formSummary Json?
// relation to files
files Files[]
Expand Down
10 changes: 10 additions & 0 deletions nest-forms-backend/prisma/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { FormSummary as _FormSummary } from 'forms-shared/summary/summary'
import type { GenericObjectType } from '@rjsf/utils'

declare global {
namespace PrismaJson {
type FormDataJson = GenericObjectType

type FormSummary = _FormSummary
}
}
7 changes: 7 additions & 0 deletions nest-forms-backend/src/convert-pdf/convert-pdf.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ export default class ConvertPdfService {
)
}

if (form.formDataJson == null) {
throw this.throwerErrorGuard.UnprocessableEntityException(
FormsErrorsEnum.EMPTY_FORM_DATA,
FormsErrorsResponseEnum.EMPTY_FORM_DATA,
)
}

// putObject requires bucket name on it's own
const filePath = await this.getPdfExportFilePathWithoutBucket(
formId,
Expand Down
26 changes: 18 additions & 8 deletions nest-forms-backend/src/convert/convert.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Response } from 'express'
import { CognitoGetUserData } from '../auth/dtos/cognito.dto'
import CognitoGuard from '../auth/guards/cognito.guard'
import {
EmptyFormDataErrorDto,
FormDefinitionNotFoundErrorDto,
FormDefinitionNotSupportedTypeErrorDto,
FormIsOwnedBySomeoneElseErrorDto,
Expand Down Expand Up @@ -70,6 +71,7 @@ export default class ConvertController {
})
@ApiExtraModels(FormNotFoundErrorDto)
@ApiExtraModels(FormDefinitionNotFoundErrorDto)
@ApiExtraModels(EmptyFormDataErrorDto)
@ApiNotFoundResponse({
status: HttpStatusCode.NotFound,
description: 'Form or form definition was not found',
Expand All @@ -87,8 +89,14 @@ export default class ConvertController {
})
@ApiUnprocessableEntityResponse({
status: HttpStatusCode.UnprocessableEntity,
description: 'Got wrong type of form definition for its slug.',
type: FormDefinitionNotSupportedTypeErrorDto,
description:
'Got wrong type of form definition for its slug or empty form data.',
schema: {
oneOf: [
{ $ref: getSchemaPath(FormDefinitionNotSupportedTypeErrorDto) },
{ $ref: getSchemaPath(EmptyFormDataErrorDto) },
],
},
})
@UseGuards(new CognitoGuard(true))
@Post('json-to-xml-v2')
Expand Down Expand Up @@ -175,17 +183,14 @@ export default class ConvertController {
summary: '',
description: 'Generates PDF for given form data.',
})
@ApiExtraModels(EmptyFormDataErrorDto)
@ApiNotFoundResponse({
status: 404,
description: 'Form or form definition not found',
schema: {
oneOf: [
{
$ref: getSchemaPath(FormNotFoundErrorDto),
},
{
$ref: getSchemaPath(FormDefinitionNotFoundErrorDto),
},
{ $ref: getSchemaPath(FormNotFoundErrorDto) },
{ $ref: getSchemaPath(FormDefinitionNotFoundErrorDto) },
],
},
})
Expand All @@ -194,6 +199,11 @@ export default class ConvertController {
description: 'Form is owned by someone else, the access is not granted.',
type: FormIsOwnedBySomeoneElseErrorDto,
})
@ApiUnprocessableEntityResponse({
status: HttpStatusCode.UnprocessableEntity,
description: 'Empty form data.',
type: EmptyFormDataErrorDto,
})
@ApiInternalServerErrorResponse({
status: HttpStatusCode.InternalServerError,
description: 'There was an error during generating tax pdf.',
Expand Down
Loading

0 comments on commit 51c25b7

Please sign in to comment.