Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
chore: mask nric (#401)
Browse files Browse the repository at this point in the history
* chore: mask nric

* fix: lint

* chore: code review

* chore: reuse existing types

Co-authored-by: Kyle Huang Junyuan <[email protected]>
  • Loading branch information
eugbyte and HJunyuan authored Dec 16, 2021
1 parent 4e8140e commit d4a5c52
Show file tree
Hide file tree
Showing 8 changed files with 1,382 additions and 1,298 deletions.
37 changes: 37 additions & 0 deletions src/common/maskNricFin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { R4 } from "@ahryman40k/ts-fhir-types";
import { fhirBundleV1, Patient } from "../types";
import { maskNricInFhirBundleV1, maskNricInFhirBundle } from "./maskNricFin";
import pdtUnwrappedSampleV1 from "../../test/fixtures/v1/example_healthcert_with_nric_unwrapped.json";
import pdtUnwrappedSampleV2 from "../../test/fixtures/v2/pdt_pcr_with_nric_unwrapped.json";

describe("test maskNricFinInPlace function", () => {
it("nric should be masked for V1 Healthcert", () => {
const { fhirBundle } = pdtUnwrappedSampleV1;

const maskedFhirBundle = maskNricInFhirBundleV1(fhirBundle as any);
const patient = (maskedFhirBundle as fhirBundleV1).entry.find(
(entry) => entry.resourceType === "Patient"
);
const nricIdentifier = (patient as Patient).identifier.find(
(i) => i.type !== "PPN" && i.type.text.toUpperCase() === "NRIC"
);

expect(fhirBundle).not.toEqual(maskNricInFhirBundle);
expect(nricIdentifier?.value).toStrictEqual("S****989Z");
});

it("nric should be masked for V2 Healthcert", () => {
const { fhirBundle } = pdtUnwrappedSampleV2;

const maskedFhirBundle = maskNricInFhirBundle(fhirBundle as any);
const patient = (maskedFhirBundle as R4.IBundle).entry?.find(
(entry: any) => entry?.resource?.resourceType === "Patient"
)?.resource as R4.IPatient;
const nricIdentifier = patient.identifier?.find(
(i) => i.id?.toUpperCase() === "NRIC-FIN"
);

expect(fhirBundle).not.toEqual(maskNricInFhirBundle);
expect(nricIdentifier?.value).toStrictEqual("S****989Z");
});
});
45 changes: 45 additions & 0 deletions src/common/maskNricFin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { R4 } from "@ahryman40k/ts-fhir-types";
import { fhirBundleV1, Patient, PDTHealthCertV2 } from "../types";

// Get the reference to the nested nric identifier object
// mask the value in place

const maskNric = (nric: string) => `${nric[0]}****${nric.slice(5)}`;

/**
* @deprecated This function should be removed when PDT HealthCert v1.0 is deprecated.
*/
export const maskNricInFhirBundleV1 = (
fhirBundle: fhirBundleV1 | R4.IBundle
) => {
const patient = (fhirBundle as fhirBundleV1).entry.find(
(entry) => entry.resourceType === "Patient"
);
const nricIdentifier = (patient as Patient).identifier.find(
(i) => i.type !== "PPN" && i.type.text.toUpperCase() === "NRIC"
);

// Mask NRIC by reference
if (nricIdentifier?.value)
nricIdentifier.value = maskNric(nricIdentifier.value);

return fhirBundle;
};

export const maskNricInFhirBundle = (
fhirBundle: PDTHealthCertV2["fhirBundle"]
) => {
// PDTHealthCertV2["fhirBundle"] and R4.IBundle can be used interchangeably
const patient = (fhirBundle as R4.IBundle).entry?.find(
(entry: any) => entry?.resource?.resourceType === "Patient"
)?.resource as R4.IPatient;
const nricIdentifier = patient.identifier?.find(
(i) => i.id?.toUpperCase() === "NRIC-FIN"
);

// Mask NRIC by reference
if (nricIdentifier?.value)
nricIdentifier.value = maskNric(nricIdentifier.value);

return fhirBundle;
};
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ it("should wrap a document and sign the document", async () => {
"type": Object {
"text": "NRIC",
},
"value": "S9098989Z",
"value": "S****989Z",
},
],
"name": Array [
Expand Down Expand Up @@ -255,7 +255,7 @@ it("should wrap a document and sign the document with signedEuHealthCert", async
"type": Object {
"text": "NRIC",
},
"value": "S9098989Z",
"value": "S****989Z",
},
],
"name": Array [
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getData, v2, WrappedDocument } from "@govtechsg/open-attestation";
import { notarise } from "@govtechsg/oa-schemata";
import { maskNricInFhirBundleV1 } from "../../../common/maskNricFin";
import { config } from "../../../config";
import { HealthCertDocument, NotarizedHealthCert } from "../../../types";
import { getParticularsFromHealthCert } from "../../healthCert";
Expand Down Expand Up @@ -66,7 +67,7 @@ export const createUnwrappedDocument = (
name: documentName,
validFrom: certificateData.validFrom,
fhirVersion,
fhirBundle,
fhirBundle: maskNricInFhirBundleV1(fhirBundle),
issuers,
$template,
notarisationMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ it("should wrap a v2 document and sign the document", async () => {
},
Object {
"id": "NRIC-FIN",
"value": "S9098989Z",
"value": "S****989Z",
},
],
"name": Array [
Expand Down Expand Up @@ -450,7 +450,7 @@ it("should wrap a v2 document and sign the document with signedEuHealthCert", as
},
Object {
"id": "NRIC-FIN",
"value": "S9098989Z",
"value": "S****989Z",
},
],
"name": Array [
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getData, v2, WrappedDocument } from "@govtechsg/open-attestation";
import { notarise } from "@govtechsg/oa-schemata";
import { maskNricInFhirBundle } from "../../../common/maskNricFin";
import { ParsedBundle } from "../../fhir/types";
import { config } from "../../../config";
import { PDTHealthCertV2, EndorsedPDTHealthCertV2 } from "../../../types";
Expand Down Expand Up @@ -68,7 +69,7 @@ export const createUnwrappedDocument = (
type,
validFrom: certificateData.validFrom,
fhirVersion,
fhirBundle,
fhirBundle: maskNricInFhirBundle(fhirBundle),
issuers,
$template,
notarisationMetadata,
Expand Down

0 comments on commit d4a5c52

Please sign in to comment.