Skip to content

Commit

Permalink
feat: change validateContained to async funtion
Browse files Browse the repository at this point in the history
  • Loading branch information
Chinlinlee committed Feb 5, 2022
1 parent 0aab652 commit 0a30cd1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion api/FHIRApiService/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = async function(req, res , resourceType) {
let containedResources = _.get(insertData, "contained");
for (let index in containedResources) {
let resource = containedResources[index];
let validation = validateContained(resource, index);
let validation = await validateContained(resource, index);
if (!validation.status) {
let operationOutcomeError = handleError.processing(`The resource in contained error. ${validation.message}`);
return doRes(400, operationOutcomeError);
Expand Down
2 changes: 1 addition & 1 deletion api/FHIRApiService/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = async function (req, res, resourceType) {
let containedResources = _.get(updateData, "contained");
for (let index in containedResources) {
let resource = containedResources[index];
let validation = validateContained(resource, index);
let validation = await validateContained(resource, index);
if (!validation.status) {
let operationOutcomeError = handleError.processing(`The resource in contained error. ${validation.message}`);
return doRes(400, operationOutcomeError);
Expand Down
45 changes: 26 additions & 19 deletions api/FHIRApiService/validateContained.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
const mongodb = require('../../models/mongodb');
const _ = require('lodash');

module.exports = (resourceItem, index) => {
let resourceType = _.get(resourceItem, "resourceType", false);
if (!resourceType) {
return {
status: false,
message: `Missing resourceType in contained[${index}]`
};
}
if (mongodb[resourceType]) {
let resourceToMongoModel = new mongodb[resourceType](resourceItem);
let validation = resourceToMongoModel.validateSync();
if (_.get(validation,"errors")) {
module.exports = async (resourceItem, index) => {
try {
let resourceType = _.get(resourceItem, "resourceType", false);
if (!resourceType) {
return {
status: false,
message: validation.message
message: `Missing resourceType in contained[${index}]`
};
}
if (mongodb[resourceType]) {
let resourceToMongoModel = new mongodb[resourceType](resourceItem);
await resourceToMongoModel.validate();
return {
status: true,
message: "success"
};
} else {
return {
status: false,
message: `Burni not support this resource type. ${resourceType}`
};
}
} catch(e) {
if (_.get(e,"errors")) {
return {
status: false,
message: e.message
};
}
return {
status: true,
message: "success"
};
} else {
return {
status: false,
message: `Burni not support this resource type. ${resourceType}`
message: e
};
}
};

0 comments on commit 0a30cd1

Please sign in to comment.