Files
old-web/ma-api/handler.js
2019-03-08 06:24:10 +01:00

84 lines
2.3 KiB
JavaScript

'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: 'Notifications sent',
input: event,
}),
};
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};
module.exports.getitems = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({
message: 'Get Items',
input: event,
}),
};
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};
module.exports.marketalerts = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({
message: 'Market alerts',
input: event,
}),
};
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};
module.exports.payforalert = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({
message: 'Pay for alert',
input: event,
}),
};
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};