From 5dadbf1be1ba3119b9991d0d7e8de79ec21ca435 Mon Sep 17 00:00:00 2001 From: make-github-pseudonymous-again <5165674+make-github-pseudonymous-again@users.noreply.github.com> Date: Sun, 5 Jan 2025 22:50:50 +0100 Subject: [PATCH] EDIT draft insertFromEid --- imports/api/_dev/populate/eids.ts | 52 ++++++++++++++ .../endpoint/patients/insertFromEid.tests.ts | 71 +++++++++++++++++++ .../api/endpoint/patients/insertFromEid.ts | 32 +++++++++ 3 files changed, 155 insertions(+) create mode 100644 imports/api/_dev/populate/eids.ts create mode 100644 imports/api/endpoint/patients/insertFromEid.tests.ts create mode 100644 imports/api/endpoint/patients/insertFromEid.ts diff --git a/imports/api/_dev/populate/eids.ts b/imports/api/_dev/populate/eids.ts new file mode 100644 index 000000000..f7c374e67 --- /dev/null +++ b/imports/api/_dev/populate/eids.ts @@ -0,0 +1,52 @@ +import {faker} from '@faker-js/faker'; + +import format from 'date-fns/format'; + +import {makeTemplate} from '../../../_test/fixtures'; + +const DATE_FORMAT = 'yyyyMMdd'; +const AGE_MAX = 130; + +export const newEidData = makeTemplate({ + xml: { + encoding: () => 'UTF-8', + version: () => '1.0', + }, + eid: { + graphpersoversion: () => faker.string.numeric(2), + version: () => faker.string.numeric(2), + }, + card: { + carddata_appl_version: () => faker.string.numeric(2), + cardnumber: () => faker.string.numeric(12), + chipnumber: () => faker.string.alphanumeric(32), + documenttype: () => "belgian_citizen", + validitydatebegin: () => format(faker.date.past({years: 30}), DATE_FORMAT), + validitydateend: () => format(faker.date.past({years: 30}), DATE_FORMAT), + deliverymunicipality: () => faker.location.county(), + }, + certificates: { + authentication: () => faker.string.uuid(), + citizenca: () => faker.string.uuid(), + root: () => faker.string.uuid(), + rrn: () => faker.string.uuid(), + signing: () => faker.string.uuid(), + }, + identity: { + nationality: () => faker.location.country(), + nationalnumber: () => faker.string.uuid(), + dateofbirth: () => format(faker.date.past({years: AGE_MAX}), DATE_FORMAT), + placeofbirth: () => faker.location.city(), + gender: () => faker.helpers.arrayElement(['male', 'female', undefined]), + specialstatus: () => faker.word.adjective(), + name: () => faker.person.lastName(), + firstname: () => faker.person.firstName(), + middlenames: () => faker.person.middleName(), + photo: () => undefined, + }, + address: { + municipality: () => faker.location.city(), + streetandnumber: () => faker.location.streetAddress(), + zip: () => faker.location.zipCode(), + }, +}); diff --git a/imports/api/endpoint/patients/insertFromEid.tests.ts b/imports/api/endpoint/patients/insertFromEid.tests.ts new file mode 100644 index 000000000..c9a29941b --- /dev/null +++ b/imports/api/endpoint/patients/insertFromEid.tests.ts @@ -0,0 +1,71 @@ +import {assert} from 'chai'; + +import {randomUserId, server, throws} from '../../../_test/fixtures'; + +import {Patients} from '../../collection/patients'; +import {Allergies} from '../../collection/allergies'; +import {Doctors} from '../../collection/doctors'; +import {Insurances} from '../../collection/insurances'; + +import {newEidData} from '../../_dev/populate/eids'; +import invoke from '../invoke'; +import insertFromEid from './insertFromEid'; +import { normalizedName, patientFieldsFromEid } from '../../patients'; + +server(__filename, () => { + it('cannot create a patient when not logged in', async () => { + return throws( + async () => invoke( + insertFromEid, + // @ts-expect-error Type-checking is working as expected. + {userId: undefined}, + [newEidData()] + ), + /not-authorized/, + ); + }); + + it('can create a patient when logged in', async () => { + const userId = randomUserId(); + + const eid = newEidData(); + + const patientId = await invoke(insertFromEid, {userId}, [eid]); + + const patients = await Patients.find({owner: userId}).fetchAsync(); + + assert.strictEqual(patients.length, 1); + + const expectedPatientFields = patientFieldsFromEid(eid); + const expectedComputedFields = { + normalizedName: normalizedName(expectedPatientFields.firstname, expectedPatientFields.lastname), + } + + const expected = { + ...expectedPatientFields, + ...expectedComputedFields, + }; + const {_id, createdAt, owner, ...actual} = patients[0]!; + + assert.strictEqual(patientId, _id); + + assert.strictEqual(owner, userId); + + assert.deepEqual(actual, expected); + + assert.isAtMost(createdAt.valueOf(), Date.now()); + + assert.sameDeepMembers( + await Allergies.find().fetchAsync(), + [], + ); + assert.sameDeepMembers( + await Doctors.find().fetchAsync(), + [], + ); + assert.sameDeepMembers( + await Insurances.find().fetchAsync(), + [], + ); + }); +}); diff --git a/imports/api/endpoint/patients/insertFromEid.ts b/imports/api/endpoint/patients/insertFromEid.ts new file mode 100644 index 000000000..c22febacc --- /dev/null +++ b/imports/api/endpoint/patients/insertFromEid.ts @@ -0,0 +1,32 @@ +import {AuthenticationLoggedIn} from '../../Authentication'; + +import schema from '../../../lib/schema'; +import { eidFields, Eids } from '../../collection/eids'; +import {patientFieldsFromEid} from '../../patients'; +import type TransactionDriver from '../../transaction/TransactionDriver'; + +import define from '../define'; +import compose from '../compose'; +import insert from './insert'; + +export default define({ + name: '/api/patients/insertFromEid', + authentication: AuthenticationLoggedIn, + schema: schema.tuple([eidFields.strict()]), + async transaction(db: TransactionDriver, eid) { + const owner = this.userId; + + await db.insertOne(Eids, { + ...eid, + createdAt: new Date(), + owner, + }); + + const patient = patientFieldsFromEid(eid); + + return compose(db, insert, this, [patient]); + }, + simulate(_patient) { + return undefined; + }, +});