Notificaciones push por países
This commit is contained in:
parent
444d364f1a
commit
ebfc1908d0
@ -3,10 +3,7 @@
|
|||||||
|
|
||||||
const _ = require("lodash");
|
const _ = require("lodash");
|
||||||
const moment = require("moment");
|
const moment = require("moment");
|
||||||
const {
|
const { generateService, parseParamsToFindOptions } = require("../../helpers/service.helper");
|
||||||
generateService,
|
|
||||||
parseParamsToFindOptions,
|
|
||||||
} = require("../../helpers/service.helper");
|
|
||||||
const models = require("../../core/models");
|
const models = require("../../core/models");
|
||||||
const Sequelize = require("sequelize");
|
const Sequelize = require("sequelize");
|
||||||
moment.locale("es");
|
moment.locale("es");
|
||||||
@ -28,12 +25,13 @@ const extraMethods = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
_getActiveUserIds: async (offset = 0, limit = 10) => {
|
_getActiveUserIds: async (offset = 0, limit = 10, country = "ES") => {
|
||||||
return models.User.findAndCountAll({
|
return models.User.findAndCountAll({
|
||||||
attributes: ["id"],
|
attributes: ["id"],
|
||||||
where: {
|
where: {
|
||||||
state: "active",
|
state: "active",
|
||||||
phone: { [Sequelize.Op.ne]: null },
|
phone: { [Sequelize.Op.ne]: null },
|
||||||
|
country: country === "*" ? { [Sequelize.Op.ne]: null } : country,
|
||||||
},
|
},
|
||||||
raw: true,
|
raw: true,
|
||||||
limit: limit,
|
limit: limit,
|
||||||
|
|||||||
@ -1,218 +1,240 @@
|
|||||||
'use strict';
|
"use strict";
|
||||||
const httpStatus = require('http-status');
|
const httpStatus = require("http-status");
|
||||||
const moment = require('moment');
|
const moment = require("moment");
|
||||||
const generateControllers = require('../../core/controllers');
|
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 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");
|
||||||
const pushHelper = require('../../helpers/push.helper');
|
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 {
|
||||||
const { get } = require('lodash');
|
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) {
|
async function _sendNotificationAllActiveUsers(notification) {
|
||||||
let limit = 33;
|
let limit = 33;
|
||||||
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, notification.recipients.country);
|
||||||
|
|
||||||
if (totalPages == -1) {
|
if (totalPages == -1) {
|
||||||
totalPages = Math.ceil(result.count / limit);
|
totalPages = Math.ceil(result.count / limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
page = page + 1;
|
page = page + 1;
|
||||||
|
|
||||||
let ids = result.rows.map(function (item) { return item.id });
|
let ids = result.rows.map(function (item) {
|
||||||
notificationService.sendNotificationToUsers(notification, ids);
|
return item.id;
|
||||||
|
});
|
||||||
|
notificationService.sendNotificationToUsers(notification, ids);
|
||||||
|
} while (page <= totalPages);
|
||||||
|
|
||||||
|
return "OK";
|
||||||
} while (page <= totalPages);
|
|
||||||
|
|
||||||
return 'OK';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function _sendNotificationAllActiveDevices(notification) {
|
async function _sendNotificationAllActiveDevices(notification) {
|
||||||
let limit = 33;
|
let limit = 33;
|
||||||
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 userDeviceService._getActiveDeviceIds(offset, limit);
|
let result = await userDeviceService._getActiveDeviceIds(offset, limit, notification.recipients.country);
|
||||||
|
|
||||||
console.log(result);
|
console.log(result);
|
||||||
|
|
||||||
if (totalPages == -1) {
|
if (totalPages == -1) {
|
||||||
totalPages = Math.ceil(result.count / limit);
|
totalPages = Math.ceil(result.count / limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
page = page + 1;
|
page = page + 1;
|
||||||
|
|
||||||
notificationService.sendNotificationToDevices(notification, result.rows);
|
notificationService.sendNotificationToDevices(notification, result.rows);
|
||||||
} 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
|
||||||
case 'ALL_VALIDATED':
|
case "ALL_VALIDATED":
|
||||||
userIds = await eventInscriptionService._getInscriptionByEventAndValidated(eventId, true);
|
userIds = await eventInscriptionService._getInscriptionByEventAndValidated(eventId, true);
|
||||||
break;
|
break;
|
||||||
//Todos los de lista de espera tanto invitados como libres (Actualmente en invitados no hay lista de espera)
|
//Todos los de lista de espera tanto invitados como libres (Actualmente en invitados no hay lista de espera)
|
||||||
case 'ALL_NOT_VALIDATED':
|
case "ALL_NOT_VALIDATED":
|
||||||
userIds = await eventInscriptionService._getInscriptionByEventAndValidated(eventId, false);
|
userIds = await eventInscriptionService._getInscriptionByEventAndValidated(eventId, false);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//Solo invitados como actualmente no se usa codigo de reserva para los coles, vale con filtrar por aquellos que tengan codigo de reserva
|
//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':
|
case "PARTNERS_ALL":
|
||||||
userIds = await eventInscriptionService._getInscriptionByEventFromPartner(eventId);
|
userIds = await eventInscriptionService._getInscriptionByEventFromPartner(eventId);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//Todos los inscritos al evento, tanto validados como en lista de espera
|
//Todos los inscritos al evento, tanto validados como en lista de espera
|
||||||
default: //ALL
|
default: //ALL
|
||||||
userIds = await eventInscriptionService._getInscriptionByEvent(eventId);
|
userIds = await eventInscriptionService._getInscriptionByEvent(eventId);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return usersIdsComposer(userIds);
|
return usersIdsComposer(userIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const extraControllers = {
|
const extraControllers = {
|
||||||
|
sendNotification: (config) => {
|
||||||
|
/**
|
||||||
|
* notificationSample = {
|
||||||
|
* "title": "título de la notificación",
|
||||||
|
* "body": "cuerpo de la notificación",
|
||||||
|
* "recipients": {
|
||||||
|
* OPCION 1- Unos usuarios determinados
|
||||||
|
* "userIds": ["*" | "f428a317-6d1f-4eda-aa3e-22baff3f48d7", ...]
|
||||||
|
* "segment": "ALL" | "LAST_YEAR" | "LAST_MONTH"
|
||||||
|
* "country": "*" | "ES" | "MX"
|
||||||
|
*
|
||||||
|
* OPCION 2 - A todos los usuarios inscritos a un evento, se puede segmentar
|
||||||
|
* "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"
|
||||||
|
* OPCION 3 - A todos los dispositivos activos
|
||||||
|
* "deviceIds": ["*" | "ExponentPushToken[YbOS1AIZjQbchZbxNaVRqC]", ...]
|
||||||
|
* "country": "*" | "ES" | "MX"
|
||||||
|
* },
|
||||||
|
* "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",
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*}
|
||||||
|
*/
|
||||||
|
|
||||||
sendNotification: (config) => {
|
return async (req, res, next) => {
|
||||||
|
config = config || {
|
||||||
|
scopes: [],
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
const context = buildContext(req, config);
|
||||||
* notificationSample = {
|
let params = extractParamsFromRequest(req, res);
|
||||||
* "title": "título de la notificación",
|
|
||||||
* "body": "cuerpo de la notificación",
|
|
||||||
* "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",
|
|
||||||
* "segment": "ALL" | "ALL_VALIDATED" | "ALL_NOT_VALIDATED" |
|
|
||||||
* "PARTNERS_ALL" | "PARTNERS_VALIDATED" | "PARTNERS_NOT_VALIDATED" |
|
|
||||||
* "COLLEGE_ALL" | "COLLEGE_VALIDATED" | "COLLEGE_NOT_VALIDATED"
|
|
||||||
* OPCION 3 - A todos los dispositivos activos
|
|
||||||
* "deviceIds": ["*" | "ExponentPushToken[YbOS1AIZjQbchZbxNaVRqC]", ...]
|
|
||||||
* },
|
|
||||||
* "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) => {
|
let receipt = undefined;
|
||||||
config = config || {
|
let userIds = undefined;
|
||||||
scopes: [],
|
let deviceIds = undefined;
|
||||||
};
|
let eventId = undefined;
|
||||||
|
let segment = undefined;
|
||||||
|
let country = undefined;
|
||||||
|
|
||||||
const context = buildContext(req, config);
|
const { body } = req;
|
||||||
let params = extractParamsFromRequest(req, res);
|
|
||||||
|
|
||||||
let receipt = undefined;
|
if (!body.title) {
|
||||||
let userIds = undefined;
|
return handleErrorResponse(
|
||||||
let deviceIds = undefined;
|
controllerOptions.MODULE_NAME,
|
||||||
let eventId = undefined;
|
"sendNotification",
|
||||||
let segment = undefined;
|
new Error("Missing message title"),
|
||||||
const { body } = req;
|
res
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (!body.title) {
|
if (!body.body) {
|
||||||
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing message title'), res)
|
return handleErrorResponse(
|
||||||
}
|
controllerOptions.MODULE_NAME,
|
||||||
|
"sendNotification",
|
||||||
|
new Error("Missing body content"),
|
||||||
|
res
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (!body.body) {
|
// Evento?
|
||||||
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing body content'), res)
|
if (body.recipients.eventId) {
|
||||||
}
|
eventId = body.recipients.eventId;
|
||||||
|
segment = body.recipients.segment;
|
||||||
|
} else if (body.recipients.userIds) {
|
||||||
|
userIds = body.recipients.userIds;
|
||||||
|
segment = body.recipients.segment;
|
||||||
|
country = body.recipients.country;
|
||||||
|
} else if (body.recipients.deviceIds) {
|
||||||
|
deviceIds = body.recipients.deviceIds;
|
||||||
|
country = body.recipients.country;
|
||||||
|
} else {
|
||||||
|
return handleErrorResponse(
|
||||||
|
controllerOptions.MODULE_NAME,
|
||||||
|
"sendNotification",
|
||||||
|
new Error("Missing user Ids or event Ids or devices Ids"),
|
||||||
|
res
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
let notification = notificationHelper.createNotification({
|
||||||
|
...body,
|
||||||
|
userId: context.user.id,
|
||||||
|
});
|
||||||
|
|
||||||
// Evento?
|
if (userIds && userIds.length == 1 && userIds[0] == "*") {
|
||||||
if (body.recipients.eventId) {
|
receipt = _sendNotificationAllActiveUsers(notification);
|
||||||
eventId = body.recipients.eventId;
|
} else if (deviceIds && deviceIds.length == 1 && deviceIds[0] == "*") {
|
||||||
segment = body.recipients.segment;
|
receipt = _sendNotificationAllActiveDevices(notification);
|
||||||
} else if (body.recipients.userIds) {
|
} else {
|
||||||
userIds = body.recipients.userIds;
|
let _userIds = null;
|
||||||
segment = body.recipients.segment;
|
|
||||||
} else if (body.recipients.deviceIds) {
|
if (userIds) {
|
||||||
deviceIds = body.recipients.deviceIds;
|
_userIds = userIds;
|
||||||
|
} else {
|
||||||
|
if (eventId) {
|
||||||
|
_userIds = await _getUserIdsForEventId(eventId, segment);
|
||||||
} else {
|
} else {
|
||||||
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing user Ids or event Ids or devices Ids'), res)
|
return handleErrorResponse(
|
||||||
|
controllerOptions.MODULE_NAME,
|
||||||
|
"sendNotification",
|
||||||
|
new Error("Missing event and segment"),
|
||||||
|
res
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_userIds) {
|
||||||
try {
|
receipt = notificationService.sendNotificationToUsers(notification, _userIds);
|
||||||
let notification = notificationHelper.createNotification({
|
}
|
||||||
...body,
|
|
||||||
userId: context.user.id
|
|
||||||
});
|
|
||||||
|
|
||||||
if ((userIds) && (userIds.length == 1) && (userIds[0] == "*")) {
|
|
||||||
receipt = _sendNotificationAllActiveUsers(notification);
|
|
||||||
} else if ((deviceIds) && (deviceIds.length == 1) && (deviceIds[0] == "*")) {
|
|
||||||
receipt = _sendNotificationAllActiveDevices(notification);
|
|
||||||
} else {
|
|
||||||
|
|
||||||
let _userIds = null;
|
|
||||||
|
|
||||||
if (userIds) {
|
|
||||||
_userIds = userIds;
|
|
||||||
} else {
|
|
||||||
if (eventId) {
|
|
||||||
_userIds = await _getUserIdsForEventId(eventId, segment);
|
|
||||||
} else {
|
|
||||||
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing event and segment'), res)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_userIds) {
|
|
||||||
receipt = notificationService.sendNotificationToUsers(notification, _userIds);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return handleResultResponse(receipt, null, null, res, httpStatus.OK);
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', error, res)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
|
|
||||||
updateNotificationsWithReceipts: (config) => {
|
return handleResultResponse(receipt, null, null, res, httpStatus.OK);
|
||||||
return async (req, res, next) => {
|
} catch (error) {
|
||||||
/*config = config || {
|
console.error(error);
|
||||||
|
return handleErrorResponse(controllerOptions.MODULE_NAME, "sendNotification", error, res);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
updateNotificationsWithReceipts: (config) => {
|
||||||
|
return async (req, res, next) => {
|
||||||
|
/*config = config || {
|
||||||
scopes: [],
|
scopes: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -232,56 +254,56 @@ const extraControllers = {
|
|||||||
})
|
})
|
||||||
.then(sendNotificationsPromise)
|
.then(sendNotificationsPromise)
|
||||||
*/
|
*/
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
registerDevice: (config) => {
|
||||||
|
return async (req, res, next) => {
|
||||||
|
config = config || {
|
||||||
|
scopes: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const context = buildContext(req, config);
|
||||||
|
const userId = context.user && context.user.id ? context.user.id : null;
|
||||||
|
|
||||||
|
let data = {
|
||||||
|
token: req.body.token,
|
||||||
|
valid: 1,
|
||||||
|
country: req.body.country,
|
||||||
|
userId,
|
||||||
|
};
|
||||||
|
|
||||||
|
let params = extractParamsFromRequest(req, res, {
|
||||||
|
includeAll: false,
|
||||||
|
paginate: { limit: 1, page: 1 },
|
||||||
|
params: {
|
||||||
|
token: data.token,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(req.body, data, params);
|
||||||
|
|
||||||
|
// Buscamos el token y el usuario
|
||||||
|
console.log(">> Busco el token", params.params);
|
||||||
|
let result = await userDeviceService.fetchOne(params, context);
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
// Borramos el registro donde aparece el token
|
||||||
|
console.log(">> Borro el registro del token", params.params, data, context);
|
||||||
|
result = await userDeviceService.delete(params, context);
|
||||||
}
|
}
|
||||||
},
|
|
||||||
|
|
||||||
registerDevice: (config) => {
|
// Dar de alta el token
|
||||||
return async (req, res, next) => {
|
console.log(">> Dar de alta el token", data);
|
||||||
config = config || {
|
result = await userDeviceService.create(data, context);
|
||||||
scopes: [],
|
} catch (error) {
|
||||||
};
|
console.error(error);
|
||||||
|
}
|
||||||
|
// En todo caso devolver OK al cliente
|
||||||
try {
|
return handleResultResponse("OK", null, null, res, httpStatus.OK);
|
||||||
const context = buildContext(req, config);
|
};
|
||||||
const userId = context.user && context.user.id ? context.user.id : null;
|
},
|
||||||
|
|
||||||
|
|
||||||
let data = {
|
|
||||||
token: req.body.token,
|
|
||||||
valid: 1,
|
|
||||||
userId
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
let params = extractParamsFromRequest(req, res, {
|
|
||||||
includeAll: false,
|
|
||||||
paginate: { limit: 1, page: 1 },
|
|
||||||
params: {
|
|
||||||
token: data.token,
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Buscamos el token y el usuario
|
|
||||||
console.log('>> Busco el token', params.params);
|
|
||||||
let result = await userDeviceService.fetchOne(params, context);
|
|
||||||
|
|
||||||
if (result) {
|
|
||||||
// Borramos el registro donde aparece el token
|
|
||||||
console.log('>> Borro el registro del token', params.params, data, context);
|
|
||||||
result = await userDeviceService.delete(params, context);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dar de alta el token
|
|
||||||
console.log('>> Dar de alta el token', data);
|
|
||||||
result = await userDeviceService.create(data, context);
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
// 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,7 +1,8 @@
|
|||||||
const Joi = require('joi');
|
const Joi = require("joi");
|
||||||
|
|
||||||
const deviceTokenInputType = Joi.object().keys({
|
const deviceTokenInputType = Joi.object().keys({
|
||||||
token: Joi.string().required(),
|
token: Joi.string().required(),
|
||||||
|
country: Joi.string().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
/*const pushSendType = Joi.object().keys({
|
/*const pushSendType = Joi.object().keys({
|
||||||
@ -12,30 +13,32 @@ const deviceTokenInputType = Joi.object().keys({
|
|||||||
});*/
|
});*/
|
||||||
|
|
||||||
const pushSendEvent = Joi.object().keys({
|
const pushSendEvent = Joi.object().keys({
|
||||||
date: Joi.date().optional(),
|
date: Joi.date().optional(),
|
||||||
|
title: Joi.string().required(),
|
||||||
|
body: Joi.string().required(),
|
||||||
|
recipients: Joi.object().keys({
|
||||||
|
userIds: Joi.array().optional(),
|
||||||
|
deviceIds: Joi.array().optional(),
|
||||||
|
eventId: Joi.string().optional(),
|
||||||
|
segment: Joi.string().optional(),
|
||||||
|
country: Joi.string().optional(),
|
||||||
|
}),
|
||||||
|
priority: Joi.string().optional(),
|
||||||
|
ttl: Joi.string().optional(),
|
||||||
|
data: Joi.object().keys({
|
||||||
|
type: Joi.string().required(),
|
||||||
title: Joi.string().required(),
|
title: Joi.string().required(),
|
||||||
body: Joi.string().required(),
|
message: Joi.string().required(),
|
||||||
recipients: Joi.object().keys({
|
button: Joi.object().keys({
|
||||||
userIds: Joi.array().optional(),
|
caption: Joi.string().required(),
|
||||||
deviceIds: Joi.array().optional(),
|
url: Joi.string().optional(),
|
||||||
eventId: Joi.string().optional(),
|
screen: Joi.string().optional(),
|
||||||
segment: Joi.string().optional(),
|
paramId: Joi.string().optional(),
|
||||||
}),
|
|
||||||
priority: Joi.string().optional(),
|
|
||||||
ttl: Joi.string().optional(),
|
|
||||||
data: Joi.object().keys({
|
|
||||||
type: Joi.string().required(),
|
|
||||||
title: Joi.string().required(),
|
|
||||||
message: Joi.string().required(),
|
|
||||||
button: Joi.object().keys({
|
|
||||||
caption: Joi.string().required(),
|
|
||||||
url: Joi.string().optional(),
|
|
||||||
screen: Joi.string().optional(),
|
|
||||||
paramId: Joi.string().optional(),
|
|
||||||
}),
|
|
||||||
}),
|
}),
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
deviceTokenInputType, pushSendEvent
|
deviceTokenInputType,
|
||||||
|
pushSendEvent,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,37 +1,44 @@
|
|||||||
module.exports = function (sequelize, DataTypes) {
|
module.exports = function (sequelize, DataTypes) {
|
||||||
const UserDevice = sequelize.define('UserDevice', {
|
const UserDevice = sequelize.define(
|
||||||
id: {
|
"UserDevice",
|
||||||
type: DataTypes.UUID,
|
{
|
||||||
defaultValue: DataTypes.UUIDV4,
|
id: {
|
||||||
primaryKey: true,
|
type: DataTypes.UUID,
|
||||||
},
|
defaultValue: DataTypes.UUIDV4,
|
||||||
token: {
|
primaryKey: true,
|
||||||
type: DataTypes.STRING,
|
},
|
||||||
allowNull: false,
|
token: {
|
||||||
},
|
type: DataTypes.STRING,
|
||||||
valid: {
|
allowNull: false,
|
||||||
type: DataTypes.BOOLEAN,
|
},
|
||||||
allowNull: false,
|
valid: {
|
||||||
},
|
type: DataTypes.BOOLEAN,
|
||||||
invalidated: {
|
allowNull: false,
|
||||||
type: DataTypes.DATE,
|
},
|
||||||
},
|
invalidated: {
|
||||||
}, {
|
type: DataTypes.DATE,
|
||||||
tableName: 'users_devices',
|
},
|
||||||
freezeTableName: true,
|
country: {
|
||||||
timestamps: true,
|
type: DataTypes.STRING,
|
||||||
|
defaultValue: "ES",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tableName: "users_devices",
|
||||||
|
freezeTableName: true,
|
||||||
|
timestamps: true,
|
||||||
|
|
||||||
defaultScope: {
|
defaultScope: {
|
||||||
where: {
|
where: {
|
||||||
valid: true,
|
valid: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
UserDevice.associate = function (models) {
|
UserDevice.associate = function (models) {
|
||||||
UserDevice.User = UserDevice.belongsTo(models.User, { foreignKey: 'userId', as: "user" });
|
UserDevice.User = UserDevice.belongsTo(models.User, { foreignKey: "userId", as: "user" });
|
||||||
};
|
};
|
||||||
|
|
||||||
return UserDevice;
|
return UserDevice;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1,44 +1,44 @@
|
|||||||
const moment = require('moment');
|
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");
|
||||||
|
const { Sequelize } = require("sequelize");
|
||||||
|
|
||||||
const extraMethods = {
|
const extraMethods = {
|
||||||
isValidPushToken: (token) => {
|
isValidPushToken: (token) => {
|
||||||
return Expo.isExpoPushToken(token);
|
return Expo.isExpoPushToken(token);
|
||||||
},
|
},
|
||||||
|
|
||||||
afterFetchAll: (result, params, context) => {
|
afterFetchAll: (result, params, context) => {
|
||||||
|
if (!result.count) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
if (!result.count) {
|
let rows = result.rows.map((row) => row.toJSON());
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
let rows = result.rows.map(row => row.toJSON());
|
return {
|
||||||
|
count: result.count,
|
||||||
|
rows: rows,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
return {
|
getPushToken: (params) => {
|
||||||
count: result.count,
|
return models.UserDevice.findOne({
|
||||||
rows: rows
|
where: params,
|
||||||
}
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
_getActiveDeviceIds: async (offset = 0, limit = 10, country = "ES") => {
|
||||||
getPushToken: (params) => {
|
return models.UserDevice.findAndCountAll({
|
||||||
return models.UserDevice.findOne({
|
where: {
|
||||||
where: params,
|
valid: 1,
|
||||||
});
|
country: country === "*" ? { [Sequelize.Op.ne]: null } : country,
|
||||||
},
|
|
||||||
|
|
||||||
_getActiveDeviceIds: async (offset = 0, limit = 10) => {
|
|
||||||
return models.UserDevice.findAndCountAll({
|
|
||||||
where: {
|
|
||||||
valid: 1,
|
|
||||||
},
|
|
||||||
raw: true,
|
|
||||||
limit: limit,
|
|
||||||
offset: offset,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
raw: true,
|
||||||
|
limit: limit,
|
||||||
|
offset: offset,
|
||||||
|
});
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = generateService(models.UserDevice, extraMethods);
|
module.exports = generateService(models.UserDevice, extraMethods);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user