Skip to content

Commit

Permalink
New government forms migration (#1640)
Browse files Browse the repository at this point in the history
* Bump form definition versions and set all Slovensko.sk forms to `newGovernmentXml`

* Handle special case of tax form in ConvertService

* Add JsonVersion to new Slovensko.sk XMLs

* Bump also "Žiadosť o nájom bytu" pospVersion as it needs to be regenerated too to include JsonVersion

---------

Co-authored-by: Marek Bodinger <marek.bodinger@gmail.com>
  • Loading branch information
MarekBodingerBA and MarekBodinger authored Nov 12, 2024
1 parent 129c99a commit abd1bf1
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 24 deletions.
15 changes: 10 additions & 5 deletions forms-shared/src/definitions/formDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const formDefinitions: FormDefinition[] = [
title: 'Žiadosť o stanovisko k investičnému zámeru',
schemas: stanoviskoKInvesticnemuZameru,
pospID: '00603481.stanoviskoKInvesticnemuZameru',
pospVersion: '0.8',
pospVersion: '0.9',
publisher: 'ico://sk/00603481',
gestor: 'Pinter Martin',
termsAndConditions: generalTermsAndConditions,
Expand All @@ -70,14 +70,15 @@ export const formDefinitions: FormDefinition[] = [
ginisPersonName: 'Simeunovičová Ľudmila',
},
isSigned: false,
newGovernmentXml: true,
},
{
type: FormDefinitionType.SlovenskoSkGeneric,
slug: 'zavazne-stanovisko-k-investicnej-cinnosti',
title: 'Žiadosť o záväzné stanovisko k investičnej činnosti',
schemas: zavazneStanoviskoKInvesticnejCinnosti,
pospID: '00603481.zavazneStanoviskoKInvesticnejCinnosti',
pospVersion: '0.7',
pospVersion: '0.8',
publisher: 'ico://sk/00603481',
gestor: 'Pinter Martin',
termsAndConditions: generalTermsAndConditions,
Expand All @@ -89,14 +90,15 @@ export const formDefinitions: FormDefinition[] = [
ginisPersonName: 'Simeunovičová Ľudmila',
},
isSigned: false,
newGovernmentXml: true,
},
{
type: FormDefinitionType.SlovenskoSkGeneric,
slug: 'predzahradky',
title: 'Predzáhradky',
schemas: predzahradky,
pospID: '00603481.predzahradky',
pospVersion: '1.0',
pospVersion: '1.1',
publisher: 'ico://sk/00603481',
gestor: 'Pinter Martin',
termsAndConditions: generalTermsAndConditions,
Expand All @@ -107,14 +109,15 @@ export const formDefinitions: FormDefinition[] = [
ginisOrganizationName: 'OUIC',
ginisPersonName: 'Simeunovičová Ľudmila',
},
newGovernmentXml: true,
},
{
type: FormDefinitionType.SlovenskoSkGeneric,
slug: 'komunitne-zahrady',
title: 'Komunitné záhrady',
schemas: komunitneZahrady,
pospID: '00603481.komunitneZahrady',
pospVersion: '1.0',
pospVersion: '1.1',
publisher: 'ico://sk/00603481',
gestor: 'Pinter Martin',
termsAndConditions: generalTermsAndConditions,
Expand All @@ -125,6 +128,7 @@ export const formDefinitions: FormDefinition[] = [
ginisOrganizationName: 'OUIC',
ginisPersonName: 'Simeunovičová Ľudmila',
},
newGovernmentXml: true,
},
{
type: FormDefinitionType.SlovenskoSkTax,
Expand All @@ -138,6 +142,7 @@ export const formDefinitions: FormDefinition[] = [
termsAndConditions: taxTermsAndConditions,
messageSubjectDefault: 'Priznanie k dani z nehnuteľností',
isSigned: true,
newGovernmentXml: true,
},
// testing integration of new posID against GINIS&NORIS, will be removed afterwards
{
Expand All @@ -160,7 +165,7 @@ export const formDefinitions: FormDefinition[] = [
schemas: ziadostONajomBytu,
// pospID contains different wording because the original form was created with a different name
pospID: '00603481.ziadostONajomnyByt',
pospVersion: '1.1',
pospVersion: '1.2',
publisher: 'ico://sk/00603481',
gestor: 'Pinter Martin',
termsAndConditions: generalTermsAndConditions,
Expand Down
11 changes: 9 additions & 2 deletions forms-shared/src/slovensko-sk/extractJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ const baseFormXmlSchema = {
},
required: ['xmlns'],
},
JsonVersion: {
type: 'array',
items: {
type: 'string',
},
minItems: 1,
maxItems: 1,
},
Json: {
type: 'array',
items: {
Expand All @@ -39,6 +47,7 @@ type BaseFormXml = {
$: {
xmlns: string
}
JsonVersion: [string]
Json: [string]
}
}
Expand Down Expand Up @@ -66,8 +75,6 @@ export enum ExtractJsonFromSlovenskoSkXmlErrorType {

/**
* Extracts JSON data from Slovensko.sk XML string
*
* TODO: Consider adding `omitExtraData`
*/
export async function extractJsonFromSlovenskoSkXml(
formDefinition: FormDefinitionSlovenskoSk,
Expand Down
1 change: 1 addition & 0 deletions forms-shared/src/slovensko-sk/file-templates/schemaXsd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const getSchemaXsd = (
<xs:element name="eform">
<xs:complexType>
<xs:sequence>
<xs:element name="JsonVersion" type="xs:string"/>
<xs:element name="Json" type="xs:string"/>
<xs:element name="Summary" type="SummaryType"/>
<xs:element name="TermsAndConditions" type="xs:string"/>
Expand Down
4 changes: 4 additions & 0 deletions forms-shared/src/slovensko-sk/generateXml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function getSlovenskoSkXmlObjectBase(
*/
export function getEmptySlovenskoSkXmlObject(formDefinition: FormDefinitionSlovenskoSk) {
return getSlovenskoSkXmlObjectBase(formDefinition, {
JsonVersion: '',
Json: JSON.stringify({}),
Summary: {
Form: {
Expand All @@ -46,6 +47,9 @@ export async function generateSlovenskoSkXmlObject(
serverFiles?: FormsBackendFile[],
) {
return getSlovenskoSkXmlObjectBase(formDefinition, {
// Before versioning for JSON is implemented, we will hardcode the version to 1.0 as we want to have the version
// in Slovensko.sk XMLs beforehand to accommodate for future changes.
JsonVersion: '1.0',
Json: JSON.stringify(formData),
Summary: await renderSlovenskoXmlSummary(formDefinition, formData, serverFiles),
TermsAndConditions: removeMarkdown(formDefinition.termsAndConditions),
Expand Down

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions forms-shared/tests/slovensko-sk/extractJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('extractJsonFromSlovenskoSkXml', () => {
const validXmlString = `
<?xml version="1.0" encoding="UTF-8"?>
<eform xmlns="http://schemas.gov.sk/form/App.GeneralAgenda/1.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<JsonVersion>1.0</JsonVersion>
<Json>{"key":"value"}</Json>
</eform>
`
Expand All @@ -24,6 +25,15 @@ describe('extractJsonFromSlovenskoSkXml', () => {
expect(result).toEqual({ key: 'value' })
})

it('should successfully extract JSON from valid XML without version (backwards compatibility)', async () => {
const xmlWithoutVersion = validXmlString
.split('\n')
.filter((line) => !line.includes('<JsonVersion>1.0</JsonVersion>'))
.join('\n')
const result = await extractJsonFromSlovenskoSkXml(formDefinition, xmlWithoutVersion)
expect(result).toEqual({ key: 'value' })
})

it('should throw InvalidXml error for malformed XML', async () => {
const invalidXml = '<invalid></xml>'
await expect(extractJsonFromSlovenskoSkXml(formDefinition, invalidXml)).rejects.toThrow(
Expand All @@ -38,6 +48,7 @@ describe('extractJsonFromSlovenskoSkXml', () => {
const incorrectXmlStructure = `
<?xml version="1.0" encoding="UTF-8"?>
<wrongRoot xmlns="http://schemas.gov.sk/form/App.GeneralAgenda/1.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<JsonVersion>1.0</JsonVersion>
<Json>{"key":"value"}</Json>
</wrongRoot>
`
Expand Down
19 changes: 19 additions & 0 deletions nest-forms-backend/src/convert/convert.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { FormDefinitionSlovenskoSkTax } from 'forms-shared/definitions/formDefinitionTypes'

/**
* Tax form is a special case, as the only usage of ConvertService in its case is when user imports/exports the XML via
* user interface. Currently, the form that is sent to NASES has different posp* (than this new one). The tax form will
* be migrated to the new posp* in the future. In order to provide users the possibility to import the form, the exported
* XML will already contain the new posp* (after the migration this patch will be removed, but the forms will be importable).
*
* Before this patch, the generated XML had the old posp*, but contained the "importable" data shape, not the one that
* is sent to NASES.
*/
// eslint-disable-next-line import/prefer-default-export
export const patchConvertServiceTaxFormDefinition = (
formDefinition: FormDefinitionSlovenskoSkTax,
): FormDefinitionSlovenskoSkTax => ({
...formDefinition,
pospID: '00603481.priznanieKDaniZNehnutelnosti',
pospVersion: '1.0',
})
9 changes: 7 additions & 2 deletions nest-forms-backend/src/convert/convert.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import ThrowerErrorGuard from '../utils/guards/thrower-error.guard'
import MinioClientSubservice from '../utils/subservices/minio-client.subservice'
import { JsonSchema } from '../utils/types/global'
import { FormWithFiles } from '../utils/types/prisma'
import { patchConvertServiceTaxFormDefinition } from './convert.helper'
import {
ConvertToPdfRequestDto,
JsonToXmlV2RequestDto,
Expand Down Expand Up @@ -85,7 +86,9 @@ export default class ConvertService {
})) as FormWithFiles

return generateSlovenskoSkXmlObject(
formDefinition,
isSlovenskoSkTaxFormDefinition(formDefinition)
? patchConvertServiceTaxFormDefinition(formDefinition)
: formDefinition,
formDataJson as GenericObjectType,
formWithFiles.files,
)
Expand Down Expand Up @@ -219,7 +222,9 @@ export default class ConvertService {
if (formDefinition.newGovernmentXml) {
try {
const jsonForm = await extractJsonFromSlovenskoSkXml(
formDefinition,
isSlovenskoSkTaxFormDefinition(formDefinition)
? patchConvertServiceTaxFormDefinition(formDefinition)
: formDefinition,
data.xmlForm,
)
return { jsonForm }
Expand Down

0 comments on commit abd1bf1

Please sign in to comment.