vet-data-utils-ts 0.5.17 → 0.5.18

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 CHANGED
@@ -30,6 +30,20 @@ shows FHIR `Immunization.occurrenceDateTime`, `vaccineCode` and
30
30
  `protocolApplied.targetDisease`. Its issuer-supplied validity interval is not
31
31
  FHIR `Immunization.expirationDate`, which means vaccine-batch expiry.
32
32
 
33
+ `prepareVeterinaryIndexTaggedBundle(...)` creates the interoperable messaging
34
+ copy of a native FHIR Bundle document. It derives only the caller-allowlisted
35
+ `<ResourceType>.<search-param-or-custom>` Coding systems in
36
+ `resource.meta.tag[]`, preserves repeated values as repeated tags for later CSV
37
+ projection, rejects generic `Resource.*` systems, and never mutates the source
38
+ Bundle.
39
+
40
+ `projectVeterinaryClaimsIndexForUi(...)` is the reverse BFF boundary for an
41
+ already role-authorized GW search result. It removes every clinical field and
42
+ `meta.claims`, returning only `fullUrl`, `resourceType`, `id` and allowlisted
43
+ `meta.tag[]`; CSV claims are restored as repeated tags.
44
+ `summarizeVeterinaryTaggedIndex(...)` then sorts and counts that minimized copy
45
+ without inspecting resource content.
46
+
33
47
  `buildSmartHealthCardJwsProofReference(...)` serves a different purpose: it
34
48
  hashes the exact compact SHC JWS, including its ES256 signature, with a domain
35
49
  separator. A product ledger/resolver may use that key to retrieve the existing
@@ -0,0 +1,27 @@
1
+ type JsonObject = Record<string, unknown>;
2
+ export type VeterinaryIndexTagPolicy = Readonly<{
3
+ allowedTagSystems: ReadonlySet<string>;
4
+ }>;
5
+ export type VeterinaryUiIndexPolicy = VeterinaryIndexTagPolicy & Readonly<{
6
+ allowedResourceTypes: ReadonlySet<string>;
7
+ }>;
8
+ /**
9
+ * Creates the interoperable message/index copy of a native FHIR Bundle.
10
+ * Only allowlisted, resource-qualified Coding tags are retained or derived;
11
+ * display/text are deliberately omitted and the caller's Bundle is unchanged.
12
+ */
13
+ export declare function prepareVeterinaryIndexTaggedBundle<T extends JsonObject>(source: T, policy: VeterinaryIndexTagPolicy): T;
14
+ /**
15
+ * Converts an authorized GW claims search response into the only shape exposed
16
+ * to an index-only UI. Clinical fields and `meta.claims` are discarded.
17
+ */
18
+ export declare function projectVeterinaryClaimsIndexForUi(source: JsonObject, policy: VeterinaryUiIndexPolicy): JsonObject;
19
+ /** Counts and sorts an already minimized tagged index without reading clinical content. */
20
+ export declare function summarizeVeterinaryTaggedIndex(source: JsonObject, options?: Readonly<{
21
+ sortSystem?: string;
22
+ }>): Readonly<{
23
+ total: number;
24
+ countsByResourceType: Record<string, number>;
25
+ entries: JsonObject[];
26
+ }>;
27
+ export {};
@@ -0,0 +1,183 @@
1
+ /**
2
+ * Creates the interoperable message/index copy of a native FHIR Bundle.
3
+ * Only allowlisted, resource-qualified Coding tags are retained or derived;
4
+ * display/text are deliberately omitted and the caller's Bundle is unchanged.
5
+ */
6
+ export function prepareVeterinaryIndexTaggedBundle(source, policy) {
7
+ if (source.resourceType !== 'Bundle' || source.type !== 'document' || !Array.isArray(source.entry)) {
8
+ throw new TypeError('veterinary_index_bundle_document_required');
9
+ }
10
+ const result = structuredClone(source);
11
+ const entries = result.entry;
12
+ for (const entry of entries) {
13
+ const resource = isObject(entry.resource) ? entry.resource : undefined;
14
+ const resourceType = typeof resource?.resourceType === 'string' ? resource.resourceType : '';
15
+ if (!resource || !resourceType)
16
+ continue;
17
+ const retained = existingTags(resource).filter(tag => (policy.allowedTagSystems.has(tag.system)
18
+ && tag.system.startsWith(`${resourceType}.`)));
19
+ const derived = [...policy.allowedTagSystems]
20
+ .filter(system => system.startsWith(`${resourceType}.`))
21
+ .flatMap(system => deriveValues(resource, system.slice(resourceType.length + 1))
22
+ .map(code => ({ system, code })));
23
+ const tags = uniqueTags([...retained, ...derived])
24
+ .sort((left, right) => left.system.localeCompare(right.system) || left.code.localeCompare(right.code));
25
+ if (!tags.length)
26
+ continue;
27
+ const meta = isObject(resource.meta) ? resource.meta : {};
28
+ resource.meta = { ...meta, tag: tags };
29
+ }
30
+ return result;
31
+ }
32
+ /**
33
+ * Converts an authorized GW claims search response into the only shape exposed
34
+ * to an index-only UI. Clinical fields and `meta.claims` are discarded.
35
+ */
36
+ export function projectVeterinaryClaimsIndexForUi(source, policy) {
37
+ if (source.resourceType !== 'Bundle' || !Array.isArray(source.entry)) {
38
+ throw new TypeError('veterinary_claims_index_bundle_required');
39
+ }
40
+ const entry = source.entry.flatMap(rawEntry => {
41
+ if (!isObject(rawEntry) || typeof rawEntry.fullUrl !== 'string' || !isObject(rawEntry.resource))
42
+ return [];
43
+ const resource = rawEntry.resource;
44
+ const resourceType = typeof resource.resourceType === 'string' ? resource.resourceType : '';
45
+ const id = typeof resource.id === 'string' ? resource.id : '';
46
+ if (!resourceType || !id || !policy.allowedResourceTypes.has(resourceType))
47
+ return [];
48
+ const meta = isObject(resource.meta) ? resource.meta : undefined;
49
+ const claims = isObject(meta?.claims) ? meta.claims : undefined;
50
+ if (!claims)
51
+ return [];
52
+ const tag = Object.entries(claims)
53
+ .filter(([system]) => policy.allowedTagSystems.has(system) && system.startsWith(`${resourceType}.`))
54
+ .flatMap(([system, raw]) => claimValues(raw).map(code => ({ system, code })))
55
+ .sort((left, right) => left.system.localeCompare(right.system) || left.code.localeCompare(right.code));
56
+ if (!tag.length)
57
+ return [];
58
+ return [{
59
+ fullUrl: rawEntry.fullUrl,
60
+ resource: { resourceType, id, meta: { tag } },
61
+ }];
62
+ });
63
+ return { resourceType: 'Bundle', type: 'searchset', total: entry.length, entry };
64
+ }
65
+ /** Counts and sorts an already minimized tagged index without reading clinical content. */
66
+ export function summarizeVeterinaryTaggedIndex(source, options = {}) {
67
+ if (source.resourceType !== 'Bundle' || !Array.isArray(source.entry)) {
68
+ throw new TypeError('veterinary_tagged_index_bundle_required');
69
+ }
70
+ const entries = source.entry.filter(isObject).map(entry => structuredClone(entry));
71
+ if (options.sortSystem)
72
+ entries.sort((left, right) => (taggedValue(right, options.sortSystem) || '').localeCompare(taggedValue(left, options.sortSystem) || ''));
73
+ const countsByResourceType = {};
74
+ for (const entry of entries) {
75
+ const resource = isObject(entry.resource) ? entry.resource : undefined;
76
+ if (typeof resource?.resourceType !== 'string')
77
+ continue;
78
+ countsByResourceType[resource.resourceType] = (countsByResourceType[resource.resourceType] || 0) + 1;
79
+ }
80
+ return { total: entries.length, countsByResourceType, entries };
81
+ }
82
+ function deriveValues(resource, parameter) {
83
+ switch (`${String(resource.resourceType)}.${parameter}`) {
84
+ case 'Composition.date': return scalar(resource.date);
85
+ case 'Composition.language': return scalar(resource.language);
86
+ case 'Composition.type': return conceptCodes(resource.type);
87
+ case 'Immunization.date': return scalar(resource.occurrenceDateTime);
88
+ case 'Immunization.status': return scalar(resource.status);
89
+ case 'Immunization.language': return scalar(resource.language);
90
+ case 'Immunization.reason-code': return conceptArrayCodes(resource.reasonCode);
91
+ case 'Immunization.vaccine-code': return conceptCodes(resource.vaccineCode);
92
+ case 'AllergyIntolerance.onset-date': return dateChoice(resource, 'onset');
93
+ case 'AllergyIntolerance.criticality': return scalar(resource.criticality);
94
+ case 'AllergyIntolerance.clinical-status': return conceptCodes(resource.clinicalStatus);
95
+ case 'AllergyIntolerance.category': return stringArray(resource.category);
96
+ case 'AllergyIntolerance.code': return conceptCodes(resource.code);
97
+ case 'AllergyIntolerance.language': return scalar(resource.language);
98
+ case 'Condition.onset-date': return dateChoice(resource, 'onset');
99
+ case 'Condition.abatement-date': return dateChoice(resource, 'abatement');
100
+ case 'Condition.clinical-status': return conceptCodes(resource.clinicalStatus);
101
+ case 'Condition.severity': return conceptCodes(resource.severity);
102
+ case 'Condition.code': return conceptCodes(resource.code);
103
+ case 'Condition.language': return scalar(resource.language);
104
+ case 'MedicationStatement.effective': return dateChoice(resource, 'effective');
105
+ case 'MedicationStatement.status': return scalar(resource.status);
106
+ case 'MedicationStatement.medication': return conceptCodes(resource.medicationCodeableConcept);
107
+ case 'MedicationStatement.language': return scalar(resource.language);
108
+ case 'Appointment.date': return scalar(resource.start);
109
+ case 'Appointment.status': return scalar(resource.status);
110
+ case 'Appointment.service-type': return conceptArrayCodes(resource.serviceType);
111
+ case 'Appointment.language': return scalar(resource.language);
112
+ case 'Coverage.period': return periodValues(resource.period);
113
+ case 'Coverage.status': return scalar(resource.status);
114
+ case 'Coverage.type': return conceptCodes(resource.type);
115
+ case 'Coverage.language': return scalar(resource.language);
116
+ case 'Contract.issued': return scalar(resource.issued);
117
+ case 'Contract.status': return scalar(resource.status);
118
+ case 'Contract.type': return conceptCodes(resource.type);
119
+ case 'Contract.language': return scalar(resource.language);
120
+ case 'DocumentReference.date': return scalar(resource.date);
121
+ case 'DocumentReference.status': return scalar(resource.status);
122
+ case 'DocumentReference.type': return conceptCodes(resource.type);
123
+ case 'DocumentReference.language': return scalar(resource.language);
124
+ default: return [];
125
+ }
126
+ }
127
+ function existingTags(resource) {
128
+ const meta = isObject(resource.meta) ? resource.meta : undefined;
129
+ return Array.isArray(meta?.tag) ? meta.tag.flatMap(value => {
130
+ if (!isObject(value) || typeof value.system !== 'string' || typeof value.code !== 'string')
131
+ return [];
132
+ return value.system && value.code ? [{ system: value.system, code: value.code }] : [];
133
+ }) : [];
134
+ }
135
+ function conceptCodes(value) {
136
+ if (!isObject(value) || !Array.isArray(value.coding))
137
+ return [];
138
+ return value.coding.flatMap(coding => isObject(coding) ? scalar(coding.code) : []);
139
+ }
140
+ function conceptArrayCodes(value) {
141
+ return Array.isArray(value) ? value.flatMap(conceptCodes) : conceptCodes(value);
142
+ }
143
+ function dateChoice(resource, prefix) {
144
+ return [
145
+ ...scalar(resource[`${prefix}DateTime`]),
146
+ ...scalar(resource[`${prefix}Date`]),
147
+ ...periodValues(resource[`${prefix}Period`]),
148
+ ];
149
+ }
150
+ function periodValues(value) {
151
+ if (!isObject(value))
152
+ return [];
153
+ return [...scalar(value.start), ...scalar(value.end)];
154
+ }
155
+ function scalar(value) {
156
+ return typeof value === 'string' && value.trim() ? [value.trim()] : [];
157
+ }
158
+ function stringArray(value) {
159
+ return Array.isArray(value) ? value.flatMap(scalar) : scalar(value);
160
+ }
161
+ function claimValues(value) {
162
+ return (Array.isArray(value) ? value : [value]).flatMap(item => (typeof item === 'string' ? item.split(',').map(part => part.trim()).filter(Boolean) : []));
163
+ }
164
+ function taggedValue(entry, system) {
165
+ const resource = isObject(entry.resource) ? entry.resource : undefined;
166
+ const meta = isObject(resource?.meta) ? resource.meta : undefined;
167
+ const tags = Array.isArray(meta?.tag) ? meta.tag : [];
168
+ const tag = tags.find(value => isObject(value) && value.system === system);
169
+ return isObject(tag) && typeof tag.code === 'string' ? tag.code : undefined;
170
+ }
171
+ function uniqueTags(tags) {
172
+ const seen = new Set();
173
+ return tags.filter(tag => {
174
+ const key = `${tag.system}\0${tag.code}`;
175
+ if (seen.has(key))
176
+ return false;
177
+ seen.add(key);
178
+ return true;
179
+ });
180
+ }
181
+ function isObject(value) {
182
+ return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
183
+ }
package/dist/index.d.ts CHANGED
@@ -9,6 +9,7 @@ export * from './financial.js';
9
9
  export * from './group.js';
10
10
  export * from './immunization.js';
11
11
  export * from './international-health-card.js';
12
+ export * from './index-projection-tags.js';
12
13
  export * from './iso-jurisdictions.js';
13
14
  export * from './organization-application.js';
14
15
  export * from './payment.js';
package/dist/index.js CHANGED
@@ -9,6 +9,7 @@ export * from './financial.js';
9
9
  export * from './group.js';
10
10
  export * from './immunization.js';
11
11
  export * from './international-health-card.js';
12
+ export * from './index-projection-tags.js';
12
13
  export * from './iso-jurisdictions.js';
13
14
  export * from './organization-application.js';
14
15
  export * from './payment.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vet-data-utils-ts",
3
- "version": "0.5.17",
3
+ "version": "0.5.18",
4
4
  "description": "Browser-safe governed VetChain data contracts",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Connecting Solution & Applications Ltd",
@@ -52,6 +52,10 @@
52
52
  "types": "./dist/international-health-card.d.ts",
53
53
  "default": "./dist/international-health-card.js"
54
54
  },
55
+ "./index-projection-tags": {
56
+ "types": "./dist/index-projection-tags.d.ts",
57
+ "default": "./dist/index-projection-tags.js"
58
+ },
55
59
  "./iso-jurisdictions": {
56
60
  "types": "./dist/iso-jurisdictions.d.ts",
57
61
  "default": "./dist/iso-jurisdictions.js"