Skip to content

Commit

Permalink
🚧 progress(eid): Deduplicate insertions.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Jan 11, 2025
1 parent f6fbfe1 commit 9b7a812
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 12 deletions.
2 changes: 2 additions & 0 deletions imports/api/collection/all.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type Collection from '../Collection';

import {Settings} from './settings';
import {Eids} from './eids';
import {Patients} from './patients';
import {PatientsSearchIndex} from './patients/search';
import {Drugs} from './drugs';
Expand All @@ -16,6 +17,7 @@ import {Availability} from './availability';

const collections: Array<Collection<any>> = [
Settings,
Eids,
Patients,
PatientsSearchIndex,
Drugs,
Expand Down
1 change: 1 addition & 0 deletions imports/api/collection/eids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const eidMetadata = schema.object({
_id: schema.string(),
owner: schema.string(),
createdAt: schema.date(),
lastUsedAt: schema.date(),
});

export type EidMetadata = schema.infer<typeof eidMetadata>;
Expand Down
31 changes: 31 additions & 0 deletions imports/api/endpoint/patients/insertFromEid.app-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {assert} from 'chai';

import {randomUserId, server} from '../../../_test/fixtures';

import {Eids} from '../../collection/eids';

import {newEidData} from '../../_dev/populate/eids';
import invoke from '../invoke';

import insertFromEid from './insertFromEid';

server(__filename, () => {
it('does not create duplicate eid entries', async () => {
const userId = randomUserId();

const eid = newEidData();

await invoke(insertFromEid, {userId}, [eid]);
const oldEntry = await Eids.findOneAsync({owner: userId});
assert.isDefined(oldEntry);

await invoke(insertFromEid, {userId}, [eid]);
const entries = await Eids.find({owner: userId}).fetchAsync();

assert.strictEqual(entries.length, 1);

const {createdAt, lastUsedAt} = entries[0]!;
assert.strictEqual(createdAt.valueOf(), oldEntry.createdAt.valueOf());
assert.isAbove(lastUsedAt.valueOf(), oldEntry.lastUsedAt.valueOf());
});
});
3 changes: 2 additions & 1 deletion imports/api/endpoint/patients/insertFromEid.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ server(__filename, () => {

assert.strictEqual(entries.length, 1);

const {_id, createdAt, owner, ...rest} = entries[0]!;
const {_id, createdAt, lastUsedAt, owner, ...rest} = entries[0]!;

assert.strictEqual(owner, userId);
assert.isAtMost(createdAt.valueOf(), Date.now());
assert.strictEqual(lastUsedAt.valueOf(), createdAt.valueOf());

assert.deepEqual(
rest,
Expand Down
25 changes: 20 additions & 5 deletions imports/api/endpoint/patients/insertFromEid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,26 @@ export default define({
async transaction(db: TransactionDriver, eid) {
const owner = this.userId;

await db.insertOne(Eids, {
...eid,
createdAt: new Date(),
owner,
});
const lastUsedAt = new Date();

await db.updateOne(
Eids,
{
owner,
...eid,
},
{
$set: {
lastUsedAt,
},
$setOnInsert: {

Check warning on line 32 in imports/api/endpoint/patients/insertFromEid.ts

View check run for this annotation

Codecov / codecov/patch

imports/api/endpoint/patients/insertFromEid.ts#L32

Added line #L32 was not covered by tests
createdAt: lastUsedAt,
},
},
{
upsert: true,
},
);

const patient = patientFieldsFromEid(eid);

Expand Down
35 changes: 35 additions & 0 deletions imports/api/endpoint/patients/updateFromEid.app-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {assert} from 'chai';

import {randomUserId, server} from '../../../_test/fixtures';

import {newPatient} from '../../_dev/populate/patients';
import invoke from '../invoke';

import {newEidData} from '../../_dev/populate/eids';

import {Eids} from '../../collection/eids';

import updateFromEid from './updateFromEid';

server(__filename, () => {
it('does not create duplicate eid entries', async () => {
const userId = randomUserId();

const eid = newEidData();

const patientId = await newPatient({userId});

await invoke(updateFromEid, {userId}, [patientId, eid]);
const oldEntry = await Eids.findOneAsync({owner: userId});
assert.isDefined(oldEntry);

await invoke(updateFromEid, {userId}, [patientId, eid]);
const entries = await Eids.find({owner: userId}).fetchAsync();

assert.strictEqual(entries.length, 1);

const {createdAt, lastUsedAt} = entries[0]!;
assert.strictEqual(createdAt.valueOf(), oldEntry.createdAt.valueOf());
assert.isAbove(lastUsedAt.valueOf(), oldEntry.lastUsedAt.valueOf());
});
});
3 changes: 2 additions & 1 deletion imports/api/endpoint/patients/updateFromEid.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ server(__filename, () => {

assert.strictEqual(entries.length, 1);

const {_id, createdAt, owner, ...rest} = entries[0]!;
const {_id, createdAt, lastUsedAt, owner, ...rest} = entries[0]!;

assert.strictEqual(owner, userId);
assert.isAtMost(createdAt.valueOf(), Date.now());
assert.strictEqual(lastUsedAt.valueOf(), createdAt.valueOf());

assert.deepEqual(
rest,
Expand Down
25 changes: 20 additions & 5 deletions imports/api/endpoint/patients/updateFromEid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,26 @@ export default define({
async transaction(db: TransactionDriver, patientId, eid) {
const owner = this.userId;

await db.insertOne(Eids, {
...eid,
createdAt: new Date(),
owner,
});
const lastUsedAt = new Date();

await db.updateOne(
Eids,
{
owner,
...eid,
},
{
$set: {
lastUsedAt,
},
$setOnInsert: {

Check warning on line 32 in imports/api/endpoint/patients/updateFromEid.ts

View check run for this annotation

Codecov / codecov/patch

imports/api/endpoint/patients/updateFromEid.ts#L32

Added line #L32 was not covered by tests
createdAt: lastUsedAt,
},
},
{
upsert: true,
},
);

const changes = patientFieldsFromEid(eid);

Expand Down
42 changes: 42 additions & 0 deletions imports/migrations/versions/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {availability} from '../../api/availability';
import {names} from '../../api/createTagCollection';
import type TagDocument from '../../api/tags/TagDocument';
import db from '../../backend/mongodb/db';
import {Eids} from '../../api/collection/eids';

Check warning on line 36 in imports/migrations/versions/v1.ts

View check run for this annotation

Codecov / codecov/patch

imports/migrations/versions/v1.ts#L36

Added line #L36 was not covered by tests

export default async () => {
// Check that all ids are strings
Expand Down Expand Up @@ -313,6 +314,47 @@ export default async () => {
await createSimpleUniqueIndex(Allergies, 'name');
await createSimpleUniqueIndex(Books, 'name');

await Eids.rawCollection().createIndex(
{
owner: 1,
// NOTE: Those are the fields that are the most likely to change.
'card.cardnumber': 1,
'card.chipnumber': 1,
// NOTE: If the ones above match, usually the rest will match too.
'xml.encoding': 1,
'xml.version': 1,
'eid.graphpersoversion': 1,
'eid.version': 1,
'card.carddata_appl_version': 1,
'card.documenttype': 1,
'card.validitydatebegin': 1,
'card.validitydateend': 1,
'card.deliverymunicipality': 1,
'certificates.authentication': 1,
'certificates.citizenca': 1,
'certificates.root': 1,
'certificates.rrn': 1,
'certificates.signing': 1,
'identity.nationality': 1,
'identity.nationalnumber': 1,
'identity.dateofbirth': 1,
'identity.placeofbirth': 1,
'identity.gender': 1,
'identity.specialstatus': 1,
'identity.name': 1,
'identity.firstname': 1,
'identity.middlenames': 1,
'identity.photo': 1,
'address.municipality': 1,
'address.streetandnumber': 1,
'address.zip': 1,
},
{
unique: true,
background: true,
},
);

await Books.rawCollection().createIndex(
{
owner: 1,
Expand Down

0 comments on commit 9b7a812

Please sign in to comment.