diff --git a/app.js b/app.js index 835ef93..e225c13 100644 --- a/app.js +++ b/app.js @@ -37,7 +37,9 @@ app.use('/script', scriptRouter); /* SCHEDULER */ -//scheduler.routineFetchInvestors(); +scheduler.routineFetchAllInvestors(); +scheduler.routineGetInvestmentSummary(); +scheduler.routineGetTransactionById(); // catch 404 and forward to error handler diff --git a/controller/Errorlog/index.js b/controller/Errorlog/index.js new file mode 100644 index 0000000..22d62c8 --- /dev/null +++ b/controller/Errorlog/index.js @@ -0,0 +1,13 @@ +const db = require("../../utils/db_config"); +const { getCurrentDateTime } = require("../helper"); + +function errorLogStatus(error = null, funName = null) { + let query = `INSERT INTO providentialadvisory.dbo.User_log +(client_code, request, status, response, created_date, created_by, api_name, Open_Close_Status, IPAddress, DeviceId, [Source], Header) +VALUES('23232323', '${error}', 0, '', '${getCurrentDateTime()}', '', '${funName}', 0, '', '', '', '')`; + return db.sequelize.query(query, { type: db.sequelize.QueryTypes.INSERT }); +} + +module.exports = { + errorLogStatus +} diff --git a/controller/getAllInvestor.js b/controller/getAllInvestor/index.js similarity index 73% rename from controller/getAllInvestor.js rename to controller/getAllInvestor/index.js index 94f7171..41f1bfa 100644 --- a/controller/getAllInvestor.js +++ b/controller/getAllInvestor/index.js @@ -1,10 +1,13 @@ 'use strict' const request = require('request'); -const db = require('../utils/db_config'); -const { getCurrentDateTime } = require("./helper"); -const {getAllInvestorId} = require('./getInvestmentSumarry') +const db = require('../../utils/db_config'); +const { getCurrentDateTime } = require("../helper"); +const { getAllInvestorId } = require('../getInvestmentSummary') const crypto = require('crypto'); +const { HASH_KEY } = require('../../utils/consts'); +const { getInvestorIdQuery } = require('./query'); +const { errorLogStatus } = require('../Errorlog'); function generateHMACSHA256(input, key) { @@ -15,7 +18,7 @@ function generateHMACSHA256(input, key) { function getHashCode(req) { const inputString_GetInvestorDashboard = `||${getCurrentDateTime()}`; - const key = "27E6A91CEE689"; + const key = HASH_KEY; const hash_GetInvestorDashboard = generateHMACSHA256(inputString_GetInvestorDashboard, key); return hash_GetInvestorDashboard @@ -28,7 +31,7 @@ const fetchInvestorDetails = ((req, res) => { }) const getAllInvstorData = async () => { - + var options = { method: 'POST', url: 'https://supply-integration.liquiloans.com/api/v2/GetAllInvestors', @@ -51,14 +54,13 @@ const getAllInvstorData = async () => { 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); + let log = await getInvestorIdQuery(item[i].investor_id); if (log == 0) { let data = {}; data.investor_id = item[i].investor_id, @@ -72,26 +74,26 @@ const getAllInvstorDataStore = async (body) => { data.rm_name = item[i].rm_name, data.last_updatedAt = new Date - storeData.push(data); + storeData.push(data); } } await db.liquiloansInvestors.bulkCreate(storeData); } - + } catch (err) { - console.log(err, "ERROR..........."); + errorLogStatus(err, "nodeJs_liquiloan_getAllInvstorDataStore") } } -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 }); +// const isInvestorPresent = async (id) => { +// try { +// await getInvestorIdQuery(id) - } catch (err) { - console.log("isInvestorPresent", err.message) - } -} +// } catch (err) { +// // console.log("isInvestorPresent", err.message) +// errorLogStatus(err, "nodeJs_liquiloan_isInvestorPresent") +// } +// } module.exports = { diff --git a/controller/getAllInvestor/query.js b/controller/getAllInvestor/query.js new file mode 100644 index 0000000..5114c87 --- /dev/null +++ b/controller/getAllInvestor/query.js @@ -0,0 +1,16 @@ +const db = require("../../utils/db_config"); +const { errorLogStatus } = require("../Errorlog"); +async function getInvestorIdQuery(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 (error) { + // console.log("isInvestorPresent", err.message) + errorLogStatus(error, "nodeJs_liquiloan_isInvestorPresent") + } + +} + +module.exports = { + getInvestorIdQuery +} \ No newline at end of file diff --git a/controller/getHashvalue.js b/controller/getHashvalue.js index b5c9989..5b17e2a 100644 --- a/controller/getHashvalue.js +++ b/controller/getHashvalue.js @@ -1,4 +1,5 @@ const crypto = require('crypto'); +const { HASH_KEY } = require('../utils/consts'); function generateHMACSHA256(input, key) { const hmac = crypto.createHmac('sha256', key); @@ -10,7 +11,7 @@ function getHashCode(type) { const inputString = "||2023-11-22 14:57:38"; const inputString_GetInvestorDashboard = "368907||2023-11-22 14:57:38"; const inputString_GetTransactionById = "5151974||368907||2023-11-22 14:57:38"; - const key = "27E6A91CEE689"; + const key = HASH_KEY; const hash = generateHMACSHA256(inputString, key); const hash_GetInvestorDashboard = generateHMACSHA256(inputString_GetInvestorDashboard, key); diff --git a/controller/getInvestmentSumarry.js b/controller/getInvestmentSummary/index.js similarity index 75% rename from controller/getInvestmentSumarry.js rename to controller/getInvestmentSummary/index.js index 10ab86d..47b359e 100644 --- a/controller/getInvestmentSumarry.js +++ b/controller/getInvestmentSummary/index.js @@ -1,10 +1,13 @@ 'use strict' const request = require("request"); -const db = require('../utils/db_config'); +const db = require('../../utils/db_config'); const crypto = require('crypto'); -const {getReqAndInvId} = require('./getTransactionById') -const { getCurrentDateTime } = require("./helper"); +const { getReqAndInvId } = require('../getTransactionById') +const { getCurrentDateTime } = require("../helper"); +const { HASH_KEY } = require("../../utils/consts"); +const { getInvSummaryIdQuery } = require("./query"); +const { errorLogStatus } = require("../Errorlog"); function generateHMACSHA256(input, key) { const hmac = crypto.createHmac('sha256', key); @@ -14,7 +17,7 @@ function generateHMACSHA256(input, key) { function getHashCode(req) { const inputString_GetInvestorDashboard = `${req}||${getCurrentDateTime()}`; - const key = "27E6A91CEE689"; + const key = HASH_KEY; const hash_GetInvestorDashboard = generateHMACSHA256(inputString_GetInvestorDashboard, key); return hash_GetInvestorDashboard @@ -24,7 +27,7 @@ function getHashCode(req) { const fetchAllInvestorId = async (sendId) => { let data = JSON.parse(sendId) for (let i = 0; i < data.length; i++) { - await createLiquiloanInvestor(data[i].investor_id,i) + await createLiquiloanInvestor(data[i].investor_id, i) } } @@ -37,7 +40,7 @@ const getAllInvestorId = async () => { }; -const createLiquiloanInvestor = (investor_id,index) => { +const createLiquiloanInvestor = (investor_id, index) => { var options = { method: 'POST', @@ -53,7 +56,7 @@ const createLiquiloanInvestor = (investor_id,index) => { }) }; - request(options, function (error, response,body) { + request(options, function (error, response, body) { if (error) throw new Error(error); const data = JSON.parse(body); let pastData = data?.data.past_investments; @@ -64,17 +67,15 @@ const createLiquiloanInvestor = (investor_id,index) => { if (currentData) { getUrlAndStore(currentData); } - console.log(index,"investor_id-->",investor_id,"currentData-->",currentData.length,"pastData-->",pastData.length) + 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); + let checkTrnId = await getInvSummaryIdQuery(callsData[i].request_id); if (checkTrnId == 0) { let record = {}; record.transaction_id = callsData[i].transaction_id, @@ -110,32 +111,40 @@ const getUrlAndStore = async (callsData) => { 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() + record.created_at = new Date().toString() + record.last_updatedAt = new Date().toString() summary.push(record); + } else { + let record = { + ...callsData[i], + last_updatedAt: new Date().toString() + } + await db.transactionById.update(record, { + where: { + transaction_id: record.transaction_id + } + }); } } - if(summary.length){ + if (summary.length) { await db.investorSummary.bulkCreate(summary); } - + } catch (err) { - console.log(err, "ERROR........."); + errorLogStatus(err, "nodeJs_liquiloan_getUrlAndStore") } } // 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) - } -} +// const isRequestIdPresent = async (id) => { +// try { +// getInvSummaryIdQuery(id) +// } catch (err) { +// errorLogStatus(err, "nodeJs_liquiloan_isRequestIdPresent") +// } +// } module.exports = { getAllInvestorId diff --git a/controller/getInvestmentSummary/query.js b/controller/getInvestmentSummary/query.js new file mode 100644 index 0000000..9e15ec2 --- /dev/null +++ b/controller/getInvestmentSummary/query.js @@ -0,0 +1,16 @@ +const db = require("../../utils/db_config"); +const { errorLogStatus } = require("../Errorlog"); + +async function getInvSummaryIdQuery(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 (error) { + errorLogStatus(error, "nodeJs_liquiloan_isRequestIdPresent") + } + +} + +module.exports = { + getInvSummaryIdQuery +} \ No newline at end of file diff --git a/controller/getTransactionById.js b/controller/getTransactionById/index.js similarity index 83% rename from controller/getTransactionById.js rename to controller/getTransactionById/index.js index 6cf092c..8561b92 100644 --- a/controller/getTransactionById.js +++ b/controller/getTransactionById/index.js @@ -1,7 +1,10 @@ const request = require('request'); -const db = require('../utils/db_config'); -const { getCurrentDateTime } = require("./helper"); +const db = require('../../utils/db_config'); +const { getCurrentDateTime } = require("../helper"); const crypto = require('crypto'); +const { HASH_KEY } = require('../../utils/consts'); +const { getInvTransIdQuery } = require('./query'); +const { errorLogStatus } = require('../Errorlog'); function generateHMACSHA256(input, key) { @@ -12,7 +15,7 @@ function generateHMACSHA256(input, key) { function getHashCode(investor_id, request_id) { const inputString_GetInvestorDashboard = `${request_id}||${investor_id}||${getCurrentDateTime()}`; - const key = "27E6A91CEE689"; + const key = HASH_KEY; const hash_GetInvestorDashboard = generateHMACSHA256(inputString_GetInvestorDashboard, key); return hash_GetInvestorDashboard @@ -30,7 +33,6 @@ const getReqAndInvId = async () => { // Send Request_Id and Investor_Id to the getTransactionById fun const fetchReqAndInvId = async (sendId) => { let data = JSON.parse(sendId) - console.log("data",data.length) for (let i = 0; i < data.length; i++) { let checkInvestorId = data[i].investor_id; let checkRequestId = data[i].request_id; @@ -68,7 +70,7 @@ const getTransactionById = (checkInvestorId, checkRequestId) => { } }); } catch { - console.log(error, "ERROR...____"); + errorLogStatus(err, "nodeJs_liquiloan_getTransactionById") } } @@ -102,9 +104,9 @@ const fetchTransactionId = async (data) => { item.transaction_date = trnId.transaction_date, item.ext_transaction_date = trnId.ext_transaction_date, item.source_payment = trnId.source_payment, - item.last_updatedAt = new Date().toString() + item.last_updatedAt = getCurrentDateTime() - let isRecordPresent = await handleRecordPresent(item); + let isRecordPresent = await getInvTransIdQuery(item); if (isRecordPresent.length) { await db.transactionById.update(item, { where: { @@ -116,20 +118,18 @@ const fetchTransactionId = async (data) => { } console.log("done--------->") } catch (err) { - console.log(err, "ERROR........."); + errorLogStatus(err, "nodeJs_liquiloan_fetchTransactionId") } } -const handleRecordPresent = async (item) => { - let id = item.request_id; - try { - let query = `select * from dbo.P2P_Liquiloans_Inv_Transactions where request_id= '${id}'`; - return await db.sequelize.query(query, { type: db.sequelize.QueryTypes.SELECT }); - - } catch (err) { - console.log("isInvestorPresent", err.message) - } -} +// const handleRecordPresent = async (item) => { +// let id = item.request_id; +// try { +// getInvTransIdQuery(id) +// } catch (err) { +// errorLogStatus(err, "nodeJs_liquiloan_handleRecordPresent") +// } +// } module.exports = { diff --git a/controller/getTransactionById/query.js b/controller/getTransactionById/query.js new file mode 100644 index 0000000..6a77e66 --- /dev/null +++ b/controller/getTransactionById/query.js @@ -0,0 +1,16 @@ +const db = require("../../utils/db_config"); +const { errorLogStatus } = require("../Errorlog"); + +async function getInvTransIdQuery(id) { + try { + let query = `select * from dbo.P2P_Liquiloans_Inv_Transactions where request_id= '${id}'`; + return await db.sequelize.query(query, { type: db.sequelize.QueryTypes.SELECT }); + } catch (error) { + errorLogStatus(error, "nodeJs_liquiloan_handleRecordPresent") + } + +} + +module.exports = { + getInvTransIdQuery +} \ No newline at end of file diff --git a/controller/helper.js b/controller/helper.js index edb36aa..4737bc0 100644 --- a/controller/helper.js +++ b/controller/helper.js @@ -1,3 +1,5 @@ +const db = require("../utils/db_config"); + function getCurrentDateTime() { var date = new Date(); var dateStr = @@ -10,7 +12,6 @@ function getCurrentDateTime() { return dateStr } - module.exports = { - getCurrentDateTime + getCurrentDateTime, } diff --git a/routes/script.js b/routes/script.js index b8a8b1d..555e819 100644 --- a/routes/script.js +++ b/routes/script.js @@ -1,22 +1,20 @@ const express = require('express'); const { route } = require("express/lib/application"); -const { getAllInvestorId } = require('../controller/getInvestmentSumarry'); +const { getAllInvestorId } = require('../controller/getInvestmentSummary'); const { fetchInvestorDetails } = require('../controller/getAllInvestor'); const { getReqAndInvId } = require('../controller/getTransactionById'); const router = express.Router(); -router.get("/api/all_investor", (req, res) => { +router.get("/allInvestor", (req, res) => { fetchInvestorDetails() }) -router.get("/api/v1", (req, res) => { +router.get("/getInvestmentSummary", (req, res) => { getAllInvestorId() }) - - -router.get("/api/transaction_id", (req, res) => { +router.get("/getTransactionId", (req, res) => { getReqAndInvId() }) diff --git a/utils/consts.js b/utils/consts.js index 183e67f..c87d9d1 100644 --- a/utils/consts.js +++ b/utils/consts.js @@ -12,16 +12,16 @@ const ERROR_MESSAGE = "Oops,something went wrong"; const BUCKET_NAME_NIVESH = "nivesh-call-logs/recording"; -const S3_BUCKET_NIVESH_CALL_URL= "https://nivesh-call-logs.s3.ap-south-1.amazonaws.com/recording/"; +const S3_BUCKET_NIVESH_CALL_URL = "https://nivesh-call-logs.s3.ap-south-1.amazonaws.com/recording/"; const BUCKET_NAME_SOA_REPORTS = "client-soa-reports/ELSS"; -const S3_BUCKET_SOA_REPORTS_URL= "https://client-soa-reports.s3.ap-south-1.amazonaws.com/ELSS/"; +const S3_BUCKET_SOA_REPORTS_URL = "https://client-soa-reports.s3.ap-south-1.amazonaws.com/ELSS/"; const AWS_ACCESS_KEY_ID_TEST = "AKIA2C5JCOWXUXB4T3MV"; const AWS_SECRET_KEY_TEST = "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"; const EXOTEL_AUTHTOKEN = "Basic NWE0NjY1ZDU0MGViNzk5NjQxMGQ1NWJhYzllZmI5MTVhOTgyYWU2MmRmYWFkZmM0OmU0YTgzNjEwNjI1NWM5ZDllY2QxMDNiOWQwZGFmYWQ3ZTA1YjNjNTU1NDJhZjU3NQ==" - +const HASH_KEY = "27E6A91CEE689"; s3://kwikfoods/2020/ module.exports = { ERROR_500, @@ -39,5 +39,6 @@ module.exports = { S3_BUCKET_SOA_REPORTS_URL, AWS_ACCESS_KEY_ID_TEST, AWS_SECRET_KEY_TEST, - EXOTEL_AUTHTOKEN + EXOTEL_AUTHTOKEN, + HASH_KEY }; diff --git a/utils/scheduler.js b/utils/scheduler.js index 0fe5831..90180d2 100644 --- a/utils/scheduler.js +++ b/utils/scheduler.js @@ -4,14 +4,28 @@ const cron = require('node-cron'); -const routineFetchInvestors = () => { - cron.schedule(process.env.SCHEDULER_RULE_EXOTEL_CALL, () => { +const routineFetchAllInvestors = () => { + cron.schedule(process.env.SCHEDULER_RULE_FETCH_ALL_INVESTOR, () => { + console.log("running scheduler"); + // recordingController.callExotelRecording(); + }) +}; +const routineGetInvestmentSummary = () => { + cron.schedule(process.env.SCHEDULER_RULE_INVESTMENT_SUMMARY, () => { + console.log("running scheduler"); + // recordingController.callExotelRecording(); + }) +}; +const routineGetTransactionById = () => { + cron.schedule(process.env.SCHEDULER_RULE_TRANS_ID, () => { console.log("running scheduler"); // recordingController.callExotelRecording(); }) }; module.exports = { - routineFetchInvestors, + routineFetchAllInvestors, + routineGetInvestmentSummary, + routineGetTransactionById };