@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,506 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getPrescription,
|
|
3
|
+
getPrescriptionTemplates,
|
|
4
|
+
syncPrescription,
|
|
5
|
+
updatePrescription,
|
|
6
|
+
type ChangeSet,
|
|
7
|
+
type NetworkClient,
|
|
8
|
+
type PrescriptionOutcome,
|
|
9
|
+
} from "../api/index.ts";
|
|
10
|
+
import type { PrescriptionFieldsFragment } from "../gql/graphql.ts";
|
|
11
|
+
import type { Context } from "./context.ts";
|
|
12
|
+
import { classify, declaredChoice, optionKey, withChoice } from "./choices.ts";
|
|
13
|
+
import { PhotonError, refuse } from "./errors.ts";
|
|
14
|
+
import { filled } from "./inputs.ts";
|
|
15
|
+
import { LiveRecord } from "./live.ts";
|
|
16
|
+
import { noChanges, toChanges, toPrescription, toPrescriptionTemplate } from "./map.ts";
|
|
17
|
+
import type {
|
|
18
|
+
ChangeOption,
|
|
19
|
+
Changes,
|
|
20
|
+
PrescriptionEdits,
|
|
21
|
+
PrescriptionRecord,
|
|
22
|
+
PrescriptionTemplate,
|
|
23
|
+
Treatment,
|
|
24
|
+
} from "./types.ts";
|
|
25
|
+
|
|
26
|
+
export interface PrescriptionExtra {
|
|
27
|
+
/**
|
|
28
|
+
* The drug for this line before its record exists — picked by the prescriber, or declared by the server
|
|
29
|
+
* (an exact match) — and, once it does, the name the prescriber saw: network-api's treatment name can drop
|
|
30
|
+
* the strength and form. `treatment`: the picked option's drug, in parts (null when the
|
|
31
|
+
* server declared it, or it was given by id).
|
|
32
|
+
*/
|
|
33
|
+
pick: { id: string; name: string; treatment: Treatment | null } | null;
|
|
34
|
+
/**
|
|
35
|
+
* The patient's in-progress prescriptions of this drug this line can continue (network-api asked which).
|
|
36
|
+
* Pick one with `continueDraft(id)`. null when not asked.
|
|
37
|
+
*/
|
|
38
|
+
drafts: PrescriptionRecord[] | null;
|
|
39
|
+
/**
|
|
40
|
+
* This draft's templates, once `loadTemplates()` asked for them: kept current as its drug or patient changes.
|
|
41
|
+
* The caller's catalog templates come first, then recommended ones (sigs worded for the patient, then units).
|
|
42
|
+
* null until then, while loading, before the draft has a record, on a signed prescription, and when the lookup
|
|
43
|
+
* fails.
|
|
44
|
+
*/
|
|
45
|
+
templates: PrescriptionTemplate[] | null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The prescription mutation on a live object: its fields, and who it's for. */
|
|
49
|
+
export interface PrescriptionUpdate extends PrescriptionEdits {
|
|
50
|
+
/**
|
|
51
|
+
* Who it's for. Before the record, a line without a patient only looks drugs up; once it has one, a picked
|
|
52
|
+
* drug is created for them. Fixed once the record exists (a different patient is a new prescription).
|
|
53
|
+
*/
|
|
54
|
+
patient?: { id: string } | null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface PrescriptionOptions {
|
|
58
|
+
/** The patient it's for; a line before its patient is known only looks drugs up. */
|
|
59
|
+
patientId?: string | null;
|
|
60
|
+
templateId?: string | null;
|
|
61
|
+
/**
|
|
62
|
+
* Prescriptions other lines of the same order hold. network-api's sync continues the patient's
|
|
63
|
+
* in-progress draft, so a second line typing a drug can get the first line's draft back; never take
|
|
64
|
+
* one of these.
|
|
65
|
+
*/
|
|
66
|
+
isTaken?: (prescriptionId: string) => boolean;
|
|
67
|
+
changes?: Changes;
|
|
68
|
+
/** The prescription it was given by id that the server doesn't have: this line stands in for it. */
|
|
69
|
+
missing?: string | null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** GetPrescription's copy — what was stored — or the given one if the read fails. */
|
|
73
|
+
async function storedCopy(client: NetworkClient, rx: PrescriptionFieldsFragment): Promise<PrescriptionFieldsFragment> {
|
|
74
|
+
try {
|
|
75
|
+
const read = await getPrescription(client, rx.id);
|
|
76
|
+
return (read.kind === "ok" && read.prescription) || rx;
|
|
77
|
+
} catch {
|
|
78
|
+
return rx;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const merged = (a: PrescriptionEdits, b: PrescriptionEdits): PrescriptionEdits => ({
|
|
83
|
+
...a,
|
|
84
|
+
...b,
|
|
85
|
+
// Dispense arrives a field at a time: keep them all, so the create carries every one.
|
|
86
|
+
...(a.dispense || b.dispense ? { dispense: { ...a.dispense, ...b.dispense } } : {}),
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* One prescription line — before its record exists and after. `sync(input)` sends what's known; the object
|
|
91
|
+
* decides what it means:
|
|
92
|
+
*
|
|
93
|
+
* - No record, drug not yet clear: look the drug up (SyncPrescription without a patient, so nothing can be
|
|
94
|
+
* created); the choices land in `changes`. Other fields wait here and go with the create.
|
|
95
|
+
* - A drug picked (`choose`), or declared by the server, and a patient known: create it (SyncPrescription
|
|
96
|
+
* with everything entered) — `id` fills in. Without a patient yet, the pick waits for `sync({ patient })`.
|
|
97
|
+
* - A record: the fields given are synced to it, then the stored copy is read back (its contentHash is what
|
|
98
|
+
* a signature must match).
|
|
99
|
+
*
|
|
100
|
+
* `update(edits)` changes the prescription by its id (UpdatePrescription). A signed prescription is final: a
|
|
101
|
+
* change is a new prescription.
|
|
102
|
+
*/
|
|
103
|
+
export class Prescription extends LiveRecord<PrescriptionRecord, PrescriptionExtra> {
|
|
104
|
+
readonly #ctx: Context;
|
|
105
|
+
#patientId: string | null;
|
|
106
|
+
readonly #templateId: string | null;
|
|
107
|
+
readonly #isTaken: (id: string) => boolean;
|
|
108
|
+
/** Everything entered on this line — what's sent at create, and what a choice's path applies to. */
|
|
109
|
+
#entered: PrescriptionEdits = {};
|
|
110
|
+
#declared = new Set<string>();
|
|
111
|
+
/** The drug lookup for the latest text: newer text aborts it (in flight, or still waiting its turn). */
|
|
112
|
+
#lookup: AbortController | null = null;
|
|
113
|
+
|
|
114
|
+
constructor(ctx: Context, record: PrescriptionRecord | null, options: PrescriptionOptions = {}) {
|
|
115
|
+
super(record, { pick: null, drafts: null, templates: null }, options.changes);
|
|
116
|
+
this.#ctx = ctx;
|
|
117
|
+
this.#patientId = options.patientId ?? record?.patient?.id ?? null;
|
|
118
|
+
this.#templateId = options.templateId ?? null;
|
|
119
|
+
this.#isTaken = options.isTaken ?? (() => false);
|
|
120
|
+
this.#missing = options.missing ?? null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
readonly #missing: string | null;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The prescription this line was given by id that the server doesn't have (e.g. an order link's `rx=` from
|
|
127
|
+
* another organization): the line stands in for it, empty, to fill in instead. null otherwise.
|
|
128
|
+
*/
|
|
129
|
+
get missing(): string | null {
|
|
130
|
+
return this.#missing;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
protected override onRecord() {
|
|
134
|
+
this.#ctx.register(this);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Whether anything asked for templates (`loadTemplates()`); until then, saves never trigger the lookup. */
|
|
138
|
+
#templatesWanted = false;
|
|
139
|
+
/** The record, drug and patient the current `templates` are for: a change to any of them fetches again. */
|
|
140
|
+
#templatesFor: string | null = null;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Fetch this draft's templates into `templates`, and refetch whenever its drug or patient changes. Separate from
|
|
144
|
+
* every other call so saves don't wait on the Medi-Span sig lookup. A failed lookup leaves `templates` null.
|
|
145
|
+
*/
|
|
146
|
+
loadTemplates(): void {
|
|
147
|
+
this.#templatesWanted = true;
|
|
148
|
+
this.#refreshTemplates();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
protected override patch(patch: Parameters<LiveRecord<PrescriptionRecord, PrescriptionExtra>["patch"]>[0]) {
|
|
152
|
+
super.patch(patch);
|
|
153
|
+
if (this.#templatesWanted) this.#refreshTemplates();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
#refreshTemplates() {
|
|
157
|
+
const rx = this.state;
|
|
158
|
+
const templatesFor = rx?.patient && !this.signed ? `${rx.id}:${rx.treatment.id}:${rx.patient.id}` : null;
|
|
159
|
+
if (templatesFor === this.#templatesFor) return;
|
|
160
|
+
this.#templatesFor = templatesFor;
|
|
161
|
+
super.patch({ templates: null });
|
|
162
|
+
if (!rx || !templatesFor) return;
|
|
163
|
+
getPrescriptionTemplates(this.#ctx.client, rx.id).then(
|
|
164
|
+
(templates) => {
|
|
165
|
+
if (this.#templatesFor === templatesFor) {
|
|
166
|
+
super.patch({ templates: templates && templates.map(toPrescriptionTemplate) });
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
() => {},
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
get signed(): boolean {
|
|
174
|
+
return this.state?.signing.state === "SIGNED";
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The drug's name as the prescriber saw it (the pick), while that pick is the record's drug; else the server's
|
|
179
|
+
* product name (`prescribableName` — its `name` can be just the drug). Never an id: a drug given by id alone (e.g.
|
|
180
|
+
* from a link) has no name until network-api gives one — once it's drafted for a patient.
|
|
181
|
+
*/
|
|
182
|
+
get name(): string | null {
|
|
183
|
+
const pick = this.snapshot.pick;
|
|
184
|
+
const named = pick && pick.name !== pick.id;
|
|
185
|
+
if (pick && !this.state) return named ? pick.name : null;
|
|
186
|
+
if (pick && named && this.state!.treatment.id === pick.id) return pick.name;
|
|
187
|
+
const treatment = this.state?.treatment;
|
|
188
|
+
return treatment ? (treatment.prescribableName ?? treatment.name) : null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The drug in parts (drug, form, route, strength) — the picked option's, while that pick is the record's drug
|
|
193
|
+
* (a lookup's option also says its DEA schedule); else the record's. null before either.
|
|
194
|
+
*/
|
|
195
|
+
get treatment(): Treatment | null {
|
|
196
|
+
const picked = this.snapshot.pick?.treatment;
|
|
197
|
+
if (picked && (!this.state || this.state.treatment.id === picked.id)) return picked;
|
|
198
|
+
return this.state?.treatment ?? null;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
get patientId(): string | null {
|
|
202
|
+
return this.#patientId;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Everything entered on this line — what a create sends, and (before the record) what's known of it: the drug's
|
|
207
|
+
* text or id, the sig, the dispense. A form starts from it.
|
|
208
|
+
*/
|
|
209
|
+
get entered(): Readonly<PrescriptionEdits> {
|
|
210
|
+
return this.#entered;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** The template it was started from: the record's, or the one it was made with before it has a record. */
|
|
214
|
+
get templateId(): string | null {
|
|
215
|
+
return this.state?.templateId ?? this.#templateId;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** SyncPrescription: what's known — see the class. */
|
|
219
|
+
sync(input: PrescriptionUpdate): Promise<{ changes: Changes }> {
|
|
220
|
+
return this.#send("sync", input);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** UpdatePrescription: change only the fields given, then read the stored copy. Needs its id. */
|
|
224
|
+
update(edits: PrescriptionEdits): Promise<{ changes: Changes }> {
|
|
225
|
+
if (!this.id)
|
|
226
|
+
return Promise.reject(
|
|
227
|
+
new PhotonError("invalid", "update() needs the prescription's id; until it has one, sync()."),
|
|
228
|
+
);
|
|
229
|
+
return this.#send("update", edits);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
#send(kind: "sync" | "update", input: PrescriptionUpdate): Promise<{ changes: Changes }> {
|
|
233
|
+
const { patient, ...given } = input;
|
|
234
|
+
// Before the record, an empty field is "not known yet" (a lookup or a create sends only what's known).
|
|
235
|
+
const edits = this.id ? given : filled(given);
|
|
236
|
+
if (patient !== undefined) {
|
|
237
|
+
const patientId = patient?.id ?? null;
|
|
238
|
+
if (!this.id) this.#patientId = patientId;
|
|
239
|
+
else if (patientId && patientId !== this.#patientId)
|
|
240
|
+
return Promise.reject(refuse("This prescription's patient is fixed: for another patient, start a new one."));
|
|
241
|
+
}
|
|
242
|
+
// Nothing to send: no field changed, and (before the record) no picked drug to create for a known patient.
|
|
243
|
+
const canCreate = !this.id && Boolean(this.#patientId && (this.snapshot.pick || this.#templateId));
|
|
244
|
+
if (!Object.keys(edits).length && !canCreate) return Promise.resolve({ changes: this.changes });
|
|
245
|
+
if (this.signed)
|
|
246
|
+
return Promise.reject(refuse("This prescription is signed, so it's final. A change is a new prescription."));
|
|
247
|
+
this.#entered = merged(this.#entered, edits);
|
|
248
|
+
// New free text for the drug drops a previous pick, and the lookup for the text before it.
|
|
249
|
+
let lookup: AbortController | null = null;
|
|
250
|
+
if (edits.treatment?.name && !edits.treatment.id && !this.id) {
|
|
251
|
+
this.patch({ pick: null });
|
|
252
|
+
this.#lookup?.abort();
|
|
253
|
+
lookup = this.#lookup = new AbortController();
|
|
254
|
+
}
|
|
255
|
+
return this.run("saving", async () => {
|
|
256
|
+
if (lookup?.signal.aborted) return { changes: this.changes }; // newer text came first: it looks up instead
|
|
257
|
+
if (this.signed) throw refuse("This prescription is signed, so it's final. A change is a new prescription.");
|
|
258
|
+
if (!this.id) return this.#beforeRecord(edits, lookup?.signal);
|
|
259
|
+
if (!Object.keys(edits).length) return { changes: this.changes }; // queued to create, created by its turn
|
|
260
|
+
const outcome =
|
|
261
|
+
kind === "sync"
|
|
262
|
+
? await syncPrescription(this.#ctx.client, { id: this.id, ...edits })
|
|
263
|
+
: await updatePrescription(this.#ctx.client, this.id, edits);
|
|
264
|
+
return this.#settleStored(outcome);
|
|
265
|
+
}).then((result) => this.#takeDeclared(result));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* A choice the server asked for. Which drug, before the record: that's the pick (created now if the patient is
|
|
270
|
+
* known). Anything else: sent again with the pick applied to what was entered.
|
|
271
|
+
*/
|
|
272
|
+
choose(option: ChangeOption): Promise<{ changes: Changes }> {
|
|
273
|
+
const path = option.path;
|
|
274
|
+
const isDrug = Boolean(path?.startsWith("treatment"));
|
|
275
|
+
const id = isDrug ? option.id : null;
|
|
276
|
+
if (id) {
|
|
277
|
+
const pick = { id, name: option.displayName || id, treatment: option.treatment };
|
|
278
|
+
// The pick is made: close the drug's choices now, not when the server answers.
|
|
279
|
+
const changes = {
|
|
280
|
+
...this.changes,
|
|
281
|
+
unapplied: this.changes.unapplied.filter((c) => !c.key.startsWith("treatment")),
|
|
282
|
+
};
|
|
283
|
+
this.patch({ pick, changes });
|
|
284
|
+
this.#entered = { ...this.#entered, treatment: { id } };
|
|
285
|
+
if (!this.id) return this.sync({});
|
|
286
|
+
return this.update({ treatment: { id } });
|
|
287
|
+
}
|
|
288
|
+
const next = withChoice(this.#entered, option);
|
|
289
|
+
const root = (path ?? "").split(".")[0] as keyof PrescriptionEdits;
|
|
290
|
+
const edits = root && root in next ? { [root]: next[root] } : next;
|
|
291
|
+
return this.id ? this.update(edits) : this.sync(edits);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** One of `drafts`: this line continues it, with what was entered here applied to it. */
|
|
295
|
+
continueDraft(prescriptionId: string): Promise<{ changes: Changes }> {
|
|
296
|
+
const draft = this.snapshot.drafts?.find((d) => d.id === prescriptionId);
|
|
297
|
+
if (!draft) return Promise.resolve({ changes: this.changes });
|
|
298
|
+
this.patch({ state: draft, drafts: null });
|
|
299
|
+
const { treatment, instructions, dispense, clinical } = this.#entered;
|
|
300
|
+
const edits = Object.fromEntries(
|
|
301
|
+
Object.entries({ treatment, instructions, dispense, clinical }).filter(([, v]) => v !== undefined),
|
|
302
|
+
) as PrescriptionEdits;
|
|
303
|
+
return Object.keys(edits).length ? this.update(edits) : Promise.resolve({ changes: this.changes });
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* The prescriber's approval. Waits for updates already sent, then signs what was stored — bound to its
|
|
308
|
+
* contentHash. Screening alerts don't stop it: signing is the prescriber's acknowledgement of
|
|
309
|
+
* what screening showed (network-api has no way to acknowledge an alert yet). Refused if
|
|
310
|
+
* the server's last answer left something open (a choice to make, a rejected field), or if an update in
|
|
311
|
+
* flight changed the screening result the prescriber saw (they review it first). A prescription marked changed-since-signing (INVALIDATED_SIGN) can be signed
|
|
312
|
+
* again: that's the prescriber approving what it says now. Sends only the signing, never content.
|
|
313
|
+
*/
|
|
314
|
+
sign(message?: string): Promise<{ changes: Changes }> {
|
|
315
|
+
const seen = this.state;
|
|
316
|
+
// Before its record, refused on the object (`error`), as a failed save is: a drug picked waits for its patient.
|
|
317
|
+
if (!seen)
|
|
318
|
+
return this.run("saving", async () => {
|
|
319
|
+
throw refuse(
|
|
320
|
+
this.snapshot.pick && !this.#patientId
|
|
321
|
+
? "A patient is required for signing"
|
|
322
|
+
: "There's nothing to sign yet: pick the drug first.",
|
|
323
|
+
);
|
|
324
|
+
});
|
|
325
|
+
return this.run("saving", async () => {
|
|
326
|
+
const current = this.state!;
|
|
327
|
+
if (current.signing.state === "SIGNED") return { changes: noChanges };
|
|
328
|
+
if (!classify(this.changes).settled)
|
|
329
|
+
throw refuse("The server left something open on this prescription (see `changes`). Resolve it, then sign.");
|
|
330
|
+
if (screening(current) !== screening(seen))
|
|
331
|
+
throw refuse("Screening changed since this was reviewed. Review it, then sign.");
|
|
332
|
+
// Only the signing, with the hash of what was stored and reviewed — never a hash fetched after the fact.
|
|
333
|
+
const result = this.#settle(
|
|
334
|
+
await updatePrescription(this.#ctx.client, current.id, {
|
|
335
|
+
signing: { signedHash: current.signing.contentHash, message },
|
|
336
|
+
}),
|
|
337
|
+
);
|
|
338
|
+
if (!this.signed) throw refuse("This changed since it was reviewed. Review it, then sign.");
|
|
339
|
+
return result;
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* The Photon Rx app's link to this record's flow (`/<flow>/<id>`); before the record, to a new one from the ids it
|
|
345
|
+
* hangs off (its patient, and its template or drug). `ph.url(this)`.
|
|
346
|
+
*/
|
|
347
|
+
url(): string {
|
|
348
|
+
return this.#ctx.url(this);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** The same, embeddable in an iframe; `ph.frame(this)`. */
|
|
352
|
+
frame(): string {
|
|
353
|
+
return this.#ctx.frame(this);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** Read it again. */
|
|
357
|
+
reload(): Promise<{ changes: Changes }> {
|
|
358
|
+
if (!this.id) return Promise.resolve({ changes: this.changes });
|
|
359
|
+
return this.run("loading", async () => this.#settle(await getPrescription(this.#ctx.client, this.id!)));
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** No record yet: look the drug up until it's clear, then create once the patient is known. */
|
|
363
|
+
async #beforeRecord(edits: PrescriptionEdits, signal?: AbortSignal): Promise<{ changes: Changes }> {
|
|
364
|
+
const pick = this.snapshot.pick;
|
|
365
|
+
const resolved = Boolean(pick || this.#entered.treatment?.id || this.#templateId);
|
|
366
|
+
if (!resolved) {
|
|
367
|
+
if (!edits.treatment || !this.#entered.treatment) return { changes: this.changes };
|
|
368
|
+
const found = await this.#lookUp(this.#entered.treatment, signal);
|
|
369
|
+
if (!found) return { changes: this.changes };
|
|
370
|
+
const changes = toChanges(found);
|
|
371
|
+
this.settle(null, changes);
|
|
372
|
+
// The server declared what the name is (it resolved it to a record): that's the pick. Without that,
|
|
373
|
+
// the prescriber picks — even when no choice came back.
|
|
374
|
+
const declared = changes.applied.find((c) => c.key === "treatment" && c.resolvedId);
|
|
375
|
+
if (!declared) return { changes };
|
|
376
|
+
this.patch({
|
|
377
|
+
pick: { id: declared.resolvedId!, name: this.#entered.treatment.name ?? declared.resolvedId!, treatment: null },
|
|
378
|
+
});
|
|
379
|
+
if (!this.#patientId) return { changes };
|
|
380
|
+
}
|
|
381
|
+
if (!this.#patientId) {
|
|
382
|
+
// A drug given by id, no patient yet: it's the line's drug — the pick, waiting for the patient. The lookup
|
|
383
|
+
// checks the id; network-api doesn't name it until there's a draft, so the name is the
|
|
384
|
+
// one given with it, or the id.
|
|
385
|
+
const given = this.#entered.treatment;
|
|
386
|
+
if (!pick && given?.id) {
|
|
387
|
+
const changes = toChanges(await this.#lookUp(given));
|
|
388
|
+
this.settle(null, changes);
|
|
389
|
+
const accepted = changes.applied.some((c) => c.key === "treatment");
|
|
390
|
+
if (accepted) this.patch({ pick: { id: given.id, name: given.name ?? given.id, treatment: null } });
|
|
391
|
+
return { changes };
|
|
392
|
+
}
|
|
393
|
+
return { changes: this.changes };
|
|
394
|
+
}
|
|
395
|
+
return this.#create();
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* SyncPrescription without a patient: a drug lookup — it can never create anything. network-api also rejects
|
|
400
|
+
* the patient and every field it would have filled ("treatment.instructions", … — blocked by the missing
|
|
401
|
+
* patient); only the drug's own change answers it: its choices, or why nothing matched. (A template isn't looked
|
|
402
|
+
* up: network-api doesn't expand one without a patient.) null when newer text aborted it.
|
|
403
|
+
*/
|
|
404
|
+
async #lookUp(
|
|
405
|
+
treatment: NonNullable<PrescriptionEdits["treatment"]>,
|
|
406
|
+
signal?: AbortSignal,
|
|
407
|
+
): Promise<ChangeSet | null> {
|
|
408
|
+
let outcome: PrescriptionOutcome;
|
|
409
|
+
try {
|
|
410
|
+
outcome = await syncPrescription(this.#ctx.client, { treatment, templateId: this.#templateId }, signal);
|
|
411
|
+
} catch (e) {
|
|
412
|
+
if (signal?.aborted) return null;
|
|
413
|
+
throw e;
|
|
414
|
+
}
|
|
415
|
+
if (signal?.aborted) return null;
|
|
416
|
+
if (outcome.kind !== "ok") return { changes: [] };
|
|
417
|
+
return {
|
|
418
|
+
...outcome.changes,
|
|
419
|
+
changes: outcome.changes.changes.filter((c) => c.status === "APPLIED" || c.key === "treatment"),
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
async #create(): Promise<{ changes: Changes }> {
|
|
424
|
+
const pick = this.snapshot.pick;
|
|
425
|
+
const entered = this.#entered;
|
|
426
|
+
const outcome = await syncPrescription(this.#ctx.client, {
|
|
427
|
+
patient: { id: this.#patientId! },
|
|
428
|
+
templateId: this.#templateId,
|
|
429
|
+
...entered,
|
|
430
|
+
treatment: pick ? { id: pick.id } : entered.treatment,
|
|
431
|
+
});
|
|
432
|
+
if (outcome.kind === "ambiguous") return this.#offerDrafts(outcome.candidates.map((c) => c.id));
|
|
433
|
+
const changes = toChanges(outcome.changes);
|
|
434
|
+
// A new line only takes a record once its drug is clear, and never a signed one or another line's:
|
|
435
|
+
// while the drug is still ambiguous, network-api hands back the patient's latest prescription — an old
|
|
436
|
+
// draft, one already signed and sent, or another line's (boson, 2026-09-29).
|
|
437
|
+
const drugStillOpen = classify(changes).needsChoice.some((c) => c.key.startsWith("treatment"));
|
|
438
|
+
const rx = outcome.prescription;
|
|
439
|
+
if (!rx || drugStillOpen || !this.#canTake(rx)) {
|
|
440
|
+
this.settle(null, changes);
|
|
441
|
+
return { changes };
|
|
442
|
+
}
|
|
443
|
+
this.settle(toPrescription(await storedCopy(this.#ctx.client, rx)), changes);
|
|
444
|
+
return { changes };
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
#canTake(rx: { id: string; signing: { state: string } }) {
|
|
448
|
+
return rx.signing.state === "UNSIGNED" && !this.#isTaken(rx.id);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* The patient already has several in-progress prescriptions and the server asks which this continues.
|
|
453
|
+
* Candidates carry no drug or signing state, so read each; offer only unsigned drafts of the same drug that
|
|
454
|
+
* no other line holds. network-api counts signed (even sent) ones as in progress.
|
|
455
|
+
*/
|
|
456
|
+
async #offerDrafts(ids: string[]): Promise<{ changes: Changes }> {
|
|
457
|
+
const read = await Promise.all(ids.map((id) => getPrescription(this.#ctx.client, id)));
|
|
458
|
+
const open = read.flatMap((o) => (o.kind === "ok" && o.prescription ? [toPrescription(o.prescription)] : []));
|
|
459
|
+
const wanted = this.snapshot.pick ? { id: this.snapshot.pick.id } : this.#entered.treatment;
|
|
460
|
+
const sameDrug = (rx: PrescriptionRecord) =>
|
|
461
|
+
wanted?.id != null
|
|
462
|
+
? rx.treatment.id === String(wanted.id)
|
|
463
|
+
: Boolean(wanted?.name && rx.treatment.name.toLowerCase().startsWith(wanted.name.trim().toLowerCase()));
|
|
464
|
+
const drafts = open.filter((rx) => this.#canTake(rx) && sameDrug(rx));
|
|
465
|
+
if (drafts.length) {
|
|
466
|
+
this.patch({ drafts });
|
|
467
|
+
return { changes: this.changes };
|
|
468
|
+
}
|
|
469
|
+
const names = [...new Set(open.map((rx) => rx.treatment.name))].join(", ");
|
|
470
|
+
// network-api won't start another prescription while ≥2 are open.
|
|
471
|
+
throw refuse(
|
|
472
|
+
open.every((rx) => rx.signing.state === "UNSIGNED")
|
|
473
|
+
? `This patient has ${open.length} unsent prescriptions (${names}). A new one can't be started until they're sent.`
|
|
474
|
+
: `Photon still counts ${open.length} of this patient's earlier prescriptions (${names}) as open, so a new one can't be started.`,
|
|
475
|
+
);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/** A choice the server declared (one option, an exact match) is taken without asking — once each. */
|
|
479
|
+
#takeDeclared(result: { changes: Changes }): Promise<{ changes: Changes }> | { changes: Changes } {
|
|
480
|
+
const option = declaredChoice(this.changes);
|
|
481
|
+
const key = option && optionKey(option);
|
|
482
|
+
if (!option || !key || this.#declared.has(key)) return result;
|
|
483
|
+
this.#declared.add(key);
|
|
484
|
+
return this.choose(option);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
#settle(outcome: PrescriptionOutcome) {
|
|
488
|
+
const changes = outcome.kind === "ok" ? toChanges(outcome.changes) : noChanges;
|
|
489
|
+
this.settle(outcome.kind === "ok" && outcome.prescription ? toPrescription(outcome.prescription) : null, changes);
|
|
490
|
+
return { changes };
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/** After a sync or update: the stored copy, not the mutation's echo — its contentHash is what a signature must match. */
|
|
494
|
+
async #settleStored(outcome: PrescriptionOutcome) {
|
|
495
|
+
const changes = outcome.kind === "ok" ? toChanges(outcome.changes) : noChanges;
|
|
496
|
+
const stored =
|
|
497
|
+
outcome.kind === "ok" && outcome.prescription
|
|
498
|
+
? toPrescription(await storedCopy(this.#ctx.client, outcome.prescription))
|
|
499
|
+
: null;
|
|
500
|
+
this.settle(stored, changes);
|
|
501
|
+
return { changes };
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/** What screening found, comparable: a sign is refused when it changed after the prescriber saw it. */
|
|
506
|
+
const screening = (rx: PrescriptionRecord) => JSON.stringify(rx.screeningAlerts);
|