#21 -> Poder enviar una notificación push a todos los usuarios activos de la APP.
This commit is contained in:
parent
5562d2ef65
commit
67afb9734c
@ -26,6 +26,20 @@ const extraMethods = {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
_getActiveUserIds: async (offset = 0, limit = 10) => {
|
||||||
|
return models.User.findAndCountAll({
|
||||||
|
attributes: ['id'],
|
||||||
|
where: {
|
||||||
|
state: 'active',
|
||||||
|
phone: { [Sequelize.Op.ne]: null },
|
||||||
|
},
|
||||||
|
raw: true,
|
||||||
|
limit: limit,
|
||||||
|
offset: offset
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
_updateLastLoginAndVersionUser: async (id, appVersion) => {
|
_updateLastLoginAndVersionUser: async (id, appVersion) => {
|
||||||
return models.User.update ({
|
return models.User.update ({
|
||||||
app_version : appVersion,
|
app_version : appVersion,
|
||||||
|
|||||||
@ -5,6 +5,7 @@ const generateControllers = require('../../core/controllers');
|
|||||||
const { buildContext } = require('../../core/controllers');
|
const { buildContext } = require('../../core/controllers');
|
||||||
const notificationService = require('./notification.service');
|
const notificationService = require('./notification.service');
|
||||||
const notificationDetailService = require('./notification_detail.service');
|
const notificationDetailService = require('./notification_detail.service');
|
||||||
|
const userService = require('../auth/user.service');
|
||||||
const userDeviceService = require('./user_device.service');
|
const userDeviceService = require('./user_device.service');
|
||||||
const eventInscriptionService = require('../events/events_inscriptions.service');
|
const eventInscriptionService = require('../events/events_inscriptions.service');
|
||||||
const { usersIdsComposer } = require('../../helpers/composes.helper');
|
const { usersIdsComposer } = require('../../helpers/composes.helper');
|
||||||
@ -12,11 +13,67 @@ const pushHelper = require('../../helpers/push.helper');
|
|||||||
const notificationHelper = require('../../helpers/notification.helpers')
|
const notificationHelper = require('../../helpers/notification.helpers')
|
||||||
|
|
||||||
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
|
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
|
||||||
|
const { get } = require('lodash');
|
||||||
|
|
||||||
// Module Name
|
// Module Name
|
||||||
const MODULE_NAME = '[notification.controller]';
|
const MODULE_NAME = '[notification.controller]';
|
||||||
const controllerOptions = { MODULE_NAME };
|
const controllerOptions = { MODULE_NAME };
|
||||||
|
|
||||||
|
|
||||||
|
async function _sendNotificationAllActiveUsers(notification) {
|
||||||
|
let limit = 4;
|
||||||
|
let page = 1;
|
||||||
|
|
||||||
|
let totalRows = 0;
|
||||||
|
let totalPages = -1;
|
||||||
|
|
||||||
|
do {
|
||||||
|
let offset = (page - 1) * limit;
|
||||||
|
let result = await userService._getActiveUserIds(offset, limit);
|
||||||
|
|
||||||
|
if (totalPages == -1) {
|
||||||
|
totalPages = Math.ceil(result.count / limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
page = page + 1;
|
||||||
|
|
||||||
|
let ids = result.rows.map(function(item) { return item.id });
|
||||||
|
notificationService.sendNotification(notification, ids);
|
||||||
|
|
||||||
|
|
||||||
|
} while (page <= totalPages);
|
||||||
|
|
||||||
|
return 'OK';
|
||||||
|
}
|
||||||
|
|
||||||
|
async function _getUserIdsForEventId(eventId, segment) {
|
||||||
|
let userIds =[];
|
||||||
|
|
||||||
|
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 usersIdsComposer(userIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const extraControllers = {
|
const extraControllers = {
|
||||||
|
|
||||||
sendNotification: (config) => {
|
sendNotification: (config) => {
|
||||||
@ -26,6 +83,11 @@ const extraControllers = {
|
|||||||
* "tittle": "título de la notificación",
|
* "tittle": "título de la notificación",
|
||||||
* "message": "cuerpo de la notificación",
|
* "message": "cuerpo de la notificación",
|
||||||
* "recipients": {
|
* "recipients": {
|
||||||
|
* OPCION 1- Unos usuarios determinados
|
||||||
|
* "userIds": ["*" | "f428a317-6d1f-4eda-aa3e-22baff3f48d7", ...]
|
||||||
|
* "segment": "ALL" | "LAST_YEAR" | "LAST_MONTH"
|
||||||
|
*
|
||||||
|
* OPCION 2 - A todos los usuarios inscritos a un evento, se puede segmentar
|
||||||
* "eventId": "xxx-xxx-xxx-xxx",
|
* "eventId": "xxx-xxx-xxx-xxx",
|
||||||
* "segment": "ALL" | "ALL_VALIDATED" | "ALL_NOT_VALIDATED" |
|
* "segment": "ALL" | "ALL_VALIDATED" | "ALL_NOT_VALIDATED" |
|
||||||
* "PARTNERS_ALL" | "PARTNERS_VALIDATED" | "PARTNERS_NOT_VALIDATED" |
|
* "PARTNERS_ALL" | "PARTNERS_VALIDATED" | "PARTNERS_NOT_VALIDATED" |
|
||||||
@ -49,11 +111,11 @@ const extraControllers = {
|
|||||||
config = config || {
|
config = config || {
|
||||||
scopes: [],
|
scopes: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
let receipt = undefined;
|
|
||||||
const context = buildContext(req, config);
|
const context = buildContext(req, config);
|
||||||
let params = extractParamsFromRequest(req, res);
|
let params = extractParamsFromRequest(req, res);
|
||||||
|
|
||||||
|
let receipt = undefined;
|
||||||
let userIds = undefined;
|
let userIds = undefined;
|
||||||
let eventId = undefined;
|
let eventId = undefined;
|
||||||
let segment = undefined;
|
let segment = undefined;
|
||||||
@ -73,21 +135,45 @@ const extraControllers = {
|
|||||||
segment = body.recipients.segment;
|
segment = body.recipients.segment;
|
||||||
} else if (body.recipients.userIds) {
|
} else if (body.recipients.userIds) {
|
||||||
userIds = body.recipients.userIds;
|
userIds = body.recipients.userIds;
|
||||||
|
segment = body.recipients.segment;
|
||||||
} else {
|
} else {
|
||||||
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing user Ids or event Ids'), res)
|
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing user Ids or event Ids'), res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
let notification = notificationHelper.createNotification ({
|
let notification = notificationHelper.createNotification ({
|
||||||
...body,
|
...body,
|
||||||
userId: context.user.id
|
userId: context.user.id
|
||||||
});
|
});
|
||||||
|
|
||||||
let getUserIds = async () => {
|
if ((userIds) && (userIds.length == 1) && (userIds[0] == "*")) {
|
||||||
|
receipt = _sendNotificationAllActiveUsers(notification);
|
||||||
|
} else {
|
||||||
|
|
||||||
|
let _userIds = null;
|
||||||
|
|
||||||
if (userIds) {
|
if (userIds) {
|
||||||
return new Promise(function(resolve) { resolve(userIds) } );
|
_userIds = userIds;
|
||||||
} else if (eventId && segment) {
|
} else {
|
||||||
|
if (eventId) {
|
||||||
|
_userIds = _getUserIdsForEventId(eventId, segment);
|
||||||
|
} else {
|
||||||
|
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing event and segment'), res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_userIds) {
|
||||||
|
receipt = notificationService.sendNotification(notification, _userIds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*let getUserIds = async () => {
|
||||||
|
if (userIds) {
|
||||||
|
return new Promise(function(resolve) { resolve(userIds) } );
|
||||||
|
} else if (eventId) {
|
||||||
switch (segment) {
|
switch (segment) {
|
||||||
//Todos los inscritos tanto invitados como libres
|
//Todos los inscritos tanto invitados como libres
|
||||||
case 'ALL_VALIDATED':
|
case 'ALL_VALIDATED':
|
||||||
@ -114,9 +200,9 @@ const extraControllers = {
|
|||||||
} else {
|
} else {
|
||||||
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing event and segment'), res)
|
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing event and segment'), res)
|
||||||
}
|
}
|
||||||
}
|
} */
|
||||||
|
|
||||||
receipt = notificationService.sendNotification(notification, await getUserIds());
|
//receipt = notificationService.sendNotification(notification, await getUserIds());
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|||||||
@ -31,7 +31,7 @@
|
|||||||
"compression": "^1.7.4",
|
"compression": "^1.7.4",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"crypto": "^1.0.1",
|
"crypto": "^1.0.1",
|
||||||
"expo-server-sdk": "^3.3.0",
|
"expo-server-sdk": "^3.5.1",
|
||||||
"express": "^4.16.4",
|
"express": "^4.16.4",
|
||||||
"express-validation": "^1.0.2",
|
"express-validation": "^1.0.2",
|
||||||
"firebase-admin": "^8.1.0",
|
"firebase-admin": "^8.1.0",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user