diff --git a/routes/price-history.min.js b/routes/price-history.min.js index bf84bb9..2113f21 100644 --- a/routes/price-history.min.js +++ b/routes/price-history.min.js @@ -1 +1 @@ -const express=require("express"),router=express.Router(),cron=require("node-cron"),axios=require("axios"),fs=require("fs"),path=require("path"),csv=require("csv-parser"),PriceHistory=require("../models/price-history"),CSV_FILE_PATH="/home/production/deployed/utility-api/btc_price_history_full.csv";function readCsvFile(){return new Promise(((e,r)=>{const t=[];fs.createReadStream(CSV_FILE_PATH).pipe(csv()).on("data",(e=>t.push(e))).on("end",(()=>e(t))).on("error",(e=>r(e)))}))}async function fetchBtcPrices(){try{const e=(await axios.get("https://bitpay.com/api/rates")).data,r=e.find((e=>"USD"===e.code&&"US Dollar"===e.name)).rate;return{usd:r,inr:e.find((e=>"INR"===e.code&&"Indian Rupee"===e.name)).rate}}catch(e){return console.error("Error fetching BTC prices from BitPay:",e),null}}async function updateDailyAverage(e){const r=new Date;r.setHours(0,0,0,0);try{const t=await PriceHistory.findOne({date:r,asset:"btc"});if(t){const a=(t.usd*t.count+e.usd)/(t.count+1),c=(t.inr*t.count+e.inr)/(t.count+1);await PriceHistory.updateOne({date:r,asset:"btc"},{$set:{usd:a,inr:c,count:t.count+1}})}else await PriceHistory.create({date:r,asset:"btc",usd:e.usd,inr:e.inr,count:1});console.log("Daily average updated successfully.")}catch(e){console.error("Error updating daily average:",e)}}async function collectAndUpdatePrices(){const e=await fetchBtcPrices();e&&await updateDailyAverage(e)}cron.schedule("0 */4 * * *",(async()=>{console.log("Starting price collection for daily averaging..."),await collectAndUpdatePrices()})),module.exports=router; +const express=require("express"),router=express.Router(),cron=require("node-cron"),axios=require("axios"),fs=require("fs"),path=require("path"),csv=require("csv-parser"),PriceHistory=require("../models/price-history"),CSV_FILE_PATH="/home/production/deployed/utility-api/btc_price_history_full.csv";function readCsvFile(){return new Promise(((e,t)=>{const r=[];fs.createReadStream(CSV_FILE_PATH).pipe(csv()).on("data",(e=>r.push(e))).on("end",(()=>e(r))).on("error",(e=>t(e)))}))}function parseDateString(e){const t=e.split("-"),r=parseInt(t[0]),s=parseInt(t[1])-1,a=parseInt(t[2]);return new Date(r,s,a).setHours(0,0,0,0)}async function fetchBtcPrices(){try{const e=(await axios.get("https://bitpay.com/api/rates")).data,t=e.find((e=>"USD"===e.code&&"US Dollar"===e.name)).rate;return{usd:t,inr:e.find((e=>"INR"===e.code&&"Indian Rupee"===e.name)).rate}}catch(e){return console.error("Error fetching BTC prices from BitPay:",e),null}}async function updateDailyAverage(e){const t=new Date;t.setHours(0,0,0,0);try{const r=await PriceHistory.findOne({date:t,asset:"btc"});if(r){const s=(r.usd*r.count+e.usd)/(r.count+1),a=(r.inr*r.count+e.inr)/(r.count+1);await PriceHistory.updateOne({date:t,asset:"btc"},{$set:{usd:s,inr:a,count:r.count+1}})}else await PriceHistory.create({date:t,asset:"btc",usd:e.usd,inr:e.inr,count:1});console.log("Daily average updated successfully.")}catch(e){console.error("Error updating daily average:",e)}}async function collectAndUpdatePrices(){const e=await fetchBtcPrices();e&&await updateDailyAverage(e)}router.get("/",(async(e,t)=>{console.log("price-history");try{let{from:r,to:s,on:a,limit:n=100,asset:i="btc",currency:o,sort:c,dates:d}=e.query;const u={asset:i};if(r&&s&&(r=parseDateString(r),s=parseDateString(s),r>s)){const e=r;r=s,s=e}if(r&&(u.date={$gte:r}),s&&(u.date={...u.date,$lte:s}),d){const e=d.split(",").map((e=>parseDateString(e.trim())));u.date={$in:e}}if(a){const e=parseDateString(a);u.date={$eq:e}}if(o&&(u[o]={$exists:!0}),c){if(!["asc","desc","ascending","descending","1","-1"].includes(c))return t.status(400).json({error:"Invalid sort. Valid values are asc | desc | ascending | descending | 1 | -1"});c={date:"asc"===c||"ascending"===c||"1"===c?1:-1}}else c={date:-1};const l={_id:0,__v:0,asset:0};"inr"===o&&(l.usd=0),"usd"===o&&(l.inr=0);const p=await PriceHistory.find(u,l).sort(c).limit("all"===n?0:parseInt(n)).lean();if(!p||0===p.length)return t.status(404).json({message:"No data found"});t.json(p)}catch(e){console.log(e),t.status(500).json({error:e})}})),cron.schedule("0 */4 * * *",(async()=>{console.log("Starting price collection for daily averaging..."),await collectAndUpdatePrices()})),module.exports=router;