@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,646 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createNetworkClient,
|
|
3
|
+
getOrder,
|
|
4
|
+
getPatient,
|
|
5
|
+
getPrescription,
|
|
6
|
+
me,
|
|
7
|
+
ping,
|
|
8
|
+
type NetworkClient,
|
|
9
|
+
} from "../api/index.ts";
|
|
10
|
+
import { isMachine, linkAuthOf, refuseSecretInBrowser, signsIn, type LinkAuth, type PhotonAuth } from "./auth.ts";
|
|
11
|
+
import { machineCredentials, type MachineCredentials } from "./machine.ts";
|
|
12
|
+
import { sessionFor, type Session } from "./session.ts";
|
|
13
|
+
import type { Context } from "./context.ts";
|
|
14
|
+
import { analyticsFor, type Analytics, type AnalyticsTarget } from "./analytics.ts";
|
|
15
|
+
import { environmentUrls, type EnvironmentUrls, type PhotonEnvironment } from "./environments.ts";
|
|
16
|
+
import { linkPath, parseLinkPath, type FlowLink } from "./links.ts";
|
|
17
|
+
import { guarded, PhotonError, refuse } from "./errors.ts";
|
|
18
|
+
import { noChanges, toChanges, toPatient, toPrescription } from "./map.ts";
|
|
19
|
+
import { adoptOrder, Order } from "./order.ts";
|
|
20
|
+
import { Patient } from "./patient.ts";
|
|
21
|
+
import { readOrders } from "./orders.ts";
|
|
22
|
+
import { Prescription } from "./prescription.ts";
|
|
23
|
+
import type {
|
|
24
|
+
Changes,
|
|
25
|
+
OrderCandidate,
|
|
26
|
+
OrderInput,
|
|
27
|
+
OrderRecord,
|
|
28
|
+
OrdersInput,
|
|
29
|
+
OrdersResult,
|
|
30
|
+
PatientCandidate,
|
|
31
|
+
PatientInput,
|
|
32
|
+
PatientRecord,
|
|
33
|
+
PrescriptionCandidate,
|
|
34
|
+
PrescriptionEdits,
|
|
35
|
+
PrescriptionInput,
|
|
36
|
+
PrescriptionRecord,
|
|
37
|
+
} from "./types.ts";
|
|
38
|
+
|
|
39
|
+
export { Order, Patient, Prescription };
|
|
40
|
+
|
|
41
|
+
// ---- Results: the record (or null), what it might be, and what the server said ----
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Each call returns the live object. Without an id it always does — with its record when the server settled
|
|
45
|
+
* on one (found, created or updated), or still without one (`id: null`): then `candidates` says who it might
|
|
46
|
+
* be, or `changes` what's missing, and the object carries the same. `null` only for an id with no record.
|
|
47
|
+
*/
|
|
48
|
+
export interface PatientResult {
|
|
49
|
+
patient: Patient | null;
|
|
50
|
+
/** When the server isn't certain: who it might be (even one). Pick with `patient.pick(id)`. */
|
|
51
|
+
candidates: PatientCandidate[];
|
|
52
|
+
changes: Changes;
|
|
53
|
+
}
|
|
54
|
+
export interface PrescriptionResult {
|
|
55
|
+
prescription: Prescription | null;
|
|
56
|
+
/** The patient's in-progress prescriptions of this drug it could continue (`prescription.continueDraft(id)`). */
|
|
57
|
+
candidates: PrescriptionCandidate[];
|
|
58
|
+
changes: Changes;
|
|
59
|
+
}
|
|
60
|
+
export interface OrderResult {
|
|
61
|
+
order: Order | null;
|
|
62
|
+
/** The patient's draft orders it could be: open one with `ph.order({ id })`. */
|
|
63
|
+
candidates: OrderCandidate[];
|
|
64
|
+
changes: Changes;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Any live object, and its snapshot — for code that handles all three (an inspector, a logger). */
|
|
68
|
+
export type LiveObject = Patient | Prescription | Order;
|
|
69
|
+
export type PatientSnapshot = Patient["snapshot"];
|
|
70
|
+
export type PrescriptionSnapshot = Prescription["snapshot"];
|
|
71
|
+
export type OrderSnapshot = Order["snapshot"];
|
|
72
|
+
export type AnySnapshot = PatientSnapshot | PrescriptionSnapshot | OrderSnapshot;
|
|
73
|
+
|
|
74
|
+
// ---- The instance ----
|
|
75
|
+
|
|
76
|
+
export interface PhotonOptions {
|
|
77
|
+
/** Which Photon environment: every URL is inferred from it. Or pass `url`. */
|
|
78
|
+
environment?: PhotonEnvironment;
|
|
79
|
+
/** network-api's GraphQL endpoint, when not inferred from `environment`. */
|
|
80
|
+
url?: string;
|
|
81
|
+
auth: PhotonAuth;
|
|
82
|
+
/** How this caller is surfaced to network-api. Embeds: EMBEDDABLE_COMPONENT. */
|
|
83
|
+
source?: "WEB_APP" | "EMBEDDABLE_COMPONENT" | "DIRECT_API" | "LLM_AGENT" | "MCP_CONNECTOR";
|
|
84
|
+
/** Prefix for each request's client name ("photon-rx/SyncPatient"). */
|
|
85
|
+
clientName?: string;
|
|
86
|
+
sourceVersion?: string;
|
|
87
|
+
fetch?: typeof fetch;
|
|
88
|
+
/**
|
|
89
|
+
* Where the Photon Rx app is: `url()` and `frame()` links start with it. Unless said, the environment's
|
|
90
|
+
* (`rx.new` in production, `rx.<env>.health` otherwise); the app itself passes its own origin, so its links follow
|
|
91
|
+
* the domain it's served from. Without either (no `environment`), they're paths (`/order/…`).
|
|
92
|
+
*/
|
|
93
|
+
appUrl?: string;
|
|
94
|
+
/**
|
|
95
|
+
* The Photon organization (`org_…`) this instance is for, when the host knows it: `url()` and `frame()` links
|
|
96
|
+
* carry it, so the page they open signs straight in to it (no organization picker). An M2M client's is its
|
|
97
|
+
* `organizationId`.
|
|
98
|
+
*/
|
|
99
|
+
organization?: string;
|
|
100
|
+
/**
|
|
101
|
+
* Turns analytics off: `ph.analytics` is null and nothing is sent. Analytics is on by default — prescribing events
|
|
102
|
+
* go to Photon's analytics-api with the prescriber's token (never for M2M, never PHI).
|
|
103
|
+
*/
|
|
104
|
+
disableAnalytics?: boolean;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Link to a flow: a live object (its record), or what a flow is given. `.parse(href)` reads a link back. */
|
|
108
|
+
export interface LinkTo {
|
|
109
|
+
(target: LiveObject | FlowLink): string;
|
|
110
|
+
/** What an app link holds — the flow and what it's given — or null if it isn't one. */
|
|
111
|
+
parse(href: string): FlowLink | null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* One mutation's calls — the same three for each. `get` reads by id and changes nothing; `sync` sends whatever's
|
|
116
|
+
* known, an id or not, and the server finds, matches, creates or updates; `update` changes that one record.
|
|
117
|
+
* Calling it with an input is `sync`; with nothing, it's a new object before its record.
|
|
118
|
+
*/
|
|
119
|
+
export interface Calls<T, Input, Fields, Result> {
|
|
120
|
+
(): T;
|
|
121
|
+
(input: Input): Promise<Result>;
|
|
122
|
+
/** Id only: the object for that record, or null when there's none. Changes nothing. */
|
|
123
|
+
get(id: string): Promise<T | null>;
|
|
124
|
+
/** What's known — with an id or without: the server decides what it means. */
|
|
125
|
+
sync(input: Input): Promise<Result>;
|
|
126
|
+
/** That one record, by id: only the fields given change. */
|
|
127
|
+
update(id: string, fields: Fields): Promise<Result>;
|
|
128
|
+
/**
|
|
129
|
+
* The app's link to this flow, given Photon ids only: a record's (`"pat_…"` or `{ id }` → `/<flow>/<id>`), or a
|
|
130
|
+
* new one's (`{ templateId, patient: { id } }` → `/<flow>?patient=…&template=…`). Anything else — a patient's
|
|
131
|
+
* details, a drug's name, a sig — is refused: sync first, then link by id. Nothing is read or written.
|
|
132
|
+
*/
|
|
133
|
+
url(given?: Input | string): string;
|
|
134
|
+
/**
|
|
135
|
+
* The same, embeddable (`/<flow>/embed/…`): an iframe's `src`, signing the prescriber in with this instance's
|
|
136
|
+
* client id (`auth: { clientId }`). Throws without one, and for an M2M client.
|
|
137
|
+
*/
|
|
138
|
+
frame(given?: Input | string): string;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface Photon {
|
|
142
|
+
/** The environment's URLs, when created from one. */
|
|
143
|
+
readonly environment: EnvironmentUrls | null;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Photon's analytics, as the signed-in prescriber: `ph.analytics.track(event, properties)`. Null for M2M, without
|
|
147
|
+
* an `environment`, or with `disableAnalytics`.
|
|
148
|
+
*/
|
|
149
|
+
readonly analytics: Analytics | null;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Patients. `sync({ demographic })` finds the patient, lists candidates, says what's missing, or creates one
|
|
153
|
+
* when nothing matches and it has enough. `ph.patient()`: a new patient to `sync` as the prescriber types.
|
|
154
|
+
*/
|
|
155
|
+
readonly patient: Calls<Patient, PatientInput, Omit<PatientInput, "id">, PatientResult>;
|
|
156
|
+
/**
|
|
157
|
+
* Prescriptions. `sync` without a patient is a drug lookup — the choices come back in `changes`; nothing is
|
|
158
|
+
* created. With a patient: created (or their in-progress one continued) once the drug is resolved. A signed
|
|
159
|
+
* prescription is final. `ph.prescription()`: a new line.
|
|
160
|
+
*/
|
|
161
|
+
readonly prescription: Calls<Prescription, PrescriptionInput, PrescriptionEdits, PrescriptionResult>;
|
|
162
|
+
/**
|
|
163
|
+
* Orders. `sync({ patient, prescriptions })` saves the patient's draft order (or a new one) once the patient
|
|
164
|
+
* and a prescription have records. `ph.order()`: a new order to fill with `sync` (live objects, ids, or
|
|
165
|
+
* `{ templateId }` for a new line); its draft saves as it goes.
|
|
166
|
+
*/
|
|
167
|
+
readonly order: Calls<Order, OrderInput, Omit<OrderInput, "id" | "patient">, OrderResult>;
|
|
168
|
+
|
|
169
|
+
/** Read-only order snapshots and optional pharmacy choices. No live objects are opened or updated. */
|
|
170
|
+
orders(input?: OrdersInput): Promise<OrdersResult>;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* network-api's health check: resolves once it's reachable and accepts this instance's token. Throws
|
|
174
|
+
* `PhotonError` — `unauthorized` (a token it won't take), `network` (unreachable, or `getToken` threw),
|
|
175
|
+
* `upstream`. Changes nothing.
|
|
176
|
+
*/
|
|
177
|
+
ping(): Promise<void>;
|
|
178
|
+
|
|
179
|
+
me(): Promise<Caller>;
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* The prescriber's sign-in, when core runs it (`auth: { clientId }`): a live object — `status`, `user`,
|
|
183
|
+
* `start()`, `signIn()`. Null when the host signs in (`auth: { getToken }`).
|
|
184
|
+
*/
|
|
185
|
+
readonly session: Session | null;
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* The Photon Rx app's link to a flow, to share — Photon ids only, in the clear: `/<flow>/<id>`, or
|
|
189
|
+
* `/<flow>?patient=…&template=…` (see links.ts) — on the app (`appUrl`, or the environment's). No client id: the
|
|
190
|
+
* app signs in with its own. A live object's: its record's id; before one, the ids it hangs off (a patient
|
|
191
|
+
* without a record is refused).
|
|
192
|
+
*/
|
|
193
|
+
readonly url: LinkTo;
|
|
194
|
+
/**
|
|
195
|
+
* The same, embeddable — `/<flow>/embed/…`, always zero chrome — for an iframe's `src`. The embed signs the
|
|
196
|
+
* prescriber in with the host's client id — the link's `auth.clientId`, or this instance's `{ clientId }` — and
|
|
197
|
+
* works only with one. Throws without one, and for an M2M client. Links carry no secret, ever.
|
|
198
|
+
*/
|
|
199
|
+
readonly frame: LinkTo;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export interface CallerName {
|
|
203
|
+
first: string | null;
|
|
204
|
+
last: string | null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export interface CallerOrganization {
|
|
208
|
+
id: string;
|
|
209
|
+
name: string;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export type Caller =
|
|
213
|
+
| {
|
|
214
|
+
type: "user";
|
|
215
|
+
id: string;
|
|
216
|
+
name: CallerName;
|
|
217
|
+
organization: CallerOrganization | null;
|
|
218
|
+
permissions: string[];
|
|
219
|
+
}
|
|
220
|
+
| { type: "patient"; id: string; name: CallerName }
|
|
221
|
+
| { type: "machine"; organization: CallerOrganization | null; permissions: string[] };
|
|
222
|
+
|
|
223
|
+
const callerOrganization = (
|
|
224
|
+
organization: { __typename: "Organization"; id: string; name: string } | { __typename: "OperationError" },
|
|
225
|
+
): CallerOrganization | null =>
|
|
226
|
+
organization.__typename === "Organization" ? { id: organization.id, name: organization.name } : null;
|
|
227
|
+
|
|
228
|
+
/** The client a photon instance calls through — internal. */
|
|
229
|
+
const clients = new WeakMap<Photon, NetworkClient>();
|
|
230
|
+
export const clientOf = (ph: Photon): NetworkClient => {
|
|
231
|
+
const client = clients.get(ph);
|
|
232
|
+
if (!client) throw new Error("Not a photon instance.");
|
|
233
|
+
return client;
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
export function photon(options: PhotonOptions): Photon {
|
|
237
|
+
refuseSecretInBrowser(options.auth); // before anything else
|
|
238
|
+
const environment = options.environment ? environmentUrls(options.environment) : null;
|
|
239
|
+
const url = options.url ?? environment?.networkApiUrl;
|
|
240
|
+
if (!url) throw new PhotonError("invalid", "photon() needs an `environment` (or a `url`).");
|
|
241
|
+
// A client id alone: core signs the prescriber in (session.ts), against the environment's Auth0 tenant.
|
|
242
|
+
const auth = options.auth;
|
|
243
|
+
let session: Session | null = null;
|
|
244
|
+
if (signsIn(auth)) {
|
|
245
|
+
if (!environment) throw new PhotonError("invalid", "Signing in needs an `environment`: it names the Auth0 tenant.");
|
|
246
|
+
session = sessionFor({
|
|
247
|
+
domain: environment.auth0Domain,
|
|
248
|
+
audience: environment.auth0Audience,
|
|
249
|
+
clientId: auth.clientId,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
// A client secret: the backend's own token, by client credentials (machine.ts).
|
|
253
|
+
let machine: MachineCredentials | null = null;
|
|
254
|
+
if (isMachine(auth)) {
|
|
255
|
+
if (!environment)
|
|
256
|
+
throw new PhotonError("invalid", "Client credentials need an `environment`: it names the Auth0 tenant.");
|
|
257
|
+
if (!auth.organizationId)
|
|
258
|
+
throw new PhotonError("invalid", "Client credentials need the `organizationId` (org_…) they're for.");
|
|
259
|
+
machine = machineCredentials({
|
|
260
|
+
domain: environment.auth0Domain,
|
|
261
|
+
audience: environment.auth0Audience,
|
|
262
|
+
clientId: auth.clientId,
|
|
263
|
+
clientSecret: auth.clientSecret,
|
|
264
|
+
organizationId: auth.organizationId,
|
|
265
|
+
fetch: options.fetch,
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
const signedIn = session;
|
|
269
|
+
const owned = machine;
|
|
270
|
+
const getToken = signedIn
|
|
271
|
+
? () => signedIn.token()
|
|
272
|
+
: owned
|
|
273
|
+
? () => owned.token()
|
|
274
|
+
: "getToken" in auth
|
|
275
|
+
? auth.getToken
|
|
276
|
+
: null;
|
|
277
|
+
if (!getToken)
|
|
278
|
+
throw new PhotonError("invalid", "photon() needs `auth`: { clientId }, { getToken } or client credentials.");
|
|
279
|
+
return photonOver(
|
|
280
|
+
createNetworkClient({
|
|
281
|
+
url,
|
|
282
|
+
getToken,
|
|
283
|
+
// A refused machine token is dropped and the call tried once more with a new one.
|
|
284
|
+
onUnauthorized: owned ? () => owned.forget() : undefined,
|
|
285
|
+
source: options.source,
|
|
286
|
+
clientName: options.clientName,
|
|
287
|
+
sourceVersion: options.sourceVersion,
|
|
288
|
+
fetch: options.fetch,
|
|
289
|
+
}),
|
|
290
|
+
environment,
|
|
291
|
+
{
|
|
292
|
+
appUrl: options.appUrl ?? environment?.appUrl,
|
|
293
|
+
session,
|
|
294
|
+
linkAuth: linkAuthOf(auth, options.organization),
|
|
295
|
+
analytics:
|
|
296
|
+
environment && !owned && !options.disableAnalytics
|
|
297
|
+
? {
|
|
298
|
+
url: environment.analyticsApiUrl,
|
|
299
|
+
token: getToken,
|
|
300
|
+
clientName: options.clientName ?? null,
|
|
301
|
+
// The MCP's events say so on every one; React's cards set their own.
|
|
302
|
+
integration: options.source === "MCP_CONNECTOR" ? "mcp" : null,
|
|
303
|
+
}
|
|
304
|
+
: null,
|
|
305
|
+
},
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** A photon instance over an existing client (tests, and internal use). */
|
|
310
|
+
export function photonOver(
|
|
311
|
+
client: NetworkClient,
|
|
312
|
+
environment: EnvironmentUrls | null = null,
|
|
313
|
+
options: { appUrl?: string; session?: Session | null; linkAuth?: LinkAuth; analytics?: AnalyticsTarget | null } = {},
|
|
314
|
+
): Photon {
|
|
315
|
+
const held = {
|
|
316
|
+
patients: new Map<string, Patient>(),
|
|
317
|
+
prescriptions: new Map<string, Prescription>(),
|
|
318
|
+
orders: new Map<string, Order>(),
|
|
319
|
+
};
|
|
320
|
+
function getOrMake<T extends { adopt(s: S, c?: Changes): void }, S extends { id: string }>(
|
|
321
|
+
map: Map<string, T>,
|
|
322
|
+
make: (s: S, c?: Changes) => T,
|
|
323
|
+
) {
|
|
324
|
+
return (s: S, changes?: Changes): T => {
|
|
325
|
+
const known = map.get(s.id);
|
|
326
|
+
if (known) {
|
|
327
|
+
known.adopt(s, changes);
|
|
328
|
+
return known;
|
|
329
|
+
}
|
|
330
|
+
const made = make(s, changes);
|
|
331
|
+
map.set(s.id, made);
|
|
332
|
+
return made;
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* What a link to this target gives its flow: a live object's ids — its record's once it has one; before, the ids
|
|
337
|
+
* it hangs off (a prescription's patient, and its template or drug; an order's patient and lines by id, template or drug) — or the link as given.
|
|
338
|
+
* A patient without a record has none: refused.
|
|
339
|
+
*/
|
|
340
|
+
const linkOf = (target: LiveObject | FlowLink): FlowLink => {
|
|
341
|
+
const idOf = (id: string | null | undefined) => (id ? { id } : undefined);
|
|
342
|
+
if (target instanceof Patient) {
|
|
343
|
+
if (!target.id)
|
|
344
|
+
throw refuse(
|
|
345
|
+
"This patient has no record yet, so there's no id to link to: sync them until the server finds or adds them.",
|
|
346
|
+
);
|
|
347
|
+
return { flow: "patient", patient: { id: target.id } };
|
|
348
|
+
}
|
|
349
|
+
if (target instanceof Prescription) {
|
|
350
|
+
if (target.id) return { flow: "prescription", prescription: { id: target.id } };
|
|
351
|
+
// A template line, by its template; else by its drug, once one's picked (or given by id).
|
|
352
|
+
const drug = target.snapshot.pick?.id ?? target.entered.treatment?.id;
|
|
353
|
+
const templateId = target.templateId;
|
|
354
|
+
return {
|
|
355
|
+
flow: "prescription",
|
|
356
|
+
patient: idOf(target.patientId),
|
|
357
|
+
prescription: templateId ? { templateId } : drug ? { treatment: { id: drug } } : undefined,
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
if (target instanceof Order) {
|
|
361
|
+
if (target.id) return { flow: "order", order: { id: target.id } };
|
|
362
|
+
const add = target.prescriptions.flatMap(
|
|
363
|
+
(line): ({ id: string } | { templateId: string } | { treatment: { id: string } })[] => {
|
|
364
|
+
const drug = line.snapshot.pick?.id ?? line.entered.treatment?.id;
|
|
365
|
+
if (line.id) return [{ id: line.id }];
|
|
366
|
+
if (line.templateId) return [{ templateId: line.templateId }];
|
|
367
|
+
return drug ? [{ treatment: { id: drug } }] : [];
|
|
368
|
+
},
|
|
369
|
+
);
|
|
370
|
+
return {
|
|
371
|
+
flow: "order",
|
|
372
|
+
patient: idOf(target.patient?.id),
|
|
373
|
+
order: add.length ? { prescriptions: { add } } : undefined,
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
return target;
|
|
377
|
+
};
|
|
378
|
+
const appUrl = (options.appUrl ?? "").replace(/\/$/, "");
|
|
379
|
+
const linkAuth = options.linkAuth ?? { clientId: null, organization: null, machine: false };
|
|
380
|
+
/**
|
|
381
|
+
* How the page a link opens signs in. An embed signs the prescriber in with the host's client — the link's, or
|
|
382
|
+
* this instance's `{ clientId }` — so it needs one, and never an M2M client's. A webapp link (`url()`) carries
|
|
383
|
+
* none: it's to be shared, and the app signs in with its own. Any link opens in the organization the link names,
|
|
384
|
+
* or this instance's. Nothing else goes in: never a secret.
|
|
385
|
+
*/
|
|
386
|
+
const signedLink = (target: LiveObject | FlowLink, embed: boolean): FlowLink => {
|
|
387
|
+
const { auth: given = {}, ...link } = linkOf(target);
|
|
388
|
+
const other = Object.keys(given).filter((key) => key !== "clientId" && key !== "organization");
|
|
389
|
+
if (other.length)
|
|
390
|
+
throw new PhotonError(
|
|
391
|
+
"invalid",
|
|
392
|
+
`A link carries only a clientId and an organization — never ${other.join(", ")}.`,
|
|
393
|
+
);
|
|
394
|
+
if (embed && linkAuth.machine)
|
|
395
|
+
throw new PhotonError(
|
|
396
|
+
"invalid",
|
|
397
|
+
"frame() is for a prescriber's browser: an M2M client can't make one. Make frames where the prescriber " +
|
|
398
|
+
"signs in — photon({ auth: { clientId } }) — or send a webapp link (url()).",
|
|
399
|
+
);
|
|
400
|
+
const clientId = embed ? given.clientId || linkAuth.clientId : null;
|
|
401
|
+
if (embed && !clientId)
|
|
402
|
+
throw new PhotonError(
|
|
403
|
+
"invalid",
|
|
404
|
+
"frame() needs your Auth0 client id — the embed signs the prescriber in with it: photon({ auth: { clientId } }), " +
|
|
405
|
+
"or ph.frame({ flow, …, auth: { clientId } }).",
|
|
406
|
+
);
|
|
407
|
+
const organization = given.organization || linkAuth.organization;
|
|
408
|
+
const auth = { ...(clientId ? { clientId } : {}), ...(organization ? { organization } : {}) };
|
|
409
|
+
return Object.keys(auth).length ? { ...link, auth } : link;
|
|
410
|
+
};
|
|
411
|
+
const linkTo = (embed: boolean): LinkTo =>
|
|
412
|
+
Object.assign((target: LiveObject | FlowLink) => `${appUrl}${linkPath(signedLink(target, embed), { embed })}`, {
|
|
413
|
+
parse: (href: string) => parseLinkPath(href)?.link ?? null,
|
|
414
|
+
});
|
|
415
|
+
const url = linkTo(false);
|
|
416
|
+
const frame = linkTo(true);
|
|
417
|
+
|
|
418
|
+
const ctx: Context = {
|
|
419
|
+
url: (obj) => url(obj),
|
|
420
|
+
frame: (obj) => frame(obj),
|
|
421
|
+
client,
|
|
422
|
+
held,
|
|
423
|
+
patient: getOrMake(held.patients, (s: PatientRecord, c) => new Patient(ctx, s, c)),
|
|
424
|
+
prescription: getOrMake(held.prescriptions, (s: PrescriptionRecord, c) => new Prescription(ctx, s, { changes: c })),
|
|
425
|
+
order: getOrMake(held.orders, (s: OrderRecord, c) => orderFrom(s, c)),
|
|
426
|
+
openPatient: (id) => openPatient(id),
|
|
427
|
+
openPrescription: (id) => openPrescription(id),
|
|
428
|
+
register(obj) {
|
|
429
|
+
if (!obj.id) return;
|
|
430
|
+
if (obj instanceof Patient && !held.patients.has(obj.id)) held.patients.set(obj.id, obj);
|
|
431
|
+
if (obj instanceof Prescription && !held.prescriptions.has(obj.id)) held.prescriptions.set(obj.id, obj);
|
|
432
|
+
if (obj instanceof Order && !held.orders.has(obj.id)) held.orders.set(obj.id, obj);
|
|
433
|
+
},
|
|
434
|
+
};
|
|
435
|
+
/** An opened order's object: its patient and prescriptions are the shared live objects. */
|
|
436
|
+
function orderFrom(record: OrderRecord, changes?: Changes) {
|
|
437
|
+
return new Order(ctx, record, {
|
|
438
|
+
patient: record.patient ? ctx.patient(record.patient) : null,
|
|
439
|
+
prescriptions: record.prescriptions.map((rx) => held.prescriptions.get(rx.id) ?? ctx.prescription(rx)),
|
|
440
|
+
changes,
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Reads in flight, by id. Asking for a record while it's already being read shares that read — two cards
|
|
446
|
+
* (or React running an effect twice) opening the same order make one GetOrder, not two.
|
|
447
|
+
*/
|
|
448
|
+
const reading = {
|
|
449
|
+
patients: new Map<string, Promise<Patient | null>>(),
|
|
450
|
+
prescriptions: new Map<string, Promise<Prescription | null>>(),
|
|
451
|
+
orders: new Map<string, Promise<Order | null>>(),
|
|
452
|
+
};
|
|
453
|
+
function once<T>(inFlight: Map<string, Promise<T>>, id: string, read: () => Promise<T>): Promise<T> {
|
|
454
|
+
const shared = inFlight.get(id);
|
|
455
|
+
if (shared) return shared;
|
|
456
|
+
const started = read().finally(() => inFlight.delete(id));
|
|
457
|
+
inFlight.set(id, started);
|
|
458
|
+
return started;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
async function openPatient(id: string) {
|
|
462
|
+
return (
|
|
463
|
+
held.patients.get(id) ??
|
|
464
|
+
once(reading.patients, id, async () => {
|
|
465
|
+
const read = await guarded(() => getPatient(client, id));
|
|
466
|
+
return read.kind === "ok" && read.patient
|
|
467
|
+
? ctx.patient(toPatient(read.patient), toChanges(read.changes))
|
|
468
|
+
: null;
|
|
469
|
+
})
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
async function openPrescription(id: string) {
|
|
473
|
+
return (
|
|
474
|
+
held.prescriptions.get(id) ??
|
|
475
|
+
once(reading.prescriptions, id, async () => {
|
|
476
|
+
const read = await guarded(() => getPrescription(client, id));
|
|
477
|
+
return read.kind === "ok" && read.prescription ? ctx.prescription(toPrescription(read.prescription)) : null;
|
|
478
|
+
})
|
|
479
|
+
);
|
|
480
|
+
}
|
|
481
|
+
async function openOrder(id: string) {
|
|
482
|
+
return (
|
|
483
|
+
held.orders.get(id) ??
|
|
484
|
+
once(reading.orders, id, async () => {
|
|
485
|
+
const read = await guarded(() => getOrder(client, id));
|
|
486
|
+
return read.kind === "ok" && read.order ? ctx.order(await adoptOrder(ctx, read.order)) : null;
|
|
487
|
+
})
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// ---- sync: what's known. With an id, that record's object syncs it; without, a new object does (its rules). ----
|
|
492
|
+
|
|
493
|
+
async function syncPatient(input: PatientInput): Promise<PatientResult> {
|
|
494
|
+
const { id, ...fields } = input;
|
|
495
|
+
if (id) {
|
|
496
|
+
const patient = await openPatient(id);
|
|
497
|
+
if (!patient) return { patient: null, candidates: [], changes: noChanges };
|
|
498
|
+
return { patient, candidates: [], ...(await patient.sync(fields)) };
|
|
499
|
+
}
|
|
500
|
+
if (input.clinical)
|
|
501
|
+
throw new PhotonError("invalid", "Clinical changes need the patient's id: sync their demographics first.");
|
|
502
|
+
if (!input.demographic && !input.contact)
|
|
503
|
+
throw new PhotonError("invalid", "Send the patient's id, or what you know of them.");
|
|
504
|
+
const patient = new Patient(ctx, null);
|
|
505
|
+
const { changes } = await patient.sync(fields);
|
|
506
|
+
return { patient: theHeld(held.patients, patient), candidates: [...patient.snapshot.candidates], changes };
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
async function syncPrescription(input: PrescriptionInput): Promise<PrescriptionResult> {
|
|
510
|
+
const { id, patient, ...edits } = input;
|
|
511
|
+
if (id) {
|
|
512
|
+
const rx = await openPrescription(id);
|
|
513
|
+
if (!rx) return { prescription: null, candidates: [], changes: noChanges };
|
|
514
|
+
return { prescription: rx, candidates: [], ...(await rx.sync({ ...edits, patient })) };
|
|
515
|
+
}
|
|
516
|
+
// No patient: a drug lookup (it can never create anything). With one: created once the drug is resolved —
|
|
517
|
+
// never taking a signed prescription, or one while the drug is open.
|
|
518
|
+
const { templateId, ...fields } = edits;
|
|
519
|
+
if (!patient && !fields.treatment && !templateId)
|
|
520
|
+
throw new PhotonError("invalid", "Send a prescription's id, a patient, or a drug to look up.");
|
|
521
|
+
const rx = new Prescription(ctx, null, { templateId });
|
|
522
|
+
const { changes } = await rx.sync({ ...fields, patient });
|
|
523
|
+
const candidates = (rx.snapshot.drafts ?? []).map((d) => ({
|
|
524
|
+
id: d.id,
|
|
525
|
+
instructions: d.instructions,
|
|
526
|
+
dispense: d.dispense,
|
|
527
|
+
}));
|
|
528
|
+
return { prescription: theHeld(held.prescriptions, rx), candidates, changes };
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
async function syncOrder(input: OrderInput): Promise<OrderResult> {
|
|
532
|
+
const { id, patient, prescriptions: rxs, pharmacy } = input;
|
|
533
|
+
if (id) {
|
|
534
|
+
const order = await openOrder(id);
|
|
535
|
+
if (!order) return { order: null, candidates: [], changes: noChanges };
|
|
536
|
+
return { order, candidates: [], ...(await order.sync({ patient, prescriptions: rxs, pharmacy })) };
|
|
537
|
+
}
|
|
538
|
+
if (!patient && !rxs?.add?.length)
|
|
539
|
+
throw new PhotonError("invalid", "Send an order's id, its patient, or prescriptions for it.");
|
|
540
|
+
const order = new Order(ctx, null);
|
|
541
|
+
const { changes } = await order.sync({ patient, prescriptions: rxs, pharmacy });
|
|
542
|
+
return { order: theHeld(held.orders, order), candidates: [...order.snapshot.candidates], changes };
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// ---- update: that one record (its object, read if it isn't held), only the fields given ----
|
|
546
|
+
|
|
547
|
+
async function updatePatient(id: string, fields: Omit<PatientInput, "id">): Promise<PatientResult> {
|
|
548
|
+
const patient = await openPatient(id);
|
|
549
|
+
if (!patient) return { patient: null, candidates: [], changes: noChanges };
|
|
550
|
+
return { patient, candidates: [], ...(await patient.update(fields)) };
|
|
551
|
+
}
|
|
552
|
+
async function updatePrescription(id: string, edits: PrescriptionEdits): Promise<PrescriptionResult> {
|
|
553
|
+
const rx = await openPrescription(id);
|
|
554
|
+
if (!rx) return { prescription: null, candidates: [], changes: noChanges };
|
|
555
|
+
return { prescription: rx, candidates: [], ...(await rx.update(edits)) };
|
|
556
|
+
}
|
|
557
|
+
async function updateOrder(id: string, fields: Omit<OrderInput, "id" | "patient">): Promise<OrderResult> {
|
|
558
|
+
const order = await openOrder(id);
|
|
559
|
+
if (!order) return { order: null, candidates: [], changes: noChanges };
|
|
560
|
+
return { order, candidates: [], ...(await order.update(fields)) };
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/** One object per record: a new object that found a record someone already holds gives way to that one. */
|
|
564
|
+
function theHeld<T extends Patient | Prescription | Order>(map: Map<string, T>, obj: T): T {
|
|
565
|
+
const known = obj.id ? map.get(obj.id) : undefined;
|
|
566
|
+
if (!known || known === obj) return obj;
|
|
567
|
+
known.adopt(obj.state as never, obj.changes);
|
|
568
|
+
return known;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
const calls = <T, I, F, R>(
|
|
572
|
+
flow: "patient" | "prescription" | "order",
|
|
573
|
+
make: () => T,
|
|
574
|
+
get: (id: string) => Promise<T | null>,
|
|
575
|
+
sync: (input: I) => Promise<R>,
|
|
576
|
+
update: (id: string, fields: F) => Promise<R>,
|
|
577
|
+
) => {
|
|
578
|
+
// A link to this flow, given its record's ids (an object, or an id).
|
|
579
|
+
const link = (given?: I | string): FlowLink => ({
|
|
580
|
+
flow,
|
|
581
|
+
...(given ? { [flow]: typeof given === "string" ? { id: given } : given } : {}),
|
|
582
|
+
});
|
|
583
|
+
return Object.assign((input?: I) => (input ? sync(input) : make()), {
|
|
584
|
+
get,
|
|
585
|
+
sync,
|
|
586
|
+
update,
|
|
587
|
+
url: (given?: I | string) => url(link(given)),
|
|
588
|
+
frame: (given?: I | string) => frame(link(given)),
|
|
589
|
+
}) as Calls<T, I, F, R>;
|
|
590
|
+
};
|
|
591
|
+
|
|
592
|
+
const ph: Photon = {
|
|
593
|
+
environment,
|
|
594
|
+
patient: calls("patient", () => new Patient(ctx, null), openPatient, syncPatient, updatePatient),
|
|
595
|
+
prescription: calls(
|
|
596
|
+
"prescription",
|
|
597
|
+
() => new Prescription(ctx, null),
|
|
598
|
+
openPrescription,
|
|
599
|
+
syncPrescription,
|
|
600
|
+
updatePrescription,
|
|
601
|
+
),
|
|
602
|
+
order: calls("order", () => new Order(ctx, null), openOrder, syncOrder, updateOrder),
|
|
603
|
+
orders: (input) => readOrders(client, input),
|
|
604
|
+
ping: async () => void (await guarded(() => ping(client))),
|
|
605
|
+
me: async (): Promise<Caller> => {
|
|
606
|
+
const caller = await guarded(() => me(client));
|
|
607
|
+
switch (caller.__typename) {
|
|
608
|
+
case "User":
|
|
609
|
+
return {
|
|
610
|
+
type: "user",
|
|
611
|
+
id: caller.id,
|
|
612
|
+
name: { first: caller.name.first ?? null, last: caller.name.last ?? null },
|
|
613
|
+
organization: callerOrganization(caller.organization),
|
|
614
|
+
permissions: caller.permissions,
|
|
615
|
+
};
|
|
616
|
+
case "Patient":
|
|
617
|
+
return {
|
|
618
|
+
type: "patient",
|
|
619
|
+
id: caller.id,
|
|
620
|
+
name: { first: caller.name.first ?? null, last: caller.name.last ?? null },
|
|
621
|
+
};
|
|
622
|
+
case "Machine":
|
|
623
|
+
return {
|
|
624
|
+
type: "machine",
|
|
625
|
+
organization: callerOrganization(caller.organization),
|
|
626
|
+
permissions: caller.permissions,
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
},
|
|
630
|
+
session: options.session ?? null,
|
|
631
|
+
analytics: options.analytics
|
|
632
|
+
? analyticsFor(options.analytics, {
|
|
633
|
+
environment,
|
|
634
|
+
providerId: async () => {
|
|
635
|
+
const caller = await ph.me();
|
|
636
|
+
return caller.type === "user" ? caller.id : null;
|
|
637
|
+
},
|
|
638
|
+
})
|
|
639
|
+
: null,
|
|
640
|
+
url,
|
|
641
|
+
frame,
|
|
642
|
+
};
|
|
643
|
+
|
|
644
|
+
clients.set(ph, client);
|
|
645
|
+
return ph;
|
|
646
|
+
}
|