344 lines
15 KiB
JavaScript
344 lines
15 KiB
JavaScript
'use strict';
|
|
const httpStatus = require('http-status');
|
|
const moment = require('moment');
|
|
const generateControllers = require('../../core/controllers');
|
|
const { buildContext } = require('../../core/controllers');
|
|
const notificationService = require('./notification.service');
|
|
const notificationDetailService = require('./notification_detail.service');
|
|
const userDeviceService = require('./user_device.service');
|
|
const eventInscriptionService = require('../events/events_inscriptions.service');
|
|
const { usersIdsComposer } = require('../../helpers/composes.helper');
|
|
|
|
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
|
|
|
|
// Module Name
|
|
const MODULE_NAME = '[notification.controller]';
|
|
const controllerOptions = { MODULE_NAME };
|
|
|
|
const extraControllers = {
|
|
|
|
sendNotificationEvent: async (req, res, next) => {
|
|
|
|
|
|
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;
|
|
|
|
//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;
|
|
|
|
//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);
|
|
},
|
|
|
|
sendNotification: (config) => {
|
|
|
|
/**
|
|
* 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",
|
|
* }
|
|
* }
|
|
*}
|
|
*/
|
|
|
|
return async (req, res, next) => {
|
|
config = config || {
|
|
scopes: [],
|
|
};
|
|
|
|
let receipt = undefined;
|
|
const context = buildContext(req, config);
|
|
let params = extractParamsFromRequest(req, res);
|
|
|
|
let userIds = undefined;
|
|
let eventId = undefined;
|
|
let segment = undefined;
|
|
const { body } = req;
|
|
|
|
if (!body.title) {
|
|
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing message title'), res)
|
|
}
|
|
|
|
if (!body.message) {
|
|
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing message content'), res)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
|
|
try {
|
|
let getUserDevicesPromise = (userId) => userDeviceService.fetchAll({ params: { userId: userId }}, context).then(function(result) {
|
|
return new Promise(function(resolve) { resolve(result.rows) });
|
|
});
|
|
let saveNotificationPromise = (notification) => notificationService.createNotification(notification);
|
|
let sendNotificationsPromise = (messages) => notificationService.sendNotification(messages);
|
|
let disableUserDevicePromise = (token) => userDeviceService.update({ params: {
|
|
token: token,
|
|
}}, { valid: 0, invalidated: moment() }, context);
|
|
|
|
let disableInvalidUserDevicesPromise = (userDevices) => {
|
|
return new Promise(function (resolve) {
|
|
let _userDevices = [];
|
|
userDevices.forEach(async function (userDevice) {
|
|
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);
|
|
|
|
const notificationRecord = {
|
|
date: body.date,
|
|
title: body.title,
|
|
body: body.message,
|
|
ttl: body.ttl,
|
|
priority: body.priority,
|
|
recipients: body.recipients,
|
|
data: body.data,
|
|
userId: context.user.id,
|
|
};
|
|
|
|
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,
|
|
userId: userDevice.userId,
|
|
to: userDevice.token,
|
|
notificationId: notificationRecord.id,
|
|
data: notificationRecord.data,
|
|
_displayInForeground: true,
|
|
sound: 'default',
|
|
});
|
|
});
|
|
|
|
resolve(messages)
|
|
});
|
|
};
|
|
|
|
|
|
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;
|
|
|
|
//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) {
|
|
let notification = result[0];
|
|
notificationRecord.id = notification.id;
|
|
|
|
userIds = result[1];
|
|
|
|
userIds.forEach(function (userId) {
|
|
console.log(userId);
|
|
getUserDevicesList.push(getUserDevicesPromise(userId));
|
|
});
|
|
|
|
return Promise.all(getUserDevicesList)
|
|
}).then(function (userDeviceList) {
|
|
console.log(userDeviceList);
|
|
return new Promise(function(resolve) { resolve(userDeviceList[0]); });
|
|
})
|
|
.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);
|
|
});
|
|
} catch (error) {
|
|
console.error(error);
|
|
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', error, res)
|
|
} finally {
|
|
return handleResultResponse(receipt, null, null, res, httpStatus.OK);
|
|
}
|
|
}
|
|
},
|
|
|
|
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) => {
|
|
return async (req, res, next) => {
|
|
config = config || {
|
|
scopes: [],
|
|
};
|
|
|
|
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
|
|
console.log('>> Busco el usuario y el token', params.params);
|
|
let result = await userDeviceService.fetchOne(params, context);
|
|
if (!result) {
|
|
// Dar de alta el token
|
|
console.log('>> Dar de alta el token', data);
|
|
result = await userDeviceService.create(data, context);
|
|
} else {
|
|
// Actualizar el token
|
|
console.log('>> Actualizar el token', params.params, data, context);
|
|
result = await userDeviceService.update(params, { valid : 1 }, context);
|
|
}
|
|
} catch(error) {
|
|
console.error(error);
|
|
} finally {
|
|
// En todo caso devolver OK al cliente
|
|
return handleResultResponse('OK', null, null, res, httpStatus.OK);
|
|
}
|
|
}
|
|
},
|
|
};
|
|
|
|
module.exports = generateControllers(notificationService, extraControllers, controllerOptions); |