@awell-health/awell-extensions 2.0.54 → 2.0.56
Sign up to get free protection for your applications and to get access to all the features.
@@ -15,14 +15,19 @@ exports.addIdentifierToPatient = {
|
|
15
15
|
previewable: false,
|
16
16
|
onEvent: async ({ payload, onComplete, onError, helpers }) => {
|
17
17
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
18
|
-
|
18
|
+
// Validate and extract the `system` and `value` fields and `currentPatientId` from the payload
|
19
|
+
const { fields: { system, value }, patient: { id: currentPatientId }, } = (0, extensions_core_1.validate)({
|
19
20
|
schema: zod_1.z.object({
|
20
21
|
fields: config_1.FieldsValidationSchema,
|
22
|
+
patient: config_1.PatientValidationSchema,
|
21
23
|
}),
|
22
24
|
payload,
|
23
25
|
});
|
24
|
-
const currentPatient = payload.patient;
|
25
26
|
const sdk = await helpers.awellSdk();
|
27
|
+
/**
|
28
|
+
* Check if a patient with the same identifier system and value already exists.
|
29
|
+
* This is necessary because identifier system-value pairs must be unique across all patients.
|
30
|
+
*/
|
26
31
|
const { patientByIdentifier: { patient: existingPatient }, } = await sdk.orchestration.query({
|
27
32
|
patientByIdentifier: {
|
28
33
|
__args: { system, value },
|
@@ -32,7 +37,11 @@ exports.addIdentifierToPatient = {
|
|
32
37
|
},
|
33
38
|
});
|
34
39
|
const patientExists = !(0, lodash_1.isEmpty)(existingPatient === null || existingPatient === void 0 ? void 0 : existingPatient.id);
|
35
|
-
const patientLookUpIsCurrentPatient = (existingPatient === null || existingPatient === void 0 ? void 0 : existingPatient.id) ===
|
40
|
+
const patientLookUpIsCurrentPatient = (existingPatient === null || existingPatient === void 0 ? void 0 : existingPatient.id) === currentPatientId;
|
41
|
+
/**
|
42
|
+
* If a different patient already has the provided identifier, we cannot assign this identifier
|
43
|
+
* to the current patient, as each identifier system-value pair must be unique.
|
44
|
+
*/
|
36
45
|
if (patientExists && !patientLookUpIsCurrentPatient) {
|
37
46
|
await onError({
|
38
47
|
events: [
|
@@ -46,11 +55,15 @@ exports.addIdentifierToPatient = {
|
|
46
55
|
});
|
47
56
|
return;
|
48
57
|
}
|
49
|
-
|
58
|
+
/**
|
59
|
+
* Although TypeScript indicates that `payload.patient.profile.identifier` should be accessible within `onEvent`,
|
60
|
+
* tests in the Dev environment show that `identifier` data is not reliably available on the extension server.
|
61
|
+
* To ensure accurate data, we directly query the API to retrieve the patient's current identifiers.
|
62
|
+
*/
|
50
63
|
const currentPatientData = await sdk.orchestration.query({
|
51
64
|
patient: {
|
52
65
|
__args: {
|
53
|
-
id:
|
66
|
+
id: currentPatientId,
|
54
67
|
},
|
55
68
|
patient: {
|
56
69
|
profile: {
|
@@ -62,11 +75,14 @@ exports.addIdentifierToPatient = {
|
|
62
75
|
},
|
63
76
|
},
|
64
77
|
});
|
65
|
-
//
|
78
|
+
// Retrieve existing identifiers, excluding any with the specified system.
|
66
79
|
const existingIdentifiers = (_d = (_c = (_b = (_a = currentPatientData === null || currentPatientData === void 0 ? void 0 : currentPatientData.patient) === null || _a === void 0 ? void 0 : _a.patient) === null || _b === void 0 ? void 0 : _b.profile) === null || _c === void 0 ? void 0 : _c.identifier) !== null && _d !== void 0 ? _d : [];
|
67
|
-
const
|
80
|
+
const existingIdentifierExcludingTheOneToUpdate = existingIdentifiers.filter((id) => id.system !== system);
|
68
81
|
const previousIdentifier = existingIdentifiers.find((id) => id.system === system);
|
69
|
-
const isUpdatingIdentifier =
|
82
|
+
const isUpdatingIdentifier = !(0, lodash_1.isEmpty)(previousIdentifier);
|
83
|
+
/**
|
84
|
+
* If the patient already has this identifier system with the same value, we skip the update to avoid redundancy.
|
85
|
+
*/
|
70
86
|
if (isUpdatingIdentifier && (previousIdentifier === null || previousIdentifier === void 0 ? void 0 : previousIdentifier.value) === value) {
|
71
87
|
await onComplete({
|
72
88
|
events: [
|
@@ -80,13 +96,20 @@ exports.addIdentifierToPatient = {
|
|
80
96
|
});
|
81
97
|
return;
|
82
98
|
}
|
83
|
-
|
84
|
-
|
99
|
+
// Prepare the updated list of identifiers, including the new or updated identifier.
|
100
|
+
const newIdentifiers = [
|
101
|
+
...existingIdentifierExcludingTheOneToUpdate,
|
102
|
+
{ system, value },
|
103
|
+
];
|
104
|
+
/**
|
105
|
+
* Perform the update to add or update the identifier on the patient's profile.
|
106
|
+
* We retrieve the updated identifiers from the API response to ensure we log the final state.
|
107
|
+
*/
|
85
108
|
const result = await sdk.orchestration.mutation({
|
86
109
|
updatePatient: {
|
87
110
|
__args: {
|
88
111
|
input: {
|
89
|
-
patient_id:
|
112
|
+
patient_id: currentPatientId,
|
90
113
|
profile: {
|
91
114
|
identifier: newIdentifiers,
|
92
115
|
},
|
@@ -104,6 +127,7 @@ exports.addIdentifierToPatient = {
|
|
104
127
|
},
|
105
128
|
});
|
106
129
|
const updatedIdentifiers = (_h = (_g = (_f = (_e = result === null || result === void 0 ? void 0 : result.updatePatient) === null || _e === void 0 ? void 0 : _e.patient) === null || _f === void 0 ? void 0 : _f.profile) === null || _g === void 0 ? void 0 : _g.identifier) !== null && _h !== void 0 ? _h : [];
|
130
|
+
// Log the result, including the old and new lists of identifiers.
|
107
131
|
await onComplete({
|
108
132
|
events: [
|
109
133
|
{
|
package/dist/extensions/awell/v1/actions/addIdentifierToPatient/addIdentifierToPatient.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"addIdentifierToPatient.js","sourceRoot":"","sources":["../../../../../../extensions/awell/v1/actions/addIdentifierToPatient/addIdentifierToPatient.ts"],"names":[],"mappings":";;;AAEA,mEAAkE;AAClE,
|
1
|
+
{"version":3,"file":"addIdentifierToPatient.js","sourceRoot":"","sources":["../../../../../../extensions/awell/v1/actions/addIdentifierToPatient/addIdentifierToPatient.ts"],"names":[],"mappings":";;;AAEA,mEAAkE;AAClE,qCAKiB;AACjB,6BAAuB;AACvB,mCAAgC;AAEnB,QAAA,sBAAsB,GAA2C;IAC5E,GAAG,EAAE,wBAAwB;IAC7B,QAAQ,EAAE,0BAAQ,CAAC,QAAQ;IAC3B,KAAK,EAAE,2BAA2B;IAClC,WAAW,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