@photonhealth/client 0.0.0-stage → 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/LICENSE +15 -0
- package/README.md +234 -2
- 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/changes/applyOption.d.ts +41 -0
- package/dist/api/changes/applyOption.d.ts.map +1 -0
- package/dist/api/changes/applyOption.js +76 -0
- package/dist/api/changes/applyOption.js.map +1 -0
- package/dist/api/changes/classifyChanges.d.ts +9 -0
- package/dist/api/changes/classifyChanges.d.ts.map +1 -0
- package/dist/api/changes/classifyChanges.js +2 -0
- package/dist/api/changes/classifyChanges.js.map +1 -0
- package/dist/api/changes/index.d.ts +3 -0
- package/dist/api/changes/index.d.ts.map +1 -0
- package/dist/api/changes/index.js +3 -0
- package/dist/api/changes/index.js.map +1 -0
- package/dist/api/client.d.ts +48 -0
- package/dist/api/client.d.ts.map +1 -0
- package/dist/api/client.js +91 -0
- package/dist/api/client.js.map +1 -0
- package/dist/api/health.d.ts +4 -0
- package/dist/api/health.d.ts.map +1 -0
- package/dist/api/health.js +6 -0
- package/dist/api/health.js.map +1 -0
- package/dist/api/index.d.ts +24 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +23 -0
- package/dist/api/index.js.map +1 -0
- 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/order.d.ts +33 -0
- package/dist/api/order.d.ts.map +1 -0
- package/dist/api/order.js +33 -0
- package/dist/api/order.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 +36 -0
- package/dist/api/patient.d.ts.map +1 -0
- package/dist/api/patient.js +33 -0
- package/dist/api/patient.js.map +1 -0
- package/dist/api/prescription.d.ts +37 -0
- package/dist/api/prescription.d.ts.map +1 -0
- package/dist/api/prescription.js +39 -0
- package/dist/api/prescription.js.map +1 -0
- package/dist/api/results.d.ts +30 -0
- package/dist/api/results.d.ts.map +1 -0
- package/dist/api/results.js +26 -0
- package/dist/api/results.js.map +1 -0
- package/dist/gql/gql.d.ts +29 -0
- package/dist/gql/gql.d.ts.map +1 -0
- package/dist/gql/gql.js +15 -0
- package/dist/gql/gql.js.map +1 -0
- package/dist/gql/graphql.d.ts +18618 -0
- package/dist/gql/graphql.d.ts.map +1 -0
- package/dist/gql/graphql.js +6579 -0
- package/dist/gql/graphql.js.map +1 -0
- package/dist/gql/index.d.ts +2 -0
- package/dist/gql/index.d.ts.map +1 -0
- package/dist/gql/index.js +3 -0
- package/dist/gql/index.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- 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 +52 -0
- package/dist/photon/auth.d.ts.map +1 -0
- package/dist/photon/auth.js +30 -0
- package/dist/photon/auth.js.map +1 -0
- package/dist/photon/choices.d.ts +44 -0
- package/dist/photon/choices.d.ts.map +1 -0
- package/dist/photon/choices.js +58 -0
- package/dist/photon/choices.js.map +1 -0
- package/dist/photon/context.d.ts +29 -0
- package/dist/photon/context.d.ts.map +1 -0
- package/dist/photon/context.js +2 -0
- package/dist/photon/context.js.map +1 -0
- package/dist/photon/environments.d.ts +27 -0
- package/dist/photon/environments.d.ts.map +1 -0
- package/dist/photon/environments.js +27 -0
- package/dist/photon/environments.js.map +1 -0
- package/dist/photon/errors.d.ts +26 -0
- package/dist/photon/errors.d.ts.map +1 -0
- package/dist/photon/errors.js +42 -0
- package/dist/photon/errors.js.map +1 -0
- package/dist/photon/index.d.ts +26 -0
- package/dist/photon/index.d.ts.map +1 -0
- package/dist/photon/index.js +12 -0
- package/dist/photon/index.js.map +1 -0
- package/dist/photon/inputs.d.ts +7 -0
- package/dist/photon/inputs.d.ts.map +1 -0
- package/dist/photon/inputs.js +25 -0
- package/dist/photon/inputs.js.map +1 -0
- package/dist/photon/links.d.ts +68 -0
- package/dist/photon/links.d.ts.map +1 -0
- package/dist/photon/links.js +187 -0
- package/dist/photon/links.js.map +1 -0
- package/dist/photon/live.d.ts +62 -0
- package/dist/photon/live.d.ts.map +1 -0
- package/dist/photon/live.js +107 -0
- package/dist/photon/live.js.map +1 -0
- 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 +14 -0
- package/dist/photon/map.d.ts.map +1 -0
- package/dist/photon/map.js +311 -0
- package/dist/photon/map.js.map +1 -0
- package/dist/photon/order.d.ts +168 -0
- package/dist/photon/order.d.ts.map +1 -0
- package/dist/photon/order.js +558 -0
- package/dist/photon/order.js.map +1 -0
- 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 +80 -0
- package/dist/photon/patient.d.ts.map +1 -0
- package/dist/photon/patient.js +347 -0
- package/dist/photon/patient.js.map +1 -0
- package/dist/photon/photon.d.ts +188 -0
- package/dist/photon/photon.d.ts.map +1 -0
- package/dist/photon/photon.js +395 -0
- package/dist/photon/photon.js.map +1 -0
- package/dist/photon/prescription.d.ts +145 -0
- package/dist/photon/prescription.d.ts.map +1 -0
- package/dist/photon/prescription.js +418 -0
- package/dist/photon/prescription.js.map +1 -0
- package/dist/photon/session.d.ts +89 -0
- package/dist/photon/session.d.ts.map +1 -0
- package/dist/photon/session.js +352 -0
- package/dist/photon/session.js.map +1 -0
- package/dist/photon/types.d.ts +643 -0
- package/dist/photon/types.d.ts.map +1 -0
- package/dist/photon/types.js +7 -0
- package/dist/photon/types.js.map +1 -0
- package/dist/test-utils/msw.d.ts +23 -0
- package/dist/test-utils/msw.d.ts.map +1 -0
- package/dist/test-utils/msw.js +51 -0
- package/dist/test-utils/msw.js.map +1 -0
- package/dist/testing/fakeNetworkApiClient.d.ts +39 -0
- package/dist/testing/fakeNetworkApiClient.d.ts.map +1 -0
- package/dist/testing/fakeNetworkApiClient.js +56 -0
- package/dist/testing/fakeNetworkApiClient.js.map +1 -0
- package/dist/testing/fixtures.d.ts +1098 -0
- package/dist/testing/fixtures.d.ts.map +1 -0
- package/dist/testing/fixtures.js +200 -0
- package/dist/testing/fixtures.js.map +1 -0
- package/dist/testing.d.ts +11 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +10 -0
- package/dist/testing.js.map +1 -0
- package/package.json +67 -4
- package/src/analytics/lifecycleEvents.test.ts +417 -0
- package/src/analytics/lifecycleEvents.ts +155 -0
- package/src/api/changes/applyOption.ts +103 -0
- package/src/api/changes/classifyChanges.ts +10 -0
- package/src/api/changes/index.ts +2 -0
- package/src/api/client.ts +149 -0
- package/src/api/health.ts +7 -0
- package/src/api/index.ts +24 -0
- package/src/api/me.ts +7 -0
- package/src/api/order.ts +64 -0
- package/src/api/orders.ts +8 -0
- package/src/api/patient.ts +67 -0
- package/src/api/prescription.ts +86 -0
- package/src/api/results.ts +45 -0
- package/src/gql/gql.ts +68 -0
- package/src/gql/graphql.ts +9027 -0
- package/src/gql/index.ts +2 -0
- package/src/index.ts +14 -0
- package/src/meta/version.test.ts +10 -0
- package/src/meta/version.ts +1 -0
- package/src/operations/fragments.graphql +497 -0
- package/src/operations/health.graphql +4 -0
- package/src/operations/me.graphql +38 -0
- package/src/operations/order.graphql +42 -0
- package/src/operations/orders.graphql +35 -0
- package/src/operations/patient.graphql +62 -0
- package/src/operations/prescription.graphql +90 -0
- package/src/photon/analytics.ts +129 -0
- package/src/photon/auth.ts +69 -0
- package/src/photon/choices.ts +79 -0
- package/src/photon/context.ts +30 -0
- package/src/photon/environments.ts +49 -0
- package/src/photon/errors.ts +56 -0
- package/src/photon/index.ts +49 -0
- package/src/photon/inputs.ts +24 -0
- package/src/photon/links.ts +197 -0
- package/src/photon/live.ts +135 -0
- package/src/photon/machine.ts +90 -0
- package/src/photon/map.ts +371 -0
- package/src/photon/order.test.ts +142 -0
- package/src/photon/order.ts +661 -0
- package/src/photon/orders.ts +42 -0
- package/src/photon/patient.ts +386 -0
- package/src/photon/photon.ts +646 -0
- package/src/photon/prescription.ts +506 -0
- package/src/photon/session.ts +454 -0
- package/src/photon/types.ts +583 -0
- package/src/test-utils/msw.ts +72 -0
- package/src/testing/fakeNetworkApiClient.ts +56 -0
- package/src/testing/fixtures.ts +233 -0
- package/src/testing.ts +25 -0
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { getPatient, syncPatient, updatePatient, type PatientOutcome } from "../api/index.ts";
|
|
2
|
+
import type { Context } from "./context.ts";
|
|
3
|
+
import { choicesIn, declaredChoice, optionKey, withChoice } from "./choices.ts";
|
|
4
|
+
import { PhotonError } from "./errors.ts";
|
|
5
|
+
import { LiveRecord } from "./live.ts";
|
|
6
|
+
import { noChanges, toChanges, toPatient } from "./map.ts";
|
|
7
|
+
import type { ChangeOption, Changes, PatientCandidate, PatientInput, PatientRecord } from "./types.ts";
|
|
8
|
+
|
|
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">;
|
|
13
|
+
type Clinical = NonNullable<PatientInput["clinical"]>;
|
|
14
|
+
|
|
15
|
+
export interface PatientExtra {
|
|
16
|
+
/** The server wasn't certain who this is: who it might be (even one). Pick with `pick(id)`. */
|
|
17
|
+
candidates: PatientCandidate[];
|
|
18
|
+
/**
|
|
19
|
+
* Names for clinical entries the server returns with name = id (allergies, diagnoses on boson): what the
|
|
20
|
+
* prescriber typed or picked, by entry id.
|
|
21
|
+
*/
|
|
22
|
+
pickedNames: Readonly<Record<string, string>>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The lists network-api can't read a "none" back for yet (allergies it can). */
|
|
26
|
+
type NoneList = "medications" | "diagnoses" | "benefits";
|
|
27
|
+
|
|
28
|
+
const candidateOf = (p: PatientRecord): PatientCandidate => ({
|
|
29
|
+
id: p.id,
|
|
30
|
+
firstName: p.demographic?.name.first ?? "",
|
|
31
|
+
lastName: p.demographic?.name.last ?? null,
|
|
32
|
+
dateOfBirth: p.demographic?.dateOfBirth ?? null,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const validDob = (dob?: string) => Boolean(dob && /^\d{4}-\d{2}-\d{2}$/.test(dob));
|
|
36
|
+
const trimmed = (v?: string) => v?.trim() || undefined;
|
|
37
|
+
|
|
38
|
+
/** What's synced before the record: trimmed, empty fields dropped, a date of birth only when it's a whole date. */
|
|
39
|
+
function cleanDemographic(d: Demographic = {}): Demographic {
|
|
40
|
+
const first = trimmed(d.name?.first);
|
|
41
|
+
const last = trimmed(d.name?.last);
|
|
42
|
+
return {
|
|
43
|
+
...(first || last ? { name: { first, last } } : {}),
|
|
44
|
+
...(validDob(d.dateOfBirth) ? { dateOfBirth: d.dateOfBirth } : {}),
|
|
45
|
+
...(d.sex ? { sex: d.sex } : {}),
|
|
46
|
+
...(trimmed(d.gender) ? { gender: trimmed(d.gender) } : {}),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
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,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const clinicalIds = (p: PatientRecord | null) =>
|
|
92
|
+
new Set(
|
|
93
|
+
[...(p?.clinical?.allergies ?? []), ...(p?.clinical?.medications ?? []), ...(p?.clinical?.diagnoses ?? [])].map(
|
|
94
|
+
(e) => e.id,
|
|
95
|
+
),
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* A patient — from an id, from a record the host has, or not yet known. `sync({ demographic, contact })` sends what
|
|
100
|
+
* you know, and the server decides — the patient (found, or created when nothing matches and it has enough),
|
|
101
|
+
* candidates (pick one), or nothing yet (`changes` says what's missing). Keep adding what you know until it
|
|
102
|
+
* has an id; then `update(fields)` changes that patient. The client never decides "now create".
|
|
103
|
+
*
|
|
104
|
+
* Sync what's been committed (a field the prescriber left), not every keystroke: a sync with enough to
|
|
105
|
+
* create can create. While they type, `search` — it only lists who it might be.
|
|
106
|
+
*/
|
|
107
|
+
export class Patient extends LiveRecord<PatientRecord, PatientExtra> {
|
|
108
|
+
readonly #ctx: Context;
|
|
109
|
+
/** Everything synced so far, before the record — each sync adds to it. */
|
|
110
|
+
#known: Known = {};
|
|
111
|
+
#lastQuery: string | null = null;
|
|
112
|
+
/** The search in flight: any newer query aborts it. */
|
|
113
|
+
#searching: AbortController | null = null;
|
|
114
|
+
/** The last clinical patch — what a choice's path applies to. */
|
|
115
|
+
#lastClinical: Clinical = {};
|
|
116
|
+
#declared = new Set<string>();
|
|
117
|
+
/**
|
|
118
|
+
* Lists recorded as "none" here: their record says `[]`, not null (not recorded). network-api can't read a
|
|
119
|
+
* "none" back for these yet — so only this object knows, until an entry is added to the list.
|
|
120
|
+
*/
|
|
121
|
+
#none = new Set<NoneList>();
|
|
122
|
+
|
|
123
|
+
constructor(ctx: Context, record: PatientRecord | null, changes?: Changes) {
|
|
124
|
+
super(record, { candidates: [], pickedNames: {} }, changes);
|
|
125
|
+
this.#ctx = ctx;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
protected override onRecord() {
|
|
129
|
+
this.#ctx.register(this);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
protected override settle(state: PatientRecord | null, changes: Changes) {
|
|
133
|
+
super.settle(state && this.#withNone(state), changes);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
override adopt(state: PatientRecord, changes?: Changes) {
|
|
137
|
+
super.adopt(this.#withNone(state), changes);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* What's been sent to find or add the patient, before the record — e.g. a host's details the server hasn't
|
|
142
|
+
* matched yet (not enough to add them). A form starts from it.
|
|
143
|
+
*/
|
|
144
|
+
get known(): Readonly<Known> {
|
|
145
|
+
return this.#known;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* SyncPatient: send what's known. Before a record it's added to what was sent before, and the server finds,
|
|
150
|
+
* lists candidates, says what's missing, or creates. With a record, the server syncs these fields to it.
|
|
151
|
+
*/
|
|
152
|
+
sync(input: Omit<PatientInput, "id">): Promise<{ changes: Changes }> {
|
|
153
|
+
if (this.id)
|
|
154
|
+
return Object.values(input).some(Boolean)
|
|
155
|
+
? this.#write("sync", input)
|
|
156
|
+
: Promise.resolve({ changes: this.changes });
|
|
157
|
+
if (input.clinical)
|
|
158
|
+
return Promise.reject(new PhotonError("invalid", "Clinical changes need the patient's id: sync them first."));
|
|
159
|
+
return this.#match(knownOf(input));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
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.
|
|
166
|
+
*/
|
|
167
|
+
update(input: Omit<PatientInput, "id">): Promise<{ changes: Changes }> {
|
|
168
|
+
if (!this.id)
|
|
169
|
+
return Promise.reject(new PhotonError("invalid", "update() needs the patient's id; until it has one, sync()."));
|
|
170
|
+
return this.#write("update", input);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Type-ahead, before the record: who it might be, from what's known plus what's being typed (not kept —
|
|
175
|
+
* `sync` commits). Sent without sex, so it can never add anyone (network-api adds a patient only with first,
|
|
176
|
+
* last, date of birth and sex), and what it finds is only listed in `candidates` — even a sure match is the
|
|
177
|
+
* prescriber's pick mid-typing. A newer search or sync aborts one in flight; the same query isn't sent twice.
|
|
178
|
+
*/
|
|
179
|
+
search(input: Omit<PatientInput, "id">): Promise<{ changes: Changes }> {
|
|
180
|
+
if (this.id) return Promise.resolve({ changes: this.changes });
|
|
181
|
+
const { demographic: { sex: _, ...demographic } = {}, contact } = cleanKnown(
|
|
182
|
+
mergeKnown(this.#known, knownOf(input)),
|
|
183
|
+
);
|
|
184
|
+
const query = { ...(nonEmpty(demographic) ? { demographic } : {}), ...(contact ? { contact } : {}) };
|
|
185
|
+
const key = JSON.stringify(query);
|
|
186
|
+
if (!Object.keys(query).length || key === this.#lastQuery) return Promise.resolve({ changes: this.changes });
|
|
187
|
+
this.#lastQuery = key;
|
|
188
|
+
this.#searching?.abort();
|
|
189
|
+
const search = new AbortController();
|
|
190
|
+
this.#searching = search;
|
|
191
|
+
return this.run("loading", async () => {
|
|
192
|
+
if (key !== this.#lastQuery || this.id) return { changes: this.changes };
|
|
193
|
+
let outcome: PatientOutcome;
|
|
194
|
+
try {
|
|
195
|
+
outcome = await syncPatient(this.#ctx.client, query, search.signal);
|
|
196
|
+
} catch (e) {
|
|
197
|
+
if (search.signal.aborted) return { changes: this.changes }; // a newer query answers instead
|
|
198
|
+
throw e;
|
|
199
|
+
}
|
|
200
|
+
if (key !== this.#lastQuery || this.id) return { changes: this.changes };
|
|
201
|
+
const found =
|
|
202
|
+
outcome.kind === "ambiguous"
|
|
203
|
+
? outcome.candidates.map((c) => ({ ...c }))
|
|
204
|
+
: outcome.patient
|
|
205
|
+
? [candidateOf(toPatient(outcome.patient))]
|
|
206
|
+
: [];
|
|
207
|
+
this.patch({ candidates: found });
|
|
208
|
+
return { changes: this.changes };
|
|
209
|
+
}).finally(() => {
|
|
210
|
+
if (this.#searching === search) this.#searching = null;
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** One of `candidates`: read it, and this becomes that patient. Phone / email sent to find them fill gaps on it. */
|
|
215
|
+
pick(candidateId: string): Promise<{ changes: Changes }> {
|
|
216
|
+
return this.run("loading", async () => {
|
|
217
|
+
const result = this.#settle(await getPatient(this.#ctx.client, candidateId));
|
|
218
|
+
this.patch({ candidates: [] });
|
|
219
|
+
return result;
|
|
220
|
+
}).then((result) => this.#fillMissingContact(result));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** A choice the server asked for (e.g. which penicillin): sent again with the pick applied to the last patch. */
|
|
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));
|
|
227
|
+
if (option.id && option.displayName)
|
|
228
|
+
this.patch({ pickedNames: { ...this.snapshot.pickedNames, [option.id]: option.displayName } });
|
|
229
|
+
const rooted = option.path?.startsWith("clinical.");
|
|
230
|
+
const clinical = rooted
|
|
231
|
+
? (withChoice({ clinical: this.#lastClinical }, option).clinical ?? {})
|
|
232
|
+
: withChoice(this.#lastClinical, option);
|
|
233
|
+
return this.update({ clinical });
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* The Photon Rx app's link to this patient (`/patient/<id>`); `ph.url(this)`. Refused before the record: there's
|
|
238
|
+
* no id to link to, and a link never carries their details.
|
|
239
|
+
*/
|
|
240
|
+
url(): string {
|
|
241
|
+
return this.#ctx.url(this);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** The same, embeddable in an iframe; `ph.frame(this)`. */
|
|
245
|
+
frame(): string {
|
|
246
|
+
return this.#ctx.frame(this);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Read it again. */
|
|
250
|
+
reload(): Promise<{ changes: Changes }> {
|
|
251
|
+
if (!this.id) return Promise.resolve({ changes: this.changes });
|
|
252
|
+
return this.run("loading", async () => this.#settle(await getPatient(this.#ctx.client, this.id!)));
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
#write(kind: "sync" | "update", input: Omit<PatientInput, "id">): Promise<{ changes: Changes }> {
|
|
256
|
+
return this.run("saving", async () => {
|
|
257
|
+
const id = this.id!;
|
|
258
|
+
const before = clinicalIds(this.state);
|
|
259
|
+
if (input.clinical) this.#lastClinical = input.clinical;
|
|
260
|
+
const outcome =
|
|
261
|
+
kind === "sync"
|
|
262
|
+
? await syncPatient(this.#ctx.client, { id, ...input })
|
|
263
|
+
: await updatePatient(this.#ctx.client, id, input);
|
|
264
|
+
this.#noteNone(input);
|
|
265
|
+
const result = this.#settle(outcome);
|
|
266
|
+
if (input.clinical) this.#nameNewEntries(input.clinical, before);
|
|
267
|
+
return result;
|
|
268
|
+
}).then((result) => this.#takeDeclared(result));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
#match(known: Known): Promise<{ changes: Changes }> {
|
|
272
|
+
this.#known = mergeKnown(this.#known, known);
|
|
273
|
+
const query = cleanKnown(this.#known);
|
|
274
|
+
if (!Object.keys(query).length) return Promise.resolve({ changes: this.changes });
|
|
275
|
+
const key = JSON.stringify(query);
|
|
276
|
+
// The same query gets the same answer: keep it (blur and a debounce often ask twice).
|
|
277
|
+
if (key === this.#lastQuery) return Promise.resolve({ changes: this.changes });
|
|
278
|
+
this.#lastQuery = key;
|
|
279
|
+
this.#searching?.abort();
|
|
280
|
+
return this.run("loading", async () => {
|
|
281
|
+
// A newer query was made while this one waited: it answers instead.
|
|
282
|
+
if (key !== this.#lastQuery || this.id) return { changes: this.changes };
|
|
283
|
+
const outcome = await syncPatient(this.#ctx.client, query);
|
|
284
|
+
// Answered after a newer query was made: only a patient found (or added) still counts.
|
|
285
|
+
if (key !== this.#lastQuery && !(outcome.kind === "ok" && outcome.patient)) return { changes: this.changes };
|
|
286
|
+
if (outcome.kind === "ambiguous") {
|
|
287
|
+
this.patch({ candidates: outcome.candidates.map((c) => ({ ...c })), changes: noChanges });
|
|
288
|
+
return { changes: noChanges };
|
|
289
|
+
}
|
|
290
|
+
this.patch({ candidates: [] });
|
|
291
|
+
return this.#settle(outcome);
|
|
292
|
+
}).then((result) => this.#fillMissingContact(result));
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Phone / email sent to help find the patient: once a sync or a pick gives the record, save the ones it
|
|
297
|
+
* doesn't have — fill in, never overwrite.
|
|
298
|
+
*/
|
|
299
|
+
async #fillMissingContact(result: { changes: Changes }) {
|
|
300
|
+
if (!this.id || !this.state) return result;
|
|
301
|
+
const had = this.state.contact;
|
|
302
|
+
const sent = this.#known.contact;
|
|
303
|
+
const missing = {
|
|
304
|
+
...(sent?.phone && !had?.phone ? { phone: trimmed(sent.phone) } : {}),
|
|
305
|
+
...(sent?.email && !had?.email ? { email: trimmed(sent.email) } : {}),
|
|
306
|
+
};
|
|
307
|
+
this.#known = {};
|
|
308
|
+
return Object.keys(missing).length ? this.update({ contact: missing }) : result;
|
|
309
|
+
}
|
|
310
|
+
|
|
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. */
|
|
312
|
+
#takeDeclared(result: { changes: Changes }) {
|
|
313
|
+
const option = declaredChoice(this.changes) ?? this.#clinicalMatch();
|
|
314
|
+
const key = option && optionKey(option);
|
|
315
|
+
if (!option || !key || this.#declared.has(key)) return result;
|
|
316
|
+
this.#declared.add(key);
|
|
317
|
+
return this.choose(option);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* An allergy, medication or diagnosis the server is sure of: its only option (a single row), or its one
|
|
322
|
+
* HIGH-confidence option ("callers may auto-resolve when HIGH"). Taken like a declared match.
|
|
323
|
+
*/
|
|
324
|
+
#clinicalMatch(): ChangeOption | null {
|
|
325
|
+
for (const change of choicesIn(this.changes)) {
|
|
326
|
+
if (!/allergies|medications|diagnoses/.test(change.key)) continue;
|
|
327
|
+
const options = change.options.filter((o) => o.path);
|
|
328
|
+
const high = options.filter((o) => o.matchReasons.some((r) => r.confidence === "HIGH"));
|
|
329
|
+
if (options.length === 1) return options[0]!;
|
|
330
|
+
if (high.length === 1) return high[0]!;
|
|
331
|
+
}
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** What a write recorded: "none" for a list, or an entry on it (it isn't "none" any more). */
|
|
336
|
+
#noteNone(input: Omit<PatientInput, "id">) {
|
|
337
|
+
const patches = {
|
|
338
|
+
medications: input.clinical?.medications,
|
|
339
|
+
diagnoses: input.clinical?.diagnoses,
|
|
340
|
+
benefits: input.benefits,
|
|
341
|
+
};
|
|
342
|
+
for (const [list, patch] of Object.entries(patches) as [NoneList, typeof patches.medications][]) {
|
|
343
|
+
if (patch?.none) this.#none.add(list);
|
|
344
|
+
if (patch?.add?.length) this.#none.delete(list);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** The record with each list recorded as "none" here as `[]` (network-api answers those as nothing on file). */
|
|
349
|
+
#withNone(p: PatientRecord): PatientRecord {
|
|
350
|
+
if (!this.#none.size) return p;
|
|
351
|
+
const none = <T>(list: T[] | null, name: NoneList): T[] | null => list ?? (this.#none.has(name) ? [] : null);
|
|
352
|
+
return {
|
|
353
|
+
...p,
|
|
354
|
+
benefits: none(p.benefits, "benefits"),
|
|
355
|
+
clinical: p.clinical && {
|
|
356
|
+
...p.clinical,
|
|
357
|
+
medications: none(p.clinical.medications, "medications"),
|
|
358
|
+
diagnoses: none(p.clinical.diagnoses, "diagnoses"),
|
|
359
|
+
},
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** An entry added from typed text can come back with name = id: label the one new entry per list with what was typed. */
|
|
364
|
+
#nameNewEntries(clinical: Clinical, before: Set<string>) {
|
|
365
|
+
const typed = {
|
|
366
|
+
allergies: clinical.allergies?.add?.[0]?.name,
|
|
367
|
+
medications: clinical.medications?.add?.[0]?.name,
|
|
368
|
+
diagnoses: clinical.diagnoses?.add?.[0]?.name ?? clinical.diagnoses?.add?.[0]?.icd10Code,
|
|
369
|
+
};
|
|
370
|
+
const named: Record<string, string> = {};
|
|
371
|
+
for (const list of ["allergies", "medications", "diagnoses"] as const) {
|
|
372
|
+
const text = typed[list];
|
|
373
|
+
const added = (this.state?.clinical?.[list] ?? []).filter(
|
|
374
|
+
(e) => !before.has(e.id) && (!e.name || e.name === e.id),
|
|
375
|
+
);
|
|
376
|
+
if (text && added.length === 1) named[added[0]!.id] = text;
|
|
377
|
+
}
|
|
378
|
+
if (Object.keys(named).length) this.patch({ pickedNames: { ...named, ...this.snapshot.pickedNames } });
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
#settle(outcome: PatientOutcome) {
|
|
382
|
+
const changes = outcome.kind === "ok" ? toChanges(outcome.changes) : noChanges;
|
|
383
|
+
this.settle(outcome.kind === "ok" && outcome.patient ? toPatient(outcome.patient) : null, changes);
|
|
384
|
+
return { changes };
|
|
385
|
+
}
|
|
386
|
+
}
|