'use strict' const request = require("request"); const db = require('../utils/db_config'); const crypto = require('crypto'); const {getReqAndInvId} = require('./getTransactionById') const { getCurrentDateTime } = require("./helper"); function generateHMACSHA256(input, key) { const hmac = crypto.createHmac('sha256', key); hmac.update(input, 'utf8'); // Ensure input encoding matches C# (UTF-8) return hmac.digest('hex'); } function getHashCode(req) { const inputString_GetInvestorDashboard = `${req}||${getCurrentDateTime()}`; const key = "27E6A91CEE689"; const hash_GetInvestorDashboard = generateHMACSHA256(inputString_GetInvestorDashboard, key); return hash_GetInvestorDashboard } const fetchAllInvestorId = async (sendId) => { let data = JSON.parse(sendId) for (let i = 0; i < data.length; i++) { await createLiquiloanInvestor(data[i].investor_id,i) } } const getAllInvestorId = async () => { let allInvestorId = await db.liquiloansInvestors.findAll({ attributes: ['investor_id'] }); let sendId = JSON.stringify(allInvestorId, null, 2) fetchAllInvestorId(sendId); }; const createLiquiloanInvestor = (investor_id,index) => { var options = { method: 'POST', url: 'https://supply-integration.liquiloans.com/api/v2/GetInvestmentSummary', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ "investor_id": investor_id, "timestamp": getCurrentDateTime(), "checksum": getHashCode(investor_id), "mid": "M00201" }) }; request(options, function (error, response,body) { if (error) throw new Error(error); const data = JSON.parse(body); let pastData = data?.data.past_investments; if (pastData) { getUrlAndStore(pastData); } let currentData = data?.data.current_investments; if (currentData) { getUrlAndStore(currentData); } console.log(index,"investor_id-->",investor_id,"currentData-->",currentData.length,"pastData-->",pastData.length) }); } const getUrlAndStore = async (callsData) => { try { let summary = [] for (let i = 0; i < callsData.length; i++) { let checkTrnId = await isRequestIdPresent(callsData[i].request_id); if (checkTrnId == 0) { let record = {}; record.transaction_id = callsData[i].transaction_id, record.investor_id = callsData[i].investor_id, record.scheme_id = callsData[i].scheme_id, record.investment_roi = callsData[i].investment_roi, record.return_type = callsData[i].return_type, record.payout_type = callsData[i].payout_type, record.lockin_tenure = callsData[i].lockin_tenure, record.lockin_break = callsData[i].lockin_break, record.last_withdrawal_at = callsData[i].last_withdrawal_at, record.transaction_sub_type = callsData[i].transaction_sub_type, record.investment_status = callsData[i].investment_status, record.parent_investment_id = callsData[i].parent_investment_id, record.master_parent_investment_id = callsData[i].master_parent_investment_id, record.quality_name = callsData[i].quality_name, record.name = callsData[i].name, record.transaction_date = callsData[i].transaction_date, record.invested_amount = callsData[i].invested_amount, record.scheme_name = callsData[i].scheme_name, record.lockin_type = callsData[i].lockin_type, record.display_scheme = callsData[i].display_scheme, record.scheme_details = callsData[i].scheme_details, record.lockin_end_date = callsData[i].lockin_end_date, record.redeemed_principal = callsData[i].redeemed_principal, record.redeemed_erest = callsData[i].redeemed_erest, record.redeemed_erest_with_request = callsData[i].redeemed_erest_with_request, record.total_redemption = callsData[i].total_redemption, record.net_principal_investment = callsData[i].net_principal_investment, record.interest_amount = callsData[i].interest_amount, record.accrued_value = callsData[i].accrued_value, record.withdrawable_balance = callsData[i].withdrawable_balance, record.scheme_closed_date = callsData[i].scheme_closed_date, record.request_id = callsData[i].request_id, record.source = callsData[i].source, record.created_at = new Date() record.last_updatedAt = new Date() summary.push(record); } } if(summary.length){ await db.investorSummary.bulkCreate(summary); } } catch (err) { console.log(err, "ERROR........."); } } // check is transaction id is present const isRequestIdPresent = async (id) => { try { let query = `select * from dbo.P2P_Liquiloans_Inv_Summarys where request_id= '${id}'`; return await db.sequelize.query(query, { type: db.sequelize.QueryTypes.SELECT }); } catch (err) { console.log("isIransactionId", err.message) } } module.exports = { getAllInvestorId }