@ovok/core 0.2.58 → 0.2.61

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.
@@ -12,14 +12,31 @@ const getPatientReference = async (client, patientId) => {
12
12
  return (0, core_1.getReferenceString)(profile);
13
13
  };
14
14
  async function generateObservationBodiesByMeasurement(params) {
15
+ var _a;
15
16
  const { device, measurement } = params;
16
17
  const measurementTypeKey = measurement.measurementTypeKey;
17
18
  const patientReference = await getPatientReference(this, params.patientId);
18
19
  const observationService = creator_1.ObservationServiceCreator.getInstance().getService(measurementTypeKey);
20
+ const deviceObject = (0, getDeviceObject_1.getDeviceObject)(device);
21
+ const observationFragments = (_a = observationService.generateObservationFragments) === null || _a === void 0 ? void 0 : _a.call(observationService, measurement);
22
+ if (observationFragments) {
23
+ return observationFragments
24
+ .map((fragment) => ({
25
+ resourceType: "Observation",
26
+ status: "registered",
27
+ subject: {
28
+ reference: patientReference,
29
+ },
30
+ device: deviceObject,
31
+ ...fragment.value,
32
+ code: fragment.code,
33
+ ...(fragment.additionalProperties || {}),
34
+ }))
35
+ .filter(hasObservationValue);
36
+ }
19
37
  const observationCodes = observationService.getObservationCodes();
20
38
  const values = observationService.generateValues(measurement);
21
39
  const additionalProperties = observationService.getAdditionalProperties(measurement);
22
- const deviceObject = (0, getDeviceObject_1.getDeviceObject)(device);
23
40
  const observationBodies = observationCodes.map((code, index) => ({
24
41
  resourceType: "Observation",
25
42
  status: "registered",
@@ -31,10 +48,11 @@ async function generateObservationBodiesByMeasurement(params) {
31
48
  code,
32
49
  ...(additionalProperties[index] || {}),
33
50
  }));
34
- return observationBodies.filter((observation) => {
35
- var _a, _b, _c, _d, _e;
36
- return ((_a = observation.valueQuantity) === null || _a === void 0 ? void 0 : _a.value) ||
37
- ((_b = observation.valueSampledData) === null || _b === void 0 ? void 0 : _b.data) ||
38
- ((_e = (_d = (_c = observation.valueCodeableConcept) === null || _c === void 0 ? void 0 : _c.coding) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.code);
39
- });
51
+ return observationBodies.filter(hasObservationValue);
40
52
  }
53
+ const hasObservationValue = (observation) => {
54
+ var _a, _b, _c, _d, _e;
55
+ return ((_a = observation.valueQuantity) === null || _a === void 0 ? void 0 : _a.value) !== undefined ||
56
+ Boolean((_b = observation.valueSampledData) === null || _b === void 0 ? void 0 : _b.data) ||
57
+ Boolean((_e = (_d = (_c = observation.valueCodeableConcept) === null || _c === void 0 ? void 0 : _c.coding) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.code);
58
+ };
@@ -15,7 +15,7 @@ class BodyTemperatureService extends base_observation_service_1.BaseObservationS
15
15
  display: "Body Temperature",
16
16
  unit: "°C",
17
17
  valueKey: "bodyTemp",
18
- toFixed: 0,
18
+ toFixed: 1,
19
19
  },
20
20
  ];
21
21
  }
@@ -0,0 +1,4 @@
1
+ import { CodeableConcept } from "@medplum/fhirtypes";
2
+ import { EcgMeasurement } from "../../../types";
3
+ import { ObservationFragment } from "../types/generate-observation-body/ObservationFragment";
4
+ export declare const generateEcgObservationFragments: (measurement: EcgMeasurement, observationCodes: CodeableConcept[]) => ObservationFragment[];
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateEcgObservationFragments = void 0;
4
+ const MAX_SEGMENT_DURATION_SECONDS = 5 * 60;
5
+ const generateEcgObservationFragments = (measurement, observationCodes) => {
6
+ const fragments = [];
7
+ const diagramPoints = measurement.diagramPoints;
8
+ const recordedAt = isValidDate(measurement.recordedAt)
9
+ ? measurement.recordedAt
10
+ : new Date();
11
+ const duration = typeof measurement.duration === "number" &&
12
+ Number.isFinite(measurement.duration) &&
13
+ measurement.duration > 0
14
+ ? measurement.duration
15
+ : undefined;
16
+ if (diagramPoints === null || diagramPoints === void 0 ? void 0 : diagramPoints.length) {
17
+ const maxSamples = duration
18
+ ? Math.max(1, Math.floor((MAX_SEGMENT_DURATION_SECONDS * diagramPoints.length) / duration))
19
+ : diagramPoints.length;
20
+ const recordingId = `ecg-${recordedAt.getTime()}-${diagramPoints.length}`;
21
+ const ecgCode = observationCodes[0];
22
+ for (let startIndex = 0, segmentIndex = 0; startIndex < diagramPoints.length; startIndex += maxSamples, segmentIndex++) {
23
+ const segment = diagramPoints.slice(startIndex, startIndex + maxSamples);
24
+ const segmentDuration = duration
25
+ ? (duration * segment.length) / diagramPoints.length
26
+ : segment.length / 1000;
27
+ const segmentStart = new Date(recordedAt.getTime() +
28
+ (duration
29
+ ? (duration * startIndex * 1000) / diagramPoints.length
30
+ : startIndex));
31
+ const segmentEnd = new Date(segmentStart.getTime() + segmentDuration * 1000);
32
+ const firstId = recordingId;
33
+ fragments.push({
34
+ code: ecgCode,
35
+ value: {
36
+ valueSampledData: {
37
+ origin: { value: 0, unit: "mV" },
38
+ period: duration
39
+ ? (duration * 1000) / diagramPoints.length
40
+ : 1,
41
+ dimensions: 1,
42
+ data: segment.map((sample) => Number(sample.toFixed(3))).join(" "),
43
+ },
44
+ },
45
+ additionalProperties: {
46
+ id: segmentIndex === 0 ? firstId : `${recordingId}-${segmentIndex}`,
47
+ effectivePeriod: {
48
+ start: segmentStart.toISOString(),
49
+ end: segmentEnd.toISOString(),
50
+ },
51
+ hasMember: segmentIndex === 0
52
+ ? []
53
+ : [{ reference: `Observation/${firstId}` }],
54
+ ...(segmentIndex === 0 && measurement.diagnosticResult
55
+ ? { interpretation: [{ text: measurement.diagnosticResult }] }
56
+ : {}),
57
+ ...(segmentIndex === 0 && duration !== undefined
58
+ ? { component: [durationComponent(duration)] }
59
+ : {}),
60
+ },
61
+ });
62
+ }
63
+ }
64
+ if (typeof measurement.heartRate === "number") {
65
+ fragments.push({
66
+ code: observationCodes[1],
67
+ value: {
68
+ valueQuantity: {
69
+ value: Number(measurement.heartRate.toFixed(0)),
70
+ unit: "bpm",
71
+ code: "8867-4",
72
+ system: "http://loinc.org",
73
+ },
74
+ },
75
+ additionalProperties: {
76
+ effectiveDateTime: recordedAt.toISOString(),
77
+ },
78
+ });
79
+ }
80
+ return fragments;
81
+ };
82
+ exports.generateEcgObservationFragments = generateEcgObservationFragments;
83
+ const durationComponent = (duration) => ({
84
+ code: {
85
+ text: "measurement-duration",
86
+ coding: [{ code: "measurement-duration" }],
87
+ },
88
+ id: "measurement-duration",
89
+ valueQuantity: {
90
+ value: duration,
91
+ unit: "s",
92
+ code: "s",
93
+ system: "http://unitsofmeasure.org",
94
+ },
95
+ });
96
+ const isValidDate = (value) => value instanceof Date && !Number.isNaN(value.getTime());
@@ -1,5 +1,6 @@
1
1
  import { Observation } from "@medplum/fhirtypes";
2
2
  import { EcgMeasurement, Measurement, MeasurementTypeKey } from "../../../types";
3
+ import { ObservationFragment } from "../types/generate-observation-body/ObservationFragment";
3
4
  import { ObservationValue } from "../types/generate-observation-body/ObservationValue";
4
5
  import { ObservationMetadata } from "../types/service/ObservationMetadata";
5
6
  import { BaseObservationService } from "./base-observation-service";
@@ -8,6 +9,8 @@ export declare class EcgService extends BaseObservationService<EcgMeasurement> {
8
9
  protected measurementTypeName: string;
9
10
  protected observationMetadata: ObservationMetadata<EcgMeasurement>[];
10
11
  generateValues(measurement: Measurement): ObservationValue[];
12
+ /** Expand long recordings into bounded, timestamped observations. */
13
+ generateObservationFragments(measurement: Measurement): ObservationFragment[];
11
14
  getAdditionalProperties(measurement: Measurement): Partial<Observation>[];
12
15
  getMeasurementDisplay(measurement: Measurement): string;
13
16
  getObservationDisplay(observations: Observation[]): string | undefined;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EcgService = void 0;
4
4
  const types_1 = require("../../../types");
5
5
  const base_observation_service_1 = require("./base-observation-service");
6
+ const ecg_observation_fragments_1 = require("./ecg-observation-fragments");
6
7
  class EcgService extends base_observation_service_1.BaseObservationService {
7
8
  constructor() {
8
9
  super(...arguments);
@@ -59,6 +60,11 @@ class EcgService extends base_observation_service_1.BaseObservationService {
59
60
  }
60
61
  return values;
61
62
  }
63
+ /** Expand long recordings into bounded, timestamped observations. */
64
+ generateObservationFragments(measurement) {
65
+ const castedMeasurement = this.getCastedMeasurement(measurement);
66
+ return (0, ecg_observation_fragments_1.generateEcgObservationFragments)(castedMeasurement, this.getObservationCodes());
67
+ }
62
68
  getAdditionalProperties(measurement) {
63
69
  const castedMeasurement = this.getCastedMeasurement(measurement);
64
70
  const additionalProperties = [
@@ -1,6 +1,7 @@
1
1
  import { Observation } from "@medplum/fhirtypes";
2
2
  import { Measurement, MeasurementTypeKey, Sample, UrineAnalyzeMeasurement } from "../../../types";
3
3
  import { RecordResult, RecordType } from "../types/generate-measurement/HealthConnectTypes";
4
+ import { ObservationFragment } from "../types/generate-observation-body/ObservationFragment";
4
5
  import { ObservationValue } from "../types/generate-observation-body/ObservationValue";
5
6
  import { ObservationMetadata } from "../types/service/ObservationMetadata";
6
7
  import { BaseObservationService } from "./base-observation-service";
@@ -11,6 +12,13 @@ export declare class UricAcidService extends BaseObservationService<UrineAnalyze
11
12
  protected observationMetadata: ObservationMetadata<UrineAnalyzeMeasurement>[];
12
13
  generateValues(measurement: Measurement): ObservationValue[];
13
14
  getAdditionalProperties(measurement: Measurement): Partial<Observation>[];
15
+ /**
16
+ * A strip contains independent analyte readings. Emit each measured value
17
+ * as its own Observation so zero is retained and decimal values never pass
18
+ * through the integer-only component representation used by the legacy
19
+ * panel implementation.
20
+ */
21
+ generateObservationFragments(measurement: Measurement): ObservationFragment[];
14
22
  getMeasurementDisplay(measurement: Measurement): string;
15
23
  getObservationDisplay(observations: Observation[]): string | undefined;
16
24
  convertSamplesToMeasurement(_samples: Sample[]): UrineAnalyzeMeasurement;
@@ -36,7 +36,9 @@ class UricAcidService extends base_observation_service_1.BaseObservationService
36
36
  getAdditionalProperties(measurement) {
37
37
  const castedMeasurement = this.getCastedMeasurement(measurement);
38
38
  const components = Object.entries(castedMeasurement)
39
- .filter(([key]) => key !== "measurementTypeKey" && key !== "urineValues")
39
+ .filter(([key]) => key !== "measurementTypeKey" &&
40
+ key !== "urineValues" &&
41
+ key !== "recordedAt")
40
42
  .map(([key, value]) => this.getComponent(key, value));
41
43
  return [
42
44
  {
@@ -45,6 +47,50 @@ class UricAcidService extends base_observation_service_1.BaseObservationService
45
47
  },
46
48
  ];
47
49
  }
50
+ /**
51
+ * A strip contains independent analyte readings. Emit each measured value
52
+ * as its own Observation so zero is retained and decimal values never pass
53
+ * through the integer-only component representation used by the legacy
54
+ * panel implementation.
55
+ */
56
+ generateObservationFragments(measurement) {
57
+ const castedMeasurement = this.getCastedMeasurement(measurement);
58
+ const effectiveDateTime = castedMeasurement.recordedAt instanceof Date &&
59
+ !Number.isNaN(castedMeasurement.recordedAt.getTime())
60
+ ? castedMeasurement.recordedAt.toISOString()
61
+ : new Date().toISOString();
62
+ return types_1.URINE_ANALYTE_DEFINITIONS.flatMap((analyte) => {
63
+ const value = castedMeasurement[analyte.key];
64
+ if (value === undefined || value === null || !Number.isFinite(value)) {
65
+ return [];
66
+ }
67
+ return [
68
+ {
69
+ code: {
70
+ coding: [
71
+ {
72
+ code: analyte.code,
73
+ display: analyte.display,
74
+ system: "http://loinc.org",
75
+ },
76
+ ],
77
+ text: analyte.display,
78
+ },
79
+ value: {
80
+ valueQuantity: {
81
+ value,
82
+ unit: "1",
83
+ code: "1",
84
+ system: "http://unitsofmeasure.org",
85
+ },
86
+ },
87
+ additionalProperties: {
88
+ effectiveDateTime,
89
+ },
90
+ },
91
+ ];
92
+ });
93
+ }
48
94
  getMeasurementDisplay(measurement) {
49
95
  const castedMeasurement = this.getCastedMeasurement(measurement);
50
96
  const problemCount = this.countProblems(castedMeasurement);
@@ -1,10 +1,12 @@
1
1
  import { CodeableConcept, Observation } from "@medplum/fhirtypes";
2
2
  import { RecordResult, RecordType } from "./generate-measurement/HealthConnectTypes";
3
3
  import { Sample } from "./generate-measurement/Sample";
4
+ import { ObservationFragment } from "./generate-observation-body/ObservationFragment";
4
5
  import { ObservationValue } from "./generate-observation-body/ObservationValue";
5
6
  import { Measurement } from "./measurement/Measurement";
6
7
  export interface IObservationService {
7
8
  generateValues: (measurement: Measurement) => ObservationValue[];
9
+ generateObservationFragments?: (measurement: Measurement) => ObservationFragment[];
8
10
  getObservationCodes: () => CodeableConcept[];
9
11
  getAdditionalProperties: (measurement: Measurement) => Partial<Observation>[];
10
12
  getUnits: () => string[];
@@ -0,0 +1,12 @@
1
+ export type UrineAnalyteKey = "uro" | "bil" | "ket" | "glu" | "pro" | "ph" | "bld" | "nit" | "leu" | "sg" | "vc";
2
+ export type UrineAnalyteDefinition = {
3
+ key: UrineAnalyteKey;
4
+ code: string;
5
+ display: string;
6
+ };
7
+ /**
8
+ * LOINC mappings for the eleven values returned by Contec's BC401 strip.
9
+ * These are exported so Native declarations and consuming apps can describe
10
+ * the same analytes without maintaining a second, drifting table.
11
+ */
12
+ export declare const URINE_ANALYTE_DEFINITIONS: readonly UrineAnalyteDefinition[];
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.URINE_ANALYTE_DEFINITIONS = void 0;
4
+ /**
5
+ * LOINC mappings for the eleven values returned by Contec's BC401 strip.
6
+ * These are exported so Native declarations and consuming apps can describe
7
+ * the same analytes without maintaining a second, drifting table.
8
+ */
9
+ exports.URINE_ANALYTE_DEFINITIONS = [
10
+ { key: "uro", code: "50563-6", display: "Urobilinogen" },
11
+ { key: "bld", code: "50559-4", display: "Hemoglobin" },
12
+ { key: "bil", code: "53327-3", display: "Bilirubin" },
13
+ { key: "ket", code: "50557-8", display: "Ketones" },
14
+ { key: "glu", code: "53328-1", display: "Glucose" },
15
+ { key: "pro", code: "50561-0", display: "Protein" },
16
+ { key: "ph", code: "50560-2", display: "Urine pH" },
17
+ { key: "nit", code: "50558-6", display: "Nitrite" },
18
+ { key: "leu", code: "60026-2", display: "Leukocyte esterase" },
19
+ { key: "sg", code: "53326-5", display: "Specific gravity" },
20
+ { key: "vc", code: "5768-7", display: "Ascorbate (vitamin C)" },
21
+ ];
@@ -0,0 +1,12 @@
1
+ import { CodeableConcept, Observation } from "@medplum/fhirtypes";
2
+ import { ObservationValue } from "./ObservationValue";
3
+ /**
4
+ * A service-specific observation that is expanded by the common observation
5
+ * body generator. This is used when one measurement has to produce more than
6
+ * one FHIR Observation, such as a long ECG or a urine strip's analytes.
7
+ */
8
+ export type ObservationFragment = {
9
+ code: CodeableConcept;
10
+ value?: ObservationValue;
11
+ additionalProperties?: Partial<Observation>;
12
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -10,4 +10,6 @@ export type EcgMeasurement = Measurement & {
10
10
  * Duration of the ECG in seconds
11
11
  */
12
12
  duration?: number;
13
+ /** Time at which the device started recording the ECG. */
14
+ recordedAt?: Date;
13
15
  };
@@ -13,6 +13,8 @@ export type UrineAnalyzeMeasurement = Measurement & {
13
13
  sg?: number;
14
14
  vc?: number;
15
15
  urineValues: UrineAnalysisValue[];
16
+ /** Time at which the analyzer recorded the strip. */
17
+ recordedAt?: Date;
16
18
  };
17
19
  type UrineAnalysisValue = {
18
20
  code: CodeableConcept;
@@ -48,7 +48,7 @@ exports.OVOK_CORE_REQUIREMENTS = {
48
48
  description: "The FHIR surface a server must provide for the @ovok/core client library's own methods to function.",
49
49
  software: {
50
50
  name: "@ovok/core",
51
- version: "0.2.57",
51
+ version: "0.2.60",
52
52
  },
53
53
  fhirVersion: "4.0.1",
54
54
  format: ["json"],
@@ -6,6 +6,7 @@ export * from "../client/auth/types/RequestDeleteUserBody";
6
6
  export * from "../client/auth/types/ResetPasswordBody";
7
7
  export * from "../client/observation/types/MeasurementTypeKey";
8
8
  export * from "../client/observation/types/ObservationCode";
9
+ export * from "../client/observation/types/UrineAnalytes";
9
10
  export * from "../client/observation/types/measurement";
10
11
  export * from "../client/observation/types/generate-measurement/Sample";
11
12
  export * from "../client/observation/types/GetLatestObservationsByCodeParams";
@@ -24,6 +24,7 @@ __exportStar(require("../client/auth/types/ResetPasswordBody"), exports);
24
24
  // Measurement types
25
25
  __exportStar(require("../client/observation/types/MeasurementTypeKey"), exports);
26
26
  __exportStar(require("../client/observation/types/ObservationCode"), exports);
27
+ __exportStar(require("../client/observation/types/UrineAnalytes"), exports);
27
28
  __exportStar(require("../client/observation/types/measurement"), exports);
28
29
  __exportStar(require("../client/observation/types/generate-measurement/Sample"), exports);
29
30
  // Observation types
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ovok/core",
3
- "version": "0.2.58",
3
+ "version": "0.2.61",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",