@quicore/task-queue-fhir-tools 1.0.0

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/.babelrc ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "presets": [
3
+ ["@babel/env", {
4
+ "targets": {
5
+ "node": "current"
6
+ }
7
+ }]
8
+ ]
9
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 quicore
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,143 @@
1
+ # @quicore/resource-id
2
+
3
+ A lightweight, pluggable utility for generating compact, deterministic, and integration-scoped resource identifiers using flexible components. Ideal for APIs, FHIR systems, and cross-service data referencing.
4
+
5
+ ---
6
+
7
+ ## ✨ Features
8
+
9
+ - 🔐 Compact deterministic ID generation using Blake2b + Base62
10
+ - 🧩 Component-based customization (resourceType, resourceId, integrationId)
11
+ - 📦 Works with any API data (not just FHIR)
12
+ - ⚙️ Chainable and extensible class structure
13
+ - 💾 MongoDB-friendly (22-character Base62 IDs by default)
14
+
15
+ ---
16
+
17
+ ## 📦 Installation
18
+
19
+ ```bash
20
+ npm install @quicore/resource-id
21
+ ````
22
+
23
+ ---
24
+
25
+ ## 🚀 Quick Start
26
+
27
+ ```js
28
+ import { ResourceIDGenerator } from '@quicore/resource-id';
29
+
30
+ const id = new ResourceIDGenerator()
31
+ .setIntegrationId('my-app')
32
+ .setResourceType('Invoice')
33
+ .setResourceId('INV-12345')
34
+ .generate();
35
+
36
+ console.log(id); // Compact, consistent ID (e.g., "004f83akdT3mJLhFtVNckT")
37
+ ```
38
+
39
+ ---
40
+
41
+ ## 🔄 Use Case
42
+
43
+ While originally designed for [FHIR](https://hl7.org/fhir/) resources, `ResourceIDGenerator` is **generic** and can be used for any API data:
44
+
45
+ * `resourceId` = the **unique identifier** of your data object
46
+ * `resourceType` = the **type/category** of your object (`"User"`, `"Invoice"`, `"Post"`, etc.)
47
+ * `integrationId` = a **scoping prefix** to prevent ID collisions across systems
48
+
49
+ ---
50
+
51
+ ## 📘 API Reference
52
+
53
+ ### `class ResourceIDGenerator`
54
+
55
+ Extends `IDGenerator` and is ready to use out of the box.
56
+
57
+ #### `.setIntegrationId(id: string)`
58
+
59
+ Sets a prefix to scope the generated ID by system/environment.
60
+
61
+ #### `.setResourceType(type: string)`
62
+
63
+ Sets the type/category of the data object (e.g., `'User'`, `'Appointment'`).
64
+
65
+ #### `.setResourceId(id: string)`
66
+
67
+ Sets the primary unique identifier (e.g., a database or API ID).
68
+
69
+ #### `.setIdLength(length: number)`
70
+
71
+ (Optional) Set the output ID length (minimum 10, default 22).
72
+
73
+ #### `.generate() → string`
74
+
75
+ Returns a compact, Base62-encoded ID derived from all components.
76
+
77
+ ---
78
+
79
+ ### `class IDGenerator`
80
+
81
+ The base abstract class for deterministic ID generation. To extend:
82
+
83
+ ```js
84
+ class MyCustomGenerator extends IDGenerator {
85
+ createCanonicalString() {
86
+ return `${this.components.a}-${this.components.b}`;
87
+ }
88
+
89
+ validateComponents() {
90
+ if (!this.components.a || !this.components.b) {
91
+ throw new Error('Missing required components');
92
+ }
93
+ }
94
+ }
95
+ ```
96
+
97
+ ---
98
+
99
+ ## 🧬 Output Format
100
+
101
+ Internally, IDs are created by hashing this structure:
102
+
103
+ ```
104
+ <integrationId>::<resourceType>::<resourceId>
105
+ ```
106
+
107
+ Then encoded into a compact **Base62** string using `blake2b`.
108
+
109
+ ---
110
+
111
+ ## 🔐 Why Deterministic IDs?
112
+
113
+ * Guarantees the same input always gives the same ID
114
+ * Useful for deduplication, data sync, upserts, or ID federation
115
+ * Scope-aware via `integrationId` to prevent overlap
116
+
117
+ ---
118
+
119
+ ## 🔧 FHIR Support (Optional)
120
+
121
+ For FHIR-specific use, the package includes:
122
+
123
+ ### `class FHIRIDGenerator`
124
+
125
+ ```js
126
+ import { FHIRIDGenerator } from '@quicore/resource-id';
127
+
128
+ const resource = {
129
+ id: 'abc123',
130
+ resourceType: 'Patient'
131
+ };
132
+
133
+ const id = new FHIRIDGenerator(resource)
134
+ .setIntegrationId('epic')
135
+ .generate();
136
+
137
+ console.log(id); // e.g., "A81dskf9e3rKW2ThjL9bc3"
138
+ ```
139
+
140
+ ---
141
+
142
+ ## 📄 License
143
+ MIT
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.TaskFHIRBundleProcessor = void 0;
7
+ var _fhirQueryMongoStore = require("@healthcare-interoperability/fhir-query-mongo-store");
8
+ var _TaskFHIRQuery = require("./TaskFHIRQuery");
9
+ class TaskFHIRBundleProcessor extends _fhirQueryMongoStore.FHIRMongoBundleProcessor {
10
+ /**
11
+ * A flag indicating whether to store data specific to each customer ID.
12
+ * @private
13
+ * @type {boolean}
14
+ */
15
+ customerSpecificData = false;
16
+
17
+ /**
18
+ * The customer ID(s) associated with the resources in the bundle.
19
+ * @private
20
+ * @type {string | string[] | undefined}
21
+ */
22
+ sourceCustomerId;
23
+
24
+ /**
25
+ * The task ID associated with the resources in the bundle.
26
+ * @private
27
+ * @type {string | undefined}
28
+ */
29
+ taskId;
30
+
31
+ /**
32
+ * Creates an instance of TaskFHIRBundleProcessor.
33
+ * @param {object} bundle - The FHIR Bundle resource.
34
+ */
35
+ constructor(bundle) {
36
+ super(bundle);
37
+ this.customerSpecificData = false;
38
+ }
39
+
40
+ /**
41
+ * Enables or disables customer-specific data storage for the bundle.
42
+ * @param {boolean} [enable=true] - Whether to enable customer-specific data storage.
43
+ * @returns {this} The FHIRBundleProcessor instance for chaining.
44
+ */
45
+ customerSpecificDataStore(enable = true) {
46
+ this.customerSpecificData = enable;
47
+ return this;
48
+ }
49
+
50
+ /**
51
+ * Sets the customer ID(s) associated with the resources in the bundle.
52
+ * @param {string | string[]} sourceCustomerId - The customer ID(s).
53
+ * @returns {this} The FHIRBundleProcessor instance for chaining.
54
+ */
55
+ setCustomerId(sourceCustomerId) {
56
+ this.sourceCustomerId = sourceCustomerId;
57
+ return this;
58
+ }
59
+
60
+ /**
61
+ * Sets the task ID associated with the resources in the bundle.
62
+ * @param {string} taskId - The task ID.
63
+ * @returns {this} The FHIRBundleProcessor instance for chaining.
64
+ */
65
+ setTaskId(taskId) {
66
+ this.taskId = taskId;
67
+ return this;
68
+ }
69
+ _getQueryInstance(resource) {
70
+ return new _TaskFHIRQuery.TaskFHIRQuery(resource);
71
+ }
72
+
73
+ /**
74
+ * Prepares a FHIRQuery object for a single resource.
75
+ * @private
76
+ * @param {object} resource - The FHIR resource.
77
+ * @returns {Array<{query: {updateOne: object}, resourceType: string | undefined}>} An array of queries.
78
+ */
79
+ _prepareQuery(resource) {
80
+ const query = this._getQueryInstance(resource);
81
+ if (this.integrationId) query.setIntegrationId(this.integrationId);
82
+ if (query instanceof _TaskFHIRQuery.TaskFHIRQuery) {
83
+ if (this.sourceCustomerId) query.setCustomerId(this.sourceCustomerId);
84
+ if (this.taskId) query.setTaskId(this.taskId);
85
+ if (this.customerSpecificData) query.customerSpecificDataStore(true);
86
+ return query.query();
87
+ }
88
+ }
89
+ }
90
+ exports.TaskFHIRBundleProcessor = TaskFHIRBundleProcessor;
@@ -0,0 +1,198 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.TaskFHIRQuery = void 0;
7
+ var _fhirQueryMongoStore = require("@healthcare-interoperability/fhir-query-mongo-store");
8
+ /**
9
+ * Generates a FHIR query for MongoDB with a deterministic ID, with added support for
10
+ * CustomerID specific deduplication and segregation, and Task ID association.
11
+ * Extends {@link FHIRMongoQuery}.
12
+ */
13
+ class TaskFHIRQuery extends _fhirQueryMongoStore.FHIRMongoQuery {
14
+ /**
15
+ * An array of customer IDs associated with this resource. Used for customer-specific data handling.
16
+ * @private
17
+ * @type {string[]}
18
+ */
19
+ customerId = [];
20
+
21
+ /**
22
+ * A flag indicating whether to store data specific to each customer ID.
23
+ * If true, separate queries will be generated for each customer ID.
24
+ * @private
25
+ * @type {boolean}
26
+ */
27
+ customerSpecificData = false;
28
+
29
+ /**
30
+ * The ID of the task associated with this resource.
31
+ * @private
32
+ * @type {string | undefined}
33
+ */
34
+ taskId;
35
+
36
+ /**
37
+ * Creates an instance of TQSFHIRQuery.
38
+ * @param {object} resource - The FHIR resource object.
39
+ */
40
+ constructor(resource) {
41
+ super(resource);
42
+ }
43
+
44
+ /**
45
+ * Sets the task ID associated with the resource.
46
+ * @param {string} taskId - The task ID.
47
+ * @returns {this} The TQSFHIRQuery instance for chaining.
48
+ */
49
+ setTaskId(taskId) {
50
+ this.taskId = taskId;
51
+ return this;
52
+ }
53
+
54
+ /**
55
+ * Sets the customer ID(s) associated with the resource. Accepts a single ID or an array of IDs.
56
+ * @param {string | string[]} customerId - The customer ID(s).
57
+ * @returns {this} The TQSFHIRQuery instance for chaining.
58
+ */
59
+ setCustomerId(customerId) {
60
+ if (!customerId) return this;
61
+ if (Array.isArray(customerId)) {
62
+ this.customerId.push(...customerId);
63
+ } else {
64
+ this.customerId.push(customerId);
65
+ }
66
+ return this;
67
+ }
68
+
69
+ /**
70
+ * Combines components in a canonical string format for hashing.
71
+ * @returns {string} Canonical string.
72
+ */
73
+ createCanonicalString() {
74
+ if (this.components.customerId) {
75
+ return `${this.components.integrationId}::${this.components.resourceType}::${this.components.resourceId}::${this.components.customerId}`;
76
+ }
77
+ return `${this.components.integrationId}::${this.components.resourceType}::${this.components.resourceId}`;
78
+ }
79
+
80
+ /**
81
+ * Enables or disables customer-specific data storage.
82
+ * When enabled, separate records are created for each customer ID.
83
+ * @param {boolean} [enable=true] - Whether to enable customer-specific data storage.
84
+ * @returns {this} The TQSFHIRQuery instance for chaining.
85
+ */
86
+ customerSpecificDataStore(enable = true) {
87
+ this.customerSpecificData = enable;
88
+ return this;
89
+ }
90
+
91
+ /**
92
+ * Builds an array of MongoDB update queries based on the resource and customer-specific settings.
93
+ * If `customerSpecificData` is enabled, it generates a query for each customer ID.
94
+ * @private
95
+ * @returns {Array<{id: string, set: object, setOnInsert: object, inc: object, addToSet: object}>}
96
+ * An array of objects, each containing the data for a MongoDB update operation.
97
+ */
98
+ queryBuilder() {
99
+ const queries = [];
100
+ const baseSetOnInsert = {
101
+ _processFlags: [],
102
+ _searchTags: [],
103
+ _changeLogs: []
104
+ };
105
+
106
+ /**
107
+ * Creates a query object with overrides for set and addToSet.
108
+ * @param {object} base - The base query data from prepareResource.
109
+ * @param {object} [addToSetOverrides={}] - Overrides for the $addToSet operator.
110
+ * @param {object} [setOverrides={}] - Overrides for the $set operator.
111
+ * @returns {{id: string, set: object, setOnInsert: object, inc: object, addToSet: object}}
112
+ */
113
+ const makeQuery = (base, addToSetOverrides = {}, setOverrides = {}) => ({
114
+ id: base.id,
115
+ set: {
116
+ ...base.set,
117
+ ...setOverrides,
118
+ syncedAt: new Date()
119
+ },
120
+ setOnInsert: {
121
+ ...base.setOnInsert,
122
+ ...baseSetOnInsert
123
+ },
124
+ inc: base.inc,
125
+ addToSet: {
126
+ ...(base.addToSet || {}),
127
+ ...addToSetOverrides
128
+ }
129
+ });
130
+ const dedupId = super.generate(); // Generate a common deduplication ID for the base resource
131
+
132
+ if (this.customerSpecificData && this.customerId?.length) {
133
+ for (const cid of this.customerId) {
134
+ this.setComponent('customerId', cid);
135
+ const base = super.prepareResource();
136
+ const addToSet = {
137
+ _sourceCustomerId: cid,
138
+ ...(this.taskId ? {
139
+ sourceTask: this.taskId
140
+ } : {})
141
+ };
142
+ queries.push(makeQuery(base, addToSet, {
143
+ _dedupId: dedupId,
144
+ _customerSpecificStore: true
145
+ }));
146
+ }
147
+ } else {
148
+ const base = super.prepareResource();
149
+ const addToSet = {
150
+ ...(this.customerId?.length ? {
151
+ _sourceCustomerId: {
152
+ $each: this.customerId
153
+ }
154
+ } : {}),
155
+ ...(this.taskId ? {
156
+ sourceTask: this.taskId
157
+ } : {})
158
+ };
159
+ queries.push(makeQuery(base, addToSet, {
160
+ _dedupId: dedupId
161
+ }));
162
+ }
163
+ return queries;
164
+ }
165
+
166
+ /**
167
+ * Generates an array of MongoDB update queries for the FHIR resource, handling customer-specific data if enabled.
168
+ * @param {boolean} [upsert=true] - Whether to perform an upsert operation.
169
+ * @returns {Array<{query: {updateOne: {filter: object, update: object, upsert: boolean}}, resourceType: string | undefined}>}
170
+ * An array of objects, each containing a MongoDB update query and the resource type.
171
+ */
172
+ query(upsert = true) {
173
+ return this.queryBuilder().map(({
174
+ id,
175
+ set,
176
+ setOnInsert,
177
+ inc,
178
+ addToSet
179
+ }) => ({
180
+ query: {
181
+ updateOne: {
182
+ filter: {
183
+ _id: id
184
+ },
185
+ update: {
186
+ $set: set,
187
+ $setOnInsert: setOnInsert,
188
+ $inc: inc,
189
+ $addToSet: addToSet
190
+ },
191
+ upsert
192
+ }
193
+ },
194
+ resourceType: this.resourceType
195
+ }));
196
+ }
197
+ }
198
+ exports.TaskFHIRQuery = TaskFHIRQuery;
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "TaskFHIRBundleProcessor", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _TaskFHIRBundleProcessor.TaskFHIRBundleProcessor;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "TaskFHIRQuery", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _TaskFHIRQuery.TaskFHIRQuery;
16
+ }
17
+ });
18
+ var _TaskFHIRBundleProcessor = require("./TaskFHIRBundleProcessor");
19
+ var _TaskFHIRQuery = require("./TaskFHIRQuery");
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@quicore/task-queue-fhir-tools",
3
+ "version": "1.0.0",
4
+ "description": "Helper tools for FHIR processing using Task Queue Server",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "build": "babel src -d dist"
8
+ },
9
+ "keywords": [
10
+ "fhir",
11
+ "healthcare",
12
+ "quicore",
13
+ "task-queue"
14
+ ],
15
+ "author": "quicore",
16
+ "license": "MIT",
17
+ "dependencies": {
18
+ "@healthcare-interoperability/fhir-query-mongo-store": "^1.0.0"
19
+ },
20
+ "devDependencies": {
21
+ "@babel/cli": "^7.27.0",
22
+ "@babel/core": "^7.26.10",
23
+ "@babel/preset-env": "^7.26.9"
24
+ },
25
+ "engines": {
26
+ "node": ">=18.0.0"
27
+ }
28
+ }
@@ -0,0 +1,88 @@
1
+ import { FHIRMongoBundleProcessor } from "@healthcare-interoperability/fhir-query-mongo-store";
2
+ import { TaskFHIRQuery } from "./TaskFHIRQuery";
3
+
4
+ export class TaskFHIRBundleProcessor extends FHIRMongoBundleProcessor {
5
+
6
+ /**
7
+ * A flag indicating whether to store data specific to each customer ID.
8
+ * @private
9
+ * @type {boolean}
10
+ */
11
+ customerSpecificData = false;
12
+
13
+ /**
14
+ * The customer ID(s) associated with the resources in the bundle.
15
+ * @private
16
+ * @type {string | string[] | undefined}
17
+ */
18
+ sourceCustomerId;
19
+
20
+ /**
21
+ * The task ID associated with the resources in the bundle.
22
+ * @private
23
+ * @type {string | undefined}
24
+ */
25
+ taskId;
26
+
27
+ /**
28
+ * Creates an instance of TaskFHIRBundleProcessor.
29
+ * @param {object} bundle - The FHIR Bundle resource.
30
+ */
31
+ constructor(bundle) {
32
+ super(bundle);
33
+ this.customerSpecificData = false;
34
+ }
35
+
36
+ /**
37
+ * Enables or disables customer-specific data storage for the bundle.
38
+ * @param {boolean} [enable=true] - Whether to enable customer-specific data storage.
39
+ * @returns {this} The FHIRBundleProcessor instance for chaining.
40
+ */
41
+ customerSpecificDataStore(enable = true) {
42
+ this.customerSpecificData = enable;
43
+ return this;
44
+ }
45
+
46
+ /**
47
+ * Sets the customer ID(s) associated with the resources in the bundle.
48
+ * @param {string | string[]} sourceCustomerId - The customer ID(s).
49
+ * @returns {this} The FHIRBundleProcessor instance for chaining.
50
+ */
51
+ setCustomerId(sourceCustomerId) {
52
+ this.sourceCustomerId = sourceCustomerId;
53
+ return this;
54
+ }
55
+
56
+ /**
57
+ * Sets the task ID associated with the resources in the bundle.
58
+ * @param {string} taskId - The task ID.
59
+ * @returns {this} The FHIRBundleProcessor instance for chaining.
60
+ */
61
+ setTaskId(taskId) {
62
+ this.taskId = taskId;
63
+ return this;
64
+ }
65
+
66
+ _getQueryInstance(resource) {
67
+ return new TaskFHIRQuery(resource);
68
+ }
69
+
70
+ /**
71
+ * Prepares a FHIRQuery object for a single resource.
72
+ * @private
73
+ * @param {object} resource - The FHIR resource.
74
+ * @returns {Array<{query: {updateOne: object}, resourceType: string | undefined}>} An array of queries.
75
+ */
76
+ _prepareQuery(resource) {
77
+ const query = this._getQueryInstance(resource);
78
+ if (this.integrationId) query.setIntegrationId(this.integrationId);
79
+
80
+ if (query instanceof TaskFHIRQuery) {
81
+ if (this.sourceCustomerId) query.setCustomerId(this.sourceCustomerId);
82
+ if (this.taskId) query.setTaskId(this.taskId);
83
+ if (this.customerSpecificData) query.customerSpecificDataStore(true);
84
+
85
+ return query.query();
86
+ }
87
+ }
88
+ }
@@ -0,0 +1,168 @@
1
+ import { FHIRMongoQuery } from "@healthcare-interoperability/fhir-query-mongo-store";
2
+
3
+ /**
4
+ * Generates a FHIR query for MongoDB with a deterministic ID, with added support for
5
+ * CustomerID specific deduplication and segregation, and Task ID association.
6
+ * Extends {@link FHIRMongoQuery}.
7
+ */
8
+ export class TaskFHIRQuery extends FHIRMongoQuery {
9
+ /**
10
+ * An array of customer IDs associated with this resource. Used for customer-specific data handling.
11
+ * @private
12
+ * @type {string[]}
13
+ */
14
+ customerId = [];
15
+
16
+ /**
17
+ * A flag indicating whether to store data specific to each customer ID.
18
+ * If true, separate queries will be generated for each customer ID.
19
+ * @private
20
+ * @type {boolean}
21
+ */
22
+ customerSpecificData = false;
23
+
24
+ /**
25
+ * The ID of the task associated with this resource.
26
+ * @private
27
+ * @type {string | undefined}
28
+ */
29
+ taskId;
30
+
31
+ /**
32
+ * Creates an instance of TQSFHIRQuery.
33
+ * @param {object} resource - The FHIR resource object.
34
+ */
35
+ constructor(resource) {
36
+ super(resource);
37
+ }
38
+
39
+ /**
40
+ * Sets the task ID associated with the resource.
41
+ * @param {string} taskId - The task ID.
42
+ * @returns {this} The TQSFHIRQuery instance for chaining.
43
+ */
44
+ setTaskId(taskId) {
45
+ this.taskId = taskId;
46
+ return this;
47
+ }
48
+
49
+ /**
50
+ * Sets the customer ID(s) associated with the resource. Accepts a single ID or an array of IDs.
51
+ * @param {string | string[]} customerId - The customer ID(s).
52
+ * @returns {this} The TQSFHIRQuery instance for chaining.
53
+ */
54
+ setCustomerId(customerId) {
55
+ if (!customerId) return this;
56
+ if (Array.isArray(customerId)) {
57
+ this.customerId.push(...customerId);
58
+ } else {
59
+ this.customerId.push(customerId);
60
+ }
61
+ return this;
62
+ }
63
+
64
+ /**
65
+ * Combines components in a canonical string format for hashing.
66
+ * @returns {string} Canonical string.
67
+ */
68
+ createCanonicalString() {
69
+ if(this.components.customerId){
70
+ return `${this.components.integrationId}::${this.components.resourceType}::${this.components.resourceId}::${this.components.customerId}`;
71
+ }
72
+ return `${this.components.integrationId}::${this.components.resourceType}::${this.components.resourceId}`;
73
+ }
74
+
75
+ /**
76
+ * Enables or disables customer-specific data storage.
77
+ * When enabled, separate records are created for each customer ID.
78
+ * @param {boolean} [enable=true] - Whether to enable customer-specific data storage.
79
+ * @returns {this} The TQSFHIRQuery instance for chaining.
80
+ */
81
+ customerSpecificDataStore(enable = true) {
82
+ this.customerSpecificData = enable;
83
+ return this;
84
+ }
85
+
86
+ /**
87
+ * Builds an array of MongoDB update queries based on the resource and customer-specific settings.
88
+ * If `customerSpecificData` is enabled, it generates a query for each customer ID.
89
+ * @private
90
+ * @returns {Array<{id: string, set: object, setOnInsert: object, inc: object, addToSet: object}>}
91
+ * An array of objects, each containing the data for a MongoDB update operation.
92
+ */
93
+ queryBuilder() {
94
+ const queries = [];
95
+ const baseSetOnInsert = {
96
+ _processFlags: [],
97
+ _searchTags: [],
98
+ _changeLogs: []
99
+ };
100
+
101
+ /**
102
+ * Creates a query object with overrides for set and addToSet.
103
+ * @param {object} base - The base query data from prepareResource.
104
+ * @param {object} [addToSetOverrides={}] - Overrides for the $addToSet operator.
105
+ * @param {object} [setOverrides={}] - Overrides for the $set operator.
106
+ * @returns {{id: string, set: object, setOnInsert: object, inc: object, addToSet: object}}
107
+ */
108
+ const makeQuery = (base, addToSetOverrides = {}, setOverrides = {}) => ({
109
+ id: base.id,
110
+ set: { ...base.set, ...setOverrides, syncedAt: new Date() },
111
+ setOnInsert: { ...base.setOnInsert, ...baseSetOnInsert },
112
+ inc: base.inc,
113
+ addToSet: { ...(base.addToSet || {}), ...addToSetOverrides }
114
+ });
115
+
116
+ const dedupId = super.generate(); // Generate a common deduplication ID for the base resource
117
+
118
+ if (this.customerSpecificData && this.customerId?.length) {
119
+ for (const cid of this.customerId) {
120
+ this.setComponent('customerId', cid);
121
+ const base = super.prepareResource();
122
+
123
+ const addToSet = {
124
+ _sourceCustomerId: cid,
125
+ ...(this.taskId ? { sourceTask: this.taskId } : {})
126
+ };
127
+
128
+ queries.push(makeQuery(base, addToSet, { _dedupId: dedupId, _customerSpecificStore: true }));
129
+ }
130
+ } else {
131
+ const base = super.prepareResource();
132
+
133
+ const addToSet = {
134
+ ...(this.customerId?.length ? { _sourceCustomerId: { $each: this.customerId } } : {}),
135
+ ...(this.taskId ? { sourceTask: this.taskId } : {})
136
+ };
137
+
138
+ queries.push(makeQuery(base, addToSet, {_dedupId: dedupId}));
139
+ }
140
+
141
+ return queries;
142
+ }
143
+
144
+
145
+ /**
146
+ * Generates an array of MongoDB update queries for the FHIR resource, handling customer-specific data if enabled.
147
+ * @param {boolean} [upsert=true] - Whether to perform an upsert operation.
148
+ * @returns {Array<{query: {updateOne: {filter: object, update: object, upsert: boolean}}, resourceType: string | undefined}>}
149
+ * An array of objects, each containing a MongoDB update query and the resource type.
150
+ */
151
+ query(upsert = true) {
152
+ return this.queryBuilder().map(({ id, set, setOnInsert, inc, addToSet }) => ({
153
+ query: {
154
+ updateOne: {
155
+ filter: { _id: id },
156
+ update: {
157
+ $set: set,
158
+ $setOnInsert: setOnInsert,
159
+ $inc: inc,
160
+ $addToSet: addToSet
161
+ },
162
+ upsert
163
+ }
164
+ },
165
+ resourceType: this.resourceType
166
+ }));
167
+ }
168
+ }
package/src/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { TaskFHIRBundleProcessor } from "./TaskFHIRBundleProcessor";
2
+ export { TaskFHIRQuery } from "./TaskFHIRQuery";