diff --git a/demo/plugins/FormService/server/RequestValidator/index.ts b/demo/plugins/FormService/server/RequestValidator/index.ts index c3ea872..f6d1c47 100644 --- a/demo/plugins/FormService/server/RequestValidator/index.ts +++ b/demo/plugins/FormService/server/RequestValidator/index.ts @@ -9,7 +9,7 @@ export class RequestValidator { const schema = Joi.object().keys({ request: Joi.object().keys({ type: Joi.string().required(), - subType: Joi.string(), // optional + subType: Joi.string().required(), action: Joi.string().required(), component: Joi.string(), // optional rootOrgId: Joi.string(), // optional @@ -50,7 +50,7 @@ export class RequestValidator { const schema = Joi.object().keys({ request: Joi.object().keys({ type: Joi.string().required(), - subType: Joi.string(), // optional + subType: Joi.string().required(), action: Joi.string().required(), component: Joi.string(), // optional rootOrgId: Joi.string(), //optional @@ -91,7 +91,7 @@ export class RequestValidator { const schema = Joi.object().keys({ request: Joi.object().keys({ type: Joi.string().required(), - subType: Joi.string(), // optional + subType: Joi.string().required(), action: Joi.string().required(), component: Joi.string(), // optional rootOrgId: Joi.string(), // optional diff --git a/demo/plugins/FormService/server/package.json b/demo/plugins/FormService/server/package.json index 2ba84e0..760f0fd 100644 --- a/demo/plugins/FormService/server/package.json +++ b/demo/plugins/FormService/server/package.json @@ -1,6 +1,6 @@ { "name": "@project-sunbird/form-service", - "version": "0.0.12", + "version": "0.0.13", "description": "Form service API for project sunbird. It helps to manage form configuration based on channel, type, subtype, component.", "scripts": { "clean-dist-dev": "node_modules/.bin/rimraf ./../../../app/plugin-repo/@project-sunbird/form-service", @@ -14,7 +14,7 @@ "test": "echo 'go to '../test' folder to execute test cases!'" }, "engines": { - "node": ">=7.8.0" + "node": ">=8.11.0" }, "repository": { "type": "git", diff --git a/demo/plugins/FormService/server/server.ts b/demo/plugins/FormService/server/server.ts index 07d76cc..a75c7c1 100644 --- a/demo/plugins/FormService/server/server.ts +++ b/demo/plugins/FormService/server/server.ts @@ -95,76 +95,79 @@ export class Server extends BaseServer { telemetryHelper.error(req, res, error); }) } + private findForm(requestBody,formList){ + if (!formList.length) return {}; + + const query: any = { + type: requestBody.type, + action: requestBody.action, + subtype: requestBody.subType || '*', + root_org: requestBody.rootOrgId || '*', + framework: requestBody.framework || '*', + component: requestBody.component || '*', + } + + let form = _.find(formList, query); + if(!_.isEmpty(form)) return form; + + form = _.find(formList, Object.assign({}, query, { root_org: "*" })); + if(!_.isEmpty(form)) return form; + + form = _.find(formList, Object.assign({}, query, { framework: "*" })); + if(!_.isEmpty(form)) return form; + + form = _.find(formList, Object.assign({}, query, { framework: "*", root_org: "*" })); + if(!_.isEmpty(form)) return form; + + form = _.find(formList, Object.assign({}, query, { component: "*" })); + if(!_.isEmpty(form)) return form; + + form = _.find(formList, Object.assign({}, query, { root_org: "*", component: "*" })); + if(!_.isEmpty(form)) return form; + + form = _.find(formList, Object.assign({}, query, { framework: "*", component: "*" })); + if(!_.isEmpty(form)) return form; + + form = _.find(formList, Object.assign({}, query, { framework: "*", root_org: "*", component: "*" })); + if(!_.isEmpty(form)) return form; + + return form; + } public async read(req: Request, res: Response) { - const data = _.pick(req.body.request, ['type', 'subType', 'action', 'rootOrgId', 'framework', 'data', 'component']); - this.convertToLowerCase(data, ['type', 'subType', 'action']); + const requestBody = _.pick(req.body.request, ['type', 'subType', 'action', 'rootOrgId', 'framework', 'data', 'component']); + this.convertToLowerCase(requestBody, ['type', 'subType', 'action']); const query = { - root_org: data.rootOrgId || '*', - framework: data.framework || '*', - type: data.type, - action: data.action, - subtype: data.subType || '*', - component: data.component || '*' + type: requestBody.type, + action: requestBody.action, + subtype: requestBody.subType || '*' } - await this.cassandra.instance.form_data.findOneAsync(query).then(async data => { - if (!data) { - // find record by specified rootOrgId with framework = '*' - await this.cassandra.instance.form_data.findOneAsync(Object.assign({}, query, { framework: "*" })) - } else { - return data; + this.cassandra.instance.form_data.findAsync(query, { raw: true, allow_filtering: true }) + .then(data => { + let formData: any = this.findForm(requestBody, data); + if (_.isEmpty(formData)) throw "form not found"; + if (_.get(formData, 'root_org')) { + formData.rootOrgId = formData.root_org; + formData = _.omit(formData, ['root_org']); } + res.status(200) + .send(new FormResponse(undefined, { + id: 'api.form.read', + data: { + form: formData + } + })) + telemetryHelper.log(req); + }) + .catch(error => { + console.log(error); + res.status(404) + .send(new FormResponse({ + id: "api.form.read", + err: "ERR_READ_FORM_DATA", + errmsg: error + })); + telemetryHelper.error(req, res, error); }) - .then(async data => { - if (!data) { - // get the default data - return await this.cassandra.instance.form_data.findOneAsync(Object.assign({}, query, { root_org: "*" })) - } else { - return data; - } - }) - .then(async data => { - if (!data) { - // get the default data - return await this.cassandra.instance.form_data.findOneAsync(Object.assign({}, query, { root_org: "*", framework: "*" })) - } else { - return data; - } - }) - .then(async data => { - if (!data) { - // get the default data - return await this.cassandra.instance.form_data.findOneAsync(Object.assign({}, query, { root_org: "*", framework: "*", component: "*" })) - } else { - return data; - } - }) - .then(data => { - if (!data) data = {} - if (data && typeof data.data === "string") data.data = JSON.parse(data.data); - - data = data.toJSON(); // it removes all the schema validator of cassandra and gives plain object; - if (_.get(data, 'root_org')) { - data.rootOrgId = data.root_org; - data = _.omit(data, ['root_org']); - } - res.status(200) - .send(new FormResponse(undefined, { - id: 'api.form.read', - data: { - form: data - } - })) - telemetryHelper.log(req); - }) - .catch(error => { - res.status(404) - .send(new FormResponse({ - id: "api.form.read", - err: "ERR_READ_FORM_DATA", - errmsg: error - })); - telemetryHelper.error(req, res, error); - }) } } \ No newline at end of file diff --git a/demo/plugins/review-comment/server/server.ts b/demo/plugins/review-comment/server/server.ts index 56ee5de..0cd775d 100644 --- a/demo/plugins/review-comment/server/server.ts +++ b/demo/plugins/review-comment/server/server.ts @@ -57,7 +57,7 @@ export class Server extends BaseServer { is_deleted: false } - if(context_details.stage_id) query.meta_data = { $contains: {'stage_id': context_details.stage_id}} + context_details.stage_id && (query.meta_data = { $contains: {'stage_id': context_details.stage_id}}); if(options.method === 'findOne'){ return this.cassandra.instance.context_details.findOneAsync(query, {raw:true, allow_filtering: true}); @@ -75,7 +75,7 @@ export class Server extends BaseServer { tag: this.getTag(requestBody.context_details) } }; - if(threadId) disData.request.thread_id = threadId; + threadId && (disData.request.thread_id = threadId); return http.post(pluginBaseUrl + discussionCreateUrl,disData) .pipe(catchError(error => throwError(_.get(error, 'response.data.params.errmsg')))).toPromise() } @@ -87,9 +87,7 @@ export class Server extends BaseServer { content_ver: requestBody.context_details.content_ver, content_type: requestBody.context_details.content_type }; - if(requestBody.context_details.stage_id){ - insertObj.meta_data = { stage_id: requestBody.context_details.stage_id }; - } + requestBody.context_details.stage_id && (insertObj.meta_data = { stage_id: requestBody.context_details.stage_id }); const model = new this.cassandra.instance.context_details(insertObj); return model.saveAsync(); } @@ -131,7 +129,7 @@ export class Server extends BaseServer { }, {}); if(!commentList) return []; return commentList.map(element => { - if(threadObj[element.thread_id]) element.stageId = threadObj[element.thread_id].meta_data.stage_id; + threadObj[element.thread_id] && (element.stageId = threadObj[element.thread_id].meta_data.stage_id); return this.toCamelCase(element) }); }