From a1b71ab08fc9bfb9ac35960a42993070318cf980 Mon Sep 17 00:00:00 2001 From: sriramv Date: Thu, 7 Oct 2021 12:51:25 +0530 Subject: [PATCH] camunda_helper:GWM --- app/controllers/financial.controller.js | 330 ++++++++++++++++++++---- app/models/Entities.js | 4 +- app/routes/routes.js | 2 +- app/services/camunda.helper.js | 110 ++++++++ 4 files changed, 395 insertions(+), 51 deletions(-) create mode 100644 app/services/camunda.helper.js diff --git a/app/controllers/financial.controller.js b/app/controllers/financial.controller.js index 7eb8680..207cd74 100644 --- a/app/controllers/financial.controller.js +++ b/app/controllers/financial.controller.js @@ -1,6 +1,7 @@ const { sequelize, Entities , Financials , Statementsmaster , Sectionsmaster , Subsectionsmaster , Brokenperiods , Entityfinyears } = require('../models') const { logMsg } = require('../services/logger.js'); const financialControllerHelper = require('../services/financial.controller.helper.js'); +const camundaHelper = require('../services/camunda.helper.js'); // all master data list for dropdown exports.MasterDatas = async (req, res) => { await Statementsmaster.findAll({ @@ -38,11 +39,11 @@ exports.MenuList = (req, res) => { try { const losid = req.query.losid // call starter function here - console.log(losid) + req_obj ={"losid":losid} starter_response = starter(req_obj); - console.log(starter_response) + setTimeout(()=>{ if(starter_response.status == 200) { @@ -64,10 +65,13 @@ exports.MenuList = (req, res) => { required :false }) .then(MasterData => { - Entities.findAll({where : { losid } , attributes: ['id', 'losid' , 'pan' , 'cin' , 'bizname' , 'entity_type' , 'activity_group' , 'biz_act_desc' ] }) + Entities.findAll({where : { losid } , attributes: [ 'id', 'losid' , 'pan' , 'cin' , 'bizname' , 'entity_type' , 'activity_group' , 'biz_act_desc' , 'process_instance_id' ] }) .then(EntitiesData => { let data = []; EntitiesData.forEach((value, key)=>{ + // instancesid = value.dataValues.process_instance_id; + // taskid = camundaHelper.financialsListOfProcessInstances(instancesid); console.log(taskid) + // camunda_status = camundaHelper.financialsFormVariables(taskid); obj_data = { ...value.dataValues , statementsmasterrData:MasterData } data.push(obj_data); @@ -447,59 +451,289 @@ exports.ConsolidatedSummary = async (req, res) => { }; -exports.testwo = async (req, res) => { - //sequelize.query("SELECT x.id as statement_id,x.statement as statement,y.id as section_id,y.section as section,e.subsection as subsection_id,z.subsection as subsection , z.itemtext as subsection_itemtext,a.itemid,e.itemtext as items,a.fy as Y1,SUM(a.value) as Y1_value ,b.fy as Y2,SUM(b.value) as Y2_value,c.fy as Y3,SUM(c.value) as Y3_value FROM financials a JOIN ( SELECT id,itemtext,statement,section,subsection,formula,is_calculated FROM itemsmaster ) e ON a.itemid = e.id JOIN ( SELECT id, statement FROM statementsmaster ) x ON e.statement = x.id JOIN ( SELECT id, section FROM sectionsmaster ) y ON e.section = y.id LEFT JOIN ( SELECT id, subsection , itemtext FROM subsectionsmaster ) z ON e.subsection = z.id LEFT JOIN(SELECT entity_id,itemid,fy,value FROM financials WHERE fy = '"+Year_3+"' and entity_id IN ("+IN+") and is_active = 1 GROUP BY itemid,fy,is_active,value,entity_id) c ON a.entity_id = c.entity_id and a.itemid = c.itemid LEFT JOIN(SELECT entity_id,itemid,fy,value FROM financials WHERE fy = '"+Year_2+"' and entity_id IN ("+IN+") and is_active = 1 GROUP BY itemid,fy,is_active,value,entity_id) b ON a.entity_id = b.entity_id and a.itemid = b.itemid WHERE a.fy = '"+Year_1+"' and a.entity_id IN ("+IN+") and is_active = 1 GROUP BY a.itemid,a.fy,a.is_active,b.fy,c.fy,x.id,x.statement,y.id,y.section,e.subsection,z.subsection,z.itemtext,e.itemtext,e.statement,e.section,e.id order by e.statement asc , e.section asc , e.id asc") - //sequelize.query("SELECT x.id as statement_id,x.statement as statement,y.id as section_id,y.section as section,e.subsection as subsection_id,z.subsection as subsection , z.itemtext as subsection_itemtext,a.itemid,e.itemtext as items,a.fy as Y1,SUM(a.value) as Y1_value ,b.fy as Y2,SUM(b.value) as Y2_value,c.fy as Y3,SUM(c.value) as Y3_value,d.fy as Y4,SUM(d.value) as Y4_value FROM financials a JOIN ( SELECT id,itemtext,statement,section,subsection,formula,is_calculated FROM itemsmaster ) e ON a.itemid = e.id JOIN ( SELECT id, statement FROM statementsmaster ) x ON e.statement = x.id JOIN ( SELECT id, section FROM sectionsmaster ) y ON e.section = y.id LEFT JOIN ( SELECT id, subsection , itemtext FROM subsectionsmaster ) z ON e.subsection = z.id LEFT JOIN(SELECT entity_id,itemid,fy,value FROM financials WHERE fy = '"+Year_4+"' and entity_id IN ("+IN+") and is_active = 1 GROUP BY itemid,fy,is_active,value,entity_id) d ON a.entity_id = d.entity_id and a.itemid = d.itemid LEFT JOIN(SELECT entity_id,itemid,fy,value FROM financials WHERE fy = '"+Year_3+"' and entity_id IN ("+IN+") and is_active = 1 GROUP BY itemid,fy,is_active,value,entity_id) c ON a.entity_id = c.entity_id and a.itemid = c.itemid LEFT JOIN(SELECT entity_id,itemid,fy,value FROM financials WHERE fy = '"+Year_2+"' and entity_id IN ("+IN+") and is_active = 1 GROUP BY itemid,fy,is_active,value,entity_id) b ON a.entity_id = b.entity_id and a.itemid = b.itemid WHERE a.fy = '"+Year_1+"' and a.entity_id IN ("+IN+") and is_active = 1 GROUP BY a.itemid,a.fy,a.is_active,b.fy,c.fy,x.id,x.statement,y.id,y.section,e.subsection,z.subsection,z.itemtext,e.itemtext,e.statement,e.section,e.id,d.fy order by e.statement asc , e.section asc , e.id asc") +exports.test = async (req, res) => { + const losid = req.query.losid; + try + { + + new Promise((resolve,reject) =>{ + //pro 42 api call using losid + const Pro42Data = [ + {"losid":losid,"pan":"AA1234AAAA","cin":"test1_cin","bizname":"test1_bizname","entity_type":"type","activity_group":"grp","biz_act_desc":"des","createdby":4}, + {"losid":losid,"pan":"BB1234BBBB","cin":"test2_cin","bizname":"test2_bizname","entity_type":"type","activity_group":"grp","biz_act_desc":"des","createdby":4} + ]; + // insert pro42data into entities table using using foreach + Pro42Data.forEach((Pro42Datavalue, key)=>{ + // check if the Pro42Data (entity) already inserted + Entities.findOne({ where: { losid : Pro42Datavalue.losid , pan : Pro42Datavalue.pan , cin : Pro42Datavalue.cin } }).then(Entity => { + if(Entity) + { + // skip inserting + } + else + { + Entities.create({ "losid":Pro42Datavalue.losid,"pan":Pro42Datavalue.pan,"cin":Pro42Datavalue.cin,"bizname":Pro42Datavalue.bizname,"entity_type":Pro42Datavalue.entity_type,"activity_group":Pro42Datavalue.activity_group,"biz_act_desc":Pro42Datavalue.biz_act_desc,"createdby":Pro42Datavalue.createdby}) + .then(Entitydata => { + + //create Entityfinyears data + const Y1 = new Date().getFullYear(); + const Y2 = Y1-1; + const Y3 = Y2-1; + Entityfinyears.create({ "losid":Entitydata.dataValues.losid,"entityid":Entitydata.dataValues.id,"Y1":Y1,"Y2":Y2,"Y3":Y3,"Y1_st_type":2,"Y2_st_type":2,"Y3_st_type":2}) + .then(Entityfinyearsdata => { + + //create financials data based on Entityfinyears + let fin_years_array = []; + fin_years_array = [ + {"entity_id":Entityfinyearsdata.dataValues.entityid,"fy":Entityfinyearsdata.dataValues.Y1,"st_type":Entityfinyearsdata.dataValues.Y1_st_type}, + {"entity_id":Entityfinyearsdata.dataValues.entityid,"fy":Entityfinyearsdata.dataValues.Y2,"st_type":Entityfinyearsdata.dataValues.Y2_st_type}, + {"entity_id":Entityfinyearsdata.dataValues.entityid,"fy":Entityfinyearsdata.dataValues.Y3,"st_type":Entityfinyearsdata.dataValues.Y3_st_type} + ]; + + // create skeletion + fin_years_array.forEach((value, key)=>{ + const entity_id = value.entity_id; + const fy = value.fy; + const st_type = value.st_type; + + // generate financials data array + let fin_data_array = []; + for (let i = 1; i < 120; i++) + { + fin_data_obj = { "entity_id":entity_id,"st_type":st_type,"itemid":i,"fy":fy,"value":0,"is_active":1,"createdby":4} + fin_data_array.push(fin_data_obj); + } //console.log("------"+entity_id +"---"+ fy) + //bulk financials data create + Financials.bulkCreate(fin_data_array); + //console.log(fin_data_array) + }); + + + }).catch(err => { + res.status(404).send({'status':404, + message: err.message || "Some error occurred while insertinging statements." ,'data': "No data" }); + }); // end of entity fin year table data insert + + }).catch(err => { + res.status(404).send({'status':404, + message: err.message || "Some error occurred while insertinging statements." ,'data': "No data" }); + }); // end of entity insert + + } //end of else condition + + }); // end of (losid,pan,cin) combination checking entity data exist or not + + }); // end of pro42 data foreach + + //return {'status':200,'message':"success",'data':"No data"}; // success message + resolve(losid); + + }) + .then(function(value){ + + Statementsmaster.findAll({ where :{is_active : 1} , include: { all: true, nested: true , where :{is_active : 1} , required:false}}) + .then(MasterData => { + Entities.findAll({where : { losid } , attributes: ['id', 'losid' , 'pan' , 'cin' , 'bizname' , 'entity_type' , 'activity_group' , 'biz_act_desc' ] }) + .then(EntitiesData => { + let data = []; + EntitiesData.forEach((value, key)=>{ + obj_data = { ...value.dataValues , statementsmasterrData:MasterData } + + data.push(obj_data); + }); // end of EntitiesData foreach + + // below code for overall sum object push process + sum_data = { ...{'losid':losid,'bizname':"Consolidated Financials Summary",'is_consolidated_api_call':1} , statementsmasterrData:MasterData }; + data.push(sum_data); + + // send respose to FE + Promise.resolve(res.send({'status':200,'message':"success",'data':data})) + + }); // end of findall EntitiesData + }); // end of findall masters datas + + }) +} catch(err) { + res.status(500).send({'status':404, + message: err.message || "Some error occurred while inserting statements." ,'data': "No data" + }); } + +}; + +exports.testwo = async (req, res) => { + const losid = req.query.losid; + const api_call = 0; + var promise = (losid) => new Promise((resolve,reject) =>{ + if(api_call == 1) + { + console.log(1); + console.log(losid); + } + resolve(losid) + }) + promise(losid).then((losid) => { + console.log(2); + console.log(losid); + return ({'losid':losid,'status':200,'message':"success",'data':"No data"}); // success message + + }) + .then(function(value){ + console.log(3); + console.log(losid); + //setTimeout(function(){ + console.log(4); + console.log(losid); + data = "output"; + Promise.resolve(res.send({'status':200,'message':"success",'data':data})) + //},1000); + }) + }; -function testhree(req , res) { - console.log( new Date().getFullYear()) - console.log( req.losid) - return {'status':200,'message':"success"}; -} +exports.menu = async (req, res) => { + const losid = req.query.losid; + try + { +let promise = Promise.resolve(); +//pro 42 api call using losid +const Pro42Data = [ + {"losid":losid,"pan":"AA1234AAAA","cin":"test1_cin","bizname":"test1_bizname","entity_type":"type","activity_group":"grp","biz_act_desc":"des","createdby":4}, + {"losid":losid,"pan":"BB1234BBBB","cin":"test2_cin","bizname":"test2_bizname","entity_type":"type","activity_group":"grp","biz_act_desc":"des","createdby":4} + ]; - // const fin_summary = await Financials.findAll({ - // attributes: [['fy','Y1'] ,'itemid',[sequelize.fn('sum', sequelize.col('value')), 'Y1_value'],],group: ['fy','itemid'], - // where: { fy : 2019 , entity_id : [6,7] , is_active : 1 } - // }, - // ); + Pro42Data.forEach((Pro42Datavalue, key) => { + promise = promise.then(() => { + // check if the Pro42Data (entity) already inserted + Entities.findOne({ where: { losid : Pro42Datavalue.losid , pan : Pro42Datavalue.pan , cin : Pro42Datavalue.cin } }).then(Entity => { + if(Entity) + { + // skip inserting + } + else + { + Entities.create({ "losid":Pro42Datavalue.losid,"pan":Pro42Datavalue.pan,"cin":Pro42Datavalue.cin,"bizname":Pro42Datavalue.bizname,"entity_type":Pro42Datavalue.entity_type,"activity_group":Pro42Datavalue.activity_group,"biz_act_desc":Pro42Datavalue.biz_act_desc,"createdby":Pro42Datavalue.createdby}) + .then(Entitydata => { -// sequelize.query("SELECT x.id as statement_id,x.statement as statement,y.id as section_id,y.section as section,e.subsection as subsection_id,z.subsection as subsection , z.itemtext as subsection_itemtext,a.itemid,e.itemtext as items,a.fy as Y1,SUM(a.value) as Y1_value ,b.fy as Y2,SUM(b.value) as Y2_value,c.fy as Y3,SUM(c.value) as Y3_value FROM financials a JOIN ( SELECT id,itemtext,statement,section,subsection,formula,is_calculated FROM itemsmaster ) e ON a.itemid = e.id JOIN ( SELECT id, statement FROM statementsmaster ) x ON e.statement = x.id JOIN ( SELECT id, section FROM sectionsmaster ) y ON e.section = y.id LEFT JOIN ( SELECT id, subsection , itemtext FROM subsectionsmaster ) z ON e.subsection = z.id LEFT JOIN(SELECT entity_id,itemid,fy,value FROM financials WHERE fy = '2019' and entity_id IN (6,7) and is_active = 1 GROUP BY itemid,fy,is_active,value,entity_id) c ON a.entity_id = c.entity_id and a.itemid = c.itemid LEFT JOIN(SELECT entity_id,itemid,fy,value FROM financials WHERE fy = '2020' and entity_id IN (6,7) and is_active = 1 GROUP BY itemid,fy,is_active,value,entity_id) b ON a.entity_id = b.entity_id and a.itemid = b.itemid WHERE a.fy = '2021' and a.entity_id IN (6,7) and is_active = 1 GROUP BY a.itemid,a.fy,a.is_active,b.fy,c.fy,x.id,x.statement,y.id,y.section,e.subsection,z.subsection,z.itemtext,e.itemtext,e.statement,e.section,e.id order by e.statement asc , e.section asc , e.id asc") - // .then(data =>{ - // res.send({'status':200,message: "success" ,'data': data }); - // }) - // .catch(err => { - // res.status(404).send({'status':404, - // message: err.message || "Some error occurred while insertinging statements." ,'data': "No data" }); - // }); + //create Entityfinyears data + const Y1 = new Date().getFullYear(); + const Y2 = Y1-1; + const Y3 = Y2-1; + Entityfinyears.create({ "losid":Entitydata.dataValues.losid,"entityid":Entitydata.dataValues.id,"Y1":Y1,"Y2":Y2,"Y3":Y3,"Y1_st_type":2,"Y2_st_type":2,"Y3_st_type":2}) + .then(Entityfinyearsdata => { + + //create financials data based on Entityfinyears + let fin_years_array = []; + fin_years_array = [ + {"entity_id":Entityfinyearsdata.dataValues.entityid,"fy":Entityfinyearsdata.dataValues.Y1,"st_type":Entityfinyearsdata.dataValues.Y1_st_type}, + {"entity_id":Entityfinyearsdata.dataValues.entityid,"fy":Entityfinyearsdata.dataValues.Y2,"st_type":Entityfinyearsdata.dataValues.Y2_st_type}, + {"entity_id":Entityfinyearsdata.dataValues.entityid,"fy":Entityfinyearsdata.dataValues.Y3,"st_type":Entityfinyearsdata.dataValues.Y3_st_type} + ]; + + // create skeletion + fin_years_array.forEach((value, key)=>{ + const entity_id = value.entity_id; + const fy = value.fy; + const st_type = value.st_type; + + // generate financials data array + let fin_data_array = []; + for (let i = 1; i < 120; i++) + { + fin_data_obj = { "entity_id":entity_id,"st_type":st_type,"itemid":i,"fy":fy,"value":0,"is_active":1,"createdby":4} + fin_data_array.push(fin_data_obj); + } //console.log("------"+entity_id +"---"+ fy) + //bulk financials data create + Financials.bulkCreate(fin_data_array); + //console.log(fin_data_array) + }); + + + }).catch(err => { + res.status(404).send({'status':404, + message: err.message || "Some error occurred while insertinging statements." ,'data': "No data" }); + }); // end of entity fin year table data insert + + }).catch(err => { + res.status(404).send({'status':404, + message: err.message || "Some error occurred while insertinging statements." ,'data': "No data" }); + }); // end of entity insert + + } //end of else condition + + }); // end of (losid,pan,cin) combination checking entity data exist or not + }); +}); + +promise.then(() => { + setTimeout(function(){ + Statementsmaster.findAll({ where :{is_active : 1} , include: { all: true, nested: true , where :{is_active : 1} , required:false}}) + .then(MasterData => { + Entities.findAll({where : { losid } , attributes: ['id', 'losid' , 'pan' , 'cin' , 'bizname' , 'entity_type' , 'activity_group' , 'biz_act_desc' ] }) + .then(EntitiesData => { + let data = []; + EntitiesData.forEach((value, key)=>{ + obj_data = { ...value.dataValues , statementsmasterrData:MasterData } + data.push(obj_data); + }); // end of EntitiesData foreach + if(EntitiesData.length > 1) + { + // below code for overall sum object push process + sum_data = { ...{'losid':losid,'bizname':"Consolidated Financials Summary",'is_consolidated_api_call':1} , statementsmasterrData:MasterData }; + data.push(sum_data); + } + // send respose to FE + Promise.resolve(res.send({'status':200,'message':"success",'data':data})) + + }); // end of findall EntitiesData + }); // end of findall masters datas + },1000); +}) +} catch(err) { + res.status(500).send({'status':404, + message: err.message || "Some error occurred while inserting statements." ,'data': "No data" + }); } +}; + + + + - -function inner_test(req , res) { - const losid = req.losid - new Promise((resolve, reject) => { - console.log("from-inner-fn="+losid) - resolve(losid); +exports.camunda = async (req, res) => { + const losid = req.query.losid; + try + { + const losid = "fb37c1e4-271e-11ec-a4f4-42010a800002"; + const cin = "test"; + const user = "test"; - }).then(value => { console.log("from-then="+value); return (value ); }) + //let promise = Promise.resolve(); + promiseA = new Promise((resolve, reject) => { + instancesid = "33f6a13d-272f-11ec-a4f4-42010a800002"; + taskid = camundaHelper.financialsListOfProcessInstances(instancesid); + return taskid; + }); + + setTimeout(function(){ + promiseB = promiseA.then(function(taskid){ + console.log(taskid); + camunda_status = camundaHelper.financialsFormVariables(taskid); + return camunda_status; + }) + },2000); -//return {'status':200,'message':"success"}; -} - - - -// let promise = Promise.resolve(); -// const posts = [{}, {}, {}]; - -// posts.forEach(post => { -// promise = promise.then(() => { -// return db.insert(post); -// }); -// }); - -// promise.then(() => { -// // now all our docs have been saved -// }) \ No newline at end of file + setTimeout(function(){ + promiseB.then(function(camunda_status){ + Promise.resolve(res.send({'status':200,'camunda': camunda_status })) + }) + },2000); + + + } catch(err) { + res.status(500).send({'status':404, + message: err.message || "Some error occurred while inserting statements." ,'data': "No data" + }); } +}; diff --git a/app/models/Entities.js b/app/models/Entities.js index e5ad7b2..8bff480 100644 --- a/app/models/Entities.js +++ b/app/models/Entities.js @@ -9,9 +9,9 @@ module.exports = (sequelize, DataTypes) => { */ // define association here static associate({ Financials }) { - this.hasMany(Financials, { foreignKey: 'entity_id', as: 'financials' }) + this.hasMany(Financials, { foreignKey: 'entity_id', as: 'financialsData' }) } - + } Entities.init( diff --git a/app/routes/routes.js b/app/routes/routes.js index 92ac9e5..d120a1a 100644 --- a/app/routes/routes.js +++ b/app/routes/routes.js @@ -8,7 +8,7 @@ module.exports = app => { var router = require("express").Router(); - + //router.get("/camunda", financials.camunda); /** * @swagger * /api/ConsolidatedSummary: diff --git a/app/services/camunda.helper.js b/app/services/camunda.helper.js new file mode 100644 index 0000000..908fa80 --- /dev/null +++ b/app/services/camunda.helper.js @@ -0,0 +1,110 @@ +const request = require('request'); +const DEFINITION_ID = process.env.CAMUNDA_PROCESS_DEFINITION_ID; +const URL = process.env.CAMUNDA_URL + + function startFinancialsProcessInstance(losid,cin,user) + { + try + { + const start_url = URL+"process-definition/"+DEFINITION_ID+"/start"; + const businessKey = losid+"-"+cin+"-"+user; + const myJSONObject = { + "variables":{ + "los_id":{"value": losid }, + "cin_no":{"value": cin }, + "user":{"value": user } + },"businessKey": businessKey + }; + request({ + url: start_url, + method: "POST", + json: true, + body: myJSONObject + }, function (error, response, body){ + let data = body; + instancesid = data.id; + return instancesid; + }); + + } + catch (error) + { + console.log(error); + } + } + + function financialsListOfProcessInstances(instancesid) + { + try + { + const task_id_url = URL+"task"; + const myJSONObject = { "processInstanceIdIn":[instancesid] }; + request({ + url: task_id_url, + method: "POST", + json: true, + body: myJSONObject + }, function (error, response, body){ + let data = body; + task_id =data[0].id; + return task_id; + }); + + } + catch (error) + { + console.log(error); + } + } + + + function financialsTaskComplete(taskid) + { + try + { + const task_complete_url = URL+"/task/"+taskid+"/complete"; + const myJSONObject = {"variables":{"review_status":{"value":true}}}; + request({ + url: task_complete_url, + method: "POST", + json: true, + body: myJSONObject + }, function (error, response, body){ + + return body; + }); + + } + catch (error) + { + console.log(error); + } + } + + function financialsFormVariables(taskid) + { + try + { + const form_variable_url = URL+"task/"+taskid+"/form-variables"; + request({ + url: form_variable_url, + method: "GET" + }, function (error, response, body){ + + return body; + }); + + } + catch (error) + { + console.log(error); + } + } + + module.exports = { + startFinancialsProcessInstance : startFinancialsProcessInstance, + financialsListOfProcessInstances : financialsListOfProcessInstances, + financialsTaskComplete : financialsTaskComplete, + financialsFormVariables : financialsFormVariables + + } \ No newline at end of file