From bf60325fac535f8b463556d39c2fe8cc84fc4349 Mon Sep 17 00:00:00 2001 From: Norbert Maciaszek Date: Thu, 20 Nov 2025 19:54:29 +0100 Subject: [PATCH] Add initial price notifications for products and adjust cron schedule to run every 15 minutes. Implement first run logic to send initial prices. --- index.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 7695bd3..e26ff5b 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,7 @@ function sendMessage(message) { }); } +let isFirstRun = true; async function compareAndSave(productsPrice) { if (!fs.existsSync("productsPrice.json")) { fs.writeFileSync("productsPrice.json", "[]"); @@ -40,12 +41,15 @@ async function compareAndSave(productsPrice) { for (const product of diffProducts) { sendMessage( - `Zmiana ceny **${product.name}**:\nCena: ${product.oldPrice} -> ${product.newPrice}\nLink: ${product.link}` + `Zmiana ceny **${product.name}**: ${product.oldPrice} -> ${product.newPrice}\nLink: ${product.link}` ); } - if (diffProducts.length === 0) { - sendMessage("Brak zmian w cenach"); + if (isFirstRun) { + for (const product of productsPrice) { + sendMessage(`Początkowa cena **${product.name}**: ${product.price}`); + } + isFirstRun = false; } fs.writeFileSync( @@ -97,9 +101,14 @@ async function init() { console.log("Sprawdzone! Aktualne ceny zapisane w productsPrice.json"); } -sendMessage("Zaczynam monitoring cen"); -const task = cron.schedule("0 6,9,12,14,16,18,21 * * *", init, { +sendMessage("Startuję monitoring cen"); +const task = cron.schedule("*/15 7-23 * * *", init, { timezone: "Europe/Warsaw", }); +cron.schedule("0 7 * * *", () => { + const date = new Date().toLocaleDateString("pl-PL"); + sendMessage(`Zaczynamy monitoring ${date}`); +}); + task.execute();