From 0a30cd193fe8ef121688bab6de3484332534df84 Mon Sep 17 00:00:00 2001 From: chin Date: Sat, 5 Feb 2022 20:30:35 +0800 Subject: [PATCH] feat: change `validateContained` to async funtion --- api/FHIRApiService/create.js | 2 +- api/FHIRApiService/update.js | 2 +- api/FHIRApiService/validateContained.js | 45 ++++++++++++++----------- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/api/FHIRApiService/create.js b/api/FHIRApiService/create.js index 61fe0106..5003cf3f 100644 --- a/api/FHIRApiService/create.js +++ b/api/FHIRApiService/create.js @@ -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); diff --git a/api/FHIRApiService/update.js b/api/FHIRApiService/update.js index 719e9d28..b3a78afc 100644 --- a/api/FHIRApiService/update.js +++ b/api/FHIRApiService/update.js @@ -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); diff --git a/api/FHIRApiService/validateContained.js b/api/FHIRApiService/validateContained.js index 7f6071a3..42996b73 100644 --- a/api/FHIRApiService/validateContained.js +++ b/api/FHIRApiService/validateContained.js @@ -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 }; } }; \ No newline at end of file