Gestión de notificaciones
This commit is contained in:
parent
dbd615086a
commit
68a2f7fc52
@ -1,14 +1,15 @@
|
||||
'use strict';
|
||||
const httpStatus = require('http-status');
|
||||
const Sequelize = require('sequelize');
|
||||
|
||||
const generateControllers = require('../../core/controllers');
|
||||
const { buildContext } = require('../../core/controllers');
|
||||
const pushService = require('./push.service');
|
||||
const userService = require('../auth/auth.service');
|
||||
const notificationService = require('./notification.service');
|
||||
const userDeviceService = require('./user_device.service');
|
||||
|
||||
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
|
||||
|
||||
// Module Name
|
||||
const MODULE_NAME = '[push.controller]';
|
||||
const MODULE_NAME = '[notification.controller]';
|
||||
const controllerOptions = { MODULE_NAME };
|
||||
|
||||
const extraControllers = {
|
||||
@ -32,13 +33,13 @@ const extraControllers = {
|
||||
}
|
||||
|
||||
try {
|
||||
let getUserDevicesPromise = (userId) => pushService.fetchAll({ params: { userId: userId }}, context);
|
||||
let sendNotificationsPromise = (messages) => pushService.sendNotification(messages);
|
||||
let getUserDevicesPromise = (userId) => userDeviceService.fetchAll({ params: { userId: userId }}, context);
|
||||
let sendNotificationsPromise = (messages) => notificationService.sendNotification(messages);
|
||||
|
||||
let buildMessagePromise = (userDevices) => {
|
||||
let message = undefined;
|
||||
userDevices.rows.forEach(function (userDevice) {
|
||||
if (pushService.isValidPushToken(userDevice.token)) {
|
||||
if (notificationService.isValidPushToken(userDevice.token)) {
|
||||
message = {
|
||||
userId: userDevice.userId,
|
||||
to: userDevice.token,
|
||||
@ -95,15 +96,15 @@ const extraControllers = {
|
||||
|
||||
// Buscamos el token y el usuario
|
||||
console.log('>> Busco el usuario y el token', params.params);
|
||||
let result = await pushService.fetchOne(params, context);
|
||||
let result = await notificationService.fetchOne(params, context);
|
||||
if (!result) {
|
||||
// Dar de alta el token
|
||||
console.log('>> Dar de alta el token', data);
|
||||
result = await pushService.create(data, context);
|
||||
result = await notificationService.create(data, context);
|
||||
} else {
|
||||
// Actualizar el token
|
||||
console.log('>> Actualizar el token', params.params, data, context);
|
||||
result = await pushService.update(params.params, data, context);
|
||||
result = await notificationService.update(params.params, data, context);
|
||||
}
|
||||
} catch(error) {
|
||||
console.error(error);
|
||||
@ -115,4 +116,4 @@ const extraControllers = {
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = generateControllers(pushService, extraControllers, controllerOptions);
|
||||
module.exports = generateControllers(notificationService, extraControllers, controllerOptions);
|
||||
56
modules/notification/notification.routes.js
Normal file
56
modules/notification/notification.routes.js
Normal file
@ -0,0 +1,56 @@
|
||||
const routes = require('express').Router();
|
||||
|
||||
const { isAdministratorUser, isLoggedUser } = require('../../middlewares/accessValidator');
|
||||
const SchemaValidator = require('../../middlewares/schemaValidator');
|
||||
const PaginateMiddleware = require('../../middlewares/paginate');
|
||||
const FieldMiddleware = require('../../middlewares/fields');
|
||||
const notificationController = require('./notification.controller');
|
||||
const { deviceTokenInputType, notificationSendType } = require('./notification.validations');
|
||||
|
||||
const generalInvalidFields = [
|
||||
'createdAt', 'updatedAt',
|
||||
];
|
||||
|
||||
routes.get('/notifications',
|
||||
isAdministratorUser,
|
||||
FieldMiddleware.middleware({
|
||||
invalidFields: generalInvalidFields
|
||||
}),
|
||||
PaginateMiddleware.middleware(),
|
||||
notificationController.find({
|
||||
scopes: ['defaultScope']
|
||||
})
|
||||
);
|
||||
|
||||
routes.get('/notifications/:id',
|
||||
isAdministratorUser,
|
||||
FieldMiddleware.middleware({
|
||||
invalidFields: generalInvalidFields
|
||||
}),
|
||||
notificationController.findOne({
|
||||
scopes: ['defaultScope']
|
||||
})
|
||||
);
|
||||
|
||||
routes.post('/notifications',
|
||||
isAdministratorUser,
|
||||
SchemaValidator(notificationSendType, true),
|
||||
notificationController.sendNotification({
|
||||
scopes: ['defaultScope']
|
||||
})
|
||||
);
|
||||
|
||||
/* Borrar cuando ya no aparezca la versión 1.0.10 */
|
||||
routes.post('/notifications/register',
|
||||
isLoggedUser,
|
||||
SchemaValidator(deviceTokenInputType, true),
|
||||
notificationController.registerUser()
|
||||
);
|
||||
|
||||
routes.post('/notifications/devices',
|
||||
isLoggedUser,
|
||||
SchemaValidator(deviceTokenInputType, true),
|
||||
notificationController.registerUser()
|
||||
);
|
||||
|
||||
module.exports = routes;
|
||||
@ -12,12 +12,6 @@ const extraMethods = {
|
||||
return Expo.isExpoPushToken(token);
|
||||
},
|
||||
|
||||
getPushToken: (params) => {
|
||||
return models.UserDevice.findOne({
|
||||
where: params,
|
||||
});
|
||||
},
|
||||
|
||||
sendNotification: async (messages) => {
|
||||
|
||||
// The Expo push notification service accepts batches of notifications so
|
||||
@ -83,7 +77,7 @@ const extraMethods = {
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = generateService(models.UserDevice, extraMethods);
|
||||
module.exports = generateService(models.Notification, extraMethods);
|
||||
|
||||
|
||||
const _sendPushNotificationsAsync = async function (chunks) {
|
||||
@ -149,7 +143,7 @@ const _getPushNotificationsResultAsync = async function (receiptIdChunks) {
|
||||
const _saveNotifications = async function (messages, tickets) {
|
||||
let notifications = [];
|
||||
messages.forEach(function (message, index) {
|
||||
let notification = models.Notification.build({
|
||||
let notification = models.NotificationDetail.build({
|
||||
...message,
|
||||
ticket: tickets[index].id,
|
||||
status: tickets[index].status,
|
||||
@ -1,6 +1,6 @@
|
||||
const Joi = require('joi');
|
||||
|
||||
const pushInputType = Joi.object().keys({
|
||||
const deviceTokenInputType = Joi.object().keys({
|
||||
token: Joi.string().required(),
|
||||
});
|
||||
|
||||
@ -12,5 +12,5 @@ const pushSendType = Joi.object().keys({
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
pushInputType,
|
||||
deviceTokenInputType, pushSendType
|
||||
};
|
||||
18
modules/notification/user_device.service.js
Normal file
18
modules/notification/user_device.service.js
Normal file
@ -0,0 +1,18 @@
|
||||
const moment = require('moment');
|
||||
const { Expo } = require('expo-server-sdk');
|
||||
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
|
||||
const models = require('../../core/models');
|
||||
|
||||
// Create a new Expo SDK client
|
||||
const expo = new Expo();
|
||||
|
||||
const extraMethods = {
|
||||
|
||||
getPushToken: (params) => {
|
||||
return models.UserDevice.findOne({
|
||||
where: params,
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = generateService(models.UserDevice, extraMethods);
|
||||
@ -1,52 +0,0 @@
|
||||
const routes = require('express').Router();
|
||||
|
||||
const { isAdministratorUser, isLoggedUser } = require('../../middlewares/accessValidator');
|
||||
const SchemaValidator = require('../../middlewares/schemaValidator');
|
||||
|
||||
const FieldMiddleware = require('../../middlewares/fields');
|
||||
const pushTokenController = require('./push.controller');
|
||||
const { pushInputType, pushSendType } = require('./push.validations');
|
||||
|
||||
const generalInvalidFields = [
|
||||
'createdAt', 'updatedAt',
|
||||
];
|
||||
|
||||
routes.get('/notifications',
|
||||
/*isAdministratorUser,
|
||||
SchemaValidator(pushSendType, true),
|
||||
pushTokenController.sendNotification({
|
||||
scopes: ['defaultScope']
|
||||
})*/
|
||||
);
|
||||
|
||||
routes.get('/notifications/:id',
|
||||
/*isAdministratorUser,
|
||||
SchemaValidator(pushSendType, true),
|
||||
pushTokenController.sendNotification({
|
||||
scopes: ['defaultScope']
|
||||
})*/
|
||||
);
|
||||
|
||||
routes.post('/notifications',
|
||||
isAdministratorUser,
|
||||
SchemaValidator(pushSendType, true),
|
||||
pushTokenController.sendNotification({
|
||||
scopes: ['defaultScope']
|
||||
})
|
||||
);
|
||||
|
||||
/* Borrar cuando ya no aparezca la versión 1.0.10 */
|
||||
routes.post('/notifications/register',
|
||||
isLoggedUser,
|
||||
SchemaValidator(pushInputType, true),
|
||||
pushTokenController.registerUser()
|
||||
);
|
||||
|
||||
routes.post('/notifications/devices',
|
||||
isLoggedUser,
|
||||
SchemaValidator(pushInputType, true),
|
||||
pushTokenController.registerUser()
|
||||
);
|
||||
|
||||
|
||||
module.exports = routes;
|
||||
Loading…
Reference in New Issue
Block a user