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

343 lines
15 KiB
JavaScript
Raw Normal View History

2019-10-03 19:37:56 +00:00
'use strict';
const httpStatus = require('http-status');
2019-10-31 12:45:18 +00:00
const moment = require('moment');
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');
2019-10-21 10:12:16 +00:00
const notificationDetailService = require('./notification_detail.service');
2019-10-17 16:59:18 +00:00
const userDeviceService = require('./user_device.service');
2019-11-07 10:39:20 +00:00
const eventInscriptionService = require('../events/events_inscriptions.service');
const { usersIdsComposer } = require('../../helpers/composes.helper');
2019-10-17 16:59:18 +00:00
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-11-06 09:10:59 +00:00
2019-11-07 10:39:20 +00:00
sendNotificationEvent: async (req, res, next) => {
2019-11-06 16:36:03 +00:00
2019-11-07 17:24:29 +00:00
2019-11-07 10:39:20 +00:00
let usersIds = null;
const params = req.body;
const eventId = params.recipients.eventId;
const segment = params.recipients.segment;
console.log('prueba de llamada>>>>> ', params);
try {
notificationService.createNotification(params.date, params.title, params.message, undefined, 'default', params.data, req.user.id);
switch (segment) {
//Todos los inscritos al evento tanto en validos como en lista de espera
case 'ALL':
usersIds = await eventInscriptionService._getInscriptionByEvent(eventId);
break;
//Todos los inscritos tanto invitados como libres
case 'ALL_VALIDATED':
usersIds = await eventInscriptionService._getInscriptionByEventAndValidated(eventId, true);
break;
//Todos los de lista de espera tanto invitados como libres (Actualmente en invitados no hay lista de espera)
case 'ALL_NOT_VALIDATED':
usersIds = await eventInscriptionService._getInscriptionByEventAndValidated(eventId, false);
break;
2019-11-06 16:36:03 +00:00
2019-11-07 10:39:20 +00:00
//Solo invitados como actualmente no se usa codigo de reserva para los coles, vale con filtrar por aquellos que tengan codigo de reserva
case 'PARTNERS_ALL':
usersIds = await eventInscriptionService._getInscriptionByEventFromPartner(eventId);
break;
2019-11-06 09:10:59 +00:00
2019-11-07 10:39:20 +00:00
//Todos los inscritos al evento, tanto validados como en lista de espera
default: //ALL
break;
}
usersIds = usersIdsComposer(usersIds);
console.log('usuarios inscritos>>>>> ', usersIds);
} catch(error) {
return handleErrorResponse(MODULE_NAME, 'sendNotificationEvent', error, res);
}
return handleResultResponse("sendNotificationEvent", null, params, res, httpStatus.OK);
2019-11-06 09:10:59 +00:00
},
2019-10-14 15:25:35 +00:00
sendNotification: (config) => {
2019-11-07 17:24:29 +00:00
/**
* notificationSample = {
* "tittle": "título de la notificación",
* "message": "cuerpo de la notificación",
* "recipients": {
* "eventId": "xxx-xxx-xxx-xxx",
* "segment": "ALL" | "ALL_VALIDATED" | "ALL_NOT_VALIDATED" |
* "PARTNERS_ALL" | "PARTNERS_VALIDATED" | "PARTNERS_NOT_VALIDATED" |
* "COLLEGE_ALL" | "COLLEGE_VALIDATED" | "COLLEGE_NOT_VALIDATED"
* },
* "data": {
* "type": "message",
* "title": "Título del mensaje",
* "message": "Cuerpo del mensaje",
* "button": {
* "caption": "Etiqueta del boton",
* "url": "https://www.site.es",
* "screen": "<RouterName>",
* "paramId": "23",
* }
* }
*}
*/
2019-10-14 15:25:35 +00:00
return async (req, res, next) => {
2019-10-21 10:12:16 +00:00
config = config || {
scopes: [],
};
2019-10-14 15:25:35 +00:00
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-11-07 17:24:29 +00:00
let userIds = undefined;
let eventId = undefined;
let segment = undefined;
const { body } = req;
2019-10-14 15:25:35 +00:00
2019-11-07 17:24:29 +00:00
if (!body.title) {
2019-10-17 16:00:50 +00:00
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing message title'), res)
}
2019-10-14 15:25:35 +00:00
2019-11-07 17:24:29 +00:00
if (!body.message) {
2019-10-17 16:00:50 +00:00
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing message content'), res)
}
2019-11-07 17:24:29 +00:00
// Evento?
if (body.recipients.eventId) {
eventId = body.recipients.eventId;
segment = body.recipients.segment;
} else if (body.recipients.userIds) {
userIds = body.recipients.userIds;
} else {
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing user Ids or event Ids'), res)
}
2019-10-21 10:12:16 +00:00
try {
2019-10-21 18:23:06 +00:00
let getUserDevicesPromise = (userId) => userDeviceService.fetchAll({ params: { userId: userId }}, context).then(function(result) {
return new Promise(function(resolve) { resolve(result.rows) });
});
2019-11-07 17:24:29 +00:00
let saveNotificationPromise = (notification) => notificationService.createNotification(notification);
2019-10-17 16:59:18 +00:00
let sendNotificationsPromise = (messages) => notificationService.sendNotification(messages);
2019-10-21 18:23:06 +00:00
let disableUserDevicePromise = (token) => userDeviceService.update({ params: {
token: token,
2019-10-31 12:45:18 +00:00
}}, { valid: 0, invalidated: moment() }, context);
2019-10-21 18:23:06 +00:00
let disableInvalidUserDevicesPromise = (userDevices) => {
return new Promise(function (resolve) {
let _userDevices = [];
userDevices.forEach(async function (userDevice) {
2019-11-07 17:45:50 +00:00
console.log(userDevice);
2019-10-21 18:23:06 +00:00
if (!userDeviceService.isValidPushToken(userDevice.token)) {
await disableUserDevicePromise(userDevice.token);
} else {
_userDevices.push(userDevice);
}
});
resolve(_userDevices)
});
};
let disableUserDevicesWithErrorStatus = (messages, tickets) => {
return new Promise(function (resolve) {
tickets.forEach(async function (ticket, index) {
if ((ticket.status === 'error') && (ticket.details.error === 'DeviceNotRegistered')) {
await disableUserDevicePromise(messages[index].to)
}
});
resolve(true);
});
}
let saveResponseStatusPromise = (messages, tickets) => notificationDetailService.saveNotificationDetails(messages, tickets);
2019-10-17 16:00:50 +00:00
2019-11-07 17:47:35 +00:00
let notificationRecord = {
2019-11-07 17:24:29 +00:00
date: body.date,
title: body.title,
body: body.message,
ttl: body.ttl,
priority: body.priority,
recipients: body.recipients,
data: body.data,
2019-10-31 12:45:18 +00:00
userId: context.user.id,
2019-10-21 10:12:16 +00:00
};
2019-10-21 18:23:06 +00:00
let buildMessagesPromise = (userDevices) => {
return new Promise(function (resolve) {
let messages = [];
userDevices.forEach(async function (userDevice) {
messages.push({
title: notificationRecord.title,
body: notificationRecord.body,
ttl: notificationRecord.ttl,
priority: notificationRecord.priority,
2019-10-17 16:00:50 +00:00
userId: userDevice.userId,
to: userDevice.token,
2019-10-21 18:23:06 +00:00
notificationId: notificationRecord.id,
2019-10-22 10:15:19 +00:00
data: notificationRecord.data,
2019-10-31 12:45:18 +00:00
_displayInForeground: true,
sound: 'default',
2019-10-21 18:23:06 +00:00
});
});
resolve(messages)
2019-10-17 16:00:50 +00:00
});
};
2019-11-07 17:24:29 +00:00
let getUserIds = async () => {
if (userIds) {
return new Promise(function(resolve) { resolve(userIds) } );
} else if (eventId && segment) {
switch (segment) {
//Todos los inscritos tanto invitados como libres
case 'ALL_VALIDATED':
userIds = await eventInscriptionService._getInscriptionByEventAndValidated(eventId, true);
break;
//Todos los de lista de espera tanto invitados como libres (Actualmente en invitados no hay lista de espera)
case 'ALL_NOT_VALIDATED':
userIds = await eventInscriptionService._getInscriptionByEventAndValidated(eventId, false);
break;
2019-10-14 15:25:35 +00:00
2019-11-07 17:24:29 +00:00
//Solo invitados como actualmente no se usa codigo de reserva para los coles, vale con filtrar por aquellos que tengan codigo de reserva
case 'PARTNERS_ALL':
userIds = await eventInscriptionService._getInscriptionByEventFromPartner(eventId);
break;
//Todos los inscritos al evento, tanto validados como en lista de espera
default: //ALL
userIds = await eventInscriptionService._getInscriptionByEvent(eventId);
break;
}
return new Promise(function (resolve) { resolve(usersIdsComposer(userIds)); });
} else {
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing event and segment'), res)
}
}
let getUserDevicesList = [];
Promise.all([
saveNotificationPromise(notificationRecord),
getUserIds()
])
.then(function(result) {
2019-11-07 17:45:50 +00:00
notificationRecord = result[0];
2019-11-07 17:24:29 +00:00
userIds = result[1];
userIds.forEach(function (userId) {
console.log(userId);
getUserDevicesList.push(getUserDevicesPromise(userId));
});
2019-10-21 10:12:16 +00:00
return Promise.all(getUserDevicesList)
2019-10-21 18:23:06 +00:00
}).then(function (userDeviceList) {
2019-11-07 17:37:53 +00:00
console.log(userDeviceList);
2019-11-07 17:45:50 +00:00
return new Promise(function(resolve) { resolve(userDeviceList); });
2019-10-21 18:23:06 +00:00
})
.then(disableInvalidUserDevicesPromise)
.then(buildMessagesPromise)
.then(sendNotificationsPromise)
.then(function({ messages, tickets }) {
let _saveStatus = saveResponseStatusPromise(messages, tickets);
let _disableDevices = disableUserDevicesWithErrorStatus(messages, tickets);
return Promise.all([_saveStatus, _disableDevices]);
})
.then(function (details) {
console.log(details);
});
2019-10-14 15:25:35 +00:00
} catch (error) {
2019-10-21 18:23:06 +00:00
console.error(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);
}
}
},
2019-10-21 10:12:16 +00:00
updateNotificationsWithReceipts: (config) => {
return async (req, res, next) => {
config = config || {
scopes: [],
};
const context = buildContext(req, config);
let params = extractParamsFromRequest(req, res);
let getNotificationsWithoutReceipt = async () => {
let params = {
where: { }
};
return await notificationDetailService.fetchAll(params, context);
}
let receipt = await Promise.all(getUserDevicesList).then(
function (userDeviceList) {
return Promise.all(userDeviceList.map(buildMessagePromise))
})
.then(sendNotificationsPromise)
}
},
registerDevice: (config) => {
2019-10-03 19:37:56 +00:00
return async (req, res, next) => {
2019-10-21 10:12:16 +00:00
config = config || {
scopes: [],
};
2019-10-03 19:37:56 +00:00
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-22 10:15:19 +00:00
let result = await userDeviceService.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-22 10:15:19 +00:00
result = await userDeviceService.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-31 12:45:18 +00:00
result = await userDeviceService.update(params, { valid : 1 }, 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);