diff --git a/ma-api/handler.js b/ma-api/handler.js index 025b0bb..6cd26e8 100644 --- a/ma-api/handler.js +++ b/ma-api/handler.js @@ -1,10 +1,38 @@ 'use strict'; +const path = require("path"); +const bodyParser = require("body-parser"); +const MarketAlert = require("./lib/MarketAlert"); +const sendNotification = require("./lib/sendnotification"); +const scrapTheItems = require("./lib/scraptheitems"); +const sequelize = require("./lib/db.js"); +const Twocheckout = require("2checkout-node"); + module.exports.sendnotification = async (event, context) => { + let marketAlerts = await MarketAlert.findAll(); + + let lastDateUpdate = await Promise.all( + marketAlerts + .map(marketAlert => { + const { id, email, olx_url, last_date } = marketAlert.dataValues; + return { id, email, olx_url, last_date }; + }) + .map(sendNotification) + ); + + lastDateUpdate = lastDateUpdate.filter(Boolean(dateUpdate)); + lastDateUpdate.length && + lastDateUpdate.forEach(dateUpdate => + MarketAlert.update( + { last_date: dateUpdate.date }, + { where: { id: dateUpdate.id } } + ) + ); + return { statusCode: 200, body: JSON.stringify({ - message: 'Send notification!', + message: 'Notifications sent', input: event, }), }; diff --git a/ma-api/serverless.yml b/ma-api/serverless.yml index e9eb0c7..aa66d56 100644 --- a/ma-api/serverless.yml +++ b/ma-api/serverless.yml @@ -55,8 +55,12 @@ provider: # - exclude-me-dir/** functions: - hello: - handler: handler.hello + sendnotification: + handler: handler.sendnotification + events: + - http: + path: notifications/send + method: post # The following are a few example events you can configure # NOTE: Please make sure to change your handler code to work with those events