Skip to content

Commit

Permalink
fix: field collection is missing when store
Browse files Browse the repository at this point in the history
- The mongoose is not allow the field name `collection`
- `myCollection` is **missing** because of
I rename the `collection` into `myCollection`,
but processing flow is not complete cause the bug
- Add `renameCollectionFieldName` in `apiService`
> Before storing, rename `collection` into `myCollection` in resource
- Use cloned req.body to store
> The req.body is polluting by some funcitons with mongoose,
> it will turn into mongoose document

# Deps
- Add `jsonpath` package
  • Loading branch information
Chinlinlee committed Jun 2, 2022
1 parent 19b05e5 commit 817fed6
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 15 deletions.
8 changes: 5 additions & 3 deletions FHIR-mongoose-Models-Generator/resourceGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,11 @@ function generateResourceSchema (type) {
let result = this;
delete result._doc._id;
delete result._doc.__v;
if (_.get(result, "myCollection")) {
let tempCollectionField = _.cloneDeep(result["myCollection"]);
_.set(result, "collection", tempCollectionField);
let myCollectionField = _.get(result, "_doc.myCollection");
if (myCollectionField) {
let tempCollectionField = _.cloneDeep(myCollectionField);
_.set(result, "_doc.collection", tempCollectionField);
delete result._doc.myCollection;
}
return result;
};
Expand Down
5 changes: 4 additions & 1 deletion api/FHIRApiService/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const FHIR = require('fhir').Fhir;
const user = require('../APIservices/user.service');
const validateContained = require('./validateContained');
const { getValidateResult } = require('../../models/FHIR/fhir-validator.js');
const { renameCollectionFieldName } = require("../apiService");
const { logger } = require('../../utils/log');
const path = require('path');

Expand Down Expand Up @@ -89,6 +90,7 @@ module.exports = async function(req, res , resourceType) {
}
try {
let insertData = req.body;
let cloneInsertData = _.cloneDeep(insertData);
if (_.get(insertData, "contained")) {
let containedResources = _.get(insertData, "contained");
for (let index in containedResources) {
Expand Down Expand Up @@ -118,7 +120,7 @@ module.exports = async function(req, res , resourceType) {
return doRes(412, operationOutcomeMessage);
}
}
let [status, doc] = await doInsertData(insertData, resourceType);
let [status, doc] = await doInsertData(cloneInsertData, resourceType);
return responseFunc[status](doc, req, res, resourceType, doRes);
} catch (e) {
let errorStr = JSON.stringify(e, Object.getOwnPropertyNames(e));
Expand All @@ -131,6 +133,7 @@ module.exports = async function(req, res , resourceType) {
async function doInsertData(insertData , resourceType) {
try {
delete insertData.text;
renameCollectionFieldName(insertData);
//delete insertData.meta;
insertData.id = uuid.v4();
let insertDataObject = new mongodb[resourceType](insertData);
Expand Down
15 changes: 8 additions & 7 deletions api/FHIRApiService/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const FHIR = require('fhir').Fhir;
const validateContained = require('./validateContained');
const { checkReference, getNotExistReferenceList } = require('../apiService');
const { getValidateResult } = require('../../models/FHIR/fhir-validator.js');
const { renameCollectionFieldName } = require("../apiService");
const { logger } = require('../../utils/log');
const path = require('path');
/**
Expand Down Expand Up @@ -68,6 +69,7 @@ module.exports = async function (req, res, resourceType) {
}
};
let updateData = req.body;
let updateDataClone = _.cloneDeep(updateData);
if (_.get(updateData, "contained")) {
let containedResources = _.get(updateData, "contained");
for (let index in containedResources) {
Expand Down Expand Up @@ -105,7 +107,7 @@ module.exports = async function (req, res, resourceType) {
1: doUpdateData,
2: doInsertData
};
let [status, result] = await dataFuncAfterCheckExist[dataExist.status](req,resourceType);
let [status, result] = await dataFuncAfterCheckExist[dataExist.status](updateDataClone, req.params.id, resourceType);

return resFunc[status](result);
};
Expand All @@ -132,13 +134,12 @@ async function isDocExist(id, resourceType) {
}
}

async function doUpdateData(req, resourceType) {
async function doUpdateData(data, id, resourceType) {
try {
let data = req.body;
let id = req.params.id;
delete data._id;
delete data.text;
delete data.meta;
renameCollectionFieldName(data);
data.id = id;
let newDoc = await mongodb[resourceType].findOneAndUpdate({
id: id
Expand All @@ -159,12 +160,12 @@ async function doUpdateData(req, resourceType) {
}
}

async function doInsertData(req, resourceType) {
async function doInsertData(data, id, resourceType) {
try {
let data = req.body;
data.id = req.params.id;
data.id = id;
delete data.text;
delete data.meta;
renameCollectionFieldName(data);
let updateData = new mongodb[resourceType](data);
let doc = await updateData.save();
return ["true", {
Expand Down
20 changes: 19 additions & 1 deletion api/apiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const AbortController = require('abort-controller');
const FHIR = require('fhir').Fhir;
const { handleError } = require('../models/FHIR/httpMessage');
const jwt = require('jsonwebtoken');
const jsonPath = require("jsonPath");
function getDeepKeys(obj) {
let keys = [];
for (let key in obj) {
Expand Down Expand Up @@ -166,10 +167,27 @@ function getNotExistReferenceList(checkReferenceRes) {
return notExistReferenceList;
}

function renameCollectionFieldName(data) {
let collectionNodes = jsonPath.nodes(data, "$..collection");
for (let node of collectionNodes) {
node.path.shift();
let originalPath = node.path.join(".");
_.omit(data, originalPath);
node.path = node.path.map( v=> {
if (v === "collection") return "myCollection";
return v;
});
let collectionPath = node.path.join(".");
let collectionItem = _.cloneDeep(node.value);
_.set(data, collectionPath, collectionItem);
}
}

module.exports = {
getDeepKeys: getDeepKeys,
isRealObject: isRealObject,
findResourceById: findResourceById,
checkReference: checkReference ,
getNotExistReferenceList: getNotExistReferenceList
getNotExistReferenceList: getNotExistReferenceList,
renameCollectionFieldName: renameCollectionFieldName
};
8 changes: 5 additions & 3 deletions models/mongodb/model/Patient.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ module.exports = function() {
let result = this;
delete result._doc._id;
delete result._doc.__v;
if (_.get(result, "myCollection")) {
let tempCollectionField = _.cloneDeep(result["myCollection"]);
_.set(result, "collection", tempCollectionField);
let myCollectionField = _.get(result, "_doc.myCollection");
if (myCollectionField) {
let tempCollectionField = _.cloneDeep(myCollectionField);
_.set(result, "_doc.collection", tempCollectionField);
delete result._doc.myCollection;
}
return result;
};
Expand Down
Loading

0 comments on commit 817fed6

Please sign in to comment.