Revisión de notificaciones
This commit is contained in:
parent
7702da6267
commit
80f3449839
@ -23,10 +23,10 @@ const controllerOptions = { MODULE_NAME };
|
|||||||
async function _sendNotificationAllActiveUsers(notification) {
|
async function _sendNotificationAllActiveUsers(notification) {
|
||||||
let limit = 4;
|
let limit = 4;
|
||||||
let page = 1;
|
let page = 1;
|
||||||
|
|
||||||
let totalRows = 0;
|
let totalRows = 0;
|
||||||
let totalPages = -1;
|
let totalPages = -1;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let offset = (page - 1) * limit;
|
let offset = (page - 1) * limit;
|
||||||
let result = await userService._getActiveUserIds(offset, limit);
|
let result = await userService._getActiveUserIds(offset, limit);
|
||||||
@ -36,18 +36,18 @@ async function _sendNotificationAllActiveUsers(notification) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
page = page + 1;
|
page = page + 1;
|
||||||
|
|
||||||
let ids = result.rows.map(function(item) { return item.id });
|
let ids = result.rows.map(function (item) { return item.id });
|
||||||
notificationService.sendNotification(notification, ids);
|
notificationService.sendNotification(notification, ids);
|
||||||
|
|
||||||
|
|
||||||
} while (page <= totalPages);
|
} while (page <= totalPages);
|
||||||
|
|
||||||
return 'OK';
|
return 'OK';
|
||||||
}
|
}
|
||||||
|
|
||||||
async function _getUserIdsForEventId(eventId, segment) {
|
async function _getUserIdsForEventId(eventId, segment) {
|
||||||
let userIds =[];
|
let userIds = [];
|
||||||
|
|
||||||
switch (segment) {
|
switch (segment) {
|
||||||
//Todos los inscritos tanto invitados como libres
|
//Todos los inscritos tanto invitados como libres
|
||||||
@ -69,7 +69,7 @@ async function _getUserIdsForEventId(eventId, segment) {
|
|||||||
userIds = await eventInscriptionService._getInscriptionByEvent(eventId);
|
userIds = await eventInscriptionService._getInscriptionByEvent(eventId);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return usersIdsComposer(userIds);
|
return usersIdsComposer(userIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,40 +78,40 @@ const extraControllers = {
|
|||||||
|
|
||||||
sendNotification: (config) => {
|
sendNotification: (config) => {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* notificationSample = {
|
* notificationSample = {
|
||||||
* "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
|
* OPCION 1- Unos usuarios determinados
|
||||||
* "userIds": ["*" | "f428a317-6d1f-4eda-aa3e-22baff3f48d7", ...]
|
* "userIds": ["*" | "f428a317-6d1f-4eda-aa3e-22baff3f48d7", ...]
|
||||||
* "segment": "ALL" | "LAST_YEAR" | "LAST_MONTH"
|
* "segment": "ALL" | "LAST_YEAR" | "LAST_MONTH"
|
||||||
*
|
*
|
||||||
* OPCION 2 - A todos los usuarios inscritos a un evento, se puede segmentar
|
* 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" |
|
||||||
* "COLLEGE_ALL" | "COLLEGE_VALIDATED" | "COLLEGE_NOT_VALIDATED"
|
* "COLLEGE_ALL" | "COLLEGE_VALIDATED" | "COLLEGE_NOT_VALIDATED"
|
||||||
* },
|
* },
|
||||||
* "data": {
|
* "data": {
|
||||||
* "type": "message",
|
* "type": "message",
|
||||||
* "title": "Título del mensaje",
|
* "title": "Título del mensaje",
|
||||||
* "message": "Cuerpo del mensaje",
|
* "message": "Cuerpo del mensaje",
|
||||||
* "button": {
|
* "button": {
|
||||||
* "caption": "Etiqueta del boton",
|
* "caption": "Etiqueta del boton",
|
||||||
* "url": "https://www.site.es",
|
* "url": "https://www.site.es",
|
||||||
* "screen": "<RouterName>",
|
* "screen": "<RouterName>",
|
||||||
* "paramId": "23",
|
* "paramId": "23",
|
||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
*}
|
*}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
return async (req, res, next) => {
|
return async (req, res, next) => {
|
||||||
config = config || {
|
config = config || {
|
||||||
scopes: [],
|
scopes: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
const context = buildContext(req, config);
|
const context = buildContext(req, config);
|
||||||
let params = extractParamsFromRequest(req, res);
|
let params = extractParamsFromRequest(req, res);
|
||||||
|
|
||||||
@ -130,21 +130,21 @@ const extraControllers = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Evento?
|
// Evento?
|
||||||
if (body.recipients.eventId) {
|
if (body.recipients.eventId) {
|
||||||
eventId = body.recipients.eventId;
|
eventId = body.recipients.eventId;
|
||||||
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;
|
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
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -161,14 +161,14 @@ const extraControllers = {
|
|||||||
_userIds = await _getUserIdsForEventId(eventId, segment);
|
_userIds = await _getUserIdsForEventId(eventId, segment);
|
||||||
} 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_userIds) {
|
if (_userIds) {
|
||||||
receipt = notificationService.sendNotification(notification, _userIds);
|
receipt = notificationService.sendNotification(notification, _userIds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*let getUserIds = async () => {
|
/*let getUserIds = async () => {
|
||||||
if (userIds) {
|
if (userIds) {
|
||||||
@ -200,22 +200,21 @@ 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());
|
||||||
|
|
||||||
|
return handleResultResponse(receipt, null, null, res, httpStatus.OK);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', error, res)
|
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', error, res)
|
||||||
} finally {
|
|
||||||
return handleResultResponse(receipt, null, null, res, httpStatus.OK);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
updateNotificationsWithReceipts: (config) => {
|
updateNotificationsWithReceipts: (config) => {
|
||||||
return async (req, res, next) => {
|
return async (req, res, next) => {
|
||||||
config = config || {
|
/*config = config || {
|
||||||
scopes: [],
|
scopes: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -224,8 +223,8 @@ const extraControllers = {
|
|||||||
|
|
||||||
let getNotificationsWithoutReceipt = async () => {
|
let getNotificationsWithoutReceipt = async () => {
|
||||||
let params = {
|
let params = {
|
||||||
where: { }
|
where: {}
|
||||||
};
|
};
|
||||||
return await notificationDetailService.fetchAll(params, context);
|
return await notificationDetailService.fetchAll(params, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,6 +233,7 @@ const extraControllers = {
|
|||||||
return Promise.all(userDeviceList.map(buildMessagePromise))
|
return Promise.all(userDeviceList.map(buildMessagePromise))
|
||||||
})
|
})
|
||||||
.then(sendNotificationsPromise)
|
.then(sendNotificationsPromise)
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -253,17 +253,16 @@ const extraControllers = {
|
|||||||
userId
|
userId
|
||||||
};
|
};
|
||||||
|
|
||||||
let params = extractParamsFromRequest(req, res, {
|
let params = extractParamsFromRequest(req, res, {
|
||||||
includeAll: false,
|
includeAll: false,
|
||||||
paginate: { limit: 1, page: 1 },
|
paginate: { limit: 1, page: 1 },
|
||||||
params: {
|
params: {
|
||||||
//userId,
|
|
||||||
token: data.token,
|
token: data.token,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Buscamos el token y el usuario
|
// Buscamos el token y el usuario
|
||||||
console.log('>> Busco el usuario y el token', params.params);
|
console.log('>> Busco el token', params.params);
|
||||||
let result = await userDeviceService.fetchOne(params, context);
|
let result = await userDeviceService.fetchOne(params, context);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
// Dar de alta el token
|
// Dar de alta el token
|
||||||
@ -272,16 +271,15 @@ const extraControllers = {
|
|||||||
} else {
|
} else {
|
||||||
// Actualizar el token
|
// Actualizar el token
|
||||||
console.log('>> Actualizar el token', params.params, data, context);
|
console.log('>> Actualizar el token', params.params, data, context);
|
||||||
result = await userDeviceService.update(params, { valid : 1 }, context);
|
result = await userDeviceService.update(params, { valid: 1 }, context);
|
||||||
}
|
}
|
||||||
} catch(error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
} finally {
|
|
||||||
// En todo caso devolver OK al cliente
|
|
||||||
return handleResultResponse('OK', null, null, res, httpStatus.OK);
|
|
||||||
}
|
}
|
||||||
|
// En todo caso devolver OK al cliente
|
||||||
|
return handleResultResponse('OK', null, null, res, httpStatus.OK);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = generateControllers(notificationService, extraControllers, controllerOptions);
|
module.exports = generateControllers(notificationService, extraControllers, controllerOptions);
|
||||||
@ -1,6 +1,6 @@
|
|||||||
const routes = require('express').Router();
|
const routes = require('express').Router();
|
||||||
|
|
||||||
const { isAdministratorUser, isLoggedUser } = require('../../middlewares/accessValidator');
|
const { isAdministratorUser, isOptionalUser, isLoggedUser } = require('../../middlewares/accessValidator');
|
||||||
const SchemaValidator = require('../../middlewares/schemaValidator');
|
const SchemaValidator = require('../../middlewares/schemaValidator');
|
||||||
const PaginateMiddleware = require('../../middlewares/paginate');
|
const PaginateMiddleware = require('../../middlewares/paginate');
|
||||||
const FieldMiddleware = require('../../middlewares/fields');
|
const FieldMiddleware = require('../../middlewares/fields');
|
||||||
@ -35,7 +35,7 @@ routes.get('/admin/notifications/:id',
|
|||||||
);
|
);
|
||||||
|
|
||||||
routes.post('/admin/notifications',
|
routes.post('/admin/notifications',
|
||||||
isAdministratorUser,
|
isAdministratorUser,
|
||||||
SchemaValidator(pushSendEvent, true),
|
SchemaValidator(pushSendEvent, true),
|
||||||
notificationController.sendNotification({
|
notificationController.sendNotification({
|
||||||
scopes: ['defaultScope']
|
scopes: ['defaultScope']
|
||||||
@ -56,7 +56,7 @@ routes.post('/admin/notifications',
|
|||||||
);*/
|
);*/
|
||||||
|
|
||||||
routes.post('/notifications/devices',
|
routes.post('/notifications/devices',
|
||||||
//isLoggedUser,
|
isOptionalUser,
|
||||||
SchemaValidator(deviceTokenInputType, true),
|
SchemaValidator(deviceTokenInputType, true),
|
||||||
notificationController.registerDevice()
|
notificationController.registerDevice()
|
||||||
);
|
);
|
||||||
|
|||||||
@ -33,7 +33,7 @@ const extraMethods = {
|
|||||||
data: data,
|
data: data,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
sendNotification: (notification, userIds) => {
|
sendNotification: (notification, userIds) => {
|
||||||
|
|
||||||
@ -132,14 +132,14 @@ const extraMethods = {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
getNotificationsWithoutReceipt: async() => {
|
getNotificationsWithoutReceipt: async () => {
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
updateNotificationsWithReceipts: async (receiptIds) => {
|
updateNotificationsWithReceipts: async (receiptIds) => {
|
||||||
|
|
||||||
let receiptIdChunks = expo.chunkPushNotificationReceiptIds(receiptIds);
|
//let receiptIdChunks = expo.chunkPushNotificationReceiptIds(receiptIds);
|
||||||
let xxx = await _getPushNotificationsResultAsync(receiptIdChunks);
|
//let xxx = await _getPushNotificationsResultAsync(receiptIdChunks);
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user