mirror of
https://github.com/orangecoding/fredy.git
synced 2026-06-16 12:31:07 +00:00
56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
const fs = require('fs');
|
|
const service = require('restana')();
|
|
const notificationAdapterRouter = service.newRouter();
|
|
|
|
const notificationAdapterList = fs.readdirSync('./lib//notification/adapter').filter((file) => file.endsWith('.js'));
|
|
|
|
const notificationAdapter = notificationAdapterList.map((pro) => {
|
|
return require(`../../notification/adapter/${pro}`);
|
|
});
|
|
|
|
notificationAdapterRouter.post('/try', async (req, res) => {
|
|
const { id, fields } = req.body;
|
|
const adapter = notificationAdapter.find((adapter) => adapter.config.id === id);
|
|
if (adapter == null) {
|
|
res.send(404);
|
|
}
|
|
const notificationConfig = [];
|
|
const notificationObject = {};
|
|
Object.keys(fields).forEach((key) => {
|
|
notificationObject[key] = fields[key].value;
|
|
});
|
|
notificationConfig.push({
|
|
fields: { ...notificationObject },
|
|
enabled: true,
|
|
id,
|
|
});
|
|
|
|
try {
|
|
await adapter.send({
|
|
serviceName: 'TestCall',
|
|
newListings: [
|
|
{
|
|
price: '42 €',
|
|
title: 'This is a test listing',
|
|
address: 'some address',
|
|
size: '666 2m',
|
|
link: 'https://www.orange-coding.net',
|
|
},
|
|
],
|
|
notificationConfig,
|
|
jobKey: 'TestJob',
|
|
});
|
|
|
|
res.send();
|
|
} catch (Exception) {
|
|
res.send(new Error(Exception));
|
|
}
|
|
});
|
|
|
|
notificationAdapterRouter.get('/', async (req, res) => {
|
|
res.body = notificationAdapter.map((adapter) => adapter.config);
|
|
res.send();
|
|
});
|
|
|
|
exports.notificationAdapterRouter = notificationAdapterRouter;
|