Skip to content

Commit

Permalink
Use identity number from yourInformation in pdf, if the forms have be…
Browse files Browse the repository at this point in the history
…en upgraded to new format.
  • Loading branch information
lotorvik committed Jan 23, 2025
1 parent e198c36 commit 944f19e
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 17 deletions.
17 changes: 15 additions & 2 deletions packages/fyllut-backend/src/routers/api/helpers/pdfService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
NavFormType,
Submission,
translationUtils,
yourInformationUtils,
} from '@navikt/skjemadigitalisering-shared-domain';
import correlator from 'express-correlation-id';
import { config } from '../../../config/config';
Expand Down Expand Up @@ -48,7 +49,19 @@ export const createPdf = async (
if (!html || Object.keys(html).length === 0) {
throw Error('Missing HTML for generating PDF.');
}
const { fodselsnummerDNummerSoker } = submission.data;

const yourInformation = yourInformationUtils.getYourInformation(form, submission.data);

let identityNumber: string;
if (yourInformation?.identitet?.identitetsnummer) {
identityNumber = yourInformation.identitet.identitetsnummer;
} else if (submission.data.fodselsnummerDNummerSoker) {
// This is the old format of the object, which is still used in some forms.
identityNumber = submission.data.fodselsnummerDNummerSoker as string;
} else {
identityNumber = '—';
}

appMetrics.exstreamPdfRequestsCounter.inc();
let errorOccurred = false;
const stopMetricRequestDuration = appMetrics.outgoingRequestDuration.startTimer({
Expand All @@ -62,7 +75,7 @@ export const createPdf = async (
form.properties.skjemanummer,
language,
html,
(fodselsnummerDNummerSoker as string | undefined) || '—',
identityNumber,
);
} catch (e) {
errorOccurred = true;
Expand Down
17 changes: 2 additions & 15 deletions packages/shared-domain/src/forsteside/forstesideUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
Recipient,
SubmissionAttachmentValue,
UkjentBruker,
yourInformationUtils,
} from '../index';
import SubmissionYourInformation from '../submission/yourInformation';
import { genererPersonalia } from './forstesideDepricatedUtils';

type BrukerInfo = KjentBruker | UkjentBruker;
Expand All @@ -22,7 +22,7 @@ const addressLine = (text?: string, prefix: string = ', ') => {
};

const getUserData = (form: NavFormType, submission: SubmissionData): BrukerInfo => {
const yourInformation = getFirstYourInformation(form, submission);
const yourInformation = yourInformationUtils.getYourInformation(form, submission);

if (!yourInformation) {
// Denne er for å støtte gamle formatet på dine opplysninger.
Expand Down Expand Up @@ -57,19 +57,6 @@ const getUserData = (form: NavFormType, submission: SubmissionData): BrukerInfo
}
};

const getFirstYourInformation = (
form: NavFormType,
submission: SubmissionData,
): SubmissionYourInformation | undefined => {
const yourInformationForm = navFormUtils
.flattenComponents(form.components)
.find((component) => component.yourInformation && submission[component.key]);

if (yourInformationForm) {
return submission[yourInformationForm.key] as SubmissionYourInformation;
}
};

const getAttachmentTitles = (form: NavFormType, submission: SubmissionData): string[] => {
return getAttachments(submission, form).map((component) => component.properties!.vedleggstittel!);
};
Expand Down
2 changes: 2 additions & 0 deletions packages/shared-domain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ import signatureUtils from './utils/signatureUtils';
import stringUtils from './utils/stringUtils';
import translationUtils from './utils/translation';
import validatorUtils from './utils/validatorUtils';
import yourInformationUtils from './utils/yourInformationUtils';

export {
DeclarationType,
Expand Down Expand Up @@ -155,6 +156,7 @@ export {
supportedEnhetstyper,
translationUtils,
validatorUtils,
yourInformationUtils,
};
export type {
AccordionSettingValue,
Expand Down
86 changes: 86 additions & 0 deletions packages/shared-domain/src/utils/yourInformationUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { describe, expect, it } from 'vitest';
import { NavFormType, SubmissionData } from '../form';
import { yourInformationUtils } from '../index';

describe('getYourInformationUtils', () => {
it('returns your information object when present in submission data', () => {
const form = {
components: [
{
key: 'yourInfo',
yourInformation: true,
label: '',
type: '',
},
],
} as NavFormType;
const submission: SubmissionData = {
yourInfo: { name: 'John Doe' },
};

const result = yourInformationUtils.getYourInformation(form, submission);
expect(result).toEqual({ name: 'John Doe' });
});

it('returns the first your information object present in submission data', () => {
const form = {
components: [
{
key: 'yourInfo',
yourInformation: true,
label: '',
type: '',
},
{
key: 'yourInfo2',
yourInformation: true,
label: '',
type: '',
},
],
} as NavFormType;
const submission: SubmissionData = {
yourInfo2: { name: 'John Doe' },
yourInfo: { name: 'Jane Doe' },
};

const result = yourInformationUtils.getYourInformation(form, submission);
expect(result).toEqual({ name: 'Jane Doe' });
});

it('returns undefined when your information object is not present in submission data', () => {
const form = {
components: [
{
key: 'yourInfo',
yourInformation: true,
label: '',
type: '',
},
],
} as NavFormType;
const submission: SubmissionData = {};

const result = yourInformationUtils.getYourInformation(form, submission);
expect(result).toBeUndefined();
});

it('returns undefined when no your information component is found in form', () => {
const form = {
components: [
{
key: 'otherInfo',
yourInformation: false,
label: '',
type: '',
},
],
} as NavFormType;
const submission: SubmissionData = {
otherInfo: { name: 'John Doe' },
};

const result = yourInformationUtils.getYourInformation(form, submission);
expect(result).toBeUndefined();
});
});
24 changes: 24 additions & 0 deletions packages/shared-domain/src/utils/yourInformationUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { NavFormType, SubmissionData } from '../form';
import { navFormUtils } from '../index';
import SubmissionYourInformation from '../submission/yourInformation';

/**
* Returns the first your information object from the submission data.
* @param form
* @param submission
*/
const getYourInformation = (form: NavFormType, submission: SubmissionData): SubmissionYourInformation | undefined => {
const yourInformationForm = navFormUtils
.flattenComponents(form.components)
.find((component) => component.yourInformation && submission[component.key]);

if (yourInformationForm) {
return submission[yourInformationForm.key] as SubmissionYourInformation;
}
};

const yourInformationUtils = {
getYourInformation,
};

export default yourInformationUtils;

0 comments on commit 944f19e

Please sign in to comment.