-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
91 lines (83 loc) · 1.83 KB
/
handler.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"use strict";
const settings = require("./config/settings");
const axios = require("axios");
const cheerio = require("cheerio");
const sites = require("./config/sites");
const smsFormat = require("./config/smsFormat");
const priceHandler = require("./priceHandler");
const sendSms = require("./sendSms");
const twilio = require("twilio");
class Scheduler {
constructor({
envs,
axiosInstance,
cheerioSvc,
sites,
sendSmsSvc,
twilioSvc,
smsFormatSvc,
}) {
this.envs = envs;
this.axiosInstance = axiosInstance;
this.cheerioSvc = cheerioSvc;
this.sites = sites;
this.sendSmsSvc = sendSmsSvc;
this.twilioSvc = twilioSvc;
this.smsFormatSvc = smsFormatSvc;
}
async main(event) {
try {
const prices = await priceHandler(
this.sites,
this.axiosInstance,
this.cheerioSvc
);
const storeLowPrice = prices.find(
({ price }) => price > 0 && price <= this.envs.min_price
);
if (!!storeLowPrice) {
await this.sendSmsSvc(
this.twilioSvc,
this.smsFormatSvc({
...storeLowPrice,
...this.envs,
})
);
}
return {
statusCode: 200,
body: `${
!!storeLowPrice
? "Preco encontrado em " +
storeLowPrice.store +
" " +
storeLowPrice.price
: "Nao foram encontrados resultados para o preco minimo de " +
this.envs.min_price
}`,
};
} catch (e) {
console.log("ERROR!", e);
return {
statusCode: 500,
body: "Internal Error " + e.stack,
};
}
}
}
const axiosInstance = axios.create();
const cheerioSvc = cheerio;
const twilioSvc = twilio(
settings.twilio_account_id,
settings.twilio_auth_token
);
const scheduler = new Scheduler({
envs: settings,
axiosInstance,
cheerioSvc,
sites,
sendSmsSvc: sendSms,
twilioSvc,
smsFormatSvc: smsFormat,
});
module.exports.scheduler = scheduler.main.bind(scheduler);