Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
add logic to add recorder field (#93)
Browse files Browse the repository at this point in the history
Still need to: 
- [x] Decide what we are going to show in the the display field
- [x] Decide what nulling a recording field looks like, or how to do
that in the fhir post
- [x] Get my practitioner id to actually return a valid id, currently
still get empty practitioner id despite the fact that I do have a
practitioner associated with my role
  • Loading branch information
luthfib authored Oct 4, 2022
1 parent 112d3df commit fb5fd96
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-experts-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zus-health/ctw-component-library": minor
---

When user edits or adds a condition, practitioner id will now be used to set the recorder field automatically. If no practitioner id is found then recorder will be nulled else the current practitioner id for the user will be used.
14 changes: 13 additions & 1 deletion src/components/content/forms/conditions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { createOrEditFhirResource } from "@/fhir/action-helper";
import { getClaims } from "@/fhir/client";
import { isFhirError } from "@/fhir/errors";
import { dateToISO } from "@/fhir/formatters";
import {
SYSTEM_CONDITION_CLINICAL,
SYSTEM_CONDITION_VERIFICATION_STATUS,
SYSTEM_PRACTITIONER_ID,
SYSTEM_SNOMED,
} from "@/fhir/system-urls";
import { ConditionModel } from "@/models/conditions";
Expand Down Expand Up @@ -43,6 +45,11 @@ export const conditionSchema = z.object({
note: z.string().optional(),
});

const setRecorderField = (practitionerId: string) => ({
reference: `Practitioner/${practitionerId}`,
type: "Practitioner",
});

export const createOrEditCondition = async (
data: FormData,
patientID: string,
Expand All @@ -53,10 +60,15 @@ export const createOrEditCondition = async (
return result;
}

const fhirClient = await getCTWFhirClient();
const practitionerId = result.data.id
? (getClaims(fhirClient)[SYSTEM_PRACTITIONER_ID] as string)
: "";
// Some fields will need to be set as they are required.
const fhirCondition: fhir4.Condition = {
resourceType: "Condition",
id: result.data.id,
...(practitionerId && { recorder: setRecorderField(practitionerId) }),
clinicalStatus: {
coding: [
{
Expand Down Expand Up @@ -96,7 +108,7 @@ export const createOrEditCondition = async (

const response = await createOrEditFhirResource({
resourceModel: conditionModel,
getCTWFhirClient,
fhirClient,
});

if (isFhirError(response)) {
Expand Down
5 changes: 2 additions & 3 deletions src/fhir/action-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ export interface FhirResourceBase {

type CreateOrEditData<T extends FhirResourceBase> = {
resourceModel: T;
getCTWFhirClient: () => Promise<Client>;
fhirClient: Client;
};

export async function createOrEditFhirResource<T extends FhirResourceBase>({
resourceModel,
getCTWFhirClient,
fhirClient,
}: CreateOrEditData<T>) {
const fhirClient = await getCTWFhirClient();
try {
if (resourceModel.id) {
return await fhirClient.update({
Expand Down
4 changes: 2 additions & 2 deletions src/fhir/search-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ export async function searchBuilderRecords<T extends ResourceTypeString>(
searchParams?: SearchParams
): Promise<SearchReturn<T>> {
const nonBuilderTags = [...THIRD_PARTY_TAGS, ...LENS_TAGS, SUMMARY_TAGS];
const jwt = getClaims(fhirClient);
const builderTag = `${SYSTEM_ZUS_OWNER}|builder/${jwt[SYSTEM_ZUS_BUILDER_ID]}`;
const claims = getClaims(fhirClient);
const builderTag = `${SYSTEM_ZUS_OWNER}|builder/${claims[SYSTEM_ZUS_BUILDER_ID]}`;
return searchAllRecords(resourceType, fhirClient, {
...searchParams,
_tag: builderTag,
Expand Down
2 changes: 2 additions & 0 deletions src/fhir/system-urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export const SYSTEM_ICD10_CM = "http://hl7.org/fhir/sid/icd-10-cm";
export const SYSTEM_MARITAL_STATUS =
"http://terminology.hl7.org/CodeSystem/v3-MaritalStatus";

export const SYSTEM_PRACTITIONER_ID = "https://zusapi.com/practitioner_id";

export const SYSTEM_RELATIONSHIP =
"http://terminology.hl7.org/CodeSystem/v2-0131";

Expand Down

0 comments on commit fb5fd96

Please sign in to comment.