275 lines
12 KiB
JavaScript
275 lines
12 KiB
JavaScript
const { sequelize , LoanDetail , EntityDetails , ApplicantsDetails , ApplicantsRelationship , ResidenceAddressDetails , PropertyOwnershipIncomeConsidered , DueDiligenceCheck , TradeReferences , CheckTypeMaster , EnquiriesAndDisbursements , PropertyAndLtv , ATNWrelatedInputs , SalesPD , ATNWrelatedInputMaster ,ATNWrelationship , OtherParameterMaster ,OtherParameterMasterValue , OtherParameterRelationship , OtherParameters , ATNErelatedInputSubVal , DataFromLos , FormulaMaster , LoanAndLtvMaster , EntityApplicantMaster , ModuleWiseCompletionStatus} = require('../models')
|
|
const { logMsg } = require('../services/logger.js');
|
|
const ApiCallHelper = require('../services/ApiCallHelper.js');
|
|
const ModuleWiseStatusHelper = require('../services/ModuleWiseStatusData.helper.js');
|
|
const moment = require('moment');
|
|
|
|
|
|
|
|
|
|
//LoanDeatilList List Api
|
|
exports.mainLandingPage = async (req, res) =>
|
|
{
|
|
try
|
|
{
|
|
|
|
if(req.query.is_loan_api_call == 0)
|
|
{
|
|
LoanDetail.findAll({raw: true ,where:{ losid:req.query.losid , is_active : 1},
|
|
include: [{ model: EntityDetails , // Set the alias for the relationship
|
|
attributes: [
|
|
[sequelize.col('entity_name'), 'loan_applicant_name'] // Alias for the 'name' column
|
|
] }] }).then(data =>
|
|
{
|
|
if(data.length > 0)
|
|
{
|
|
res.send({'status':200,'message':"success",'data':data});
|
|
}
|
|
else {
|
|
res.send({'status':404,'message':"failed",'data':"No data"});
|
|
}
|
|
})
|
|
.catch(err => {
|
|
res.send({'status':404,
|
|
message: err.message || "Some error occurred while retrieving statements." ,'data': "No data" });
|
|
});
|
|
|
|
}else if(req.query.is_loan_api_call == 1){
|
|
|
|
Loan_Detail_Losid = await LoanDetail.findOne({ where : { losid : req.query.losid , is_active : 1}})
|
|
if (Loan_Detail_Losid == undefined)
|
|
{
|
|
ResData = await ApiCallHelper.loanDetailsDataApiCall(req.query.losid);
|
|
|
|
if (ResData.status == "Successful" && ResData.lstLoanApplication.length != 0)
|
|
{
|
|
loanDetailData = ResData.lstLoanApplication[0];
|
|
loanDetailObject = {
|
|
applicationNo : loanDetailData.applicationNo,
|
|
losid : loanDetailData.loginNo ,
|
|
date_of_login : moment(new Date(loanDetailData.loginDate)).format("YYYY-MM-DD"),
|
|
sourcing_channel_type : loanDetailData.source,
|
|
additional_loan_amnt_applied_for : Number(loanDetailData.loanAmount),
|
|
product : loanDetailData.loanProductName,
|
|
pd_id : loanDetailData.pD_ID
|
|
}
|
|
// Loan Detail Create
|
|
LoanDetail.create(loanDetailObject).then(LoanDetailData =>
|
|
{
|
|
//Module Wise Completion Status Data Creation Helper Call
|
|
ModuleWiseStatusHelper.moduleCompletionStatusDataCreationHelper(loanDetailData.loginNo);
|
|
// Final Response
|
|
LoanDetail.findAll({raw: true ,where:{ losid:req.query.losid , is_active : 1},
|
|
include: [{ model: EntityDetails , // Set the alias for the relationship
|
|
attributes: [
|
|
[sequelize.col('entity_name'), 'loan_applicant_name'] // Alias for the 'name' column
|
|
] }] }).then(data => { if(data.length > 0) { res.send({'status':200,'message':"success",'data':data}); } else { res.send({'status':404,'message':"failed",'data':"No data"}); } })
|
|
|
|
// Entity detail promise
|
|
var EntityKYCDetailDataInsert = new Promise(async(resolve, reject) =>
|
|
{
|
|
lstEntityKYCDetailDataArray = [];
|
|
ResData.lstApplicantKYCDetail.forEach(async(value,index,array) => {
|
|
if (value.kycType.trim() == "Non-Individual")
|
|
{
|
|
lstEntityKYCDetailObj =
|
|
{
|
|
losid : LoanDetailData.dataValues.losid ,
|
|
loan_no_id : LoanDetailData.dataValues.id ,
|
|
entity_name : [value.firstName, value.middleName, value.lastName ].filter(Boolean).join(" "),
|
|
entity_formation_date : moment(new Date(value.doi)).format("YYYY-MM-DD"),
|
|
pan_no : value.panNo,
|
|
entity_type : null
|
|
}
|
|
ResData.lstApplicantAddress.forEach(async(address_value,index) =>
|
|
{
|
|
if (value.iD_ApplicantKYCDetail == address_value.iD_ApplicantKYCDetail && address_value.iD_OwnershipTypes == "Permanent" )
|
|
{
|
|
lstEntityAddressDetailObj =
|
|
{
|
|
registered_address_of_business: [address_value.address1, address_value.address2, address_value.address3 ].filter(Boolean).join(" "),
|
|
registered_address_state: address_value.iD_States,
|
|
registered_address_city: address_value.city,
|
|
registered_address_pin: address_value.pin,
|
|
registered_address_ownership_status:address_value.iD_OwnershipTypes
|
|
}
|
|
Object.assign(lstEntityKYCDetailObj,lstEntityAddressDetailObj)
|
|
}
|
|
else if(value.iD_ApplicantKYCDetail == address_value.iD_ApplicantKYCDetail && address_value.iD_OwnershipTypes == "Current" )
|
|
{
|
|
lstEntityAddress1DetailObj =
|
|
{
|
|
operating_address_of_business: [address_value.address1, address_value.address2, address_value.address3 ].filter(Boolean).join(" "),
|
|
operating_address_state: address_value.iD_States,
|
|
operating_address_city: address_value.city,
|
|
operating_address_pin: address_value.pin,
|
|
operating_address_ownership_status:address_value.iD_OwnershipTypes,
|
|
// operating_address_city_category : address_value.addressCategory
|
|
}
|
|
Object.assign(lstEntityKYCDetailObj,lstEntityAddress1DetailObj)
|
|
}
|
|
});
|
|
lstEntityKYCDetailDataArray.push(lstEntityKYCDetailObj);
|
|
}
|
|
if (index === array.length -1)
|
|
{
|
|
resolve(lstEntityKYCDetailDataArray);
|
|
}
|
|
});
|
|
}); // end of entity detail promise
|
|
|
|
|
|
// Create Entity detail
|
|
EntityKYCDetailDataInsert.then(async(lstEntityKYCDetailDataArray)=>
|
|
{
|
|
lstEntityKYCDetailDataArray.forEach(async(element) =>
|
|
{
|
|
await EntityDetails.create(element)
|
|
.then(async(data)=>
|
|
{
|
|
logMsg.info("EntityDetails inserted");
|
|
await ApiCallHelper.EntityChildTable(data.dataValues.id)
|
|
})
|
|
.catch(err=>{ logMsg.info("EntityDetails Failed , Msg :" +err); });
|
|
});
|
|
});
|
|
|
|
|
|
// Application detail promise
|
|
var ApplicantKYCDetailDataInsert = new Promise(async(resolve, reject) =>
|
|
{
|
|
lstApplicantKYCDetailDataArray = [];
|
|
ResData.lstApplicantKYCDetail.forEach(async(value,index,array) => {
|
|
if (value.kycType.trim() == "Individual")
|
|
{
|
|
lstApplicantKYCDetailObj =
|
|
{
|
|
Applicant_id : value.iD_ApplicantKYCDetail,
|
|
losid : LoanDetailData.dataValues.losid ,
|
|
loan_no_id : LoanDetailData.dataValues.id ,
|
|
name : [value.firstName, value.middleName, value.lastName ].filter(Boolean).join(" "), // concat and ignore null
|
|
pan_no : value.panNo,
|
|
entity_type : value.kycType.trim(),
|
|
applicant_type : 'Main Applicant',
|
|
is_data_from_los : 1
|
|
}
|
|
lstApplicantKYCDetailDataArray.push(lstApplicantKYCDetailObj);
|
|
}
|
|
if (index === array.length -1)
|
|
{
|
|
resolve(lstApplicantKYCDetailDataArray);
|
|
}
|
|
});
|
|
}); // end of application detail promise
|
|
// Create Application detail, Address detail , Relationship detail
|
|
ApplicantKYCDetailDataInsert.then(async(lstApplicantKYCDetailDataArray)=>
|
|
{
|
|
await ApplicantsDetails.bulkCreate(lstApplicantKYCDetailDataArray)
|
|
.then(async(ApplicantsDetailsData)=>
|
|
{
|
|
logMsg.info("Applicants inserted");
|
|
ApplicantsDetailsData.forEach(async(ApplicantsDetailsDataElement , index ) =>
|
|
{
|
|
await ApiCallHelper.ApplicantChildTable(ApplicantsDetailsDataElement.name , LoanDetailData.dataValues.id ,ApplicantsDetailsDataElement.id ,ApplicantsDetailsDataElement.losid ,lstApplicantKYCDetailDataArray[index].Applicant_entity_type , ResData , "LFMP" , lstApplicantKYCDetailDataArray[index].Applicant_id )
|
|
});
|
|
}).catch(err=>{ logMsg.info("ApplicantsDetails Failed , Msg :" +err); }); // ApplicantsDetails Create catch block
|
|
});
|
|
}).catch(err=>{ logMsg.info("LoanDetails Failed , Msg :" +err); }) // Loan deatil Create catch block
|
|
} // end of if lstLoanApplication
|
|
else
|
|
{
|
|
logMsg.info("Los api call Failed " +req.query.losid);
|
|
LoanDetail.findAll({raw: true ,where:{ losid:req.query.losid , is_active : 1},
|
|
include: [{ model: EntityDetails , // Set the alias for the relationship
|
|
attributes: [
|
|
[sequelize.col('entity_name'), 'loan_applicant_name'] // Alias for the 'name' column
|
|
] }] }).then(data =>
|
|
{
|
|
if(data.length > 0)
|
|
{
|
|
res.send({'status':200,'message':"success",'data':data});
|
|
}
|
|
else {
|
|
res.send({'status':404,'message':"failed",'data':"No data"});
|
|
}
|
|
})
|
|
.catch(err => {
|
|
res.send({'status':404,
|
|
message: err.message || "Some error occurred while retrieving statements." ,'data': "No data" });
|
|
});
|
|
}
|
|
}
|
|
else
|
|
{
|
|
logMsg.info("Data already inserted " +req.query.losid);
|
|
// Final Response
|
|
LoanDetail.findAll({raw: true ,where:{ losid:req.query.losid , is_active : 1},
|
|
include: [{ model: EntityDetails , // Set the alias for the relationship
|
|
attributes: [
|
|
[sequelize.col('entity_name'), 'loan_applicant_name'] // Alias for the 'name' column
|
|
] }] }).then(data =>
|
|
{
|
|
if(data.length > 0)
|
|
{
|
|
res.send({'status':200,'message':"success",'data':data});
|
|
}
|
|
else {
|
|
res.send({'status':404,'message':"failed",'data':"No data"});
|
|
}
|
|
})
|
|
.catch(err => {
|
|
res.send({'status':404,
|
|
message: err.message || "Some error occurred while retrieving statements." ,'data': "No data" });
|
|
});
|
|
} // Los api call failed
|
|
|
|
|
|
}
|
|
|
|
} // end of try block
|
|
catch(err)
|
|
{
|
|
res.send({'status':404,message: err.message || "Some error occurred while inserting statements." ,'data': "No data" });
|
|
} //end of try catch
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.updateModuleCompletionStatus = async (req, res) =>
|
|
{
|
|
try
|
|
{
|
|
Losid = req.query.losid;
|
|
ModuleName = req.query.module_name;
|
|
|
|
ModuleWiseCompletionStatus.update({status:1},{ where: { losid:Losid , module_name:ModuleName }}).then(async(data) =>
|
|
{
|
|
StatusData = await ModuleWiseCompletionStatus.findAll({ where: { losid:Losid }});
|
|
count = 0;
|
|
for (const value of StatusData) {
|
|
count += value.status;
|
|
}
|
|
console.log(count);
|
|
if(count == 9)
|
|
{
|
|
LoanDetail.update({module_completion_status:1},{ where: { losid:Losid}});
|
|
}
|
|
res.send({'status':200, message: "success" ,'data': "No data" });
|
|
})
|
|
.catch(err => {
|
|
res.send({'status':404,
|
|
message: err.message || "Some error occurred while retrieving statements." ,'data': "No data" });
|
|
});
|
|
|
|
|
|
} // end of try block
|
|
catch(err)
|
|
{
|
|
res.send({'status':404,message: err.message || "Some error occurred while inserting statements." ,'data': "No data" });
|
|
} //end of try catch
|
|
|
|
};
|
|
|