From 2aab778ffe8022f57944490cd8007029617c778a Mon Sep 17 00:00:00 2001 From: sriramv Date: Thu, 27 Jan 2022 11:05:15 +0530 Subject: [PATCH] redirectionApi --- app/controllers/financial.controller.js | 98 ++++++++++++------- app/routes/routes.js | 2 +- .../financial.controller.helper.js.js | 54 +++++++++- app/services/financials.apicall.helper.js | 56 +++++++++-- 4 files changed, 165 insertions(+), 45 deletions(-) diff --git a/app/controllers/financial.controller.js b/app/controllers/financial.controller.js index 07cecbd..718fccc 100644 --- a/app/controllers/financial.controller.js +++ b/app/controllers/financial.controller.js @@ -55,19 +55,23 @@ exports.MenuList = (req, res) => { { try { - const losid = req.query.losid; - //pro 42 api call using losid - const Pro42Data = [ - {"losid":losid,"pan":"AA1234AAAA","cin":"L74120MH1985PLC035308","bizname":"test1_bizname","entity_type":"type","activity_group":"grp","biz_act_desc":"des","createdby":4}, - {"losid":losid,"pan":"BB1234BBBB","cin":"L74120MH1985PLC035308","bizname":"test2_bizname","entity_type":"type","activity_group":"grp","biz_act_desc":"des","createdby":4} - ]; - -var EntityInsert = new Promise((resolve, reject) => { - // insert pro42data into entities table using using foreach - Pro42Data.forEach((Pro42Datavalue, key,array)=>{ + const losid = req.query.losid; + + var EntityInsert = new Promise((resolve, reject) => { + //Redirection api call with losid + redirectionApi = ApiCallHelper.redirectionApiCall(losid); + redirectionApi.then(function(redirectionApiData){ + if(redirectionApiData == false) + { + resolve(); + } + else + { + // insert pro42data into entities table using using foreach + redirectionApiData.forEach((Pro42Datavalue, key,array)=>{ - // check if the Pro42Data (entity) already inserted - Entities.findOne({ where: { losid : Pro42Datavalue.losid , pan : Pro42Datavalue.pan , cin : Pro42Datavalue.cin } }).then(Entity => { + // check if the Pro42Data (entity) already inserted + Entities.findOne({ where: { losid : Pro42Datavalue.loginNo , pan : Pro42Datavalue.pan , cin : Pro42Datavalue.ciN_LLPIN } }).then(Entity => { if(Entity) { // skip inserting @@ -75,13 +79,14 @@ var EntityInsert = new Promise((resolve, reject) => { } 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}) + + Entities.create({ "losid":Pro42Datavalue.loginNo,"pan":Pro42Datavalue.pan,"cin":Pro42Datavalue.ciN_LLPIN,"bizname":Pro42Datavalue.entityName,"entity_type":Pro42Datavalue.entityType,"createdby":Pro42Datavalue.createdby}) .then(Entitydata => { - - //process instance Id update const cin = Entitydata.dataValues.cin; + if(cin != "") + { + //process instance Id update const entityid = Entitydata.dataValues.id; const user = "SMC"; processInstanceId = camundaHelper.startFinancialsProcessInstance(Entitydata.dataValues.losid,Entitydata.dataValues.pan,user); @@ -92,6 +97,13 @@ var EntityInsert = new Promise((resolve, reject) => { // call and get financial year wise data for entity (probe42 api call) financials_data_api_response = ApiCallHelper.probe42ApiCall(cin); financials_data_api_response.then(function(result){ + if(result == false) + { + // create skeleton here + financialControllerHelper.createSkeleton(losid,entityid); + } + else + { //filter lineitems from api responce and construct to needed format lineItem_data = Probe42DataMappingHelper.lineItemFetching(result); lineItem_data.then(function(lineItem_result){ @@ -116,19 +128,15 @@ var EntityInsert = new Promise((resolve, reject) => { }); SkeletonInsert.then((Query_data)=>{ Financials.bulkCreate(Query_data).then(data=>{ - //line item data update - // lineItem_result.forEach((value,index,array) => { + //line item data update // mapping of probe42 api lineItem with itemMaster primary key final_array = Probe42DataMappingHelper.lineItemMapping(value,entityid); final_array.then(function(update_array_data){ // update mapped lineitem value here Probe42DataMappingHelper.lineItemUpdate(update_array_data); });//end of lineItemUpdate helper call - // });//end of lineItem mapping with itemMaster primary key helper call - }).catch(err=>(console.log(err))); //skeleton bulk create query }); - }).catch(err=>(console.log(err))); //itemmaster query @@ -138,15 +146,12 @@ var EntityInsert = new Promise((resolve, reject) => { message: err.message || "Some error occurred while insertinging statements." ,'data': "No data" }); }); // end of entity fin year table data insert - });//end of foreach + });//end of line item foreach + });//end of lineItem filter from api response helper call + } // end of probe42 api responce success else condition + });//end of probe42 api call + } // end of cin number exist check in redirection api response - - - });//end of lineItem filter from api response helper call - });//end of probe42 api call - - - // resolve here if (key === array.length -1) { resolve(); } @@ -159,13 +164,15 @@ var EntityInsert = new Promise((resolve, reject) => { }); // end of (losid,pan,cin) combination checking entity data exist or not - }); // end of pro42 data foreach - + }); // end of Redirection api data foreach + } // end of redirectionApiData else condition + }) // end of Redirection api call }); // end of promise + EntityInsert.then(() => { setTimeout(()=>{ Statementsmaster.findAll({ where :{is_active : 1} , include: { all: true, nested: true , where :{is_active : 1} , required:false}}) @@ -385,11 +392,14 @@ Entities.findOne({where : { id } , attributes: [ 'id','losid','process_instance_ .then(EntitiesData => { const instancesid = EntitiesData.dataValues.process_instance_id; sequelize.query("select a.losid,a.entityid,a.FY,a.st_type as st_type_id, b.statement_name as st_type_name,a.additional_year_type FROM entity_fin_year a LEFT JOIN (SELECT id,statement_name FROM statement_type) b ON a.st_type = b.id WHERE entityid ="+id+" order by a.fy desc") -.then(EntityfinyearData => { +.then(EntityfinyearData => { + if(EntityfinyearData[0].length != 0) + { const data = EntityfinyearData[0]; const year_1 = EntityfinyearData[0][0].FY; const additional_year_type = EntityfinyearData[0][0].additional_year_type; - const years = {losid:EntitiesData.dataValues.losid,entityid:EntitiesData.dataValues.id,entity_type:EntitiesData.dataValues.entity_type,additional_year_type:additional_year_type}; + if(EntitiesData.dataValues.entity_type == "COMPANY"){entity_type_key = "pvtltd"} else if(EntitiesData.dataValues.entity_type == "INDIVIDUAL"){entity_type_key = "llp"} else {entity_type_key = "others"} + const years = {losid:EntitiesData.dataValues.losid,entityid:EntitiesData.dataValues.id,entity_type_key:entity_type_key,additional_year_type:additional_year_type}; // below function call is for calculation of additional new year flag generation add_new_FY_flag = financialControllerHelper.addNewFyFlag(year_1); // below code for constructing query for dynamic financial years @@ -414,7 +424,7 @@ sequelize.query("select a.losid,a.entityid,a.FY,a.st_type as st_type_id, b.state var query = "Select a.entity_id,x.id as statement_id,x.statement as statement,y.id as section_id,y.section as section,w.subsection as subsection_id,z.subsection as subsection , z.itemtext as subsection_itemtext ,a.itemid,w.itemtext as items,w.formula,w.is_calculated,w.is_summary,w.pvtltd,w.llp,w.others,a.st_type as Y1_st_type,a.fy as Y1,a.id as Y1_id ,a.is_editable,a.value as Y1_value "+ select_items +",v.remarks , v.id as remarks_id FROM financials a LEFT JOIN ( SELECT id,entity_id,itemid,remarks FROM fin_remarks WHERE is_active = 1) v ON a.entity_id = v.entity_id and a.itemid = v.itemid JOIN ( SELECT id,itemtext,statement,section,subsection,formula,is_calculated,is_summary,pvtltd,llp,others FROM itemsmaster ) w ON a.itemid = w.id JOIN ( SELECT id, statement FROM statementsmaster ) x ON w.statement = x.id JOIN ( SELECT id, section FROM sectionsmaster ) y ON w.section = y.id LEFT JOIN ( SELECT id, subsection , itemtext FROM subsectionsmaster ) z ON w.subsection = z.id "+ sub_query +"WHERE a.fy = '"+year_1+"' and a.entity_id = "+id+" and a.is_active = 1 order by w.statement asc , w.section asc , w.subsection asc"; setTimeout(function(){ - sequelize.query(query).then(summary => { + sequelize.query(query).then(summary => { // is_summary filter from fin_summary data let is_summary_item_array = []; const fin_summary = summary[0]; @@ -446,7 +456,7 @@ sequelize.query("select a.losid,a.entityid,a.FY,a.st_type as st_type_id, b.state }); }, 2000);// end of set time out - + } else { res.status(404).send({'status':404,message: "Financials data not found." ,'data': "No data" });} }) // end of EntityfinyearData query @@ -936,7 +946,23 @@ exports.demoone = (req, res) => { try { - + console.log("inside function"); + losid = "MW00075486"; + redirectionApi = ApiCallHelper.redirectionApiCall(losid); + redirectionApi.then(function(redirectionApiData){ + res.send(redirectionApiData) + redirectionApiData.forEach(element => { + cin = element.ciN_LLPIN; + if(cin != "") + { + prob42 = ApiCallHelper.probe42ApiCall(cin) + prob42.then(function(prob42Date){ + console.log(prob42Date) + }) + } + }); + }) + }//try diff --git a/app/routes/routes.js b/app/routes/routes.js index 2af5a08..75f10fc 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("/demoone", financials.demoone); /** * @swagger * /api/ConsolidatedSummary: diff --git a/app/services/financial.controller.helper.js.js b/app/services/financial.controller.helper.js.js index c3bd118..834e134 100644 --- a/app/services/financial.controller.helper.js.js +++ b/app/services/financial.controller.helper.js.js @@ -1,3 +1,7 @@ +const { sequelize, Financials , Itemsmaster , Entityfinyear} = require('../models') +const { Op } = require("sequelize"); +const { logMsg } = require('../services/logger.js'); + function addNewFyFlag(Y1) { @@ -23,7 +27,55 @@ } } + +function createSkeleton(losid,entityid) +{ + try + { + + // create skeletion with itemmaster data (itemmaster(id) == financial(itemid)) + Itemsmaster.findAll({ where :{is_active : 1},attributes: ['id'] , order: [['id', 'ASC']] }) + .then(ItemsmasterData => { + year_data = [{fy:2021},{fy:2020},{fy:2019}]; + year_data.forEach(year_value => { + + Entityfinyear.create({ "losid":losid,"entityid":entityid,"FY":year_value.fy,"st_type": 1 }) + .then(Entityfinyearsdata => { + + //construct lineitem array for bulk insert using itemmaster primarykey (id) + var SkeletonInsert = new Promise((resolve, reject) => { + let fin_data_array = []; + ItemsmasterData.forEach( (itemvalue , index , itemvaluearray) => { + fin_data_obj = { "entity_id":Entityfinyearsdata.dataValues.entityid,"st_type":Entityfinyearsdata.dataValues.st_type,"itemid":itemvalue.dataValues.id,"fy":Entityfinyearsdata.dataValues.FY,"value":0,"is_active":1,"createdby":4} + fin_data_array.push(fin_data_obj); + if (index === itemvaluearray.length -1) { resolve(fin_data_array); } + }) + }); + SkeletonInsert.then((Query_data)=>{ + Financials.bulkCreate(Query_data).then(data=>{ + logMsg.info("Skeleton created for LosId ="+losid+" - EntityId = "+entityid); + }).catch(err=>(console.log(err))); //skeleton bulk create query + }); + + }).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 + + }); //end of forEach + + }).catch(err=>(console.log(err))); //itemmaster query + + + + } + catch (error) + { + console.log(error); + } +} module.exports = { - addNewFyFlag : addNewFyFlag + addNewFyFlag : addNewFyFlag , + createSkeleton : createSkeleton } \ No newline at end of file diff --git a/app/services/financials.apicall.helper.js b/app/services/financials.apicall.helper.js index 4ab5d8c..82f736c 100644 --- a/app/services/financials.apicall.helper.js +++ b/app/services/financials.apicall.helper.js @@ -1,8 +1,15 @@ const request = require('request'); -const PROBE42_URL = process.env.PROBE42_URL +// probe42 +PROBE42_URL = process.env.PROBE42_URL PROBE42_USER_ID = process.env.PROBE42_USER_ID PROBE42_PASSWORD = process.env.PROBE42_PASSWORD PROBE42_APPLICATION_NAME = process.env.PROBE42_APPLICATION_NAME +//redirection +REDIRECTION_URL = process.env.REDIRECTION_URL +REDIRECTION_USERNAME = process.env.REDIRECTION_USERNAME +REDIRECTION_PASSWORD = process.env.REDIRECTION_PASSWORD +REDIRECTION_APPLICATION_NAME = process.env.REDIRECTION_APPLICATION_NAME + function probe42ApiCall(cin) { @@ -19,10 +26,14 @@ PROBE42_APPLICATION_NAME = process.env.PROBE42_APPLICATION_NAME method: "POST", json: true, body: myJSONObject - }, function (error, response, body){ - let data = body - financial_data = data.data.financials; + }, function (error, response, body){ + + if(response.statusCode == 200 && body.hasOwnProperty("data") && body.data.financials.length != 0) + { + financial_data = body.data.financials; resolve(financial_data); + } else { resolve(false); } + }); }); @@ -33,10 +44,41 @@ PROBE42_APPLICATION_NAME = process.env.PROBE42_APPLICATION_NAME } } + + + function redirectionApiCall(losid) + { + try + { + const redirection_url = REDIRECTION_URL+"getdetail"; + const myJSONObject = { "Username": REDIRECTION_USERNAME, + "Password": REDIRECTION_PASSWORD, + "ApplicationName": REDIRECTION_APPLICATION_NAME, + "LoginNo": losid }; + return new Promise((resolve, reject) => { + request({ + url: redirection_url, + method: "POST", + json: true, + body: myJSONObject + }, function (error, response, body){ + + if(response.statusCode == 200 && body.status == "Success" && body.data.length != 0) + { + resolve(body.data); + } else { resolve(false); } + }); + + }); + } + catch (error) + { + console.log(error); + } + } module.exports = { - probe42ApiCall : probe42ApiCall - - + probe42ApiCall : probe42ApiCall , + redirectionApiCall : redirectionApiCall } \ No newline at end of file