Add initial price notifications for products and adjust cron schedule to run every 15 minutes. Implement first run logic to send initial prices.

This commit is contained in:
Norbert Maciaszek
2025-11-20 19:54:29 +01:00
parent 63bae4f805
commit bf60325fac

View File

@@ -12,6 +12,7 @@ function sendMessage(message) {
}); });
} }
let isFirstRun = true;
async function compareAndSave(productsPrice) { async function compareAndSave(productsPrice) {
if (!fs.existsSync("productsPrice.json")) { if (!fs.existsSync("productsPrice.json")) {
fs.writeFileSync("productsPrice.json", "[]"); fs.writeFileSync("productsPrice.json", "[]");
@@ -40,12 +41,15 @@ async function compareAndSave(productsPrice) {
for (const product of diffProducts) { for (const product of diffProducts) {
sendMessage( 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) { if (isFirstRun) {
sendMessage("Brak zmian w cenach"); for (const product of productsPrice) {
sendMessage(`Początkowa cena **${product.name}**: ${product.price}`);
}
isFirstRun = false;
} }
fs.writeFileSync( fs.writeFileSync(
@@ -97,9 +101,14 @@ async function init() {
console.log("Sprawdzone! Aktualne ceny zapisane w productsPrice.json"); console.log("Sprawdzone! Aktualne ceny zapisane w productsPrice.json");
} }
sendMessage("Zaczynam monitoring cen"); sendMessage("Startuję monitoring cen");
const task = cron.schedule("0 6,9,12,14,16,18,21 * * *", init, { const task = cron.schedule("*/15 7-23 * * *", init, {
timezone: "Europe/Warsaw", timezone: "Europe/Warsaw",
}); });
cron.schedule("0 7 * * *", () => {
const date = new Date().toLocaleDateString("pl-PL");
sendMessage(`Zaczynamy monitoring ${date}`);
});
task.execute(); task.execute();