@photonhealth/client 2.0.0-rc.1 → 2.0.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +150 -5
- package/dist/analytics/lifecycleEvents.d.ts +29 -0
- package/dist/analytics/lifecycleEvents.d.ts.map +1 -0
- package/dist/analytics/lifecycleEvents.js +72 -0
- package/dist/analytics/lifecycleEvents.js.map +1 -0
- package/dist/api/client.d.ts +6 -1
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js +47 -33
- package/dist/api/client.js.map +1 -1
- package/dist/api/index.d.ts +2 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +2 -0
- package/dist/api/index.js.map +1 -1
- package/dist/api/me.d.ts +37 -0
- package/dist/api/me.d.ts.map +1 -0
- package/dist/api/me.js +6 -0
- package/dist/api/me.js.map +1 -0
- package/dist/api/orders.d.ts +1047 -0
- package/dist/api/orders.d.ts.map +1 -0
- package/dist/api/orders.js +7 -0
- package/dist/api/orders.js.map +1 -0
- package/dist/api/patient.d.ts +5 -2
- package/dist/api/patient.d.ts.map +1 -1
- package/dist/api/patient.js +1 -1
- package/dist/api/patient.js.map +1 -1
- package/dist/api/prescription.d.ts +7 -1
- package/dist/api/prescription.d.ts.map +1 -1
- package/dist/api/prescription.js +10 -1
- package/dist/api/prescription.js.map +1 -1
- package/dist/gql/gql.d.ts +11 -3
- package/dist/gql/gql.d.ts.map +1 -1
- package/dist/gql/gql.js +5 -3
- package/dist/gql/gql.js.map +1 -1
- package/dist/gql/graphql.d.ts +10507 -1563
- package/dist/gql/graphql.d.ts.map +1 -1
- package/dist/gql/graphql.js +2984 -435
- package/dist/gql/graphql.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/meta/version.d.ts +2 -0
- package/dist/meta/version.d.ts.map +1 -0
- package/dist/meta/version.js +2 -0
- package/dist/meta/version.js.map +1 -0
- package/dist/photon/analytics.d.ts +39 -0
- package/dist/photon/analytics.d.ts.map +1 -0
- package/dist/photon/analytics.js +91 -0
- package/dist/photon/analytics.js.map +1 -0
- package/dist/photon/auth.d.ts +9 -5
- package/dist/photon/auth.d.ts.map +1 -1
- package/dist/photon/auth.js +2 -6
- package/dist/photon/auth.js.map +1 -1
- package/dist/photon/choices.d.ts +1 -1
- package/dist/photon/choices.js +2 -2
- package/dist/photon/choices.js.map +1 -1
- package/dist/photon/environments.d.ts +2 -1
- package/dist/photon/environments.d.ts.map +1 -1
- package/dist/photon/environments.js +2 -1
- package/dist/photon/environments.js.map +1 -1
- package/dist/photon/index.d.ts +6 -3
- package/dist/photon/index.d.ts.map +1 -1
- package/dist/photon/index.js +2 -1
- package/dist/photon/index.js.map +1 -1
- package/dist/photon/machine.d.ts +28 -0
- package/dist/photon/machine.d.ts.map +1 -0
- package/dist/photon/machine.js +53 -0
- package/dist/photon/machine.js.map +1 -0
- package/dist/photon/map.d.ts +6 -2
- package/dist/photon/map.d.ts.map +1 -1
- package/dist/photon/map.js +133 -33
- package/dist/photon/map.js.map +1 -1
- package/dist/photon/order.d.ts +21 -1
- package/dist/photon/order.d.ts.map +1 -1
- package/dist/photon/order.js +79 -12
- package/dist/photon/order.js.map +1 -1
- package/dist/photon/orders.d.ts +5 -0
- package/dist/photon/orders.d.ts.map +1 -0
- package/dist/photon/orders.js +39 -0
- package/dist/photon/orders.js.map +1 -0
- package/dist/photon/patient.d.ts +8 -6
- package/dist/photon/patient.d.ts.map +1 -1
- package/dist/photon/patient.js +61 -30
- package/dist/photon/patient.js.map +1 -1
- package/dist/photon/photon.d.ts +39 -1
- package/dist/photon/photon.d.ts.map +1 -1
- package/dist/photon/photon.js +85 -5
- package/dist/photon/photon.js.map +1 -1
- package/dist/photon/prescription.d.ts +17 -4
- package/dist/photon/prescription.d.ts.map +1 -1
- package/dist/photon/prescription.js +48 -11
- package/dist/photon/prescription.js.map +1 -1
- package/dist/photon/session.d.ts +15 -10
- package/dist/photon/session.d.ts.map +1 -1
- package/dist/photon/session.js +5 -3
- package/dist/photon/session.js.map +1 -1
- package/dist/photon/types.d.ts +278 -46
- package/dist/photon/types.d.ts.map +1 -1
- package/dist/test-utils/msw.d.ts +2 -2
- package/dist/test-utils/msw.d.ts.map +1 -1
- package/dist/test-utils/msw.js +5 -5
- package/dist/test-utils/msw.js.map +1 -1
- package/dist/testing/{scriptedClient.d.ts → fakeNetworkApiClient.d.ts} +5 -4
- package/dist/testing/fakeNetworkApiClient.d.ts.map +1 -0
- package/dist/testing/{scriptedClient.js → fakeNetworkApiClient.js} +8 -4
- package/dist/testing/fakeNetworkApiClient.js.map +1 -0
- package/dist/testing/fixtures.d.ts +1047 -0
- package/dist/testing/fixtures.d.ts.map +1 -1
- package/dist/testing/fixtures.js +47 -13
- package/dist/testing/fixtures.js.map +1 -1
- package/dist/testing.d.ts +4 -4
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +4 -4
- package/dist/testing.js.map +1 -1
- package/package.json +2 -2
- package/src/analytics/lifecycleEvents.test.ts +417 -0
- package/src/analytics/lifecycleEvents.ts +155 -0
- package/src/api/client.ts +71 -46
- package/src/api/index.ts +2 -0
- package/src/api/me.ts +7 -0
- package/src/api/orders.ts +8 -0
- package/src/api/patient.ts +11 -2
- package/src/api/prescription.ts +18 -0
- package/src/gql/gql.ts +21 -9
- package/src/gql/graphql.ts +4280 -597
- package/src/index.ts +1 -1
- package/src/meta/version.test.ts +10 -0
- package/src/meta/version.ts +1 -0
- package/src/operations/fragments.graphql +166 -20
- package/src/operations/me.graphql +38 -0
- package/src/operations/orders.graphql +35 -0
- package/src/operations/patient.graphql +29 -3
- package/src/operations/prescription.graphql +20 -0
- package/src/photon/analytics.ts +129 -0
- package/src/photon/auth.ts +9 -10
- package/src/photon/choices.ts +2 -2
- package/src/photon/environments.ts +3 -1
- package/src/photon/index.ts +16 -2
- package/src/photon/machine.ts +90 -0
- package/src/photon/map.ts +151 -35
- package/src/photon/order.test.ts +142 -0
- package/src/photon/order.ts +92 -12
- package/src/photon/orders.ts +42 -0
- package/src/photon/patient.ts +73 -33
- package/src/photon/photon.ts +139 -6
- package/src/photon/prescription.ts +71 -12
- package/src/photon/session.ts +18 -9
- package/src/photon/types.ts +280 -26
- package/src/test-utils/msw.ts +16 -5
- package/src/testing/{scriptedClient.ts → fakeNetworkApiClient.ts} +8 -4
- package/src/testing/fixtures.ts +55 -13
- package/src/testing.ts +6 -3
- package/dist/testing/scriptedClient.d.ts.map +0 -1
- package/dist/testing/scriptedClient.js.map +0 -1
package/src/photon/order.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { PhotonError, refuse, toPhotonError } from "./errors.ts";
|
|
|
6
6
|
import { LiveRecord } from "./live.ts";
|
|
7
7
|
import { noChanges, toChanges, toOrder, toOrderCandidate, toPrescription } from "./map.ts";
|
|
8
8
|
import { Patient } from "./patient.ts";
|
|
9
|
+
import { readOrders } from "./orders.ts";
|
|
9
10
|
import { Prescription } from "./prescription.ts";
|
|
10
11
|
import type {
|
|
11
12
|
ChangeOption,
|
|
@@ -29,6 +30,12 @@ export interface OrderExtra {
|
|
|
29
30
|
* (this order's prescriptions go on it); nothing is saved until then.
|
|
30
31
|
*/
|
|
31
32
|
candidates: OrderCandidate[];
|
|
33
|
+
/**
|
|
34
|
+
* The patient's draft order with prescriptions on it, found once a new order has its patient (before any
|
|
35
|
+
* prescription has a record): what's on it, for the prescriber to decide. `continueDraft()` makes this order that one; `startNew()` empties it and
|
|
36
|
+
* starts over on it. Nothing is saved until then.
|
|
37
|
+
*/
|
|
38
|
+
draft: OrderRecord | null;
|
|
32
39
|
/**
|
|
33
40
|
* The server wouldn't send it until the patient can be reached (its reason). Cleared once the patient's
|
|
34
41
|
* phone, email or address changes.
|
|
@@ -63,12 +70,12 @@ type NewLine = { templateId?: string | null } | { treatment: { id: string } };
|
|
|
63
70
|
|
|
64
71
|
/** What makes a patient reachable — the contact request clears when this changes. */
|
|
65
72
|
const contactOf = (p: Patient | null) => {
|
|
66
|
-
const
|
|
73
|
+
const c = p?.state?.contact;
|
|
67
74
|
return JSON.stringify([
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
75
|
+
c?.phone ?? null,
|
|
76
|
+
c?.email ?? null,
|
|
77
|
+
c?.address?.street1 ?? null,
|
|
78
|
+
c?.address?.postalCode ?? null,
|
|
72
79
|
]);
|
|
73
80
|
};
|
|
74
81
|
|
|
@@ -76,7 +83,11 @@ const contactOf = (p: Patient | null) => {
|
|
|
76
83
|
const alertKey = (a: ScreeningAlert) =>
|
|
77
84
|
JSON.stringify([a.type, a.severity, a.description, a.involvedEntities.map((e) => e.id).sort()]);
|
|
78
85
|
|
|
79
|
-
/**
|
|
86
|
+
/** A new line nothing was written in: no record, no drug picked, no template, nothing entered. */
|
|
87
|
+
const untouched = (line: Prescription) =>
|
|
88
|
+
!line.id && !line.snapshot.pick && !line.templateId && Object.keys(line.entered).length === 0;
|
|
89
|
+
|
|
90
|
+
/** network-api won't start another order for a patient whose last one was sent. */
|
|
80
91
|
const alreadySent = () =>
|
|
81
92
|
refuse("This patient's last order was already sent, and Photon can't start another for them yet.");
|
|
82
93
|
|
|
@@ -99,6 +110,8 @@ export class Order extends LiveRecord<OrderRecord, OrderExtra> {
|
|
|
99
110
|
* are, and become lines when it answers.
|
|
100
111
|
*/
|
|
101
112
|
#refs: { externalId: string }[] = [];
|
|
113
|
+
/** The patient whose draft orders were looked for: once per patient. */
|
|
114
|
+
#lookedFor: string | null = null;
|
|
102
115
|
/** Held before the record, so a draft created once its lines are ready includes the pharmacy input. */
|
|
103
116
|
#pharmacy: OrderPharmacyInput | undefined;
|
|
104
117
|
|
|
@@ -107,7 +120,11 @@ export class Order extends LiveRecord<OrderRecord, OrderExtra> {
|
|
|
107
120
|
record: OrderRecord | null,
|
|
108
121
|
extra: { patient?: Patient | null; prescriptions?: Prescription[]; changes?: Changes } = {},
|
|
109
122
|
) {
|
|
110
|
-
super(
|
|
123
|
+
super(
|
|
124
|
+
record,
|
|
125
|
+
{ patient: null, prescriptions: [], candidates: [], draft: null, contactRequest: null },
|
|
126
|
+
extra.changes,
|
|
127
|
+
);
|
|
111
128
|
this.#ctx = ctx;
|
|
112
129
|
this.#setLines(extra.prescriptions ?? []);
|
|
113
130
|
if (extra.patient) this.#follow(extra.patient);
|
|
@@ -249,12 +266,38 @@ export class Order extends LiveRecord<OrderRecord, OrderExtra> {
|
|
|
249
266
|
}
|
|
250
267
|
|
|
251
268
|
/**
|
|
252
|
-
* One of `candidates`: this becomes that draft order (read), and this order's prescriptions go on it
|
|
269
|
+
* One of `candidates`: this becomes that draft order (read), and this order's prescriptions go on it — a line
|
|
270
|
+
* nothing was written in yet gives way to the draft's, so it can be sent as it is.
|
|
253
271
|
*/
|
|
254
272
|
pick(orderId: string): Promise<{ changes: Changes }> {
|
|
255
273
|
return this.run("loading", async () => {
|
|
256
274
|
const result = await this.#settle(await getOrder(this.#ctx.client, orderId));
|
|
257
|
-
this.patch({ candidates: [] });
|
|
275
|
+
this.patch({ candidates: [], draft: null });
|
|
276
|
+
if (this.prescriptions.some((l) => l.id)) this.#setLines(this.prescriptions.filter((l) => !untouched(l)));
|
|
277
|
+
return result;
|
|
278
|
+
}).then(() => this.#save());
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** Go on with the patient's draft order (`draft`): this becomes that order, and what's added goes on it. */
|
|
282
|
+
continueDraft(): Promise<{ changes: Changes }> {
|
|
283
|
+
if (!this.snapshot.draft) return Promise.reject(new PhotonError("invalid", "This order has no draft to continue."));
|
|
284
|
+
return this.pick(this.snapshot.draft.id);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Start over instead of continuing the patient's draft order (`draft`): its prescriptions come off it (taken off,
|
|
289
|
+
* not canceled) and this order is that draft, holding only what's written here. network-api continues a patient's
|
|
290
|
+
* draft whenever an order is saved, so emptying it is how a new order starts. Turned down, the draft is still
|
|
291
|
+
* there to decide on (its reason in `changes`).
|
|
292
|
+
*/
|
|
293
|
+
startNew(): Promise<{ changes: Changes }> {
|
|
294
|
+
const draft = this.snapshot.draft;
|
|
295
|
+
if (!draft) return Promise.resolve({ changes: this.changes });
|
|
296
|
+
return this.run("saving", async () => {
|
|
297
|
+
const result = await this.#settle(
|
|
298
|
+
await updateOrder(this.#ctx.client, draft.id, { prescriptions: { none: true } }),
|
|
299
|
+
);
|
|
300
|
+
if (this.id) this.patch({ draft: null });
|
|
258
301
|
return result;
|
|
259
302
|
}).then(() => this.#save());
|
|
260
303
|
}
|
|
@@ -351,6 +394,8 @@ export class Order extends LiveRecord<OrderRecord, OrderExtra> {
|
|
|
351
394
|
const nothing = { changes: this.changes };
|
|
352
395
|
if (this.final && !pharmacy) return Promise.resolve(nothing);
|
|
353
396
|
if (!this.id) {
|
|
397
|
+
if (this.#toLookFor())
|
|
398
|
+
return this.run("loading", () => this.#findDraft()).then(() => this.#save(remove, pharmacy));
|
|
354
399
|
if (!this.#readyToSync()) return Promise.resolve(nothing);
|
|
355
400
|
// Queued behind other calls: by its turn, another save may have made the order — then just add to it.
|
|
356
401
|
return this.run("saving", async () =>
|
|
@@ -372,10 +417,45 @@ export class Order extends LiveRecord<OrderRecord, OrderExtra> {
|
|
|
372
417
|
Boolean(this.patient?.id) &&
|
|
373
418
|
(this.prescriptions.some((line) => line.id) || this.#refs.length > 0) &&
|
|
374
419
|
!this.snapshot.candidates.length &&
|
|
420
|
+
!this.snapshot.draft &&
|
|
375
421
|
this.#declined !== this.#draftKey()
|
|
376
422
|
);
|
|
377
423
|
}
|
|
378
424
|
|
|
425
|
+
/** A new order's patient, not looked for yet: nothing on this order has a record that would decide which order. */
|
|
426
|
+
#toLookFor(): string | null {
|
|
427
|
+
const id = this.patient?.id ?? null;
|
|
428
|
+
if (!id || this.id || this.#lookedFor === id || this.#refs.length || this.prescriptions.some((l) => l.id))
|
|
429
|
+
return null;
|
|
430
|
+
return id;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* The patient's draft orders (the read-only Orders query): one is `draft`, several are `candidates`. A read that
|
|
435
|
+
* fails leaves the order as it was — the server's sync still continues a draft.
|
|
436
|
+
*/
|
|
437
|
+
async #findDraft(): Promise<{ changes: Changes }> {
|
|
438
|
+
const patientId = this.#toLookFor();
|
|
439
|
+
if (!patientId) return { changes: this.changes };
|
|
440
|
+
this.#lookedFor = patientId;
|
|
441
|
+
const found = await readOrders(this.#ctx.client, { filter: { patient: { id: patientId }, state: "DRAFT" } }).catch(
|
|
442
|
+
() => null,
|
|
443
|
+
);
|
|
444
|
+
// Moved on while it read: another patient, or a record now decides.
|
|
445
|
+
const decided = Boolean(this.id || this.#refs.length || this.prescriptions.some((l) => l.id));
|
|
446
|
+
if (!found || decided || this.patient?.id !== patientId) return { changes: this.changes };
|
|
447
|
+
// A draft with nothing on it isn't worth asking about: the server's sync continues it, as a new order would start.
|
|
448
|
+
const drafts = found.orders
|
|
449
|
+
.filter((order) => order.prescriptions.length > 0)
|
|
450
|
+
.map(({ routablePharmacies: _, ...order }) => order);
|
|
451
|
+
if (drafts.length === 1) this.patch({ draft: drafts[0]! });
|
|
452
|
+
else if (drafts.length > 1)
|
|
453
|
+
this.patch({
|
|
454
|
+
candidates: drafts.map((o) => ({ id: o.id, pharmacy: o.pharmacy, prescriptions: o.prescriptions })),
|
|
455
|
+
});
|
|
456
|
+
return { changes: this.changes };
|
|
457
|
+
}
|
|
458
|
+
|
|
379
459
|
/** SyncOrder with the patient and every prescription that has a record: the patient's draft order, or a new one. */
|
|
380
460
|
async #syncDraft(pharmacy?: OrderPharmacyInput): Promise<{ changes: Changes }> {
|
|
381
461
|
const key = this.#draftKey();
|
|
@@ -442,7 +522,7 @@ export class Order extends LiveRecord<OrderRecord, OrderExtra> {
|
|
|
442
522
|
// drug waiting for its patient would be created for this one.
|
|
443
523
|
if (switching) {
|
|
444
524
|
this.#contactAtRequest = null;
|
|
445
|
-
this.patch({ contactRequest: null, candidates: [] });
|
|
525
|
+
this.patch({ contactRequest: null, candidates: [], draft: null });
|
|
446
526
|
this.#setLines([this.#newLine(null, patient?.id ?? null)]);
|
|
447
527
|
}
|
|
448
528
|
this.#follow(patient);
|
|
@@ -460,7 +540,7 @@ export class Order extends LiveRecord<OrderRecord, OrderExtra> {
|
|
|
460
540
|
const line = this.#newLine("templateId" in rx ? (rx.templateId ?? null) : null);
|
|
461
541
|
const drug = "treatment" in rx ? { treatment: { id: rx.treatment.id } } : {};
|
|
462
542
|
// A drug by id is looked up now; a template line is drafted once it has its patient (network-api doesn't expand
|
|
463
|
-
// a template without one
|
|
543
|
+
// a template without one).
|
|
464
544
|
if (drug.treatment) void line.sync(drug).catch(() => {}); // a failure lands on the line
|
|
465
545
|
return line;
|
|
466
546
|
}
|
|
@@ -505,7 +585,7 @@ export class Order extends LiveRecord<OrderRecord, OrderExtra> {
|
|
|
505
585
|
/** The server won't send it until the patient can be reached: a missing phone, email or address (its words). */
|
|
506
586
|
#askForContact(changes: Changes, message?: string) {
|
|
507
587
|
const contact = changes.unapplied.find(
|
|
508
|
-
(c) => c.key.startsWith("patient.
|
|
588
|
+
(c) => c.key.startsWith("patient.contact.") || asksForAddress({ applied: [], unapplied: [c] }),
|
|
509
589
|
);
|
|
510
590
|
const reason = message ?? contact?.reason ?? null;
|
|
511
591
|
if (!reason && !contact) return;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { orders, type NetworkClient } from "../api/index.ts";
|
|
2
|
+
import { guarded } from "./errors.ts";
|
|
3
|
+
import { toChoice, toOrder } from "./map.ts";
|
|
4
|
+
import type { OrdersInput, OrdersResult } from "./types.ts";
|
|
5
|
+
|
|
6
|
+
/** Map the query directly: no live-object hydration, follow-up mutations, or local workflow effects. */
|
|
7
|
+
export async function readOrders(client: NetworkClient, input: OrdersInput = {}): Promise<OrdersResult> {
|
|
8
|
+
const result = await guarded(() =>
|
|
9
|
+
orders(client, {
|
|
10
|
+
filter: input.filter,
|
|
11
|
+
limit: input.limit,
|
|
12
|
+
pharmacy: input.routablePharmacies?.pharmacy,
|
|
13
|
+
page: input.routablePharmacies?.page,
|
|
14
|
+
includePharmacies: input.routablePharmacies !== undefined,
|
|
15
|
+
}),
|
|
16
|
+
);
|
|
17
|
+
if (result.__typename === "AmbiguousPatientMatch")
|
|
18
|
+
return {
|
|
19
|
+
orders: [],
|
|
20
|
+
candidates: result.candidates.map((c) => ({
|
|
21
|
+
id: c.id,
|
|
22
|
+
firstName: c.firstName,
|
|
23
|
+
lastName: c.lastName,
|
|
24
|
+
dateOfBirth: c.dateOfBirth,
|
|
25
|
+
})),
|
|
26
|
+
reason: result.reason,
|
|
27
|
+
};
|
|
28
|
+
return {
|
|
29
|
+
orders: result.orders.map((o) => ({
|
|
30
|
+
...toOrder(o),
|
|
31
|
+
routablePharmacies: o.routablePharmacies
|
|
32
|
+
? {
|
|
33
|
+
options: o.routablePharmacies.options.map(toChoice),
|
|
34
|
+
totalCount: o.routablePharmacies.totalCount,
|
|
35
|
+
nextOffset: o.routablePharmacies.nextOffset,
|
|
36
|
+
}
|
|
37
|
+
: null,
|
|
38
|
+
})),
|
|
39
|
+
candidates: [],
|
|
40
|
+
reason: null,
|
|
41
|
+
};
|
|
42
|
+
}
|
package/src/photon/patient.ts
CHANGED
|
@@ -7,6 +7,9 @@ import { noChanges, toChanges, toPatient } from "./map.ts";
|
|
|
7
7
|
import type { ChangeOption, Changes, PatientCandidate, PatientInput, PatientRecord } from "./types.ts";
|
|
8
8
|
|
|
9
9
|
type Demographic = NonNullable<PatientInput["demographic"]>;
|
|
10
|
+
type Contact = NonNullable<PatientInput["contact"]>;
|
|
11
|
+
/** sent before the record */
|
|
12
|
+
type Known = Pick<PatientInput, "demographic" | "contact" | "preferredPharmacy">;
|
|
10
13
|
type Clinical = NonNullable<PatientInput["clinical"]>;
|
|
11
14
|
|
|
12
15
|
export interface PatientExtra {
|
|
@@ -14,7 +17,7 @@ export interface PatientExtra {
|
|
|
14
17
|
candidates: PatientCandidate[];
|
|
15
18
|
/**
|
|
16
19
|
* Names for clinical entries the server returns with name = id (allergies, diagnoses on boson): what the
|
|
17
|
-
* prescriber typed or picked, by entry id
|
|
20
|
+
* prescriber typed or picked, by entry id.
|
|
18
21
|
*/
|
|
19
22
|
pickedNames: Readonly<Record<string, string>>;
|
|
20
23
|
}
|
|
@@ -33,26 +36,56 @@ const validDob = (dob?: string) => Boolean(dob && /^\d{4}-\d{2}-\d{2}$/.test(dob
|
|
|
33
36
|
const trimmed = (v?: string) => v?.trim() || undefined;
|
|
34
37
|
|
|
35
38
|
/** What's synced before the record: trimmed, empty fields dropped, a date of birth only when it's a whole date. */
|
|
36
|
-
function cleanDemographic(d: Demographic): Demographic {
|
|
39
|
+
function cleanDemographic(d: Demographic = {}): Demographic {
|
|
37
40
|
const first = trimmed(d.name?.first);
|
|
38
41
|
const last = trimmed(d.name?.last);
|
|
39
|
-
|
|
42
|
+
return {
|
|
40
43
|
...(first || last ? { name: { first, last } } : {}),
|
|
41
44
|
...(validDob(d.dateOfBirth) ? { dateOfBirth: d.dateOfBirth } : {}),
|
|
42
45
|
...(d.sex ? { sex: d.sex } : {}),
|
|
43
46
|
...(trimmed(d.gender) ? { gender: trimmed(d.gender) } : {}),
|
|
44
|
-
...(trimmed(d.phone) ? { phone: trimmed(d.phone) } : {}),
|
|
45
|
-
...(trimmed(d.email) ? { email: trimmed(d.email) } : {}),
|
|
46
|
-
...(d.address ? { address: d.address } : {}),
|
|
47
47
|
};
|
|
48
|
-
return clean;
|
|
49
48
|
}
|
|
50
49
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
50
|
+
function cleanContact(c: Contact = {}): Contact {
|
|
51
|
+
return {
|
|
52
|
+
...(trimmed(c.phone) ? { phone: trimmed(c.phone) } : {}),
|
|
53
|
+
...(trimmed(c.email) ? { email: trimmed(c.email) } : {}),
|
|
54
|
+
...(c.address ? { address: c.address } : {}),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const nonEmpty = <T extends object>(v: T) => (Object.keys(v).length ? v : undefined);
|
|
59
|
+
|
|
60
|
+
/** drops empty groups */
|
|
61
|
+
function cleanKnown(k: Known): Known {
|
|
62
|
+
const demographic = nonEmpty(cleanDemographic(k.demographic));
|
|
63
|
+
const contact = nonEmpty(cleanContact(k.contact));
|
|
64
|
+
return {
|
|
65
|
+
...(demographic ? { demographic } : {}),
|
|
66
|
+
...(contact ? { contact } : {}),
|
|
67
|
+
...(k.preferredPharmacy ? { preferredPharmacy: k.preferredPharmacy } : {}),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const mergeKnown = (a: Known, b: Known): Known => ({
|
|
72
|
+
preferredPharmacy: b.preferredPharmacy ?? a.preferredPharmacy,
|
|
73
|
+
demographic: {
|
|
74
|
+
...a.demographic,
|
|
75
|
+
...b.demographic,
|
|
76
|
+
...(a.demographic?.name || b.demographic?.name ? { name: { ...a.demographic?.name, ...b.demographic?.name } } : {}),
|
|
77
|
+
},
|
|
78
|
+
contact: {
|
|
79
|
+
...a.contact,
|
|
80
|
+
...b.contact,
|
|
81
|
+
...(a.contact?.address || b.contact?.address ? { address: { ...a.contact?.address, ...b.contact?.address } } : {}),
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const knownOf = ({ demographic, contact, preferredPharmacy }: Omit<PatientInput, "id">): Known => ({
|
|
86
|
+
demographic,
|
|
87
|
+
contact,
|
|
88
|
+
preferredPharmacy,
|
|
56
89
|
});
|
|
57
90
|
|
|
58
91
|
const clinicalIds = (p: PatientRecord | null) =>
|
|
@@ -63,7 +96,7 @@ const clinicalIds = (p: PatientRecord | null) =>
|
|
|
63
96
|
);
|
|
64
97
|
|
|
65
98
|
/**
|
|
66
|
-
* A patient — from an id, from a record the host has, or not yet known. `sync({ demographic })` sends what
|
|
99
|
+
* A patient — from an id, from a record the host has, or not yet known. `sync({ demographic, contact })` sends what
|
|
67
100
|
* you know, and the server decides — the patient (found, or created when nothing matches and it has enough),
|
|
68
101
|
* candidates (pick one), or nothing yet (`changes` says what's missing). Keep adding what you know until it
|
|
69
102
|
* has an id; then `update(fields)` changes that patient. The client never decides "now create".
|
|
@@ -74,7 +107,7 @@ const clinicalIds = (p: PatientRecord | null) =>
|
|
|
74
107
|
export class Patient extends LiveRecord<PatientRecord, PatientExtra> {
|
|
75
108
|
readonly #ctx: Context;
|
|
76
109
|
/** Everything synced so far, before the record — each sync adds to it. */
|
|
77
|
-
#known:
|
|
110
|
+
#known: Known = {};
|
|
78
111
|
#lastQuery: string | null = null;
|
|
79
112
|
/** The search in flight: any newer query aborts it. */
|
|
80
113
|
#searching: AbortController | null = null;
|
|
@@ -83,7 +116,7 @@ export class Patient extends LiveRecord<PatientRecord, PatientExtra> {
|
|
|
83
116
|
#declared = new Set<string>();
|
|
84
117
|
/**
|
|
85
118
|
* Lists recorded as "none" here: their record says `[]`, not null (not recorded). network-api can't read a
|
|
86
|
-
* "none" back for these yet
|
|
119
|
+
* "none" back for these yet — so only this object knows, until an entry is added to the list.
|
|
87
120
|
*/
|
|
88
121
|
#none = new Set<NoneList>();
|
|
89
122
|
|
|
@@ -108,7 +141,7 @@ export class Patient extends LiveRecord<PatientRecord, PatientExtra> {
|
|
|
108
141
|
* What's been sent to find or add the patient, before the record — e.g. a host's details the server hasn't
|
|
109
142
|
* matched yet (not enough to add them). A form starts from it.
|
|
110
143
|
*/
|
|
111
|
-
get known(): Readonly<
|
|
144
|
+
get known(): Readonly<Known> {
|
|
112
145
|
return this.#known;
|
|
113
146
|
}
|
|
114
147
|
|
|
@@ -118,17 +151,18 @@ export class Patient extends LiveRecord<PatientRecord, PatientExtra> {
|
|
|
118
151
|
*/
|
|
119
152
|
sync(input: Omit<PatientInput, "id">): Promise<{ changes: Changes }> {
|
|
120
153
|
if (this.id)
|
|
121
|
-
return
|
|
154
|
+
return Object.values(input).some(Boolean)
|
|
122
155
|
? this.#write("sync", input)
|
|
123
156
|
: Promise.resolve({ changes: this.changes });
|
|
124
157
|
if (input.clinical)
|
|
125
158
|
return Promise.reject(new PhotonError("invalid", "Clinical changes need the patient's id: sync them first."));
|
|
126
|
-
return this.#match(input
|
|
159
|
+
return this.#match(knownOf(input));
|
|
127
160
|
}
|
|
128
161
|
|
|
129
162
|
/**
|
|
130
|
-
* UpdatePatient: change this patient — only what's given: demographics
|
|
131
|
-
* (`{ allergies: { add: [{ name }] } }`, `{ allergies: { none: true } }` for no known
|
|
163
|
+
* UpdatePatient: change this patient — only what's given: demographics, contact, benefits, preferred pharmacy
|
|
164
|
+
* and/or clinical lists (`{ allergies: { add: [{ name }] } }`, `{ allergies: { none: true } }` for no known
|
|
165
|
+
* allergies). Needs its id.
|
|
132
166
|
*/
|
|
133
167
|
update(input: Omit<PatientInput, "id">): Promise<{ changes: Changes }> {
|
|
134
168
|
if (!this.id)
|
|
@@ -144,7 +178,10 @@ export class Patient extends LiveRecord<PatientRecord, PatientExtra> {
|
|
|
144
178
|
*/
|
|
145
179
|
search(input: Omit<PatientInput, "id">): Promise<{ changes: Changes }> {
|
|
146
180
|
if (this.id) return Promise.resolve({ changes: this.changes });
|
|
147
|
-
const { sex: _, ...
|
|
181
|
+
const { demographic: { sex: _, ...demographic } = {}, contact } = cleanKnown(
|
|
182
|
+
mergeKnown(this.#known, knownOf(input)),
|
|
183
|
+
);
|
|
184
|
+
const query = { ...(nonEmpty(demographic) ? { demographic } : {}), ...(contact ? { contact } : {}) };
|
|
148
185
|
const key = JSON.stringify(query);
|
|
149
186
|
if (!Object.keys(query).length || key === this.#lastQuery) return Promise.resolve({ changes: this.changes });
|
|
150
187
|
this.#lastQuery = key;
|
|
@@ -155,7 +192,7 @@ export class Patient extends LiveRecord<PatientRecord, PatientExtra> {
|
|
|
155
192
|
if (key !== this.#lastQuery || this.id) return { changes: this.changes };
|
|
156
193
|
let outcome: PatientOutcome;
|
|
157
194
|
try {
|
|
158
|
-
outcome = await syncPatient(this.#ctx.client,
|
|
195
|
+
outcome = await syncPatient(this.#ctx.client, query, search.signal);
|
|
159
196
|
} catch (e) {
|
|
160
197
|
if (search.signal.aborted) return { changes: this.changes }; // a newer query answers instead
|
|
161
198
|
throw e;
|
|
@@ -185,6 +222,8 @@ export class Patient extends LiveRecord<PatientRecord, PatientExtra> {
|
|
|
185
222
|
|
|
186
223
|
/** A choice the server asked for (e.g. which penicillin): sent again with the pick applied to the last patch. */
|
|
187
224
|
choose(option: ChangeOption): Promise<{ changes: Changes }> {
|
|
225
|
+
if (option.operation === "patient" && /^(demographic|contact|benefits|preferredPharmacy)\./.test(option.path ?? ""))
|
|
226
|
+
return this.update(withChoice<Omit<PatientInput, "id">>({}, option));
|
|
188
227
|
if (option.id && option.displayName)
|
|
189
228
|
this.patch({ pickedNames: { ...this.snapshot.pickedNames, [option.id]: option.displayName } });
|
|
190
229
|
const rooted = option.path?.startsWith("clinical.");
|
|
@@ -229,9 +268,9 @@ export class Patient extends LiveRecord<PatientRecord, PatientExtra> {
|
|
|
229
268
|
}).then((result) => this.#takeDeclared(result));
|
|
230
269
|
}
|
|
231
270
|
|
|
232
|
-
#match(
|
|
233
|
-
this.#known =
|
|
234
|
-
const query =
|
|
271
|
+
#match(known: Known): Promise<{ changes: Changes }> {
|
|
272
|
+
this.#known = mergeKnown(this.#known, known);
|
|
273
|
+
const query = cleanKnown(this.#known);
|
|
235
274
|
if (!Object.keys(query).length) return Promise.resolve({ changes: this.changes });
|
|
236
275
|
const key = JSON.stringify(query);
|
|
237
276
|
// The same query gets the same answer: keep it (blur and a debounce often ask twice).
|
|
@@ -241,7 +280,7 @@ export class Patient extends LiveRecord<PatientRecord, PatientExtra> {
|
|
|
241
280
|
return this.run("loading", async () => {
|
|
242
281
|
// A newer query was made while this one waited: it answers instead.
|
|
243
282
|
if (key !== this.#lastQuery || this.id) return { changes: this.changes };
|
|
244
|
-
const outcome = await syncPatient(this.#ctx.client,
|
|
283
|
+
const outcome = await syncPatient(this.#ctx.client, query);
|
|
245
284
|
// Answered after a newer query was made: only a patient found (or added) still counts.
|
|
246
285
|
if (key !== this.#lastQuery && !(outcome.kind === "ok" && outcome.patient)) return { changes: this.changes };
|
|
247
286
|
if (outcome.kind === "ambiguous") {
|
|
@@ -258,14 +297,15 @@ export class Patient extends LiveRecord<PatientRecord, PatientExtra> {
|
|
|
258
297
|
* doesn't have — fill in, never overwrite.
|
|
259
298
|
*/
|
|
260
299
|
async #fillMissingContact(result: { changes: Changes }) {
|
|
261
|
-
|
|
262
|
-
|
|
300
|
+
if (!this.id || !this.state) return result;
|
|
301
|
+
const had = this.state.contact;
|
|
302
|
+
const sent = this.#known.contact;
|
|
263
303
|
const missing = {
|
|
264
|
-
...(
|
|
265
|
-
...(
|
|
304
|
+
...(sent?.phone && !had?.phone ? { phone: trimmed(sent.phone) } : {}),
|
|
305
|
+
...(sent?.email && !had?.email ? { email: trimmed(sent.email) } : {}),
|
|
266
306
|
};
|
|
267
307
|
this.#known = {};
|
|
268
|
-
return Object.keys(missing).length ? this.update({
|
|
308
|
+
return Object.keys(missing).length ? this.update({ contact: missing }) : result;
|
|
269
309
|
}
|
|
270
310
|
|
|
271
311
|
/** A choice the server declared (one option, an exact match) or a clinical match it is sure of is taken without asking — once each. */
|
|
@@ -297,7 +337,7 @@ export class Patient extends LiveRecord<PatientRecord, PatientExtra> {
|
|
|
297
337
|
const patches = {
|
|
298
338
|
medications: input.clinical?.medications,
|
|
299
339
|
diagnoses: input.clinical?.diagnoses,
|
|
300
|
-
benefits: input.
|
|
340
|
+
benefits: input.benefits,
|
|
301
341
|
};
|
|
302
342
|
for (const [list, patch] of Object.entries(patches) as [NoneList, typeof patches.medications][]) {
|
|
303
343
|
if (patch?.none) this.#none.add(list);
|
|
@@ -311,7 +351,7 @@ export class Patient extends LiveRecord<PatientRecord, PatientExtra> {
|
|
|
311
351
|
const none = <T>(list: T[] | null, name: NoneList): T[] | null => list ?? (this.#none.has(name) ? [] : null);
|
|
312
352
|
return {
|
|
313
353
|
...p,
|
|
314
|
-
|
|
354
|
+
benefits: none(p.benefits, "benefits"),
|
|
315
355
|
clinical: p.clinical && {
|
|
316
356
|
...p.clinical,
|
|
317
357
|
medications: none(p.clinical.medications, "medications"),
|