@openhi/constructs 0.0.34 → 0.0.35

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.
@@ -347,6 +347,11 @@ function requireJsonBody(req, res) {
347
347
  }
348
348
  return { body: raw };
349
349
  }
350
+ function requireJsonBodyAs(req, res) {
351
+ const result = requireJsonBody(req, res);
352
+ if ("errorResponse" in result) return result;
353
+ return { body: result.body };
354
+ }
350
355
  function buildSearchsetBundle(basePath, entries) {
351
356
  return {
352
357
  resourceType: "Bundle",
@@ -1644,6 +1649,32 @@ async function createDataEntityRecord(entity, tenantId, workspaceId, id, resourc
1644
1649
  resource: resourceWithAudit
1645
1650
  };
1646
1651
  }
1652
+ function buildUpdatedResourceWithAudit(body, id, date, actorId, actorName, existingResourceStr, resourceType) {
1653
+ const existingMeta = JSON.parse(existingResourceStr).meta;
1654
+ const bodyWithMeta = body;
1655
+ const resourceWithVersion = {
1656
+ ...body,
1657
+ resourceType,
1658
+ id,
1659
+ meta: {
1660
+ ...bodyWithMeta.meta ?? {},
1661
+ lastUpdated: date,
1662
+ versionId: "2"
1663
+ }
1664
+ };
1665
+ const resourceWithAudit = {
1666
+ ...resourceWithVersion,
1667
+ meta: mergeAuditIntoMeta(resourceWithVersion.meta ?? existingMeta, {
1668
+ modifiedDate: date,
1669
+ modifiedById: actorId,
1670
+ modifiedByName: actorName
1671
+ })
1672
+ };
1673
+ return {
1674
+ resource: resourceWithAudit,
1675
+ lastUpdated: date
1676
+ };
1677
+ }
1647
1678
  async function updateDataEntityById(entity, tenantId, workspaceId, id, resourceLabel, context, buildPatched) {
1648
1679
  const existing = await entity.get({
1649
1680
  tenantId,
@@ -1675,9 +1706,9 @@ async function updateDataEntityById(entity, tenantId, workspaceId, id, resourceL
1675
1706
 
1676
1707
  // src/data/operations/data/encounter/encounter-create-operation.ts
1677
1708
  async function createEncounterOperation(params) {
1678
- const { context, body, id: optionalId, tableName } = params;
1709
+ const { context, body, tableName } = params;
1679
1710
  const { tenantId, workspaceId, date, actorId, actorName } = context;
1680
- const id = body.id ?? optionalId ?? ulid();
1711
+ const id = body.id ?? ulid();
1681
1712
  const meta = {
1682
1713
  ...body.meta ?? {},
1683
1714
  lastUpdated: date,
@@ -1709,7 +1740,7 @@ async function createEncounterOperation(params) {
1709
1740
 
1710
1741
  // src/data/rest-api/routes/data/encounter/encounter-create-route.ts
1711
1742
  async function createEncounterRoute(req, res) {
1712
- const bodyResult = requireJsonBody(req, res);
1743
+ const bodyResult = requireJsonBodyAs(req, res);
1713
1744
  if ("errorResponse" in bodyResult) return bodyResult.errorResponse;
1714
1745
  const ctx = req.openhiContext;
1715
1746
  const body = bodyResult.body;
@@ -1724,13 +1755,7 @@ async function createEncounterRoute(req, res) {
1724
1755
  });
1725
1756
  return res.status(201).location(`${BASE_PATH.ENCOUNTER}/${result.id}`).json(result.resource);
1726
1757
  } catch (err) {
1727
- console.error("POST Encounter error:", err);
1728
- return res.status(500).json({
1729
- resourceType: "OperationOutcome",
1730
- issue: [
1731
- { severity: "error", code: "exception", diagnostics: String(err) }
1732
- ]
1733
- });
1758
+ return sendOperationOutcome500(res, err, "POST Encounter error:");
1734
1759
  }
1735
1760
  }
1736
1761
 
@@ -1826,38 +1851,21 @@ async function updateEncounterOperation(params) {
1826
1851
  id,
1827
1852
  "Encounter",
1828
1853
  context,
1829
- (existingResourceStr) => {
1830
- const bodyWithResource = body;
1831
- const existingMeta = JSON.parse(existingResourceStr).meta;
1832
- const encounter = {
1833
- ...body,
1834
- resourceType: "Encounter",
1835
- id,
1836
- meta: {
1837
- ...bodyWithResource.meta ?? {},
1838
- lastUpdated: date,
1839
- versionId: "2"
1840
- }
1841
- };
1842
- const encounterWithMeta = {
1843
- ...encounter,
1844
- meta: mergeAuditIntoMeta(encounter.meta ?? existingMeta, {
1845
- modifiedDate: date,
1846
- modifiedById: actorId,
1847
- modifiedByName: actorName
1848
- })
1849
- };
1850
- return {
1851
- resource: encounterWithMeta,
1852
- lastUpdated: date
1853
- };
1854
- }
1854
+ (existingResourceStr) => buildUpdatedResourceWithAudit(
1855
+ body,
1856
+ id,
1857
+ date,
1858
+ actorId,
1859
+ actorName,
1860
+ existingResourceStr,
1861
+ "Encounter"
1862
+ )
1855
1863
  );
1856
1864
  }
1857
1865
 
1858
1866
  // src/data/rest-api/routes/data/encounter/encounter-update-route.ts
1859
1867
  async function updateEncounterRoute(req, res) {
1860
- const bodyResult = requireJsonBody(req, res);
1868
+ const bodyResult = requireJsonBodyAs(req, res);
1861
1869
  if ("errorResponse" in bodyResult) return bodyResult.errorResponse;
1862
1870
  const id = String(req.params.id);
1863
1871
  const ctx = req.openhiContext;
@@ -2024,9 +2032,9 @@ if (__require.main === module) {
2024
2032
 
2025
2033
  // src/data/operations/data/patient/patient-create-operation.ts
2026
2034
  async function createPatientOperation(params) {
2027
- const { context, body, id: optionalId } = params;
2035
+ const { context, body } = params;
2028
2036
  const { tenantId, workspaceId, date, actorId, actorName } = context;
2029
- const id = body.id ?? optionalId ?? ulid2();
2037
+ const id = body.id ?? ulid2();
2030
2038
  const meta = {
2031
2039
  ...body.meta ?? {},
2032
2040
  lastUpdated: date,
@@ -2059,7 +2067,7 @@ async function createPatientOperation(params) {
2059
2067
 
2060
2068
  // src/data/rest-api/routes/data/patient/patient-create-route.ts
2061
2069
  async function createPatientRoute(req, res) {
2062
- const bodyResult = requireJsonBody(req, res);
2070
+ const bodyResult = requireJsonBodyAs(req, res);
2063
2071
  if ("errorResponse" in bodyResult) return bodyResult.errorResponse;
2064
2072
  const ctx = req.openhiContext;
2065
2073
  const body = bodyResult.body;
@@ -2074,13 +2082,7 @@ async function createPatientRoute(req, res) {
2074
2082
  });
2075
2083
  return res.status(201).location(`${BASE_PATH.PATIENT}/${result.id}`).json(result.resource);
2076
2084
  } catch (err) {
2077
- console.error("POST Patient error:", err);
2078
- return res.status(500).json({
2079
- resourceType: "OperationOutcome",
2080
- issue: [
2081
- { severity: "error", code: "exception", diagnostics: String(err) }
2082
- ]
2083
- });
2085
+ return sendOperationOutcome500(res, err, "POST Patient error:");
2084
2086
  }
2085
2087
  }
2086
2088
 
@@ -2176,38 +2178,21 @@ async function updatePatientOperation(params) {
2176
2178
  id,
2177
2179
  "Patient",
2178
2180
  context,
2179
- (existingResourceStr) => {
2180
- const bodyWithResource = body;
2181
- const existingMeta = JSON.parse(existingResourceStr).meta;
2182
- const patient = {
2183
- ...body,
2184
- resourceType: "Patient",
2185
- id,
2186
- meta: {
2187
- ...bodyWithResource.meta ?? {},
2188
- lastUpdated: date,
2189
- versionId: "2"
2190
- }
2191
- };
2192
- const patientWithMeta = {
2193
- ...patient,
2194
- meta: mergeAuditIntoMeta(patient.meta ?? existingMeta, {
2195
- modifiedDate: date,
2196
- modifiedById: actorId,
2197
- modifiedByName: actorName
2198
- })
2199
- };
2200
- return {
2201
- resource: patientWithMeta,
2202
- lastUpdated: date
2203
- };
2204
- }
2181
+ (existingResourceStr) => buildUpdatedResourceWithAudit(
2182
+ body,
2183
+ id,
2184
+ date,
2185
+ actorId,
2186
+ actorName,
2187
+ existingResourceStr,
2188
+ "Patient"
2189
+ )
2205
2190
  );
2206
2191
  }
2207
2192
 
2208
2193
  // src/data/rest-api/routes/data/patient/patient-update-route.ts
2209
2194
  async function updatePatientRoute(req, res) {
2210
- const bodyResult = requireJsonBody(req, res);
2195
+ const bodyResult = requireJsonBodyAs(req, res);
2211
2196
  if ("errorResponse" in bodyResult) return bodyResult.errorResponse;
2212
2197
  const id = String(req.params.id);
2213
2198
  const ctx = req.openhiContext;
@@ -2248,9 +2233,9 @@ import express4 from "express";
2248
2233
  // src/data/operations/data/practitioner/practitioner-create-operation.ts
2249
2234
  import { ulid as ulid3 } from "ulid";
2250
2235
  async function createPractitionerOperation(params) {
2251
- const { context, body, id: optionalId, tableName } = params;
2236
+ const { context, body, tableName } = params;
2252
2237
  const { tenantId, workspaceId, date, actorId, actorName } = context;
2253
- const id = body.id ?? optionalId ?? ulid3();
2238
+ const id = body.id ?? ulid3();
2254
2239
  const meta = {
2255
2240
  ...body.meta ?? {},
2256
2241
  lastUpdated: date,
@@ -2282,7 +2267,7 @@ async function createPractitionerOperation(params) {
2282
2267
 
2283
2268
  // src/data/rest-api/routes/data/practitioner/practitioner-create-route.ts
2284
2269
  async function createPractitionerRoute(req, res) {
2285
- const bodyResult = requireJsonBody(req, res);
2270
+ const bodyResult = requireJsonBodyAs(req, res);
2286
2271
  if ("errorResponse" in bodyResult) return bodyResult.errorResponse;
2287
2272
  const ctx = req.openhiContext;
2288
2273
  const body = bodyResult.body;
@@ -2297,13 +2282,7 @@ async function createPractitionerRoute(req, res) {
2297
2282
  });
2298
2283
  return res.status(201).location(`${BASE_PATH.PRACTITIONER}/${result.id}`).json(result.resource);
2299
2284
  } catch (err) {
2300
- console.error("POST Practitioner error:", err);
2301
- return res.status(500).json({
2302
- resourceType: "OperationOutcome",
2303
- issue: [
2304
- { severity: "error", code: "exception", diagnostics: String(err) }
2305
- ]
2306
- });
2285
+ return sendOperationOutcome500(res, err, "POST Practitioner error:");
2307
2286
  }
2308
2287
  }
2309
2288
 
@@ -2399,38 +2378,21 @@ async function updatePractitionerOperation(params) {
2399
2378
  id,
2400
2379
  "Practitioner",
2401
2380
  context,
2402
- (existingResourceStr) => {
2403
- const bodyWithResource = body;
2404
- const existingMeta = JSON.parse(existingResourceStr).meta;
2405
- const practitioner = {
2406
- ...body,
2407
- resourceType: "Practitioner",
2408
- id,
2409
- meta: {
2410
- ...bodyWithResource.meta ?? {},
2411
- lastUpdated: date,
2412
- versionId: "2"
2413
- }
2414
- };
2415
- const practitionerWithMeta = {
2416
- ...practitioner,
2417
- meta: mergeAuditIntoMeta(practitioner.meta ?? existingMeta, {
2418
- modifiedDate: date,
2419
- modifiedById: actorId,
2420
- modifiedByName: actorName
2421
- })
2422
- };
2423
- return {
2424
- resource: practitionerWithMeta,
2425
- lastUpdated: date
2426
- };
2427
- }
2381
+ (existingResourceStr) => buildUpdatedResourceWithAudit(
2382
+ body,
2383
+ id,
2384
+ date,
2385
+ actorId,
2386
+ actorName,
2387
+ existingResourceStr,
2388
+ "Practitioner"
2389
+ )
2428
2390
  );
2429
2391
  }
2430
2392
 
2431
2393
  // src/data/rest-api/routes/data/practitioner/practitioner-update-route.ts
2432
2394
  async function updatePractitionerRoute(req, res) {
2433
- const bodyResult = requireJsonBody(req, res);
2395
+ const bodyResult = requireJsonBodyAs(req, res);
2434
2396
  if ("errorResponse" in bodyResult) return bodyResult.errorResponse;
2435
2397
  const id = String(req.params.id);
2436
2398
  const ctx = req.openhiContext;