@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.
@@ -377,6 +377,11 @@ function requireJsonBody(req, res) {
377
377
  }
378
378
  return { body: raw };
379
379
  }
380
+ function requireJsonBodyAs(req, res) {
381
+ const result = requireJsonBody(req, res);
382
+ if ("errorResponse" in result) return result;
383
+ return { body: result.body };
384
+ }
380
385
  function buildSearchsetBundle(basePath, entries) {
381
386
  return {
382
387
  resourceType: "Bundle",
@@ -1670,6 +1675,32 @@ async function createDataEntityRecord(entity, tenantId, workspaceId, id, resourc
1670
1675
  resource: resourceWithAudit
1671
1676
  };
1672
1677
  }
1678
+ function buildUpdatedResourceWithAudit(body, id, date, actorId, actorName, existingResourceStr, resourceType) {
1679
+ const existingMeta = JSON.parse(existingResourceStr).meta;
1680
+ const bodyWithMeta = body;
1681
+ const resourceWithVersion = {
1682
+ ...body,
1683
+ resourceType,
1684
+ id,
1685
+ meta: {
1686
+ ...bodyWithMeta.meta ?? {},
1687
+ lastUpdated: date,
1688
+ versionId: "2"
1689
+ }
1690
+ };
1691
+ const resourceWithAudit = {
1692
+ ...resourceWithVersion,
1693
+ meta: mergeAuditIntoMeta(resourceWithVersion.meta ?? existingMeta, {
1694
+ modifiedDate: date,
1695
+ modifiedById: actorId,
1696
+ modifiedByName: actorName
1697
+ })
1698
+ };
1699
+ return {
1700
+ resource: resourceWithAudit,
1701
+ lastUpdated: date
1702
+ };
1703
+ }
1673
1704
  async function updateDataEntityById(entity, tenantId, workspaceId, id, resourceLabel, context, buildPatched) {
1674
1705
  const existing = await entity.get({
1675
1706
  tenantId,
@@ -1701,9 +1732,9 @@ async function updateDataEntityById(entity, tenantId, workspaceId, id, resourceL
1701
1732
 
1702
1733
  // src/data/operations/data/encounter/encounter-create-operation.ts
1703
1734
  async function createEncounterOperation(params) {
1704
- const { context, body, id: optionalId, tableName } = params;
1735
+ const { context, body, tableName } = params;
1705
1736
  const { tenantId, workspaceId, date, actorId, actorName } = context;
1706
- const id = body.id ?? optionalId ?? (0, import_ulid.ulid)();
1737
+ const id = body.id ?? (0, import_ulid.ulid)();
1707
1738
  const meta = {
1708
1739
  ...body.meta ?? {},
1709
1740
  lastUpdated: date,
@@ -1735,7 +1766,7 @@ async function createEncounterOperation(params) {
1735
1766
 
1736
1767
  // src/data/rest-api/routes/data/encounter/encounter-create-route.ts
1737
1768
  async function createEncounterRoute(req, res) {
1738
- const bodyResult = requireJsonBody(req, res);
1769
+ const bodyResult = requireJsonBodyAs(req, res);
1739
1770
  if ("errorResponse" in bodyResult) return bodyResult.errorResponse;
1740
1771
  const ctx = req.openhiContext;
1741
1772
  const body = bodyResult.body;
@@ -1750,13 +1781,7 @@ async function createEncounterRoute(req, res) {
1750
1781
  });
1751
1782
  return res.status(201).location(`${BASE_PATH.ENCOUNTER}/${result.id}`).json(result.resource);
1752
1783
  } catch (err) {
1753
- console.error("POST Encounter error:", err);
1754
- return res.status(500).json({
1755
- resourceType: "OperationOutcome",
1756
- issue: [
1757
- { severity: "error", code: "exception", diagnostics: String(err) }
1758
- ]
1759
- });
1784
+ return sendOperationOutcome500(res, err, "POST Encounter error:");
1760
1785
  }
1761
1786
  }
1762
1787
 
@@ -1852,38 +1877,21 @@ async function updateEncounterOperation(params) {
1852
1877
  id,
1853
1878
  "Encounter",
1854
1879
  context,
1855
- (existingResourceStr) => {
1856
- const bodyWithResource = body;
1857
- const existingMeta = JSON.parse(existingResourceStr).meta;
1858
- const encounter = {
1859
- ...body,
1860
- resourceType: "Encounter",
1861
- id,
1862
- meta: {
1863
- ...bodyWithResource.meta ?? {},
1864
- lastUpdated: date,
1865
- versionId: "2"
1866
- }
1867
- };
1868
- const encounterWithMeta = {
1869
- ...encounter,
1870
- meta: mergeAuditIntoMeta(encounter.meta ?? existingMeta, {
1871
- modifiedDate: date,
1872
- modifiedById: actorId,
1873
- modifiedByName: actorName
1874
- })
1875
- };
1876
- return {
1877
- resource: encounterWithMeta,
1878
- lastUpdated: date
1879
- };
1880
- }
1880
+ (existingResourceStr) => buildUpdatedResourceWithAudit(
1881
+ body,
1882
+ id,
1883
+ date,
1884
+ actorId,
1885
+ actorName,
1886
+ existingResourceStr,
1887
+ "Encounter"
1888
+ )
1881
1889
  );
1882
1890
  }
1883
1891
 
1884
1892
  // src/data/rest-api/routes/data/encounter/encounter-update-route.ts
1885
1893
  async function updateEncounterRoute(req, res) {
1886
- const bodyResult = requireJsonBody(req, res);
1894
+ const bodyResult = requireJsonBodyAs(req, res);
1887
1895
  if ("errorResponse" in bodyResult) return bodyResult.errorResponse;
1888
1896
  const id = String(req.params.id);
1889
1897
  const ctx = req.openhiContext;
@@ -2050,9 +2058,9 @@ if (require.main === module) {
2050
2058
 
2051
2059
  // src/data/operations/data/patient/patient-create-operation.ts
2052
2060
  async function createPatientOperation(params) {
2053
- const { context, body, id: optionalId } = params;
2061
+ const { context, body } = params;
2054
2062
  const { tenantId, workspaceId, date, actorId, actorName } = context;
2055
- const id = body.id ?? optionalId ?? (0, import_ulid2.ulid)();
2063
+ const id = body.id ?? (0, import_ulid2.ulid)();
2056
2064
  const meta = {
2057
2065
  ...body.meta ?? {},
2058
2066
  lastUpdated: date,
@@ -2085,7 +2093,7 @@ async function createPatientOperation(params) {
2085
2093
 
2086
2094
  // src/data/rest-api/routes/data/patient/patient-create-route.ts
2087
2095
  async function createPatientRoute(req, res) {
2088
- const bodyResult = requireJsonBody(req, res);
2096
+ const bodyResult = requireJsonBodyAs(req, res);
2089
2097
  if ("errorResponse" in bodyResult) return bodyResult.errorResponse;
2090
2098
  const ctx = req.openhiContext;
2091
2099
  const body = bodyResult.body;
@@ -2100,13 +2108,7 @@ async function createPatientRoute(req, res) {
2100
2108
  });
2101
2109
  return res.status(201).location(`${BASE_PATH.PATIENT}/${result.id}`).json(result.resource);
2102
2110
  } catch (err) {
2103
- console.error("POST Patient error:", err);
2104
- return res.status(500).json({
2105
- resourceType: "OperationOutcome",
2106
- issue: [
2107
- { severity: "error", code: "exception", diagnostics: String(err) }
2108
- ]
2109
- });
2111
+ return sendOperationOutcome500(res, err, "POST Patient error:");
2110
2112
  }
2111
2113
  }
2112
2114
 
@@ -2202,38 +2204,21 @@ async function updatePatientOperation(params) {
2202
2204
  id,
2203
2205
  "Patient",
2204
2206
  context,
2205
- (existingResourceStr) => {
2206
- const bodyWithResource = body;
2207
- const existingMeta = JSON.parse(existingResourceStr).meta;
2208
- const patient = {
2209
- ...body,
2210
- resourceType: "Patient",
2211
- id,
2212
- meta: {
2213
- ...bodyWithResource.meta ?? {},
2214
- lastUpdated: date,
2215
- versionId: "2"
2216
- }
2217
- };
2218
- const patientWithMeta = {
2219
- ...patient,
2220
- meta: mergeAuditIntoMeta(patient.meta ?? existingMeta, {
2221
- modifiedDate: date,
2222
- modifiedById: actorId,
2223
- modifiedByName: actorName
2224
- })
2225
- };
2226
- return {
2227
- resource: patientWithMeta,
2228
- lastUpdated: date
2229
- };
2230
- }
2207
+ (existingResourceStr) => buildUpdatedResourceWithAudit(
2208
+ body,
2209
+ id,
2210
+ date,
2211
+ actorId,
2212
+ actorName,
2213
+ existingResourceStr,
2214
+ "Patient"
2215
+ )
2231
2216
  );
2232
2217
  }
2233
2218
 
2234
2219
  // src/data/rest-api/routes/data/patient/patient-update-route.ts
2235
2220
  async function updatePatientRoute(req, res) {
2236
- const bodyResult = requireJsonBody(req, res);
2221
+ const bodyResult = requireJsonBodyAs(req, res);
2237
2222
  if ("errorResponse" in bodyResult) return bodyResult.errorResponse;
2238
2223
  const id = String(req.params.id);
2239
2224
  const ctx = req.openhiContext;
@@ -2274,9 +2259,9 @@ var import_express4 = __toESM(require("express"));
2274
2259
  // src/data/operations/data/practitioner/practitioner-create-operation.ts
2275
2260
  var import_ulid3 = require("ulid");
2276
2261
  async function createPractitionerOperation(params) {
2277
- const { context, body, id: optionalId, tableName } = params;
2262
+ const { context, body, tableName } = params;
2278
2263
  const { tenantId, workspaceId, date, actorId, actorName } = context;
2279
- const id = body.id ?? optionalId ?? (0, import_ulid3.ulid)();
2264
+ const id = body.id ?? (0, import_ulid3.ulid)();
2280
2265
  const meta = {
2281
2266
  ...body.meta ?? {},
2282
2267
  lastUpdated: date,
@@ -2308,7 +2293,7 @@ async function createPractitionerOperation(params) {
2308
2293
 
2309
2294
  // src/data/rest-api/routes/data/practitioner/practitioner-create-route.ts
2310
2295
  async function createPractitionerRoute(req, res) {
2311
- const bodyResult = requireJsonBody(req, res);
2296
+ const bodyResult = requireJsonBodyAs(req, res);
2312
2297
  if ("errorResponse" in bodyResult) return bodyResult.errorResponse;
2313
2298
  const ctx = req.openhiContext;
2314
2299
  const body = bodyResult.body;
@@ -2323,13 +2308,7 @@ async function createPractitionerRoute(req, res) {
2323
2308
  });
2324
2309
  return res.status(201).location(`${BASE_PATH.PRACTITIONER}/${result.id}`).json(result.resource);
2325
2310
  } catch (err) {
2326
- console.error("POST Practitioner error:", err);
2327
- return res.status(500).json({
2328
- resourceType: "OperationOutcome",
2329
- issue: [
2330
- { severity: "error", code: "exception", diagnostics: String(err) }
2331
- ]
2332
- });
2311
+ return sendOperationOutcome500(res, err, "POST Practitioner error:");
2333
2312
  }
2334
2313
  }
2335
2314
 
@@ -2425,38 +2404,21 @@ async function updatePractitionerOperation(params) {
2425
2404
  id,
2426
2405
  "Practitioner",
2427
2406
  context,
2428
- (existingResourceStr) => {
2429
- const bodyWithResource = body;
2430
- const existingMeta = JSON.parse(existingResourceStr).meta;
2431
- const practitioner = {
2432
- ...body,
2433
- resourceType: "Practitioner",
2434
- id,
2435
- meta: {
2436
- ...bodyWithResource.meta ?? {},
2437
- lastUpdated: date,
2438
- versionId: "2"
2439
- }
2440
- };
2441
- const practitionerWithMeta = {
2442
- ...practitioner,
2443
- meta: mergeAuditIntoMeta(practitioner.meta ?? existingMeta, {
2444
- modifiedDate: date,
2445
- modifiedById: actorId,
2446
- modifiedByName: actorName
2447
- })
2448
- };
2449
- return {
2450
- resource: practitionerWithMeta,
2451
- lastUpdated: date
2452
- };
2453
- }
2407
+ (existingResourceStr) => buildUpdatedResourceWithAudit(
2408
+ body,
2409
+ id,
2410
+ date,
2411
+ actorId,
2412
+ actorName,
2413
+ existingResourceStr,
2414
+ "Practitioner"
2415
+ )
2454
2416
  );
2455
2417
  }
2456
2418
 
2457
2419
  // src/data/rest-api/routes/data/practitioner/practitioner-update-route.ts
2458
2420
  async function updatePractitionerRoute(req, res) {
2459
- const bodyResult = requireJsonBody(req, res);
2421
+ const bodyResult = requireJsonBodyAs(req, res);
2460
2422
  if ("errorResponse" in bodyResult) return bodyResult.errorResponse;
2461
2423
  const id = String(req.params.id);
2462
2424
  const ctx = req.openhiContext;