app2-api/modules/notification/notification.controller.js

119 lines
5.0 KiB
JavaScript
Raw Normal View History

2019-10-03 19:37:56 +00:00
'use strict';
const httpStatus = require('http-status');
2019-10-17 16:59:18 +00:00
2019-10-03 19:37:56 +00:00
const generateControllers = require('../../core/controllers');
const { buildContext } = require('../../core/controllers');
2019-10-17 16:59:18 +00:00
const notificationService = require('./notification.service');
const userDeviceService = require('./user_device.service');
2019-10-03 19:37:56 +00:00
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
// Module Name
2019-10-17 16:59:18 +00:00
const MODULE_NAME = '[notification.controller]';
2019-10-03 19:37:56 +00:00
const controllerOptions = { MODULE_NAME };
const extraControllers = {
2019-10-14 15:25:35 +00:00
sendNotification: (config) => {
return async (req, res, next) => {
let receipt = undefined;
2019-10-17 16:00:50 +00:00
const context = buildContext(req, config);
let params = extractParamsFromRequest(req, res);
2019-10-14 15:25:35 +00:00
2019-10-17 16:00:50 +00:00
let userIds = req.body.userIds;
if (!userIds) {
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing user Ids'), res)
}
2019-10-14 15:25:35 +00:00
2019-10-17 16:00:50 +00:00
if (!req.body.title) {
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing message title'), res)
}
2019-10-14 15:25:35 +00:00
2019-10-17 16:00:50 +00:00
if (!req.body.message) {
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing message content'), res)
}
try {
2019-10-17 16:59:18 +00:00
let getUserDevicesPromise = (userId) => userDeviceService.fetchAll({ params: { userId: userId }}, context);
let sendNotificationsPromise = (messages) => notificationService.sendNotification(messages);
2019-10-17 16:00:50 +00:00
let buildMessagePromise = (userDevices) => {
let message = undefined;
userDevices.rows.forEach(function (userDevice) {
2019-10-17 16:59:18 +00:00
if (notificationService.isValidPushToken(userDevice.token)) {
2019-10-17 16:00:50 +00:00
message = {
userId: userDevice.userId,
to: userDevice.token,
title: req.body.title,
body: req.body.message,
ttl: req.body.ttl,
priority: req.body.priority,
data: req.body.data,
sound: 'default',
};
}
});
return new Promise(function(resolve) { resolve(message) });
};
let getUserDevicesList = [];
userIds.forEach(function (userId) {
getUserDevicesList.push(getUserDevicesPromise(userId));
});
2019-10-14 15:25:35 +00:00
2019-10-17 16:00:50 +00:00
receipt = await Promise.all(getUserDevicesList).then(
function(userDeviceList) {
return Promise.all(userDeviceList.map(buildMessagePromise))
})
.then(sendNotificationsPromise)
2019-10-14 15:25:35 +00:00
} catch (error) {
2019-10-17 16:00:50 +00:00
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', error, res)
2019-10-14 15:25:35 +00:00
} finally {
return handleResultResponse(receipt, null, null, res, httpStatus.OK);
}
}
},
registerUser: (config) => {
2019-10-03 19:37:56 +00:00
return async (req, res, next) => {
try {
const context = buildContext(req, config);
var data = {
token: req.body.token,
valid: 1,
userId: context.user.id
};
let params = extractParamsFromRequest(req, res, {
includeAll: false,
paginate: { limit: 1, page: 1 },
params: {
userId: context.user.id,
token: data.token,
}
});
// Buscamos el token y el usuario
2019-10-15 09:34:30 +00:00
console.log('>> Busco el usuario y el token', params.params);
2019-10-17 16:59:18 +00:00
let result = await notificationService.fetchOne(params, context);
2019-10-03 19:37:56 +00:00
if (!result) {
// Dar de alta el token
2019-10-15 09:34:30 +00:00
console.log('>> Dar de alta el token', data);
2019-10-17 16:59:18 +00:00
result = await notificationService.create(data, context);
2019-10-03 19:37:56 +00:00
} else {
// Actualizar el token
2019-10-15 09:34:30 +00:00
console.log('>> Actualizar el token', params.params, data, context);
2019-10-17 16:59:18 +00:00
result = await notificationService.update(params.params, data, context);
2019-10-03 19:37:56 +00:00
}
} catch(error) {
console.error(error);
} finally {
// En todo caso devolver OK al cliente
return handleResultResponse('OK', null, null, res, httpStatus.OK);
}
}
},
};
2019-10-17 16:59:18 +00:00
module.exports = generateControllers(notificationService, extraControllers, controllerOptions);