Skip to content

Commit 8958536

Browse files
committed
Add condition_code to saveFhirData
1 parent 0dc0df6 commit 8958536

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

containers/ecr-viewer/integration/saveFhirDataService/core.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ const baseCoreMetadata: BundleMetadata = {
1919
rr: [],
2020
report_date: "12/20/2024",
2121
};
22+
const condition_reference = {
23+
code: "123",
24+
concept_name: "condition (disease)",
25+
condition_name: "condition",
26+
condition_category: "category",
27+
};
2228

2329
const makePromiseResolveWithStatus = (status: number): Promise<BlobResponse> =>
2430
new Promise((resolve) => resolve({ message: "hi there", status }));
@@ -210,4 +216,51 @@ describe("saveFhirData - core", () => {
210216
expect(resp.status).toEqual(400);
211217
expect(rolledback).toBeFalse();
212218
});
219+
220+
it("should reference the condition_code foreign key", async () => {
221+
const metadata: BundleMetadata = {
222+
...baseCoreMetadata,
223+
rr: [
224+
{
225+
condition: "flu",
226+
rule_summaries: [{ summary: "fever" }, { summary: "influenza" }],
227+
},
228+
],
229+
};
230+
231+
const db = getDb<Core>();
232+
await db
233+
.insertInto("condition_reference")
234+
.values({
235+
code: "123",
236+
concept_name: "condition (disease)",
237+
condition_name: "flu",
238+
condition_category: "category",
239+
})
240+
.execute();
241+
242+
let rolledback = false;
243+
const resp = await saveFhirMetadata(
244+
"1-2-3-4",
245+
"unknown" as "core", // appease typescript
246+
metadata,
247+
makePromiseResolveWithStatus(200),
248+
() => {
249+
rolledback = true;
250+
return makePromiseResolveWithStatus(200);
251+
},
252+
);
253+
254+
const conditions = await db
255+
.selectFrom("ecr_rr_conditions")
256+
.selectAll()
257+
.execute();
258+
259+
expect(resp.message).toEqual("Success. Saved metadata to database.");
260+
expect(resp.status).toEqual(200);
261+
expect(conditions).toHaveLength(1);
262+
expect(conditions[0].condition_code).toEqual("123");
263+
264+
await clearCore();
265+
});
213266
});

containers/ecr-viewer/src/app/api/save-fhir-data/service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,25 @@ const saveRR = async (
308308
// Loop through each condition/rule object in rr array
309309
for (const rrItem of metadata.rr) {
310310
const rr_conditions_uuid = randomUUID();
311+
312+
const rr_code = await trx
313+
.selectFrom("condition_reference")
314+
.select("code")
315+
.where("condition_name", "=", rrItem.condition)
316+
.executeTakeFirst();
317+
318+
if (!rr_code) {
319+
console.error(`Condition ${rrItem.condition} not found in condition_reference table`);
320+
}
321+
311322
// Insert condition into ecr_rr_conditions
312323
await trx
313324
.insertInto("ecr_rr_conditions")
314325
.values({
315326
uuid: rr_conditions_uuid,
316327
eicr_id: ecrId,
317328
condition: rrItem.condition,
329+
condition_code: rr_code?.code,
318330
})
319331
.execute();
320332
// Loop through the rule summaries array

0 commit comments

Comments
 (0)