updating price update logic

This commit is contained in:
sairaj mote 2024-01-12 01:14:35 +05:30
parent df8cb9c189
commit 794ba968c6
3 changed files with 6 additions and 13 deletions

View File

@ -168,22 +168,15 @@ function loadHistoricToDb() {
inr: parseFloat(parseFloat(close2).toFixed(2)), inr: parseFloat(parseFloat(close2).toFixed(2)),
}); });
} }
// update many
await PriceHistory.deleteMany({ asset: 'btc' });
await PriceHistory.insertMany(priceHistoryData); await PriceHistory.insertMany(priceHistoryData);
}) })
.catch((err) => { .catch((err) => {
console.log(err); console.log(err);
}) })
} }
// run loadHistoricToDb() only once when the server starts and no data is present in the database
PriceHistory.countDocuments()
.then((count) => {
if (count === 0) {
loadHistoricToDb(); loadHistoricToDb();
}
})
.catch((err) => {
console.log(err);
})
app.get("/price-history", async (req, res) => { app.get("/price-history", async (req, res) => {
try { try {
@ -240,7 +233,7 @@ app.post("/price-history", async (req, res) => {
app.listen(PORT, HOST, () => { app.listen(PORT, HOST, () => {
console.log(`Listening on ${HOST}:${PORT}`); console.log(`Listening on ${HOST}:${PORT}`);
}); });
cron.schedule('0 12 * * *', async () => { cron.schedule('0 */4 * * *', async () => {
try { try {
// will return a csv file // will return a csv file
const [usd, inr] = await Promise.all([ const [usd, inr] = await Promise.all([

2
index.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -2,7 +2,7 @@ GET http://localhost:3000/
### ###
GET http://localhost:3000/price-history?limit=10 GET http://localhost:3000/price-history?from=2024-01-12
### ###
POST http://localhost:3000/price-history POST http://localhost:3000/price-history