Skip to content

Commit

Permalink
EDIT draft insertFromEid
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Jan 6, 2025
1 parent 6b605e5 commit 5dadbf1
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
52 changes: 52 additions & 0 deletions imports/api/_dev/populate/eids.ts
Original file line number Diff line number Diff line change
@@ -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(),
},
});
71 changes: 71 additions & 0 deletions imports/api/endpoint/patients/insertFromEid.tests.ts
Original file line number Diff line number Diff line change
@@ -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(),
[],
);
});
});
32 changes: 32 additions & 0 deletions imports/api/endpoint/patients/insertFromEid.ts
Original file line number Diff line number Diff line change
@@ -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;
},
});

0 comments on commit 5dadbf1

Please sign in to comment.