Skip to content

Instantly share code, notes, and snippets.

@codebanesr
Last active July 8, 2021 03:07
Show Gist options
  • Save codebanesr/157bbab39829a1e4844110e9721604dd to your computer and use it in GitHub Desktop.
Save codebanesr/157bbab39829a1e4844110e9721604dd to your computer and use it in GitHub Desktop.
Codementor session
const document_create = async (req, res, next) => {
try {
console.log("routeLog", "document_create");
console.log(req.body, "body")
const CPMRN = req.params.CPMRN;
const encounters = req.params.encounters;
const tags = req.body.props.tags;
const attributes = req.body.props.attributes;
const user = req.userData;
const secondaryKeys = [];
req.body.forEach(async obj => {
const files = obj.files;
if (files && files.length) {
obj.key = files[0].key;
// second file onwards store in the secondary key section
files.forEach((file, i) => {
if (i > 0) {
secondaryKeys.push({
key: file.key,
timestamp: new Date(),
});
}
});
obj.secondaryKeys = secondaryKeys;
}
obj.tags = tags;
obj.attributes = attributes;
obj.reportedAt = req.body.props.reportedAt;
let codes = [];
try {
codes = makeSnomedCodeArray(tags, attributes);
} catch (e) {
console.log('Documents create. Error in getting snomed codes.');
}
obj.snomedCode = codes;
const updateObj = {};
updateObj["$push"] = {
documents: obj,
};
if (
req.body.props.attributes["Lactic"] &&
req.body.props.attributes["Lactic"] > 4
) {
updateObj["$set"] = {
severity: "Unstable",
};
}
const affected = await Patient.findOneAndUpdate({
CPMRN: CPMRN,
encounters: encounters,
},
updateObj, {
new: true
}).lean().exec();
if (affected) {
if (req.body.sbar.createSbar) {
// get the document_id for the sbar
const documents = affected.documents;
const sbarDoc = documents[documents.length - 1]._id;
const sbarTag = documents[documents.length - 1].tags
// Create sbar
const sbar = Object.assign(req.body.sbar, {
docRef: {
id: sbarDoc,
name: "documents",
tagName: sbarTag
}
});
await create_newsbar(sbar, user);
}
// create_newsbar()
// global.io.emit(CPMRN + encounters + "DOC", obj);
SOCKET.getDocsForSocket(CPMRN, encounters, req.userData.userId);
SOCKET.getUpdatedPatientForSocket(CPMRN, encounters, req.userData.userId); // docs are shown in signout
res.locals.cpdata = {
success: 1,
obj,
};
return next();
} else {
res.locals.cpdata = {
success: 0,
};
return next();
}
})
} catch (e) {
patientLastOpened({ CPMRN, encounters }, user);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment