From 59dfff60ce37f8d59b2f09d21d3ae61a15c7390f Mon Sep 17 00:00:00 2001 From: sairaj mote Date: Sat, 13 Jan 2024 03:15:31 +0530 Subject: [PATCH] better from - to date handling --- routes/price-history.js | 9 +++++++++ routes/price-history.min.js | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/routes/price-history.js b/routes/price-history.js index ffe8ae7..3785d37 100644 --- a/routes/price-history.js +++ b/routes/price-history.js @@ -41,6 +41,15 @@ router.get("/", async (req, res) => { const searchParams = { asset } + if (from && to) { + from = new Date(from).getTime(); + to = new Date(to).getTime(); + if (from > to) { + const temp = from; + from = to; + to = temp; + } + } if (from) { searchParams.date = { $gte: new Date(from).getTime() }; } diff --git a/routes/price-history.min.js b/routes/price-history.min.js index ee9f70b..8cb895a 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"),PriceHistory=require("../models/price-history");function loadHistoricToDb(){const now=parseInt(Date.now()/1e3);Promise.all([fetch(`https://query1.finance.yahoo.com/v7/finance/download/BTC-USD?period1=1410912000&period2=${now}&interval=1d&events=history&includeAdjustedClose=true`).then((res=>res.text())),fetch(`https://query1.finance.yahoo.com/v7/finance/download/BTC-INR?period1=1410912000&period2=${now}&interval=1d&events=history&includeAdjustedClose=true`).then((res=>res.text()))]).then((async([usd,inr])=>{const usdData=usd.split("\n").slice(1),inrData=inr.split("\n").slice(1),priceHistoryData=[];for(let i=0;i{console.log(err)}))}loadHistoricToDb(),router.get("/",(async(req,res)=>{console.log("price-history");try{let{from:from,to:to,on:on,limit:limit=100,asset:asset="btc",currency:currency,sort:sort,dates:dates}=req.query;const searchParams={asset:asset};if(from&&(searchParams.date={$gte:new Date(from).getTime()}),to&&(searchParams.date={...searchParams.date,$lte:new Date(to).getTime()}),dates){const datesArray=dates.split(",");searchParams.date={$in:datesArray.map((date=>new Date(date).getTime()))}}if(on&&(searchParams.date={$eq:new Date(on).getTime()}),currency&&(searchParams[currency]={$exists:!0}),sort){if(!["asc","desc","ascending","descending","1","-1"].includes(sort))return res.status(400).json({error:"Invalid sort. Valid values are asc | desc | ascending | descending | 1 | -1"});sort={date:"asc"===sort||"ascending"===sort||"1"===sort?1:-1}}else sort={date:-1};const dataFormat={_id:0,__v:0,asset:0};"inr"===currency&&(dataFormat.usd=0),"usd"===currency&&(dataFormat.inr=0);const priceHistory=await PriceHistory.find(searchParams,dataFormat).sort(sort).limit("all"===limit?0:parseInt(limit));res.json(priceHistory)}catch(err){console.log(err),res.status(500).json({error:err})}})),cron.schedule("0 */4 * * *",(async()=>{try{const[usd,inr]=await Promise.all([fetch("https://query1.finance.yahoo.com/v7/finance/download/BTC-USD").then((res=>res.text())),fetch("https://query1.finance.yahoo.com/v7/finance/download/BTC-INR").then((res=>res.text()))]),usdData=usd.split("\n").slice(1),inrData=inr.split("\n").slice(1);for(let i=0;ires.text())),fetch(`https://query1.finance.yahoo.com/v7/finance/download/BTC-INR?period1=1410912000&period2=${now}&interval=1d&events=history&includeAdjustedClose=true`).then((res=>res.text()))]).then((async([usd,inr])=>{const usdData=usd.split("\n").slice(1),inrData=inr.split("\n").slice(1),priceHistoryData=[];for(let i=0;i{console.log(err)}))}loadHistoricToDb(),router.get("/",(async(req,res)=>{console.log("price-history");try{let{from:from,to:to,on:on,limit:limit=100,asset:asset="btc",currency:currency,sort:sort,dates:dates}=req.query;const searchParams={asset:asset};if(from&&to&&(from=new Date(from).getTime(),to=new Date(to).getTime(),from>to)){const temp=from;from=to,to=temp}if(from&&(searchParams.date={$gte:new Date(from).getTime()}),to&&(searchParams.date={...searchParams.date,$lte:new Date(to).getTime()}),dates){const datesArray=dates.split(",");searchParams.date={$in:datesArray.map((date=>new Date(date).getTime()))}}if(on&&(searchParams.date={$eq:new Date(on).getTime()}),currency&&(searchParams[currency]={$exists:!0}),sort){if(!["asc","desc","ascending","descending","1","-1"].includes(sort))return res.status(400).json({error:"Invalid sort. Valid values are asc | desc | ascending | descending | 1 | -1"});sort={date:"asc"===sort||"ascending"===sort||"1"===sort?1:-1}}else sort={date:-1};const dataFormat={_id:0,__v:0,asset:0};"inr"===currency&&(dataFormat.usd=0),"usd"===currency&&(dataFormat.inr=0);const priceHistory=await PriceHistory.find(searchParams,dataFormat).sort(sort).limit("all"===limit?0:parseInt(limit));res.json(priceHistory)}catch(err){console.log(err),res.status(500).json({error:err})}})),cron.schedule("0 */4 * * *",(async()=>{try{const[usd,inr]=await Promise.all([fetch("https://query1.finance.yahoo.com/v7/finance/download/BTC-USD").then((res=>res.text())),fetch("https://query1.finance.yahoo.com/v7/finance/download/BTC-INR").then((res=>res.text()))]),usdData=usd.split("\n").slice(1),inrData=inr.split("\n").slice(1);for(let i=0;i