-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (54 loc) · 2.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Load preffered configuration from .env file
require('dotenv').config();
// Load the required modules
const { checkKeychains } = require("./utils/keychains.js");
// Get arguments from cli
const args = process.argv.slice(2);
// If arguments are given, check the keychains and return the results
if(args.length != 0) {
console.log('Anahtarlıklar aranıyor...');
checkKeychains(args).then(results => console.table(results));
return;
}
// If no arguments are given, start the server and check the watchlist periodically
const { getSubscribers } = require('./server/helpers/databaseHelper.js');
const { initializeExpressServer } = require("./server/server.js");
const { initializeWebpush, createNotification, sendNotification } = require("./server/helpers/webpushHelper.js");
console.log("Herhangi bir argüman verilmedi, program izleme listesini kontrol etme modunda çalışıyor.");
initializeExpressServer();
initializeWebpush();
// Schedule the task
const cron = require('node-cron');
cron.schedule(process.env.CRON_INTERVAL || "0 */4 * * *", async () => {
// Get subscribers and their watchlists
const subscribers = await getSubscribers();
const everySubscriberWatchlist = subscribers.map(subscriber => subscriber.watchlist.split(",")).flat();
if(everySubscriberWatchlist.length === 0) return console.log("İzleme listesi boş.");
console.log("İzleme listesi: ", everySubscriberWatchlist);
console.log('Anahtarlıklar aranıyor...');
var results = await checkKeychains(everySubscriberWatchlist);
// Filter the results to only include the ones that are in stock
results = results.filter(result => result["Stok Durumu"] == "Stokta Var");
// Return if no products are in stock
if(results.length == 0) return console.log("Stokta hiçbir ürün yok.");
console.log('İzlenen ürünlerden biri veya daha fazlası stokta, bildirim gönderiliyor...');
// Iterate over the subscribers and send a notification
subscribers.forEach(subscriber => {
// Return if the subscriber's watchlist does not include any of the products in stock
if(!results.some(result=>subscriber.watchlist.includes(result["Kod"]))) return;
watchedProducts = results.filter(result=>subscriber.watchlist.includes(result["Kod"])).map(result=>result["İsim"]);
try {
sendNotification(
JSON.parse(subscriber.subscription),
createNotification(
"İzleme listenizdeki bir ya da daha fazla anahtarlık stokta!",
`${watchedProducts.join(",\n")} stokta!`
)
);
} catch (err) {
console.error("Bildirim gönderilemedi.", err);
}
});
console.table(results);
});
return;