Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createTestContext } from '../../utilities';
import {
fakeResourcesOfFhirServiceRequest,
fakeResourcesOfFhirServiceRequestWithLabRequest,
fakeTestTypes,
} from '../../fake/fhir';
import { v4 as uuidv4 } from 'uuid';

Expand Down Expand Up @@ -198,6 +199,56 @@ describe('Create Observation', () => {
expect(labTest.result).toBe(result);
});

it('Will add a reflex test if the lab test code is not in the original request', async () => {
const result = '100';

const { FhirServiceRequest, LabTest, LabTestType } = ctx.store.models;
const { labRequest, category } = await fakeResourcesOfFhirServiceRequestWithLabRequest(
ctx.store.models,
resources,
false,
{
status: LAB_REQUEST_STATUSES.RESULTS_PENDING,
},
);
const [newTestType] = await fakeTestTypes(10, LabTestType, category.id);

const mat = await FhirServiceRequest.materialiseFromUpstream(labRequest.id);
const serviceRequestId = mat.id;

const body = {
resourceType: 'Observation',
basedOn: [
{
type: 'ServiceRequest',
reference: `ServiceRequest/${serviceRequestId}`,
},
],
status: FHIR_OBSERVATION_STATUS.FINAL,
code: {
coding: [
{
system: config.hl7.dataDictionaries.serviceRequestLabTestCodeSystem,
code: newTestType.code,
},
],
},
valueString: result,
};

const response = await app.post(endpoint).send(body);
expect(response).toHaveSucceeded();

const labTest = await LabTest.findOne({
include: [{ model: LabTestType, as: 'labTestType' }],
where: {
labRequestId: labRequest.id,
'$labTestType.id$': newTestType.id,
},
});
expect(labTest.result).toBe(result);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since the logic for updating a LabTest now also sets the completedDate, it would be beneficial to add an assertion here to ensure it's being set correctly. This will improve the test coverage for the new functionality.

      expect(labTest.result).toBe(result);
      expect(labTest.completedDate).not.toBeNull();

});

describe('errors', () => {
it('returns invalid value if the ServiceRequest does not exist', async () => {
const nonExistentServiceRequestId = uuidv4();
Expand Down Expand Up @@ -241,7 +292,7 @@ describe('Create Observation', () => {
expect(response.status).toBe(400);
});

it('returns invalid value if the Observation code does not match any test in the ServiceRequest', async () => {
it('returns invalid value if the Observation code does not match any test types', async () => {
const { FhirServiceRequest } = ctx.store.models;
const { labRequest } = await fakeResourcesOfFhirServiceRequestWithLabRequest(
ctx.store.models,
Expand Down Expand Up @@ -289,7 +340,7 @@ describe('Create Observation', () => {
diagnostics: expect.any(String),
details: {
text: expect.stringContaining(
`No LabTest with code: '${invalidCode}' found for LabRequest: '${labRequest.id}'`,
`Cannot create reflex test, no lab test type found with code '${invalidCode}'`,
),
},
},
Expand Down
43 changes: 35 additions & 8 deletions packages/database/src/models/fhir/FhirObservation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DataTypes } from 'sequelize';
import * as yup from 'yup';

import { FHIR_INTERACTIONS, FHIR_ISSUE_TYPE } from '@tamanu/constants';
import { getCurrentDateString, getCurrentDateTimeString } from '@tamanu/utils/dateTime';
import {
FhirCodeableConcept,
FhirCoding,
Expand Down Expand Up @@ -157,7 +158,7 @@ export class FhirObservation extends FhirResource {
const labTest = await this.getLabTestForObservation(labRequest);
const value = this.getValue();

await labTest.update({ result: value });
await labTest.update({ result: value, completedDate: getCurrentDateTimeString() });
return labTest;
}

Expand Down Expand Up @@ -198,7 +199,7 @@ export class FhirObservation extends FhirResource {
});
}

const labTest =
let labTest =
(labTestCode &&
(await LabTest.findOne({
include: [{ model: LabTestType, as: 'labTestType' }],
Expand All @@ -214,12 +215,38 @@ export class FhirObservation extends FhirResource {
})));

if (!labTest) {
throw new Invalid(
`No LabTest with code: '${labTestCode}' found for LabRequest: '${labRequest.id}'`,
{
code: FHIR_ISSUE_TYPE.INVALID.VALUE,
},
);
const labTestType =
(labTestCode &&
(await LabTestType.findOne({
where: { code: labTestCode },
}))) ||
(labTestExternalCode &&
(await LabTestType.findOne({
where: { externalCode: labTestExternalCode },
})));

if (!labTestType) {
const identifiers = [];
if (labTestCode) {
identifiers.push(`code '${labTestCode}'`);
}
if (labTestExternalCode) {
identifiers.push(`externalCode '${labTestExternalCode}'`);
}
throw new Invalid(
`Cannot create reflex test, no lab test type found with ${identifiers.join(' or ')}`,
{
code: FHIR_ISSUE_TYPE.INVALID.VALUE,
},
);
Comment thread
NavarroEmilioLuis marked this conversation as resolved.
}

// No pre-existing lab test found, this must be a reflex test, so create a new test to track that
labTest = await LabTest.create({
labRequestId: labRequest.id,
labTestTypeId: labTestType.id,
date: getCurrentDateString(),
});
}

return labTest;
Expand Down
Loading