'use strict' const request = require('request'); const db = require('../utils/db_config'); const { getCurrentDateTime } = require("./helper"); const {getAllInvestorId} = require('./getInvestmentSumarry') const crypto = require('crypto'); function generateHMACSHA256(input, key) { const hmac = crypto.createHmac('sha256', key); hmac.update(input, 'utf8'); return hmac.digest('hex'); } function getHashCode(req) { const inputString_GetInvestorDashboard = `||${getCurrentDateTime()}`; const key = "27E6A91CEE689"; const hash_GetInvestorDashboard = generateHMACSHA256(inputString_GetInvestorDashboard, key); return hash_GetInvestorDashboard } const fetchInvestorDetails = ((req, res) => { getAllInvstorData(); res.send("in procress"); }) const getAllInvstorData = async () => { var options = { method: 'POST', url: 'https://supply-integration.liquiloans.com/api/v2/GetAllInvestors', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ mid: "M00201", checksum: getHashCode(), timestamp: getCurrentDateTime(), filter_on: "", filter_data: [] }) }; request(options, function (error, response) { if (error) throw new Error(error); getAllInvstorDataStore(response.body); }); } const getAllInvstorDataStore = async (body) => { try { const records = JSON.parse(body) let item = records?.data let storeData = []; if (item.length) { for (let i = 0; i < item?.length; i++) { let log = await isInvestorPresent(item[i].investor_id); if (log == 0) { let data = {}; data.investor_id = item[i].investor_id, data.name = item[i].name, data.entity_type = item[i].entity_type, data.pan = item[i].pan, data.email = item[i].email, data.contact_number = item[i].contact_number, data.ifa_id = item[i].ifa_id, data.created_at = item[i].created_at, data.rm_name = item[i].rm_name, data.last_updatedAt = new Date storeData.push(data); } } await db.liquiloansInvestors.bulkCreate(storeData); } } catch (err) { console.log(err, "ERROR..........."); } } const isInvestorPresent = async (id) => { try { let query = `select * from dbo.P2P_Liquiloans_Investors where investor_id= '${id}'`; return await db.sequelize.query(query, { type: db.sequelize.QueryTypes.SELECT }); } catch (err) { console.log("isInvestorPresent", err.message) } } module.exports = { fetchInvestorDetails }