Notificaciones push
This commit is contained in:
parent
9fd846af95
commit
98fc529e92
@ -258,9 +258,7 @@ const usersIdsComposer = (inscriptions) => {
|
|||||||
let usersId = []
|
let usersId = []
|
||||||
if (inscriptions) {
|
if (inscriptions) {
|
||||||
inscriptions.map((inscription) => {
|
inscriptions.map((inscription) => {
|
||||||
usersId.push({
|
usersId.push(inscription.userId);
|
||||||
userId: inscription.userId,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
return usersId;
|
return usersId;
|
||||||
|
|||||||
@ -19,29 +19,7 @@ const extraControllers = {
|
|||||||
|
|
||||||
sendNotificationEvent: async (req, res, next) => {
|
sendNotificationEvent: async (req, res, next) => {
|
||||||
|
|
||||||
/**
|
|
||||||
* 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",
|
|
||||||
* }
|
|
||||||
* }
|
|
||||||
*}
|
|
||||||
*/
|
|
||||||
let usersIds = null;
|
let usersIds = null;
|
||||||
const params = req.body;
|
const params = req.body;
|
||||||
const eventId = params.recipients.eventId;
|
const eventId = params.recipients.eventId;
|
||||||
@ -88,6 +66,31 @@ const extraControllers = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
sendNotification: (config) => {
|
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) => {
|
return async (req, res, next) => {
|
||||||
config = config || {
|
config = config || {
|
||||||
scopes: [],
|
scopes: [],
|
||||||
@ -97,24 +100,35 @@ const extraControllers = {
|
|||||||
const context = buildContext(req, config);
|
const context = buildContext(req, config);
|
||||||
let params = extractParamsFromRequest(req, res);
|
let params = extractParamsFromRequest(req, res);
|
||||||
|
|
||||||
let userIds = req.body.userIds;
|
let userIds = undefined;
|
||||||
if (!userIds) {
|
let eventId = undefined;
|
||||||
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing user Ids'), res)
|
let segment = undefined;
|
||||||
}
|
const { body } = req;
|
||||||
|
|
||||||
if (!req.body.title) {
|
if (!body.title) {
|
||||||
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing message title'), res)
|
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing message title'), res)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!req.body.message) {
|
if (!body.message) {
|
||||||
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing message content'), res)
|
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 {
|
try {
|
||||||
let getUserDevicesPromise = (userId) => userDeviceService.fetchAll({ params: { userId: userId }}, context).then(function(result) {
|
let getUserDevicesPromise = (userId) => userDeviceService.fetchAll({ params: { userId: userId }}, context).then(function(result) {
|
||||||
return new Promise(function(resolve) { resolve(result.rows) });
|
return new Promise(function(resolve) { resolve(result.rows) });
|
||||||
});
|
});
|
||||||
let saveNotificationPromise = (notification) => notificationService.create(notification, context);
|
let saveNotificationPromise = (notification) => notificationService.createNotification(notification);
|
||||||
let sendNotificationsPromise = (messages) => notificationService.sendNotification(messages);
|
let sendNotificationsPromise = (messages) => notificationService.sendNotification(messages);
|
||||||
let disableUserDevicePromise = (token) => userDeviceService.update({ params: {
|
let disableUserDevicePromise = (token) => userDeviceService.update({ params: {
|
||||||
token: token,
|
token: token,
|
||||||
@ -149,12 +163,13 @@ const extraControllers = {
|
|||||||
let saveResponseStatusPromise = (messages, tickets) => notificationDetailService.saveNotificationDetails(messages, tickets);
|
let saveResponseStatusPromise = (messages, tickets) => notificationDetailService.saveNotificationDetails(messages, tickets);
|
||||||
|
|
||||||
const notificationRecord = {
|
const notificationRecord = {
|
||||||
date: moment(),
|
date: body.date,
|
||||||
title: req.body.title,
|
title: body.title,
|
||||||
body: req.body.message,
|
body: body.message,
|
||||||
ttl: req.body.ttl || undefined,
|
ttl: body.ttl,
|
||||||
priority: req.body.priority || 'default',
|
priority: body.priority,
|
||||||
data: req.body.data || { userIds: req.body.userIds },
|
recipients: body.recipients,
|
||||||
|
data: body.data,
|
||||||
userId: context.user.id,
|
userId: context.user.id,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -181,15 +196,56 @@ const extraControllers = {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
let getUserDevicesList = [];
|
|
||||||
|
|
||||||
userIds.forEach(function (userId) {
|
let getUserIds = async () => {
|
||||||
getUserDevicesList.push(getUserDevicesPromise(userId));
|
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;
|
||||||
|
|
||||||
saveNotificationPromise(notificationRecord)
|
//Solo invitados como actualmente no se usa codigo de reserva para los coles, vale con filtrar por aquellos que tengan codigo de reserva
|
||||||
.then(function(notification) {
|
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;
|
notificationRecord.id = notification.id;
|
||||||
|
|
||||||
|
userIds = result[1];
|
||||||
|
|
||||||
|
userIds.forEach(function (userId) {
|
||||||
|
console.log(userId);
|
||||||
|
getUserDevicesList.push(getUserDevicesPromise(userId));
|
||||||
|
});
|
||||||
|
|
||||||
return Promise.all(getUserDevicesList)
|
return Promise.all(getUserDevicesList)
|
||||||
}).then(function (userDeviceList) {
|
}).then(function (userDeviceList) {
|
||||||
return new Promise(function(resolve) { resolve(userDeviceList[0]); });
|
return new Promise(function(resolve) { resolve(userDeviceList[0]); });
|
||||||
|
|||||||
@ -26,6 +26,10 @@ module.exports = function (sequelize, DataTypes) {
|
|||||||
allowNull: false,
|
allowNull: false,
|
||||||
default: 'default',
|
default: 'default',
|
||||||
},
|
},
|
||||||
|
recipients: {
|
||||||
|
type: DataTypes.JSON,
|
||||||
|
allowNull: true,
|
||||||
|
},
|
||||||
data: {
|
data: {
|
||||||
type: DataTypes.JSON,
|
type: DataTypes.JSON,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
|
|||||||
@ -6,7 +6,7 @@ const PaginateMiddleware = require('../../middlewares/paginate');
|
|||||||
const FieldMiddleware = require('../../middlewares/fields');
|
const FieldMiddleware = require('../../middlewares/fields');
|
||||||
const SortMiddleware = require('../../middlewares/sort');
|
const SortMiddleware = require('../../middlewares/sort');
|
||||||
const notificationController = require('./notification.controller');
|
const notificationController = require('./notification.controller');
|
||||||
const { pushSendEvent, deviceTokenInputType, notificationSendType } = require('./notification.validations');
|
const { pushSendEvent, deviceTokenInputType } = require('./notification.validations');
|
||||||
|
|
||||||
const generalInvalidFields = [
|
const generalInvalidFields = [
|
||||||
'createdAt', 'updatedAt',
|
'createdAt', 'updatedAt',
|
||||||
@ -36,17 +36,17 @@ routes.get('/admin/notifications/:id',
|
|||||||
|
|
||||||
routes.post('/admin/notifications',
|
routes.post('/admin/notifications',
|
||||||
isAdministratorUser,
|
isAdministratorUser,
|
||||||
SchemaValidator(notificationSendType, true),
|
SchemaValidator(pushSendEvent, true),
|
||||||
notificationController.sendNotification({
|
notificationController.sendNotification({
|
||||||
scopes: ['defaultScope']
|
scopes: ['defaultScope']
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
routes.post('/admin/notifications/event',
|
/*routes.post('/admin/notifications/event',
|
||||||
isAdministratorUser,
|
isAdministratorUser,
|
||||||
SchemaValidator(pushSendEvent, true),
|
SchemaValidator(pushSendEvent, true),
|
||||||
notificationController.sendNotificationEvent
|
notificationController.sendNotificationEvent
|
||||||
);
|
);*/
|
||||||
|
|
||||||
/* Borrar cuando ya no aparezca la versión 1.0.10 */
|
/* Borrar cuando ya no aparezca la versión 1.0.10 */
|
||||||
routes.post('/notifications/register',
|
routes.post('/notifications/register',
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
const moment = require('moment');
|
||||||
const { Expo } = require('expo-server-sdk');
|
const { Expo } = require('expo-server-sdk');
|
||||||
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
|
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
|
||||||
const models = require('../../core/models');
|
const models = require('../../core/models');
|
||||||
@ -7,26 +8,19 @@ const expo = new Expo();
|
|||||||
|
|
||||||
const extraMethods = {
|
const extraMethods = {
|
||||||
|
|
||||||
createNotification: (date, title, body, ttl, priority, data, userId) => {
|
createNotification: ({ date, title, body, ttl, priority, recipients, data, userId }) => {
|
||||||
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<valores de la notificacion');
|
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<valores de la notificacion');
|
||||||
console.log(title, body, ttl, priority, data, userId);
|
console.log(date, title, body, ttl, priority, recipients, data, userId);
|
||||||
|
|
||||||
return new Promise(function (resolve, reject) {
|
return models.Notification.create({
|
||||||
models.Notification.create({
|
date: moment(date),
|
||||||
date: date,
|
title: title,
|
||||||
title: title,
|
body: body,
|
||||||
body: body,
|
userId: userId,
|
||||||
userId: userId,
|
ttl: ttl || undefined,
|
||||||
ttl: ttl,
|
priority: priority || 'default',
|
||||||
priority: priority,
|
recipients: recipients,
|
||||||
data: data,
|
data: data,
|
||||||
})
|
|
||||||
.then(function (result) {
|
|
||||||
resolve(result);
|
|
||||||
})
|
|
||||||
.catch(function (error) {
|
|
||||||
reject(error)
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@ -4,20 +4,21 @@ const deviceTokenInputType = Joi.object().keys({
|
|||||||
token: Joi.string().required(),
|
token: Joi.string().required(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const pushSendType = Joi.object().keys({
|
/*const pushSendType = Joi.object().keys({
|
||||||
userIds: Joi.array().required(),
|
userIds: Joi.array().required(),
|
||||||
title: Joi.string().required(),
|
title: Joi.string().required(),
|
||||||
message: Joi.string().required(),
|
message: Joi.string().required(),
|
||||||
//token: Joi.string().required(),
|
//token: Joi.string().required(),
|
||||||
});
|
});*/
|
||||||
|
|
||||||
const pushSendEvent = Joi.object().keys({
|
const pushSendEvent = Joi.object().keys({
|
||||||
date: Joi.date().required(),
|
date: Joi.date().optional(),
|
||||||
title: Joi.string().required(),
|
title: Joi.string().required(),
|
||||||
message: Joi.string().required(),
|
message: Joi.string().required(),
|
||||||
recipients: Joi.object().keys({
|
recipients: Joi.object().keys({
|
||||||
eventId: Joi.string().required(),
|
userIds: Joi.array().optional(),
|
||||||
segment: Joi.string().required(),
|
eventId: Joi.string().optional(),
|
||||||
|
segment: Joi.string().optional(),
|
||||||
}),
|
}),
|
||||||
data: Joi.object().keys({
|
data: Joi.object().keys({
|
||||||
type: Joi.string().required(),
|
type: Joi.string().required(),
|
||||||
@ -33,5 +34,5 @@ const pushSendEvent = Joi.object().keys({
|
|||||||
});
|
});
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
deviceTokenInputType, pushSendType, pushSendEvent
|
deviceTokenInputType, pushSendEvent
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user