diff --git a/app/controllers/cibil.controller.js b/app/controllers/cibil.controller.js index 243c253..ce4d436 100644 --- a/app/controllers/cibil.controller.js +++ b/app/controllers/cibil.controller.js @@ -190,11 +190,9 @@ exports.ConsolidatedActiveAndClosedLoansDetailsList =async (req, res) => try { - - //ActiveLoanData = await OutputActiveAndClosedLoans.findAll({raw : true ,attributes:[ sequelize.literal("STRING_AGG(consumer_name,',')") ],attributes: {exclude: ['id','ref_no','is_active','createdAt','createdby','updatedAt','updatedby']} , where:{losid:req.query.losid , DateClosed:null },group: ['grp_str','losid','account_type','ownership','reported_and_certified_date','opened_date','sanctioned_amount','current_balance','repayment_tenure','emi_amount','overdue_amount','status','current_dpd','highest_dpd_month_wise_bucket_or_count_0_to_6','highest_dpd_month_wise_bucket_or_count_07_to_12','highest_dpd_month_wise_bucket_or_count_13_to_24','highest_dpd_month_wise_bucket_or_count_25_to_36','DateClosed','consumer_name']}); - ActiveLoanData = await sequelize.query("SELECT [losid],STRING_AGG(consumer_name, ',') as consumer_name, [account_type],count(*) as loan_count, [ownership], [reported_and_certified_date], [opened_date], [sanctioned_amount], [current_balance], [repayment_tenure], [emi_amount], [overdue_amount], [status], [current_dpd], [highest_dpd_month_wise_bucket_or_count_0_to_6], [highest_dpd_month_wise_bucket_or_count_07_to_12], [highest_dpd_month_wise_bucket_or_count_13_to_24], [highest_dpd_month_wise_bucket_or_count_25_to_36], [DateClosed], [grp_str] FROM [out_active_and_closed_loans_details] AS [OutputActiveAndClosedLoans] WHERE [OutputActiveAndClosedLoans].[losid] = '"+req.query.losid+"' AND [OutputActiveAndClosedLoans].[DateClosed] IS NULL GROUP BY [grp_str], [losid], [account_type], [ownership], [reported_and_certified_date], [opened_date], [sanctioned_amount], [current_balance], [repayment_tenure], [emi_amount], [overdue_amount], [status], [current_dpd], [highest_dpd_month_wise_bucket_or_count_0_to_6], [highest_dpd_month_wise_bucket_or_count_07_to_12], [highest_dpd_month_wise_bucket_or_count_13_to_24], [highest_dpd_month_wise_bucket_or_count_25_to_36], [DateClosed]"); - //ClosedLoanData = await OutputActiveAndClosedLoans.findAll({raw : true ,attributes:[ sequelize.literal("STRING_AGG(consumer_name,',')") ],attributes: {exclude: ['id','ref_no','is_active','createdAt','createdby','updatedAt','updatedby']}, where:{losid:req.query.losid , DateClosed:{ [Op.ne]: null} },group: ['grp_str','losid','account_type','ownership','reported_and_certified_date','opened_date','sanctioned_amount','current_balance','repayment_tenure','emi_amount','overdue_amount','status','current_dpd','highest_dpd_month_wise_bucket_or_count_0_to_6','highest_dpd_month_wise_bucket_or_count_07_to_12','highest_dpd_month_wise_bucket_or_count_13_to_24','highest_dpd_month_wise_bucket_or_count_25_to_36','DateClosed','consumer_name']}); - ClosedLoanData = await sequelize.query("SELECT [losid],STRING_AGG(consumer_name, ',') as consumer_name, [account_type],count(*) as loan_count, [ownership], [reported_and_certified_date], [opened_date], [sanctioned_amount], [current_balance], [repayment_tenure], [emi_amount], [overdue_amount], [status], [current_dpd], [highest_dpd_month_wise_bucket_or_count_0_to_6], [highest_dpd_month_wise_bucket_or_count_07_to_12], [highest_dpd_month_wise_bucket_or_count_13_to_24], [highest_dpd_month_wise_bucket_or_count_25_to_36], [DateClosed], [grp_str] FROM [out_active_and_closed_loans_details] AS [OutputActiveAndClosedLoans] WHERE [OutputActiveAndClosedLoans].[losid] = '"+req.query.losid+"' AND [OutputActiveAndClosedLoans].[DateClosed] IS NOT NULL GROUP BY [grp_str], [losid], [account_type], [ownership], [reported_and_certified_date], [opened_date], [sanctioned_amount], [current_balance], [repayment_tenure], [emi_amount], [overdue_amount], [status], [current_dpd], [highest_dpd_month_wise_bucket_or_count_0_to_6], [highest_dpd_month_wise_bucket_or_count_07_to_12], [highest_dpd_month_wise_bucket_or_count_13_to_24], [highest_dpd_month_wise_bucket_or_count_25_to_36], [DateClosed]"); + ActiveLoanData = await sequelize.query("SELECT [losid],STRING_AGG(consumer_name, ',') as consumer_name,STRING_AGG(account_type, ',') as account_type,count(*) as loan_count, STRING_AGG(ownership, ',') as ownership,MAX(reported_and_certified_date) as reported_and_certified_date,MAX(opened_date) as opened_date,MAX(sanctioned_amount) assanctioned_amount,MAX(current_balance) as current_balance,MAX(repayment_tenure) asrepayment_tenure,MAX(emi_amount) as emi_amount, MAX(overdue_amount) as overdue_amount,MAX(status) as status, MAX(current_dpd) as current_dpd, MAX(highest_dpd_month_wise_bucket_or_count_0_to_6) as highest_dpd_month_wise_bucket_or_count_0_to_6,MAX(highest_dpd_month_wise_bucket_or_count_07_to_12) as highest_dpd_month_wise_bucket_or_count_07_to_12,MAX(highest_dpd_month_wise_bucket_or_count_13_to_24) as highest_dpd_month_wise_bucket_or_count_13_to_24,MAX(highest_dpd_month_wise_bucket_or_count_25_to_36) as highest_dpd_month_wise_bucket_or_count_25_to_36,MAX(DateClosed) as DateClosed, [grp_str] FROM [out_active_and_closed_loans_details] AS [OutputActiveAndClosedLoans] WHERE [OutputActiveAndClosedLoans].[losid] = '"+req.query.losid+"' AND [OutputActiveAndClosedLoans].[DateClosed] IS NULL GROUP BY [grp_str], [losid]"); + + ClosedLoanData = await sequelize.query("SELECT [losid],STRING_AGG(consumer_name, ',') as consumer_name,STRING_AGG(account_type, ',') as account_type,count(*) as loan_count, STRING_AGG(ownership, ',') as ownership,MAX(reported_and_certified_date) as reported_and_certified_date,MAX(opened_date) as opened_date,MAX(sanctioned_amount) assanctioned_amount,MAX(current_balance) as current_balance,MAX(repayment_tenure) asrepayment_tenure,MAX(emi_amount) as emi_amount, MAX(overdue_amount) as overdue_amount,MAX(status) as status, MAX(current_dpd) as current_dpd, MAX(highest_dpd_month_wise_bucket_or_count_0_to_6) as highest_dpd_month_wise_bucket_or_count_0_to_6,MAX(highest_dpd_month_wise_bucket_or_count_07_to_12) as highest_dpd_month_wise_bucket_or_count_07_to_12,MAX(highest_dpd_month_wise_bucket_or_count_13_to_24) as highest_dpd_month_wise_bucket_or_count_13_to_24,MAX(highest_dpd_month_wise_bucket_or_count_25_to_36) as highest_dpd_month_wise_bucket_or_count_25_to_36,MAX(DateClosed) as DateClosed, [grp_str] FROM [out_active_and_closed_loans_details] AS [OutputActiveAndClosedLoans] WHERE [OutputActiveAndClosedLoans].[losid] = '"+req.query.losid+"' AND [OutputActiveAndClosedLoans].[DateClosed] IS NOT NULL GROUP BY [grp_str], [losid]"); if(ActiveLoanData.length != 0 || ClosedLoanData.length != 0) { @@ -557,6 +555,8 @@ exports.ConsolidatedSanctionAmountSplitUp =async (req, res) => //merge-unmerge api's + + exports.ConsolidatedActiveAndClosedLoansReviewAction =async (req, res) => { try @@ -588,7 +588,6 @@ exports.ConsolidatedActiveAndClosedLoansReviewAction =async (req, res) => setTimeout(() => { res.send({'status':200 , message:"Success." ,'data': FinalLoanData}); }, 1000); - }); @@ -609,6 +608,78 @@ exports.ConsolidatedActiveAndClosedLoansReviewAction =async (req, res) => +exports.LoansDetailsMergeUnmergeApi =async (req, res) => +{ + try + { + request_object = req.body; + if(request_object.unmergeData.length + request_object.mergeData.length != 0) + { + + + //unmerge loans + request_object.unmergeData.forEach(async(unmergeValue,unmergeIndex,unmergeArray) => { + let Unique_Group_string = Math.random().toString(36).slice(2, 10).toUpperCase(); + await InCreditFacility.update({grp_str:Unique_Group_string},{where:{losid:request_object.losid,ReferenceNumber:unmergeValue.refno,grp_str:unmergeValue.grp_str}}); + await OutputActiveAndClosedLoans.update({grp_str:Unique_Group_string},{where:{losid:request_object.losid,ref_no:unmergeValue.refno,grp_str:unmergeValue.grp_str}}); + console.log(unmergeValue.refno); + }); + //merge loans + request_object.mergeData.forEach(async(mergeValue,mergeIndex,mergeArray) => { + if(mergeValue.type == 'SINGLE_TO_GROUP') + { + Unique_Group_string = mergeValue.mergeable_grp_str; + mergeValue.data.forEach(async(Value,Index,Array) => { + await InCreditFacility.update({grp_str:Unique_Group_string},{where:{losid:request_object.losid,ReferenceNumber:Value.refno,grp_str:Value.grp_str}}); + await OutputActiveAndClosedLoans.update({grp_str:Unique_Group_string},{where:{losid:request_object.losid,ref_no:Value.refno,grp_str:Value.grp_str}}); + console.log(Value.refno); + }); + } + else + { + Unique_Group_string = Math.random().toString(36).slice(2, 10).toUpperCase()+"-"+Math.random().toString(36).slice(2, 6)+"-"+Math.random().toString(36).slice(2, 6)+"-"+Math.random().toString(36).slice(2, 12).toUpperCase(); + mergeValue.data.forEach(async(Value,Index,Array) => { + await InCreditFacility.update({grp_str:Unique_Group_string},{where:{losid:request_object.losid,ReferenceNumber:Value.refno,grp_str:Value.grp_str}}); + await OutputActiveAndClosedLoans.update({grp_str:Unique_Group_string},{where:{losid:request_object.losid,ref_no:Value.refno,grp_str:Value.grp_str}}); + console.log(Value.refno); + }); + } + + }); + count = request_object.unmergeData.length + request_object.mergeData.length + timeout = 600 * count; + + await OutSanctionAmt_P.destroy({ where:{losid:req.query.losid},include: [ { model: OutSanctionAmt_C }] }); + setTimeout(async () => { + + BorrowerDetailData = await InBorrowerDetail.findAll({where:{losid:request_object.losid}}).catch(ett=>{}); + var DataPromises = new Promise(async(resolve, reject) => { + BorrowerDetailData.forEach(async(BorrowerValue,BorrowerIndex,BorrowerArray) => { + await Helper.cibilOutputGenerateApiCall(BorrowerValue.losid,BorrowerValue.ReferenceNumber,BorrowerValue.cibil_type,[2],1); + if (BorrowerIndex === BorrowerArray.length -1) { resolve(); } + }); + }); + DataPromises.then(async()=>{ + setTimeout(async() => { + await Helper.cibilOutputGenerateApiCall(BorrowerValue.losid,BorrowerValue.ReferenceNumber,cibil_type,[2],2); + res.send({'status':200 , message:"Success." ,'data': "No data"}); + }, 1500); + }); + },1000 + timeout); +} +else{ + res.send({'status':404 , message:"Failed." ,'data': "No data"}); +} + } catch(err) { + res.status(500).send({'status':404, + message: err.message || "Some error occurred while inserting statements." ,'data': "No data" + }); } //end of try catch + +}; + + + + @@ -626,87 +697,6 @@ exports.ConsolidatedActiveAndClosedLoansReviewAction =async (req, res) => - - // end_date = '2022-05-05' - // sk = await Helper.createSkeleton(end_date); - // res.send({sk}) - // dpd_dates = ['2019-10-01','2019-09-20','2019-08-20','2019-07-20','2019-06-20','2019-05-20','2019-04-20','2019-03-20']; - // dpd_dates.forEach(async(element) => { - // data = await Helper.findBucketCount(sk,element); - // console.log(data); - // }); - - - - -exports.test = async (req, res) => -{ - try - { - // data = await Helper.cibilInputDataApiCall('MW55904793','AORPK5766N',2); - // DataInsertHelper.consumerDataInsert(data); - // res.send(data); - - - // query = " test_group_loans @los_id = 'MW112233', @ref_no = '202201121115264000000' "; - // sequelize.query(query).then(async(data) => { - - // res.send({'status':200 , message:"Success." ,'data':data}); - // }).catch(err=>{ res.send({'status':400 , message: "Failed" ,'data': "Nodata"}); }) - - } catch(err) { - res.status(500).send({'status':404, - message: err.message || "Some error occurred while inserting statements." ,'data': "No data" - }); -} //end of try catch - -}; - -exports.testone =async (req, res) => -{ - try - { - data = await Helper.cibilInputDataApiCall('MW00045413','AAACR4282J',1); - - DataInsertHelper.commercialDataInsert(data); - res.send(data); - } catch(err) { - res.status(500).send({'status':404, - message: err.message || "Some error occurred while inserting statements." ,'data': "No data" - }); -} //end of try catch - -}; - - - - - - -exports.test2 =async (req, res) => -{ - try - { - //token = await Helper.getLoginToken() - data = await Helper.cibilInputDataApiCall('MW00045413','AAACR4282J',1); - res.send(data); - // beforedate = data.lstConsumerBorrowersDetails[0].dateTimeProcessed; - // var date= moment(beforedate).format('YYYY-MM-DD'); - // console.log(date); - - - req_obj ={ - "losid":'MW112233', - Me - } - - - } catch(err) { - res.status(500).send({'status':404, - message: err.message || "Some error occurred while inserting statements." ,'data': "No data" - }); } //end of try catch - -}; @@ -749,10 +739,6 @@ exports.starter = async (req, res) => DataInsertHelper.consumerDataInsert(CibilData); //move cibil data from input table to intermediate table DataInsertHelper.consumerIntermediateDataInsert(PanListData.losid,ref_no); - - - //cibil output data generating python script api call - Helper.cibilOutputGenerateApiCall(losid,ref_no,cibil_type,1); } else { @@ -763,28 +749,31 @@ exports.starter = async (req, res) => DataInsertHelper.commercialDataInsert(CibilData); //move cibil data from input table to intermediate table DataInsertHelper.commercialIntermediateDataInsert(PanListData.losid,ref_no); - - - //cibil output data generating python script api call - Helper.cibilOutputGenerateApiCall(losid,ref_no,cibil_type,1); } - + //resolve here + if (PanListIndex === PanListArray.length -1) { resolve(req.query.losid); } }).catch(err=>{ }); - //resolve here - if (PanListIndex === PanListArray.length -1) { resolve(req.query.losid); } + } });//end of pan list foreach - });//end of promises - + });//end of promises CibilDataInsert.then(async(losid)=>{ - //joint loan unique key update procedure call - - BorrowerDetailData = await InBorrowerDetail.findAll({where:{losid:losid}}); - res.send({'status':200 , message:"Success." ,'data':BorrowerDetailData}); - + var OutputDataGenerator = new Promise(async(resolve, reject) => { + BorrowerDetailDataArray = await InBorrowerDetail.findAll({where:{losid:losid}}); + BorrowerDetailDataArray.forEach(async(value,index,array) => { + await Helper.cibilOutputGenerateApiCall(value.losid,value.ReferenceNumber,value.cibil_type,[1,2,3],1); + if (index === array.length -1) { resolve(value.losid); } + }); + }); + OutputDataGenerator.then(async(losid)=>{ + ProcCall = await sequelize.query("DECLARE @uniuqe_loans_found int,@row_updated int EXEC IdentifyUniqueLoans '"+losid+"',@uniuqe_loans_found OUTPUT,@row_updated OUTPUT SELECT @uniuqe_loans_found as uniuqe_loans_found,@row_updated as row_updated "); + await Helper.cibilOutputGenerateApiCall(value.losid,value.ReferenceNumber,value.cibil_type,[2,3],2); + BorrowerData = await InBorrowerDetail.findAll({where:{losid:losid}}); + res.send({'status':200 , message:"Success." ,'data':BorrowerData}); + }); }); } else @@ -806,3 +795,37 @@ exports.starter = async (req, res) => + + + + + + + + + + + + + +exports.test = async (req, res) => +{ + try + { + date = '04-AUG-2017'; + data = moment(new Date(date)).format("YYYY-MM-DD"); + console.log(data); + res.send({data}); + +} catch(err) { + res.status(500).send({'status':404, + message: err.message || "Some error occurred while inserting statements." ,'data': "No data" +}); +} //end of try catch + +}; + + + + + diff --git a/app/routes/routes.js b/app/routes/routes.js index 91c6baa..0e2d11a 100644 --- a/app/routes/routes.js +++ b/app/routes/routes.js @@ -5,9 +5,8 @@ module.exports = app => { const { logMsg } = require('../services/logger.js'); var router = require("express").Router(); - // router.get("/testone", Cibilcontroller.testone); - // router.get("/starter", Cibilcontroller.starter); - router.get("/test2", Cibilcontroller.test2); + + router.get("/test", Cibilcontroller.test); /** * @swagger @@ -401,6 +400,21 @@ router.get("/consolidatedActiveClosedLoanSplitUp", Cibilcontroller.consolidatedA router.get("/ConsolidatedActiveAndClosedLoansReviewAction", Cibilcontroller.ConsolidatedActiveAndClosedLoansReviewAction); + + +/** +* @swagger +* /api/LoansDetailsMergeUnmergeApi: +* post: +* summary: Merge / Un merge +* description: +* responses: +* 200: +* description: success +*/ +router.post("/LoansDetailsMergeUnmergeApi", Cibilcontroller.LoansDetailsMergeUnmergeApi); + + // routes started here app.use('/api', router); }; diff --git a/app/services/cibil.controller.helper.js b/app/services/cibil.controller.helper.js index f5d0109..e88679c 100644 --- a/app/services/cibil.controller.helper.js +++ b/app/services/cibil.controller.helper.js @@ -98,10 +98,10 @@ try { } -async function cibilOutputGenerateApiCall(losid,ref_no,cibil_type,output_type) { +async function cibilOutputGenerateApiCall(losid,ref_no,cibil_type,ActionDataArray,output_type) { try { var promises = new Promise(async(resolve, reject) => { - ActionData = [1,2,3]; + ActionData = ActionDataArray; ActionData.forEach((value,index,array) => { let url = process.env.PYTHON_SCRIPT_API+"cibil?reference_number="+ref_no+"&los_id="+losid+"&cibil_type="+cibil_type+"&action="+value+"&output_type="+output_type; diff --git a/app/services/cibil.datainsert.helper.js b/app/services/cibil.datainsert.helper.js index adfaca4..70bf01e 100644 --- a/app/services/cibil.datainsert.helper.js +++ b/app/services/cibil.datainsert.helper.js @@ -1020,10 +1020,9 @@ async function consumerIntermediateDataInsert(losid,refNo) - //month bucket count - // DateTimeProcessed = ConBorrowDetailData - end_date = '2022-05-05' - sk = await Helper.createSkeleton(end_date); + //month bucket count skeleton creation + DateTimeProcessed = ConBorrowDetailData[0].DateTimeProcessed; + sk = await Helper.createSkeleton(DateTimeProcessed); ConEnquiryDetailData = await ConEnquiryDetail.findAll({attributes: ['losid','MemberReferenceNumber','DateOfEnquiry','EnquiryPurpose','EnquiryAmount'],raw:true ,where:{losid:losid,MemberReferenceNumber:refNo}}); @@ -1159,33 +1158,8 @@ async function commercialIntermediateDataInsert(losid,refNo) await InBorrowerDetail.update(BorrowerProfileDataObj,{where:{losid:BorrowerProfileDataObj.losid,PAN:BorrowerProfileDataObj.PAN,cibil_type:2}}); }); - // BorrowerProfileData = await BorrowerProfile.findAll({attributes: ['losid','ApplicationRefno','EnqInfoBorrowerName','EnqInfoPan'],raw:true ,where:{losid:'MW112233'}}); - // var BorrowerProfileDataInsert = new Promise(async(resolve, reject) => { - // BorrowerProfileArray = []; - // BorrowerProfileData.forEach(async(value,index,array) => { - // BorrowerProfileDataObj = { - // losid : value.losid, - // ReferenceNumber : value.ApplicationRefno, - // ConsumerNameField1 : value.EnqInfoBorrowerName, - // ConsumerNameField2 : null, - // ConsumerNameField3 : null, - // ConsumerNameField4 : null, - // ConsumerNameField5 : null, - // PAN : value.EnqInfoPan, - // cibil_type : 2 - // } - // BorrowerProfileArray.push(BorrowerProfileDataObj); - // if (index === array.length -1) { resolve(BorrowerProfileArray); } - // }); - // }); - // BorrowerProfileDataInsert.then(async(BorrowerProfileArray)=>{ - // await InBorrowerDetail.bulkCreate(BorrowerProfileArray); - // }); - - - - - + + //month bucket count skeleton creation end_date = '2022-05-05' sk = await Helper.createSkeleton(end_date);