Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle original echis service request updates #87

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
74 changes: 43 additions & 31 deletions src/controllers/referral.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const axios = require('axios');
const { generateFHIRServiceRequest } = require('../utils/referral');
const { generateFHIRServiceRequest, FHIRServiceRequestStatus, followUpInstruction, healthFacilityContact} = require('../utils/referral');
const {generateToken} = require("../utils/auth");
const { FHIR, CHT } = require('../../config');
const FHIR_URL = FHIR.url;
Expand Down Expand Up @@ -85,35 +85,43 @@ const createCommunityReferral = async (serviceRequest) => {

const createTaskReferral = async (serviceRequest) => {
try {
let axiosInstance = axios.create({
baseURL: FHIR.url,
headers: {
"Content-Type": "application/json",
},
});

axiosInstance.interceptors.response.use(
(response) => {
return response;
},
async function (error) {
const originalRequest = error.config;
if (error.response && error.response.status === 401 && !originalRequest._retry) {
originalRequest._retry = true;
const token = await generateToken();
axiosInstance.defaults.headers.common[
"Authorization"
] = `Bearer ${token}`;
return axiosInstance(originalRequest);
let serviceRequestId = null;
/*
get id of service request from afya ke servicee request, which does not come with a uuid. we use the identifier object to search in fhir server
*/
if(serviceRequest?.status === FHIRServiceRequestStatus[0]){
const identifier = serviceRequest?.identifier[0];
const searchParam = `${identifier.system}|${identifier.value}`;
const axiosInstance = axios.create({
baseURL: FHIR.url,
headers: {
"Content-Type": "application/json",
},
});

axiosInstance.interceptors.response.use(
(response) => {
return response;
},
async function (error) {
const originalRequest = error.config;
if (error.response && error.response.status === 401 && !originalRequest._retry) {
originalRequest._retry = true;
const token = await generateToken();
axiosInstance.defaults.headers.common[
"Authorization"
] = `Bearer ${token}`;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
return axiosInstance(originalRequest);
}
return Promise.reject(error);
}
return Promise.reject(error);
}
);
const response = await axiosInstance.post(`${FHIR_URL}/ServiceRequest`, JSON.stringify(serviceRequest));
const location = response.headers.location.split("/");
console.log(`Service Request Id ${location.at(-3)}`);

axiosInstance = axios.create({
);
const response = await axiosInstance.post(`${FHIR_URL}/ServiceRequest/_search?identifier=${searchParam}`, ``);
serviceRequestId = response.entry[0].resource.id;
}
// todo: get id of service request from payload that updates service request oriinally from echis
const axiosInstance = axios.create({
baseURL: CHT.url,
headers: {
"Content-Type": "application/json",
Expand All @@ -139,8 +147,12 @@ const createTaskReferral = async (serviceRequest) => {
authored_on: serviceRequest?.authoredOn,
date_service_offered: serviceRequest?.authoredOn,
date_of_visit: serviceRequest?.authoredOn,
follow_up_instruction: notesDeserialize.follow_up_instruction,
health_facility_contact: notesDeserialize.health_facility_contact
status: serviceRequest?.status,
needs_follow_up: `${FHIRServiceRequestStatus.includes(serviceRequest?.status)}` || `false`,
follow_up_instruction: followUpInstruction[serviceRequest?.status] || notesDeserialize.follow_up_instruction,
health_facility_contact: healthFacilityContact[serviceRequest?.status] || notesDeserialize.health_facility_contact,
fhir_service_request_uuid: serviceRequest?.status === 'draft'? serviceRequestId: serviceRequestIdserviceRequest?.entry[0].resource.id,
source_report_uuid: serviceRequest?.entry[0].resource.identifier.value
};

const response = await axiosInstance.post(`api/v2/records`, body);
Expand Down
23 changes: 17 additions & 6 deletions src/utils/referral.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,21 @@ const extractReasonCode = (data) => {
return [{coding: coding}];
};

const status = [
const FHIRServiceRequestStatus = [
`draft`,
`active`,
`revoked`,
`completed`
`revoked`
];

const followUpInstruction = {
revoked: `Missed visit`,
completed: `completed`
};

const healthFacilityContact = {
revoked: `revoked`,
Copy link

Choose a reason for hiding this comment

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

I'm curious what these states mean with respect to a health facility contact

Copy link
Collaborator Author

@billwambua billwambua Aug 20, 2023

Choose a reason for hiding this comment

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

I believe revoked means whether the instruction still applies while completed is whether it was, well: completed.

cc @kitsao

completed: `completed`
};

const generateFHIRServiceRequest = (dataRecord) => {
const reportedDate = DateTime.fromMillis(dataRecord.reported_date);
const FHITServiceRequest = {
Expand All @@ -145,7 +153,7 @@ const generateFHIRServiceRequest = (dataRecord) => {
value: dataRecord._id
}
],
status: status[0],
status: FHIRServiceRequestStatus[0],
intent: `order`,
category: [
{
Expand Down Expand Up @@ -190,5 +198,8 @@ const generateFHIRServiceRequest = (dataRecord) => {
};

module.exports = {
generateFHIRServiceRequest
generateFHIRServiceRequest,
FHIRServiceRequestStatus,
followUpInstruction,
healthFacilityContact
};