const { sequelize , LoanDetail , EntityDetails , ApplicantsDetails , ApplicantsRelationship , ResidenceAddressDetails , DueDiligenceCheck , TradeReferences , CheckTypeMaster , EnquiriesAndDisbursements , PropertyAndLtv , ATNWrelatedInputs , SalesPD , ATNWrelatedInputMaster ,ATNWrelationship , OtherParameterMaster ,OtherParameterMasterValue , OtherParameterRelationship , OtherParameters , ATNErelatedInputSubVal , DataFromLos , FormulaMaster , LoanAndLtvMaster , EntityApplicantMaster , PropertyOwnershipIncomeConsidered } = require('../models') const { logMsg } = require('../services/logger.js'); const request = require('request'); async function loanDetailsDataApiCall(losid) { try { LOAN_URL = process.env.LOS_DATA_URL; token = await getLoginToken(); request_object = { "losid": losid }; return new Promise((resolve, reject) => { request({ url: LOAN_URL, method: "POST", headers: { Authorization: 'Bearer ' + token }, json: true, body: request_object }, function (error, response, body){ // let data = JSON.parse(response); console.log(response); // if(data.status == 'Successful') // { // resolve(data); // } resolve(body); }); }); } catch (error) { console.log(error); } } function getLoginToken() { try { const url = process.env.TOKEN_URL; return new Promise((resolve, reject) => { request({ url: url, method: "GET" }, function (error, response, body){ data = JSON.parse(body); if(data.status === 200) { resolve(data.data.token); } else { resolve(false); } }) }); } catch (error) { console.log(error); } } async function EntityChildTable(EntityId) { try { var DueDiligenceCheckPromise = new Promise(async(resolve, reject) => { // DueDiligenceCheck CheckTypeMaster.findAll({raw: true , where : {is_active : 1}}).then(async(CheckTypeMasterData) => { entityid = EntityId; DueDiligenceCheckData = []; for (const value of CheckTypeMasterData) { check_type_obj = {entity_id:entityid , check_type_id:value.id , check_type:value.value}; DueDiligenceCheckData.push(check_type_obj); } setTimeout(() => { DueDiligenceCheck.bulkCreate(DueDiligenceCheckData) .then(DueDiligenceCheckData => { // logMsg.info("DueDiligenceCheck Data Creation Success for EntityId= "+entityid); resolve("DueDiligenceCheck") }) .catch(err =>{ logMsg.info("DueDiligenceCheck Data Creation Failed for EntityId= "+entityid+" Error= "+err); }); }, 500); }) .catch(err => { logMsg.info("DueDiligenceCheckType Data Fetching Failed for EntityId= "+entityid+" Error= "+err); }); }); //promise // ATNWrelatedInput var ATNWrelatedInputsPromise = new Promise(async(resolve, reject) => { ATNWrelatedInputMaster.findAll({raw: true , where : {is_active : 1}}).then(async(ATNWrelatedInputMasterData) => { entityid = EntityId; ATNWrelatedInputMasterData_arr = []; ATNWrelatedInputMasterData_arr_val = []; for (const value of ATNWrelatedInputMasterData) { if ( value.id == 9 || value.id == 10 ) { atnw_master_value_data = { atnw_related_input :value.master_name }; ATNWrelatedInputMasterData_arr_val.push(atnw_master_value_data); } else { atnw_master_data = {entity_id:entityid , atnw_related_input_id:value.id , atnw_related_input:value.master_name}; ATNWrelatedInputMasterData_arr.push(atnw_master_data); } } setTimeout(() => { ATNWrelatedInputs.bulkCreate(ATNWrelatedInputMasterData_arr) .then(ATNWrelatedInputMasterData_arr => { ATNWrelatedInputMasterData_arr.forEach( async (atnw_element) => { if (atnw_element.atnw_related_input_id == 8) { ATNWrelatedInputMasterData_arr_val.forEach( async (atnw_related_input_id_element) => { Object.assign( atnw_related_input_id_element, { master_id : atnw_element.id }); ATNErelatedInputSubVal.create(atnw_related_input_id_element).then(data => { logMsg.info("ATNWrelatedInputs_subValue Data Creation Success for EntityId= "+entityid); }) }); } }); // logMsg.info("ATNWrelatedInputs Data Creation Success for EntityId= "+entityid); resolve("ATNWrelatedInputs"); }) .catch(err =>{ logMsg.info("ATNWrelatedInputs Data Creation Failed for EntityId= "+entityid+" Error= "+err); }); }, 500); }) .catch(err => { logMsg.info("ATNWrelatedInputs Data Fetching Failed for EntityId= "+entityid+" Error= "+err); }); }); // Promise // otherParameter data create var OtherParametersPromise = new Promise(async(resolve, reject) => { OtherParameterMaster.findAll({raw: true , where : {is_active : 1}}).then(async(OtherParameterMasterData) => { entityid = EntityId; OtherParameterMasterData_arr = []; for (const value of OtherParameterMasterData) { op_master_data = {entity_id:entityid , op_master_id:value.id , master_name:value.master_name}; OtherParameterMasterData_arr.push(op_master_data); } setTimeout(() => { OtherParameters.bulkCreate(OtherParameterMasterData_arr) .then(OtherParameterMasterData_arr => { // logMsg.info("otherParameter Data Creation Success for EntityId= "+entityid); resolve("otherParameter") }) .catch(err =>{ logMsg.info("otherParameter Data Creation Failed for EntityId= "+entityid+" Error= "+err); }); }, 500); }) .catch(err => { logMsg.info("otherParameter Data Fetching Failed for EntityId= "+entityid+" Error= "+err); }); })// Promise // TradeReferencesDetails var TradeReferencesPromise = new Promise(async(resolve, reject) => { TradeReferencesData = { "entity_id" : EntityId } await TradeReferences.create(TradeReferencesData) .then(data => {resolve("TradeReferences"); }) .catch(err => { logMsg.info("TradeReferences Data Creation Failed for EntityId= "+EntityId+" Error= "+err); }); })// Promise // EnquiriesAndDisbursements var EnquiriesAndDisbursementsPromise = new Promise(async(resolve, reject) => { EnquiriesAndDisbursementsData = { "entity_id" : EntityId } EnquiriesAndDisbursements.create(EnquiriesAndDisbursementsData) .then(data => {resolve("EnquiriesAndDisbursements"); }) .catch(err => { logMsg.info("EnquiriesAndDisbursements Data Creation Failed for EntityId= "+EntityId+" Error= "+err); }); }) // Promise Promise.all([DueDiligenceCheckPromise, ATNWrelatedInputsPromise, OtherParametersPromise , TradeReferencesPromise , EnquiriesAndDisbursementsPromise ]) .then(values => { values.forEach(element => { logMsg.info(element+ " Data Creation Success for EntityId= "+EntityId); // [3, 1337, "foo"] }); }) } catch (error) { console.log(error); } } function ApplicantChildTable(ApplicantName , LoanId , ApplicantId , Losid , EntityType , ApiArray , Product , Applicant_id ) { try { // ApplicantsRelationship Table var ApplicantsRelationshipPromise = new Promise(async(resolve, reject) => { if(EntityType == "Individual") { ApplicantsRelationshipData = { } Object.assign(ApplicantsRelationshipData, {name_of_the_individual : ApplicantName ,applicant_id: ApplicantId, loan_no_id: LoanId ,losid: Losid }); await ApplicantsRelationship.create(ApplicantsRelationshipData) .then(data=>{ resolve("ApplicantsRelationship")}).catch(err=>{logMsg.info("ApplicantsRelationship Failed , Msg :" +err);}); } }); // ResidenceAddressDetails Table var ResidenceAddressDetailsPromise = new Promise(async(resolve, reject) => { if(ApiArray.lstApplicantAddress.length != 0 && EntityType == "Individual" ) { // ResidenceAddressDetails data ApiArray.lstApplicantAddress.forEach(async(ApplicantAddress,index) => { // Applicant Address Create if (ApplicantAddress.iD_ApplicantKYCDetail == String(Applicant_id) && ApplicantAddress.iD_OwnershipTypes == "Permanent") { ApplicantAddress = { name_of_the_individual : ApplicantName, loan_no_id : LoanId , losid : Losid , applicant_id: ApplicantId , residence_address : [ApplicantAddress.address1, ApplicantAddress.address2, ApplicantAddress.address3 , ApplicantAddress.iD_States , ApplicantAddress.city , ApplicantAddress.pin ].filter(Boolean).join(" "), // concat and ignore null ownership_status : ApplicantAddress.iD_OwnershipTypes , // residence_location_area } await ResidenceAddressDetails.create(ApplicantAddress) .then(data=> { if ([ApiArray.lstApplicantAddress].length - 1 == index) { resolve("ResidenceAddressDetails") } }) .catch(err=>{logMsg.info("ResidenceAddressDetails Failed , Msg :" +err);}); } }); } }) // PropertyOwnershipIncomeConsidered Table var PropertyOwnershipIncomeConsideredPromise = new Promise(async(resolve, reject) => { if (Product == "LFMP") { PropertyOwnershipIncomeConsideredData = { } Object.assign(PropertyOwnershipIncomeConsideredData, {name_of_the_loan_applicants : ApplicantName ,applicant_id: ApplicantId, loan_no_id: LoanId ,losid: Losid }); await PropertyOwnershipIncomeConsidered.create(PropertyOwnershipIncomeConsideredData) .then(data=>{ resolve("PropertyOwnershipIncomeConsidered") }).catch(err=>{logMsg.info("PropertyOwnershipIncomeConsidered Failed , Msg :" +err);}); } }); Promise.all([ApplicantsRelationshipPromise, ResidenceAddressDetailsPromise, PropertyOwnershipIncomeConsideredPromise ]) .then((values) => { values.forEach(element => { logMsg.info(element+ " Data Creation Success for ApplicantId= "+ApplicantId); // [3, 1337, "foo"] }); }) } catch (error) { console.log(error); } } module.exports = { loanDetailsDataApiCall : loanDetailsDataApiCall, getLoginToken : getLoginToken, EntityChildTable : EntityChildTable, ApplicantChildTable : ApplicantChildTable }