@photonhealth/client 2.0.0-rc.1 → 2.0.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +150 -5
- package/dist/analytics/lifecycleEvents.d.ts +29 -0
- package/dist/analytics/lifecycleEvents.d.ts.map +1 -0
- package/dist/analytics/lifecycleEvents.js +72 -0
- package/dist/analytics/lifecycleEvents.js.map +1 -0
- package/dist/api/client.d.ts +6 -1
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js +47 -33
- package/dist/api/client.js.map +1 -1
- package/dist/api/index.d.ts +2 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +2 -0
- package/dist/api/index.js.map +1 -1
- package/dist/api/me.d.ts +37 -0
- package/dist/api/me.d.ts.map +1 -0
- package/dist/api/me.js +6 -0
- package/dist/api/me.js.map +1 -0
- package/dist/api/orders.d.ts +1047 -0
- package/dist/api/orders.d.ts.map +1 -0
- package/dist/api/orders.js +7 -0
- package/dist/api/orders.js.map +1 -0
- package/dist/api/patient.d.ts +5 -2
- package/dist/api/patient.d.ts.map +1 -1
- package/dist/api/patient.js +1 -1
- package/dist/api/patient.js.map +1 -1
- package/dist/api/prescription.d.ts +7 -1
- package/dist/api/prescription.d.ts.map +1 -1
- package/dist/api/prescription.js +10 -1
- package/dist/api/prescription.js.map +1 -1
- package/dist/gql/gql.d.ts +11 -3
- package/dist/gql/gql.d.ts.map +1 -1
- package/dist/gql/gql.js +5 -3
- package/dist/gql/gql.js.map +1 -1
- package/dist/gql/graphql.d.ts +10507 -1563
- package/dist/gql/graphql.d.ts.map +1 -1
- package/dist/gql/graphql.js +2984 -435
- package/dist/gql/graphql.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/meta/version.d.ts +2 -0
- package/dist/meta/version.d.ts.map +1 -0
- package/dist/meta/version.js +2 -0
- package/dist/meta/version.js.map +1 -0
- package/dist/photon/analytics.d.ts +39 -0
- package/dist/photon/analytics.d.ts.map +1 -0
- package/dist/photon/analytics.js +91 -0
- package/dist/photon/analytics.js.map +1 -0
- package/dist/photon/auth.d.ts +9 -5
- package/dist/photon/auth.d.ts.map +1 -1
- package/dist/photon/auth.js +2 -6
- package/dist/photon/auth.js.map +1 -1
- package/dist/photon/choices.d.ts +1 -1
- package/dist/photon/choices.js +2 -2
- package/dist/photon/choices.js.map +1 -1
- package/dist/photon/environments.d.ts +2 -1
- package/dist/photon/environments.d.ts.map +1 -1
- package/dist/photon/environments.js +2 -1
- package/dist/photon/environments.js.map +1 -1
- package/dist/photon/index.d.ts +6 -3
- package/dist/photon/index.d.ts.map +1 -1
- package/dist/photon/index.js +2 -1
- package/dist/photon/index.js.map +1 -1
- package/dist/photon/machine.d.ts +28 -0
- package/dist/photon/machine.d.ts.map +1 -0
- package/dist/photon/machine.js +53 -0
- package/dist/photon/machine.js.map +1 -0
- package/dist/photon/map.d.ts +6 -2
- package/dist/photon/map.d.ts.map +1 -1
- package/dist/photon/map.js +133 -33
- package/dist/photon/map.js.map +1 -1
- package/dist/photon/order.d.ts +21 -1
- package/dist/photon/order.d.ts.map +1 -1
- package/dist/photon/order.js +79 -12
- package/dist/photon/order.js.map +1 -1
- package/dist/photon/orders.d.ts +5 -0
- package/dist/photon/orders.d.ts.map +1 -0
- package/dist/photon/orders.js +39 -0
- package/dist/photon/orders.js.map +1 -0
- package/dist/photon/patient.d.ts +8 -6
- package/dist/photon/patient.d.ts.map +1 -1
- package/dist/photon/patient.js +61 -30
- package/dist/photon/patient.js.map +1 -1
- package/dist/photon/photon.d.ts +39 -1
- package/dist/photon/photon.d.ts.map +1 -1
- package/dist/photon/photon.js +85 -5
- package/dist/photon/photon.js.map +1 -1
- package/dist/photon/prescription.d.ts +17 -4
- package/dist/photon/prescription.d.ts.map +1 -1
- package/dist/photon/prescription.js +48 -11
- package/dist/photon/prescription.js.map +1 -1
- package/dist/photon/session.d.ts +15 -10
- package/dist/photon/session.d.ts.map +1 -1
- package/dist/photon/session.js +5 -3
- package/dist/photon/session.js.map +1 -1
- package/dist/photon/types.d.ts +278 -46
- package/dist/photon/types.d.ts.map +1 -1
- package/dist/test-utils/msw.d.ts +2 -2
- package/dist/test-utils/msw.d.ts.map +1 -1
- package/dist/test-utils/msw.js +5 -5
- package/dist/test-utils/msw.js.map +1 -1
- package/dist/testing/{scriptedClient.d.ts → fakeNetworkApiClient.d.ts} +5 -4
- package/dist/testing/fakeNetworkApiClient.d.ts.map +1 -0
- package/dist/testing/{scriptedClient.js → fakeNetworkApiClient.js} +8 -4
- package/dist/testing/fakeNetworkApiClient.js.map +1 -0
- package/dist/testing/fixtures.d.ts +1047 -0
- package/dist/testing/fixtures.d.ts.map +1 -1
- package/dist/testing/fixtures.js +47 -13
- package/dist/testing/fixtures.js.map +1 -1
- package/dist/testing.d.ts +4 -4
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +4 -4
- package/dist/testing.js.map +1 -1
- package/package.json +2 -2
- package/src/analytics/lifecycleEvents.test.ts +417 -0
- package/src/analytics/lifecycleEvents.ts +155 -0
- package/src/api/client.ts +71 -46
- package/src/api/index.ts +2 -0
- package/src/api/me.ts +7 -0
- package/src/api/orders.ts +8 -0
- package/src/api/patient.ts +11 -2
- package/src/api/prescription.ts +18 -0
- package/src/gql/gql.ts +21 -9
- package/src/gql/graphql.ts +4280 -597
- package/src/index.ts +1 -1
- package/src/meta/version.test.ts +10 -0
- package/src/meta/version.ts +1 -0
- package/src/operations/fragments.graphql +166 -20
- package/src/operations/me.graphql +38 -0
- package/src/operations/orders.graphql +35 -0
- package/src/operations/patient.graphql +29 -3
- package/src/operations/prescription.graphql +20 -0
- package/src/photon/analytics.ts +129 -0
- package/src/photon/auth.ts +9 -10
- package/src/photon/choices.ts +2 -2
- package/src/photon/environments.ts +3 -1
- package/src/photon/index.ts +16 -2
- package/src/photon/machine.ts +90 -0
- package/src/photon/map.ts +151 -35
- package/src/photon/order.test.ts +142 -0
- package/src/photon/order.ts +92 -12
- package/src/photon/orders.ts +42 -0
- package/src/photon/patient.ts +73 -33
- package/src/photon/photon.ts +139 -6
- package/src/photon/prescription.ts +71 -12
- package/src/photon/session.ts +18 -9
- package/src/photon/types.ts +280 -26
- package/src/test-utils/msw.ts +16 -5
- package/src/testing/{scriptedClient.ts → fakeNetworkApiClient.ts} +8 -4
- package/src/testing/fixtures.ts +55 -13
- package/src/testing.ts +6 -3
- package/dist/testing/scriptedClient.d.ts.map +0 -1
- package/dist/testing/scriptedClient.js.map +0 -1
package/src/photon/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The public API: `photon()` and what it returns. network-api's three upsert mutations, one call each,
|
|
3
3
|
* returning live objects with the workflow rules inside — objects that can exist before their record. See
|
|
4
|
-
*
|
|
4
|
+
* the package README.
|
|
5
5
|
*/
|
|
6
6
|
export { photon, Patient, Prescription, Order } from "./photon.ts";
|
|
7
7
|
export type {
|
|
@@ -17,14 +17,28 @@ export type {
|
|
|
17
17
|
PatientSnapshot,
|
|
18
18
|
PrescriptionSnapshot,
|
|
19
19
|
OrderSnapshot,
|
|
20
|
+
Caller,
|
|
21
|
+
CallerName,
|
|
22
|
+
CallerOrganization,
|
|
20
23
|
} from "./photon.ts";
|
|
24
|
+
export { clientVersion } from "../meta/version.ts";
|
|
25
|
+
export type { Analytics, AnalyticsTarget, AnalyticsProperties } from "./analytics.ts";
|
|
26
|
+
export type {
|
|
27
|
+
DraftPrescriptionForLifecycleAnalytics,
|
|
28
|
+
DraftPrescriptionSource,
|
|
29
|
+
LifecycleAnalytics,
|
|
30
|
+
LifecycleEventName,
|
|
31
|
+
OrderForLifecycleAnalytics,
|
|
32
|
+
PatientForLifecycleAnalytics,
|
|
33
|
+
PrescriptionForLifecycleAnalytics,
|
|
34
|
+
} from "../analytics/lifecycleEvents.ts";
|
|
21
35
|
export type { PatientExtra } from "./patient.ts";
|
|
22
36
|
export type { PrescriptionExtra, PrescriptionUpdate } from "./prescription.ts";
|
|
23
37
|
export type { OrderExtra, OrderUpdate } from "./order.ts";
|
|
24
38
|
export type { LiveStatus, LiveSnapshot, LiveBase } from "./live.ts";
|
|
25
39
|
export type { PhotonAuth } from "./auth.ts";
|
|
26
40
|
export { Session } from "./session.ts";
|
|
27
|
-
export type { SessionStatus, SessionSnapshot, SessionUser, SessionConfig } from "./session.ts";
|
|
41
|
+
export type { SessionStatus, SessionSnapshot, SessionUser, SessionConfig, SignInOptions } from "./session.ts";
|
|
28
42
|
export { PhotonError } from "./errors.ts";
|
|
29
43
|
export type { Flow, FlowLink } from "./links.ts";
|
|
30
44
|
export type { PhotonErrorKind } from "./errors.ts";
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { PhotonError } from "./errors.ts";
|
|
2
|
+
import { claimsOf } from "./session.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A backend's own sign-in: OAuth 2 client credentials against the environment's Auth0 tenant, with the M2M client
|
|
6
|
+
* Photon made for the organization (`client_metadata.org_id`, which Auth0 puts on the token as its org claim).
|
|
7
|
+
* Servers only — `photon()` refuses a secret in a browser before it gets here.
|
|
8
|
+
*
|
|
9
|
+
* token() the access token: one from Auth0, kept until shortly before it expires; concurrent calls share one
|
|
10
|
+
* request.
|
|
11
|
+
* forget() drop it, so the next call gets a new one (network-api refused it: a 401).
|
|
12
|
+
*
|
|
13
|
+
* The token's organization must be `organizationId`: links name that organization, so calls made in another would
|
|
14
|
+
* go somewhere the links don't.
|
|
15
|
+
*/
|
|
16
|
+
export interface MachineConfig {
|
|
17
|
+
/** The Auth0 tenant (`auth.<env>.health`). */
|
|
18
|
+
domain: string;
|
|
19
|
+
/** The token audience network-api accepts. */
|
|
20
|
+
audience: string;
|
|
21
|
+
clientId: string;
|
|
22
|
+
clientSecret: string;
|
|
23
|
+
organizationId: string;
|
|
24
|
+
fetch?: typeof fetch;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface MachineCredentials {
|
|
28
|
+
token(): Promise<string>;
|
|
29
|
+
forget(): void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Renew this long before the token expires. */
|
|
33
|
+
const EARLY_MS = 60_000;
|
|
34
|
+
/** Where Photon's Auth0 action puts the organization on a machine token. */
|
|
35
|
+
const ORG_CLAIM = "http://photon.health/org_id";
|
|
36
|
+
|
|
37
|
+
export function machineCredentials(config: MachineConfig): MachineCredentials {
|
|
38
|
+
let held: { token: string; expiresAt: number } | null = null;
|
|
39
|
+
let pending: Promise<string> | null = null;
|
|
40
|
+
|
|
41
|
+
const request = async (): Promise<string> => {
|
|
42
|
+
const response = await (config.fetch ?? fetch)(`https://${config.domain}/oauth/token`, {
|
|
43
|
+
method: "POST",
|
|
44
|
+
headers: { "Content-Type": "application/json" },
|
|
45
|
+
body: JSON.stringify({
|
|
46
|
+
grant_type: "client_credentials",
|
|
47
|
+
client_id: config.clientId,
|
|
48
|
+
client_secret: config.clientSecret,
|
|
49
|
+
audience: config.audience,
|
|
50
|
+
}),
|
|
51
|
+
}).catch((e: unknown) => {
|
|
52
|
+
throw new PhotonError("network", "Couldn't reach Photon's sign-in for a machine token.", true, { cause: e });
|
|
53
|
+
});
|
|
54
|
+
const json = (await response.json().catch(() => ({}))) as {
|
|
55
|
+
access_token?: string;
|
|
56
|
+
expires_in?: number;
|
|
57
|
+
error?: string;
|
|
58
|
+
error_description?: string;
|
|
59
|
+
};
|
|
60
|
+
if (!response.ok || !json.access_token) {
|
|
61
|
+
const reason = json.error_description ?? json.error ?? `Auth0 responded ${response.status}`;
|
|
62
|
+
if (response.status >= 500) throw new PhotonError("network", reason, true);
|
|
63
|
+
throw new PhotonError("unauthorized", `The client credentials were refused: ${reason}`);
|
|
64
|
+
}
|
|
65
|
+
const claims = claimsOf(json.access_token);
|
|
66
|
+
const organization = claims[ORG_CLAIM] ?? claims.org_id;
|
|
67
|
+
if (organization !== config.organizationId) {
|
|
68
|
+
throw new PhotonError(
|
|
69
|
+
"unauthorized",
|
|
70
|
+
`This client's tokens are for ${typeof organization === "string" ? organization : "no organization"}, ` +
|
|
71
|
+
`not ${config.organizationId}: use the client Photon made for that organization.`,
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
held = { token: json.access_token, expiresAt: Date.now() + (json.expires_in ?? 0) * 1000 };
|
|
75
|
+
return json.access_token;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
token() {
|
|
80
|
+
if (held && held.expiresAt - EARLY_MS > Date.now()) return Promise.resolve(held.token);
|
|
81
|
+
pending ??= request().finally(() => {
|
|
82
|
+
pending = null;
|
|
83
|
+
});
|
|
84
|
+
return pending;
|
|
85
|
+
},
|
|
86
|
+
forget() {
|
|
87
|
+
held = null;
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
package/src/photon/map.ts
CHANGED
|
@@ -4,10 +4,14 @@ import type {
|
|
|
4
4
|
OrderCandidateFieldsFragment,
|
|
5
5
|
OrderFieldsFragment,
|
|
6
6
|
PatientFieldsFragment,
|
|
7
|
+
PharmacyEventFieldsFragment,
|
|
8
|
+
OrganizationFieldsFragment,
|
|
7
9
|
PharmacyFieldsFragment,
|
|
8
10
|
PharmacyOfferFieldsFragment,
|
|
11
|
+
RedemptionMethodFieldsFragment,
|
|
9
12
|
PrescriptionFieldsFragment,
|
|
10
13
|
ScreeningAlertFieldsFragment,
|
|
14
|
+
PrescriptionTemplateFieldsFragment,
|
|
11
15
|
TreatmentFieldsFragment,
|
|
12
16
|
} from "../gql/graphql.ts";
|
|
13
17
|
import { isInlineOption, optionPath, parseOptionValue, type ChangeSet } from "../api/index.ts";
|
|
@@ -19,9 +23,13 @@ import type {
|
|
|
19
23
|
OrderRecord,
|
|
20
24
|
PatientRecord,
|
|
21
25
|
Pharmacy,
|
|
26
|
+
PharmacyEvent,
|
|
27
|
+
Organization,
|
|
22
28
|
PharmacyOffer,
|
|
23
29
|
PrescriptionRecord,
|
|
30
|
+
RedemptionMethod,
|
|
24
31
|
ScreeningAlert,
|
|
32
|
+
PrescriptionTemplate,
|
|
25
33
|
Treatment,
|
|
26
34
|
} from "./types.ts";
|
|
27
35
|
|
|
@@ -30,28 +38,76 @@ import type {
|
|
|
30
38
|
* so a schema change stops here (and typecheck says where).
|
|
31
39
|
*/
|
|
32
40
|
|
|
41
|
+
/** The union has one member today, tagged by `type` so a second one doesn't change how callers switch. */
|
|
42
|
+
const redemptionMethod = (m: RedemptionMethodFieldsFragment): RedemptionMethod => ({
|
|
43
|
+
type: "discountCard",
|
|
44
|
+
id: m.id,
|
|
45
|
+
prescriptionId: m.prescriptionId,
|
|
46
|
+
bin: m.bin,
|
|
47
|
+
pcn: m.pcn,
|
|
48
|
+
group: m.group,
|
|
49
|
+
memberId: m.memberId,
|
|
50
|
+
externalUrl: m.externalUrl,
|
|
51
|
+
});
|
|
52
|
+
|
|
33
53
|
const offer = (o: PharmacyOfferFieldsFragment): PharmacyOffer => ({
|
|
34
54
|
source: o.source,
|
|
55
|
+
prescriptionId: o.prescriptionId,
|
|
56
|
+
priceType: o.priceType ?? null,
|
|
35
57
|
isPromoted: o.isPromoted,
|
|
36
58
|
attributeTags: o.attributeTags.map(({ kind, label }) => ({ kind, label })),
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
59
|
+
price: o.price && {
|
|
60
|
+
amount: o.price.amount,
|
|
61
|
+
retailAmount: o.price.retailAmount,
|
|
62
|
+
promotions: o.price.promotions.map((p) => ({ type: p.type, baseAmount: p.baseAmount, amountSaved: p.amountSaved })),
|
|
63
|
+
},
|
|
64
|
+
deliveryEstimate: o.deliveryEstimate && {
|
|
65
|
+
deliveryPromise: o.deliveryEstimate.deliveryPromise,
|
|
66
|
+
deliveryPromiseRangeStart: o.deliveryEstimate.deliveryPromiseRangeStart,
|
|
67
|
+
deliveryPromiseRangeEnd: o.deliveryEstimate.deliveryPromiseRangeEnd,
|
|
68
|
+
},
|
|
69
|
+
redemptionMethods: o.redemptionMethods.map(redemptionMethod),
|
|
70
|
+
redemptionState: o.redemptionState,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const pharmacyEvent = (event: PharmacyEventFieldsFragment): PharmacyEvent =>
|
|
74
|
+
event.__typename === "PharmacyOpen24HrEvent"
|
|
75
|
+
? { type: "24hr" }
|
|
76
|
+
: { type: event.__typename === "PharmacyOpenEvent" ? "open" : "close", datetime: event.datetime };
|
|
77
|
+
|
|
78
|
+
const organization = (o: OrganizationFieldsFragment): Organization => ({
|
|
79
|
+
id: o.id,
|
|
80
|
+
name: o.name,
|
|
81
|
+
customer: o.customer?.__typename === "Customer" ? { id: o.customer.id, name: o.customer.name } : null,
|
|
82
|
+
settings: o.settings && {
|
|
83
|
+
brandColor: o.settings.brandColor,
|
|
84
|
+
brandLogo: o.settings.brandLogo,
|
|
85
|
+
priorAuthorizationExceptionMessage: o.settings.priorAuthorizationExceptionMessage,
|
|
86
|
+
patientUx: {
|
|
87
|
+
enablePatientRerouting: o.settings.patientUx.enablePatientRerouting,
|
|
88
|
+
enableAutomatedOps: o.settings.patientUx.enableAutomatedOps,
|
|
89
|
+
},
|
|
42
90
|
},
|
|
43
|
-
prescriptionDeliveryEstimates: o.prescriptionDeliveryEstimates.map((d) => ({
|
|
44
|
-
prescriptionId: d.prescriptionId,
|
|
45
|
-
deliveryPromise: d.deliveryPromise,
|
|
46
|
-
deliveryPromiseRangeStart: d.deliveryPromiseRangeStart,
|
|
47
|
-
deliveryPromiseRangeEnd: d.deliveryPromiseRangeEnd,
|
|
48
|
-
})),
|
|
49
91
|
});
|
|
50
92
|
|
|
51
93
|
const pharmacy = (p: PharmacyFieldsFragment | null): Pharmacy | null =>
|
|
52
94
|
p && {
|
|
53
95
|
id: p.id,
|
|
54
96
|
name: p.name,
|
|
97
|
+
logo: p.logo ?? null,
|
|
98
|
+
phone: p.phone ?? null,
|
|
99
|
+
hours:
|
|
100
|
+
p.hours?.map((h) => ({
|
|
101
|
+
openFrom: h.openFrom,
|
|
102
|
+
openUntil: h.openUntil,
|
|
103
|
+
dayOfWeek: h.dayOfWeek,
|
|
104
|
+
is24Hr: h.is24Hr,
|
|
105
|
+
timezone: h.timezone,
|
|
106
|
+
})) ?? null,
|
|
107
|
+
isOpen: p.isOpen ?? null,
|
|
108
|
+
nextEvents: p.nextEvents
|
|
109
|
+
? { open: pharmacyEvent(p.nextEvents.open), close: pharmacyEvent(p.nextEvents.close) }
|
|
110
|
+
: null,
|
|
55
111
|
address: p.address && {
|
|
56
112
|
street1: p.address.street1,
|
|
57
113
|
street2: p.address.street2,
|
|
@@ -74,6 +130,7 @@ const recorded = <T>({ status, entries }: { status?: ListDocumentationStatus | n
|
|
|
74
130
|
|
|
75
131
|
export function toPatient(p: PatientFieldsFragment): PatientRecord {
|
|
76
132
|
const d = p.demographic;
|
|
133
|
+
const ct = p.contact;
|
|
77
134
|
const c = p.clinical;
|
|
78
135
|
return {
|
|
79
136
|
id: p.id,
|
|
@@ -82,26 +139,28 @@ export function toPatient(p: PatientFieldsFragment): PatientRecord {
|
|
|
82
139
|
dateOfBirth: d.dateOfBirth,
|
|
83
140
|
sex: d.sex,
|
|
84
141
|
gender: d.gender,
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
142
|
+
},
|
|
143
|
+
contact: ct && {
|
|
144
|
+
email: ct.email,
|
|
145
|
+
phone: ct.phone,
|
|
146
|
+
address: ct.address && {
|
|
147
|
+
street1: ct.address.street1,
|
|
148
|
+
street2: ct.address.street2,
|
|
149
|
+
city: ct.address.city,
|
|
150
|
+
state: ct.address.state,
|
|
151
|
+
postalCode: ct.address.postalCode,
|
|
152
|
+
country: ct.address.country,
|
|
94
153
|
},
|
|
95
|
-
benefits:
|
|
96
|
-
recorded(d.benefits)?.map((b) => ({
|
|
97
|
-
id: b.id,
|
|
98
|
-
bin: b.bin,
|
|
99
|
-
pcn: b.pcn,
|
|
100
|
-
groupId: b.groupId,
|
|
101
|
-
memberId: b.memberId,
|
|
102
|
-
})) ?? null,
|
|
103
|
-
preferredPharmacy: pharmacy(d.preferredPharmacy),
|
|
104
154
|
},
|
|
155
|
+
benefits:
|
|
156
|
+
recorded(p.benefits)?.map((b) => ({
|
|
157
|
+
id: b.id,
|
|
158
|
+
bin: b.bin,
|
|
159
|
+
pcn: b.pcn,
|
|
160
|
+
groupId: b.groupId,
|
|
161
|
+
memberId: b.memberId,
|
|
162
|
+
})) ?? null,
|
|
163
|
+
preferredPharmacy: pharmacy(p.preferredPharmacy),
|
|
105
164
|
clinical: c && {
|
|
106
165
|
allergies: recorded(c.allergies)?.map((a) => ({ id: a.id, name: a.name, rxNormId: a.rxNormId })) ?? null,
|
|
107
166
|
medications:
|
|
@@ -120,7 +179,9 @@ const toScreeningAlert = (a: ScreeningAlertFieldsFragment): ScreeningAlert => ({
|
|
|
120
179
|
involvedEntities: a.involvedEntities.map((e) => ({ id: e.id, name: e.name })),
|
|
121
180
|
});
|
|
122
181
|
|
|
123
|
-
const toTreatment = (
|
|
182
|
+
const toTreatment = (
|
|
183
|
+
t: TreatmentFieldsFragment & { templates?: readonly PrescriptionTemplateFieldsFragment[] | null },
|
|
184
|
+
): Treatment => ({
|
|
124
185
|
id: String(t.id),
|
|
125
186
|
name: t.name,
|
|
126
187
|
prescribableName: t.prescribableName,
|
|
@@ -135,12 +196,44 @@ const toTreatment = (t: TreatmentFieldsFragment): Treatment => ({
|
|
|
135
196
|
strength: t.strength,
|
|
136
197
|
medicationType: t.medicationType,
|
|
137
198
|
schedule: t.schedule,
|
|
138
|
-
|
|
199
|
+
templates: t.templates ? t.templates.map(toPrescriptionTemplate) : null,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
export const toPrescriptionTemplate = (t: PrescriptionTemplateFieldsFragment): PrescriptionTemplate => ({
|
|
203
|
+
id: t.id,
|
|
204
|
+
source: t.source,
|
|
205
|
+
name: t.name,
|
|
206
|
+
instructions: t.instructions,
|
|
207
|
+
dispense: t.dispense && {
|
|
208
|
+
quantity: t.dispense.quantity,
|
|
209
|
+
unit: t.dispense.unit,
|
|
210
|
+
daysSupply: t.dispense.daysSupply,
|
|
211
|
+
refillsAllowed: t.dispense.refillsAllowed,
|
|
212
|
+
dispenseAsWritten: t.dispense.dispenseAsWritten,
|
|
213
|
+
},
|
|
139
214
|
});
|
|
140
215
|
|
|
141
216
|
export function toPrescription(rx: PrescriptionFieldsFragment): PrescriptionRecord {
|
|
142
217
|
return {
|
|
143
218
|
id: rx.id,
|
|
219
|
+
prescriber: rx.prescriber
|
|
220
|
+
? {
|
|
221
|
+
id: rx.prescriber.id,
|
|
222
|
+
name: { first: rx.prescriber.name.first, middle: rx.prescriber.name.middle, last: rx.prescriber.name.last },
|
|
223
|
+
}
|
|
224
|
+
: null,
|
|
225
|
+
fulfillment: rx.fulfillment
|
|
226
|
+
? {
|
|
227
|
+
state: rx.fulfillment.state,
|
|
228
|
+
pharmacyEstimatedReadyAt: rx.fulfillment.pharmacyEstimatedReadyAt,
|
|
229
|
+
exceptions: rx.fulfillment.exceptions.map((x) => ({
|
|
230
|
+
id: x.id,
|
|
231
|
+
type: x.type,
|
|
232
|
+
message: x.message,
|
|
233
|
+
createdAt: x.createdAt,
|
|
234
|
+
})),
|
|
235
|
+
}
|
|
236
|
+
: null,
|
|
144
237
|
templateId: rx.templateId,
|
|
145
238
|
patient: rx.patient && { id: rx.patient.id },
|
|
146
239
|
treatment: toTreatment(rx.treatment),
|
|
@@ -170,11 +263,32 @@ export function toOrder(o: OrderFieldsFragment): OrderRecord {
|
|
|
170
263
|
return {
|
|
171
264
|
id: o.id,
|
|
172
265
|
state: o.state,
|
|
266
|
+
organization: o.organization?.__typename === "Organization" ? organization(o.organization) : null,
|
|
173
267
|
patient: o.patient?.__typename === "Patient" ? toPatient(o.patient) : null,
|
|
174
268
|
prescriptions: o.prescriptions.map(toPrescription),
|
|
175
269
|
screeningAlerts: (o.screeningAlerts ?? []).map(toScreeningAlert),
|
|
270
|
+
// Null until a pharmacy is chosen — kept as null, not flattened to [], so "unpriced" stays distinct.
|
|
271
|
+
offers: { atPharmacy: o.offers.atPharmacy?.map(offer) ?? null },
|
|
176
272
|
pharmacy: pharmacy(o.pharmacy),
|
|
177
|
-
|
|
273
|
+
fulfillment: o.fulfillment && {
|
|
274
|
+
state: o.fulfillment.state,
|
|
275
|
+
history: o.fulfillment.history.map((entry) => ({
|
|
276
|
+
pharmacy: pharmacy(entry.pharmacy)!,
|
|
277
|
+
selector: entry.selector,
|
|
278
|
+
reason: entry.reason,
|
|
279
|
+
createdAt: entry.createdAt,
|
|
280
|
+
})),
|
|
281
|
+
routability: o.fulfillment.routability && {
|
|
282
|
+
allowed: o.fulfillment.routability.allowed,
|
|
283
|
+
reason: o.fulfillment.routability.reason,
|
|
284
|
+
},
|
|
285
|
+
exceptions: o.fulfillment.exceptions.map((x) => ({
|
|
286
|
+
id: x.id,
|
|
287
|
+
type: x.type,
|
|
288
|
+
message: x.message,
|
|
289
|
+
createdAt: x.createdAt,
|
|
290
|
+
})),
|
|
291
|
+
},
|
|
178
292
|
};
|
|
179
293
|
}
|
|
180
294
|
|
|
@@ -193,7 +307,7 @@ type Choice = NonNullable<ChangeFieldsFragment["after"]>;
|
|
|
193
307
|
|
|
194
308
|
const choicePath = (o: Choice) => (isInlineOption(o) ? optionPath(o) : null);
|
|
195
309
|
|
|
196
|
-
const
|
|
310
|
+
export const toChoice = (o: Choice): ChangeOption => {
|
|
197
311
|
const path = choicePath(o);
|
|
198
312
|
const value = parseOptionValue(o.value);
|
|
199
313
|
return {
|
|
@@ -209,12 +323,13 @@ const choice = (o: Choice): ChangeOption => {
|
|
|
209
323
|
type: r.type,
|
|
210
324
|
label: r.label,
|
|
211
325
|
confidence: r.confidence ?? null,
|
|
326
|
+
value: r.value ? { displayValue: r.value.displayValue, value: r.value.value, unit: r.value.unit } : null,
|
|
212
327
|
})),
|
|
213
328
|
};
|
|
214
329
|
};
|
|
215
330
|
|
|
216
331
|
const change = (c: ChangeFieldsFragment, sent: unknown): Change => {
|
|
217
|
-
const after = c.after ?
|
|
332
|
+
const after = c.after ? toChoice(c.after) : null;
|
|
218
333
|
return {
|
|
219
334
|
key: c.key,
|
|
220
335
|
label: c.label,
|
|
@@ -223,7 +338,8 @@ const change = (c: ChangeFieldsFragment, sent: unknown): Change => {
|
|
|
223
338
|
reason: c.reason,
|
|
224
339
|
category: c.category,
|
|
225
340
|
operation: c.operation,
|
|
226
|
-
options: (c.options ?? []).map(
|
|
341
|
+
options: (c.options ?? []).map(toChoice),
|
|
342
|
+
after,
|
|
227
343
|
inputText: inputTextOf(c, sent),
|
|
228
344
|
resolvedId: c.status === "APPLIED" ? (after?.id ?? after?.treatment?.id ?? after?.pharmacy?.id ?? null) : null,
|
|
229
345
|
};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
change,
|
|
4
|
+
fakeNetworkApiClient,
|
|
5
|
+
listedOrder,
|
|
6
|
+
order,
|
|
7
|
+
orders,
|
|
8
|
+
patient,
|
|
9
|
+
payload,
|
|
10
|
+
photonOver,
|
|
11
|
+
rx,
|
|
12
|
+
treatment,
|
|
13
|
+
} from "../testing.ts";
|
|
14
|
+
|
|
15
|
+
const metoprolol = rx({ id: "rx_9", treatment: treatment({ id: "trt_9", name: "Metoprolol Tartrate" }) });
|
|
16
|
+
const draft = (id = "ord_9", prescriptions = [metoprolol]) => listedOrder({ id, prescriptions });
|
|
17
|
+
|
|
18
|
+
/** A new order whose patient has these draft orders (Orders answers with them). */
|
|
19
|
+
const setUp = (drafts: ReturnType<typeof draft>[], more: Parameters<typeof fakeNetworkApiClient>[0] = {}) => {
|
|
20
|
+
const fake = fakeNetworkApiClient({
|
|
21
|
+
GetPatient: () => payload("patient", "PatientPayload", patient()),
|
|
22
|
+
Orders: () => orders(drafts),
|
|
23
|
+
GetOrder: (vars) => payload("order", "OrderPayload", order({ id: vars.id, prescriptions: [metoprolol] })),
|
|
24
|
+
GetPrescription: () => payload("prescription", "PrescriptionPayload", metoprolol),
|
|
25
|
+
UpdateOrder: (vars) => payload("order", "OrderPayload", order({ id: vars.id, prescriptions: [] })),
|
|
26
|
+
...more,
|
|
27
|
+
});
|
|
28
|
+
return { ...fake, ph: photonOver(fake.client) };
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
describe("a new order looks for its patient's draft order", () => {
|
|
32
|
+
it("one draft: it's `draft`, for the prescriber to decide — nothing saved, the order still new", async () => {
|
|
33
|
+
const { ph, names, calls } = setUp([draft()]);
|
|
34
|
+
const order = ph.order();
|
|
35
|
+
await order.sync({ patient: { id: "pat_1" } });
|
|
36
|
+
expect(calls.find((c) => c.op === "Orders")!.vars.filter).toEqual({ patient: { id: "pat_1" }, state: "DRAFT" });
|
|
37
|
+
expect(order.snapshot.draft).toMatchObject({ id: "ord_9", prescriptions: [{ id: "rx_9" }] });
|
|
38
|
+
expect(order.id).toBeNull();
|
|
39
|
+
expect(names()).not.toContain("SyncOrder");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("continueDraft(): the order becomes the draft, showing only the draft's prescriptions", async () => {
|
|
43
|
+
const { ph } = setUp([draft()]);
|
|
44
|
+
const order = ph.order();
|
|
45
|
+
await order.sync({ patient: { id: "pat_1" }, prescriptions: { add: [{}] } });
|
|
46
|
+
await order.continueDraft();
|
|
47
|
+
expect(order.id).toBe("ord_9");
|
|
48
|
+
expect(order.snapshot.draft).toBeNull();
|
|
49
|
+
expect(order.prescriptions.map((l) => l.id)).toEqual(["rx_9"]); // the empty line gives way: send it as it is
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("continueDraft(): a prescription already started (from a template) stays, after the draft's", async () => {
|
|
53
|
+
const { ph } = setUp([draft()]);
|
|
54
|
+
const order = ph.order();
|
|
55
|
+
await order.sync({ patient: { id: "pat_1" }, prescriptions: { add: [{ templateId: "tmp_1" }] } });
|
|
56
|
+
await order.continueDraft();
|
|
57
|
+
expect(order.prescriptions.map((l) => [l.id, l.templateId])).toEqual([
|
|
58
|
+
["rx_9", null],
|
|
59
|
+
[null, "tmp_1"],
|
|
60
|
+
]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("startNew(): the draft is emptied, and the order starts over on it", async () => {
|
|
64
|
+
const { ph, calls } = setUp([draft()]);
|
|
65
|
+
const order = ph.order();
|
|
66
|
+
await order.sync({ patient: { id: "pat_1" }, prescriptions: { add: [{}] } });
|
|
67
|
+
await order.startNew();
|
|
68
|
+
expect(calls.find((c) => c.op === "UpdateOrder")!.vars).toMatchObject({
|
|
69
|
+
id: "ord_9",
|
|
70
|
+
prescriptions: { none: true },
|
|
71
|
+
});
|
|
72
|
+
expect(order.snapshot.draft).toBeNull();
|
|
73
|
+
expect(order.id).toBe("ord_9");
|
|
74
|
+
expect(order.prescriptions.map((l) => l.id)).toEqual([null]); // only its own empty line, nothing of the draft's
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("startNew() turned down: the draft is still there to decide on", async () => {
|
|
78
|
+
const { ph } = setUp([draft()], {
|
|
79
|
+
UpdateOrder: () =>
|
|
80
|
+
payload("order", "OrderPayload", null, {
|
|
81
|
+
changes: [change({ key: "prescriptions", status: "REJECTED", reason: "Not allowed." })],
|
|
82
|
+
}),
|
|
83
|
+
});
|
|
84
|
+
const order = ph.order();
|
|
85
|
+
await order.sync({ patient: { id: "pat_1" } });
|
|
86
|
+
await order.startNew();
|
|
87
|
+
expect(order.id).toBeNull();
|
|
88
|
+
expect(order.snapshot.draft).toMatchObject({ id: "ord_9" });
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("a draft with no prescriptions isn't asked about: the order goes on, and saves as it would", async () => {
|
|
92
|
+
const { ph, calls } = setUp([draft("ord_9", [])], {
|
|
93
|
+
GetPrescription: () => payload("prescription", "PrescriptionPayload", rx({ id: "rx_new" })),
|
|
94
|
+
SyncOrder: () => payload("order", "OrderPayload", order({ id: "ord_9", prescriptions: [rx({ id: "rx_new" })] })),
|
|
95
|
+
});
|
|
96
|
+
const started = ph.order();
|
|
97
|
+
await started.sync({ patient: { id: "pat_1" } });
|
|
98
|
+
expect(started.snapshot.draft).toBeNull();
|
|
99
|
+
await started.sync({ prescriptions: { add: [{ id: "rx_new" }] } });
|
|
100
|
+
expect(calls.find((c) => c.op === "SyncOrder")!.vars).toMatchObject({ prescriptions: { add: [{ id: "rx_new" }] } });
|
|
101
|
+
expect(started.id).toBe("ord_9");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("several drafts: they're `candidates`, as when the server asks which", async () => {
|
|
105
|
+
const { ph } = setUp([draft("ord_8"), draft("ord_9")]);
|
|
106
|
+
const order = ph.order();
|
|
107
|
+
await order.sync({ patient: { id: "pat_1" } });
|
|
108
|
+
expect(order.snapshot.draft).toBeNull();
|
|
109
|
+
expect(order.snapshot.candidates.map((c) => c.id)).toEqual(["ord_8", "ord_9"]);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("no draft, or a read that fails: nothing changes", async () => {
|
|
113
|
+
for (const answer of [() => orders([]), () => Promise.reject(new Error("down"))]) {
|
|
114
|
+
const { ph } = setUp([], { Orders: answer });
|
|
115
|
+
const order = ph.order();
|
|
116
|
+
await order.sync({ patient: { id: "pat_1" } });
|
|
117
|
+
expect(order.snapshot.draft).toBeNull();
|
|
118
|
+
expect(order.snapshot.candidates).toEqual([]);
|
|
119
|
+
expect(order.snapshot.error).toBeNull();
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("once per patient", async () => {
|
|
124
|
+
const { ph, names } = setUp([draft()]);
|
|
125
|
+
const order = ph.order();
|
|
126
|
+
await order.sync({ patient: { id: "pat_1" } });
|
|
127
|
+
await order.startNew();
|
|
128
|
+
await order.sync({ patient: { id: "pat_1" } });
|
|
129
|
+
expect(names().filter((n) => n === "Orders")).toHaveLength(1);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("an order handed existing prescriptions isn't asked: they decide the order", async () => {
|
|
133
|
+
const { ph, names } = setUp([draft()], {
|
|
134
|
+
GetPrescription: () => payload("prescription", "PrescriptionPayload", rx()),
|
|
135
|
+
SyncOrder: () => payload("order", "OrderPayload", order()),
|
|
136
|
+
});
|
|
137
|
+
const handed = ph.order();
|
|
138
|
+
await handed.sync({ patient: { id: "pat_1" }, prescriptions: { add: [{ id: "rx_1" }] } });
|
|
139
|
+
expect(names()).not.toContain("Orders");
|
|
140
|
+
expect(handed.id).toBe("ord_1");
|
|
141
|
+
});
|
|
142
|
+
});
|