app2-api/modules/notification/notification.service.js

200 lines
6.0 KiB
JavaScript
Raw Normal View History

2019-11-07 17:24:29 +00:00
const moment = require('moment');
2019-11-11 16:43:08 +00:00
2019-10-03 19:37:56 +00:00
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
2019-11-11 16:43:08 +00:00
const userDeviceService = require('./user_device.service');
const notificationDetailService = require('./notification_detail.service');
const pushHelper = require('../../helpers/push.helper');
2019-10-03 19:37:56 +00:00
const models = require('../../core/models');
2019-10-17 16:00:50 +00:00
2023-04-20 12:20:09 +00:00
const saveNotification = (notification) => extraMethods.saveNotification(notification);
const getUserDevices = async (userId) => {
const result = await userDeviceService.fetchAll({ params: { userId: userId } }, {});
return result.rows;
};
const disableUserDevice = (token) => userDeviceService.update({
params: {
token: token,
}
}, { valid: 0, invalidated: moment() }, {});
const disableInvalidUserDevices = async (devices) => {
console.log(devices);
const okDevices = [];
devices.forEach(async (device) => {
if (!userDeviceService.isValidPushToken(device.token)) {
await disableUserDevice(device.token);
} else {
okDevices.push(device);
}
});
return okDevices;
};
const buildPushMessages = (notification, devices) => {
let messages = [];
devices.forEach(async function (device) {
messages.push(pushHelper.createPushMessage({
...notification,
userId: device.userId,
to: device.token,
}));
});
return messages;
};
const disableUserDevicesWithErrorStatus = (messages, tickets) => {
tickets.forEach(async function (ticket, index) {
if ((ticket.status === 'error') && (ticket.details.error === 'DeviceNotRegistered')) {
disableUserDevice(messages[index].to)
}
});
}
let saveResponseStatusPromise = (messages, tickets) => notificationDetailService.saveNotificationDetails(messages, tickets);
2019-10-14 15:25:35 +00:00
const extraMethods = {
2019-11-07 10:39:20 +00:00
2023-04-20 12:20:09 +00:00
/*createNotification: (data) => {
2019-11-11 16:43:08 +00:00
return {
date: data.date,
title: data.title,
2019-11-11 17:20:59 +00:00
body: data.body,
2019-11-11 16:43:08 +00:00
ttl: data.ttl,
priority: data.priority,
recipients: data.recipients,
data: data.data,
userId: data.userId,
}
2023-04-20 12:20:09 +00:00
},*/
2019-11-11 16:43:08 +00:00
saveNotification: ({ date, title, body, ttl, priority, recipients, data, userId }) => {
2019-11-07 17:24:29 +00:00
return models.Notification.create({
date: moment(date),
title: title,
body: body,
userId: userId,
2019-11-07 18:06:45 +00:00
ttl: ttl ? ttl : undefined,
priority: priority ? priority : 'default',
2019-11-07 17:24:29 +00:00
recipients: recipients,
data: data,
2019-11-07 10:39:20 +00:00
});
},
2022-03-21 16:39:56 +00:00
2019-11-07 10:39:20 +00:00
2023-04-20 12:20:09 +00:00
/*sendNotificationToUsers: (notification, userIds) => {
2019-11-11 16:43:08 +00:00
2023-04-20 12:20:09 +00:00
console.log('sendNotificationToUsers -----------------------------------------------');
2019-11-11 16:43:08 +00:00
console.log(notification, userIds);
let getUserDevicesList = [];
saveNotificationPromise(notification)
.then(function (notificationRecord) {
notification = notificationRecord.toJSON();
userIds.forEach(function (userId) {
getUserDevicesList.push(getUserDevicesPromise(userId));
});
return Promise.all(getUserDevicesList)
}).then(function (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;
});
2023-04-20 12:20:09 +00:00
},*/
sendNotificationToUsers: async (notification, userIds) => {
console.log('sendNotificationToUsers -----------------------------------------------');
console.log(notification);
const savedNotification = (await saveNotification(notification)).toJSON();
const userDevicesList = await Promise.all(userIds.map((userId) => getUserDevices(userId)));
const allDevices = userDevicesList.flat();
const validDevices = await disableInvalidUserDevices(allDevices);
const pushMessages = buildPushMessages(savedNotification, validDevices);
const { messages, tickets } = await pushHelper.sendPushMessage(pushMessages);
saveResponseStatusPromise(messages, tickets);
disableUserDevicesWithErrorStatus(messages, tickets);
return;
2019-10-21 10:12:16 +00:00
},
2019-11-11 16:43:08 +00:00
2023-04-20 12:20:09 +00:00
sendNotificationToDevices: async (notification, devicesIds) => {
console.log('sendNotificationToDevices -----------------------------------------------');
console.log(notification);
const savedNotification = (await saveNotification(notification)).toJSON();
const validDevices = await disableInvalidUserDevices(devicesIds);
const pushMessages = buildPushMessages(savedNotification, validDevices);
const { messages, tickets } = await pushHelper.sendPushMessage(pushMessages);
saveResponseStatusPromise(messages, tickets);
disableUserDevicesWithErrorStatus(messages, tickets);
return;
},
2019-11-11 16:43:08 +00:00
2022-03-21 16:39:56 +00:00
getNotificationsWithoutReceipt: async () => {
2019-10-21 10:12:16 +00:00
},
updateNotificationsWithReceipts: async (receiptIds) => {
2022-03-21 16:39:56 +00:00
//let receiptIdChunks = expo.chunkPushNotificationReceiptIds(receiptIds);
//let xxx = await _getPushNotificationsResultAsync(receiptIdChunks);
2019-10-17 16:00:50 +00:00
}
};
2019-10-17 16:59:18 +00:00
module.exports = generateService(models.Notification, extraMethods);
2019-10-17 16:00:50 +00:00