From 68a2f7fc5251df784fc9f5fa69bec3c209fecf51 Mon Sep 17 00:00:00 2001 From: david Date: Thu, 17 Oct 2019 18:59:18 +0200 Subject: [PATCH] =?UTF-8?q?Gesti=C3=B3n=20de=20notificaciones?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notification.controller.js} | 23 ++++---- .../notification.model.js | 0 modules/notification/notification.routes.js | 56 +++++++++++++++++++ .../notification.service.js} | 10 +--- .../notification.validations.js} | 4 +- .../notification_detail.model.js | 0 .../user_device.model.js | 0 modules/notification/user_device.service.js | 18 ++++++ modules/push/push.routes.js | 52 ----------------- 9 files changed, 90 insertions(+), 73 deletions(-) rename modules/{push/push.controller.js => notification/notification.controller.js} (83%) rename modules/{push => notification}/notification.model.js (100%) create mode 100644 modules/notification/notification.routes.js rename modules/{push/push.service.js => notification/notification.service.js} (96%) rename modules/{push/push.validations.js => notification/notification.validations.js} (76%) rename modules/{push => notification}/notification_detail.model.js (100%) rename modules/{push => notification}/user_device.model.js (100%) create mode 100644 modules/notification/user_device.service.js delete mode 100644 modules/push/push.routes.js diff --git a/modules/push/push.controller.js b/modules/notification/notification.controller.js similarity index 83% rename from modules/push/push.controller.js rename to modules/notification/notification.controller.js index 8c691d0..1cf2fd1 100644 --- a/modules/push/push.controller.js +++ b/modules/notification/notification.controller.js @@ -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); \ No newline at end of file +module.exports = generateControllers(notificationService, extraControllers, controllerOptions); \ No newline at end of file diff --git a/modules/push/notification.model.js b/modules/notification/notification.model.js similarity index 100% rename from modules/push/notification.model.js rename to modules/notification/notification.model.js diff --git a/modules/notification/notification.routes.js b/modules/notification/notification.routes.js new file mode 100644 index 0000000..52b1445 --- /dev/null +++ b/modules/notification/notification.routes.js @@ -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; \ No newline at end of file diff --git a/modules/push/push.service.js b/modules/notification/notification.service.js similarity index 96% rename from modules/push/push.service.js rename to modules/notification/notification.service.js index 3a97f1e..4e6dfa8 100644 --- a/modules/push/push.service.js +++ b/modules/notification/notification.service.js @@ -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, diff --git a/modules/push/push.validations.js b/modules/notification/notification.validations.js similarity index 76% rename from modules/push/push.validations.js rename to modules/notification/notification.validations.js index d51f9fa..ca8fdcf 100644 --- a/modules/push/push.validations.js +++ b/modules/notification/notification.validations.js @@ -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 }; diff --git a/modules/push/notification_detail.model.js b/modules/notification/notification_detail.model.js similarity index 100% rename from modules/push/notification_detail.model.js rename to modules/notification/notification_detail.model.js diff --git a/modules/push/user_device.model.js b/modules/notification/user_device.model.js similarity index 100% rename from modules/push/user_device.model.js rename to modules/notification/user_device.model.js diff --git a/modules/notification/user_device.service.js b/modules/notification/user_device.service.js new file mode 100644 index 0000000..1a903e8 --- /dev/null +++ b/modules/notification/user_device.service.js @@ -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); diff --git a/modules/push/push.routes.js b/modules/push/push.routes.js deleted file mode 100644 index 0ca10c1..0000000 --- a/modules/push/push.routes.js +++ /dev/null @@ -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; \ No newline at end of file