157 lines
5.5 KiB
JavaScript
157 lines
5.5 KiB
JavaScript
const moment = require('moment');
|
|
|
|
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
|
|
const userDeviceService = require('./user_device.service');
|
|
const notificationDetailService = require('./notification_detail.service');
|
|
const pushHelper = require('../../helpers/push.helper');
|
|
const models = require('../../core/models');
|
|
|
|
const extraMethods = {
|
|
|
|
createNotification: (data) => {
|
|
return {
|
|
date: data.date,
|
|
title: data.title,
|
|
body: data.message,
|
|
ttl: data.ttl,
|
|
priority: data.priority,
|
|
recipients: data.recipients,
|
|
data: data.data,
|
|
userId: data.userId,
|
|
}
|
|
},
|
|
|
|
saveNotification: ({ date, title, body, ttl, priority, recipients, data, userId }) => {
|
|
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<valores de la notificacion');
|
|
console.log(date, title, body, ttl, priority, recipients, data, userId);
|
|
|
|
return models.Notification.create({
|
|
date: moment(date),
|
|
title: title,
|
|
body: body,
|
|
userId: userId,
|
|
ttl: ttl ? ttl : undefined,
|
|
priority: priority ? priority : 'default',
|
|
recipients: recipients,
|
|
data: data,
|
|
});
|
|
},
|
|
|
|
|
|
sendNotification: (notification, userIds) => {
|
|
|
|
console.log('sendNofitication -----------------------------------------------');
|
|
console.log(notification, userIds);
|
|
|
|
let getUserDevicesPromise = (userId) => userDeviceService.fetchAll({ params: { userId: userId } }, {}).then(function (result) {
|
|
return new Promise(function (resolve) { resolve(result.rows) });
|
|
});
|
|
let saveNotificationPromise = (notification) => extraMethods.saveNotification(notification);
|
|
let sendNotificationsPromise = (messages) => pushHelper.sendPushMessage(messages);
|
|
let disableUserDevicePromise = (token) => userDeviceService.update({
|
|
params: {
|
|
token: token,
|
|
}
|
|
}, { valid: 0, invalidated: moment() }, {});
|
|
|
|
let disableInvalidUserDevicesPromise = (userDevices) => {
|
|
return new Promise(function (resolve) {
|
|
let _userDevices = [];
|
|
userDevices.forEach(async function (userDevice) {
|
|
console.log(userDevice);
|
|
if (!userDeviceService.isValidPushToken(userDevice.token)) {
|
|
await disableUserDevicePromise(userDevice.token);
|
|
} else {
|
|
_userDevices.push(userDevice);
|
|
}
|
|
});
|
|
|
|
resolve(_userDevices)
|
|
});
|
|
};
|
|
|
|
let disableUserDevicesWithErrorStatus = (messages, tickets) => {
|
|
return new Promise(function (resolve) {
|
|
tickets.forEach(async function (ticket, index) {
|
|
if ((ticket.status === 'error') && (ticket.details.error === 'DeviceNotRegistered')) {
|
|
await disableUserDevicePromise(messages[index].to)
|
|
}
|
|
});
|
|
resolve(true);
|
|
});
|
|
}
|
|
|
|
let saveResponseStatusPromise = (messages, tickets) => notificationDetailService.saveNotificationDetails(messages, tickets);
|
|
|
|
|
|
let buildMessagesPromise = (userDevices) => {
|
|
return new Promise(function (resolve) {
|
|
let messages = [];
|
|
|
|
userDevices.forEach(async function (userDevice) {
|
|
messages.push(pushHelper.createPushMessage({
|
|
...notification,
|
|
userId: userDevice.userId,
|
|
to: userDevice.token,
|
|
}));
|
|
});
|
|
resolve(messages)
|
|
});
|
|
};
|
|
|
|
|
|
let getUserDevicesList = [];
|
|
|
|
saveNotificationPromise(notification)
|
|
.then(function (notificationRecord) {
|
|
notification = notificationRecord.toJSON();
|
|
userIds.forEach(function (userId) {
|
|
console.log(userId);
|
|
getUserDevicesList.push(getUserDevicesPromise(userId));
|
|
});
|
|
|
|
return Promise.all(getUserDevicesList)
|
|
}).then(function (userDeviceList) {
|
|
console.log(userDeviceList);
|
|
let result = [];
|
|
userDeviceList.forEach(function (elements) {
|
|
elements.forEach(function (item) {
|
|
result.push(item)
|
|
})
|
|
});
|
|
return new Promise(function (resolve) { resolve(result); });
|
|
})
|
|
.then(disableInvalidUserDevicesPromise)
|
|
.then(buildMessagesPromise)
|
|
.then(sendNotificationsPromise)
|
|
.then(function ({ messages, tickets }) {
|
|
let _saveStatus = saveResponseStatusPromise(messages, tickets);
|
|
let _disableDevices = disableUserDevicesWithErrorStatus(messages, tickets);
|
|
|
|
return Promise.all([_saveStatus, _disableDevices]);
|
|
})
|
|
.then(function (details) {
|
|
console.log(details);
|
|
return details;
|
|
});
|
|
},
|
|
|
|
|
|
|
|
getNotificationsWithoutReceipt: async() => {
|
|
|
|
},
|
|
|
|
updateNotificationsWithReceipts: async (receiptIds) => {
|
|
|
|
let receiptIdChunks = expo.chunkPushNotificationReceiptIds(receiptIds);
|
|
let xxx = await _getPushNotificationsResultAsync(receiptIdChunks);
|
|
|
|
}
|
|
};
|
|
|
|
module.exports = generateService(models.Notification, extraMethods);
|
|
|
|
|
|
|