.
This commit is contained in:
parent
2035343245
commit
47b1f87a79
@ -4,6 +4,7 @@ const httpStatus = require('http-status');
|
||||
const generateControllers = require('../../core/controllers');
|
||||
const { buildContext } = require('../../core/controllers');
|
||||
const notificationService = require('./notification.service');
|
||||
const notificationDetailService = require('./notification_detail.service');
|
||||
const userDeviceService = require('./user_device.service');
|
||||
|
||||
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
|
||||
@ -15,6 +16,10 @@ const controllerOptions = { MODULE_NAME };
|
||||
const extraControllers = {
|
||||
sendNotification: (config) => {
|
||||
return async (req, res, next) => {
|
||||
config = config || {
|
||||
scopes: [],
|
||||
};
|
||||
|
||||
let receipt = undefined;
|
||||
const context = buildContext(req, config);
|
||||
let params = extractParamsFromRequest(req, res);
|
||||
@ -32,24 +37,35 @@ const extraControllers = {
|
||||
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing message content'), res)
|
||||
}
|
||||
|
||||
try {
|
||||
let getUserDevicesPromise = (userId) => userDeviceService.fetchAll({ params: { userId: userId }}, context);
|
||||
try {
|
||||
let getUserDevicesPromise = (userId) => userDeviceService.fetchAll({ params: { userId: userId }}, context);
|
||||
let saveNotificationPromise = (notification) => notificationService.create(notification, context);
|
||||
let sendNotificationsPromise = (messages) => notificationService.sendNotification(messages);
|
||||
let disableUserDevicePromise = (userDevice) => userDeviceService.update({
|
||||
userId: userDevice.userId,
|
||||
token: userDevice.token,
|
||||
}, { valid: false }, context);
|
||||
|
||||
let buildMessagePromise = (userDevices) => {
|
||||
const notificationRecord = {
|
||||
title: req.body.title,
|
||||
body: req.body.message,
|
||||
ttl: req.body.ttl,
|
||||
priority: req.body.priority,
|
||||
data: req.body.data || req.body.userIds,
|
||||
};
|
||||
|
||||
let buildMessagePromise = async (userDevices) => {
|
||||
let message = undefined;
|
||||
userDevices.rows.forEach(function (userDevice) {
|
||||
userDevices.rows.forEach(async function (userDevice) {
|
||||
if (notificationService.isValidPushToken(userDevice.token)) {
|
||||
message = {
|
||||
...notificationRecord,
|
||||
userId: userDevice.userId,
|
||||
to: userDevice.token,
|
||||
title: req.body.title,
|
||||
body: req.body.message,
|
||||
ttl: req.body.ttl,
|
||||
priority: req.body.priority,
|
||||
data: req.body.data,
|
||||
sound: 'default',
|
||||
};
|
||||
} else {
|
||||
await disableUserDevicePromise(userDevice);
|
||||
}
|
||||
});
|
||||
return new Promise(function(resolve) { resolve(message) });
|
||||
@ -61,11 +77,13 @@ const extraControllers = {
|
||||
getUserDevicesList.push(getUserDevicesPromise(userId));
|
||||
});
|
||||
|
||||
receipt = await Promise.all(getUserDevicesList).then(
|
||||
function(userDeviceList) {
|
||||
return Promise.all(userDeviceList.map(buildMessagePromise))
|
||||
})
|
||||
.then(sendNotificationsPromise)
|
||||
receipt = await saveNotificationPromise(notificationRecord)
|
||||
.then(function() {
|
||||
return Promise.all(getUserDevicesList)
|
||||
}).then(function(userDeviceList) {
|
||||
return Promise.all(userDeviceList.map(buildMessagePromise))
|
||||
}).then(sendNotificationsPromise)
|
||||
|
||||
|
||||
} catch (error) {
|
||||
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', error, res)
|
||||
@ -75,8 +93,36 @@ const extraControllers = {
|
||||
}
|
||||
},
|
||||
|
||||
registerUser: (config) => {
|
||||
updateNotificationsWithReceipts: (config) => {
|
||||
return async (req, res, next) => {
|
||||
config = config || {
|
||||
scopes: [],
|
||||
};
|
||||
|
||||
const context = buildContext(req, config);
|
||||
let params = extractParamsFromRequest(req, res);
|
||||
|
||||
let getNotificationsWithoutReceipt = async () => {
|
||||
let params = {
|
||||
where: { }
|
||||
};
|
||||
return await notificationDetailService.fetchAll(params, context);
|
||||
}
|
||||
|
||||
let receipt = await Promise.all(getUserDevicesList).then(
|
||||
function (userDeviceList) {
|
||||
return Promise.all(userDeviceList.map(buildMessagePromise))
|
||||
})
|
||||
.then(sendNotificationsPromise)
|
||||
}
|
||||
},
|
||||
|
||||
registerDevice: (config) => {
|
||||
return async (req, res, next) => {
|
||||
config = config || {
|
||||
scopes: [],
|
||||
};
|
||||
|
||||
try {
|
||||
const context = buildContext(req, config);
|
||||
var data = {
|
||||
|
||||
@ -26,11 +26,6 @@ module.exports = function (sequelize, DataTypes) {
|
||||
type: DataTypes.JSON,
|
||||
allowNull: true,
|
||||
},
|
||||
userId: {
|
||||
type: DataTypes.UUID,
|
||||
allowNull: false,
|
||||
foreignKey: true
|
||||
}
|
||||
}, {
|
||||
tableName: 'notifications',
|
||||
freezeTableName: true,
|
||||
|
||||
@ -44,13 +44,13 @@ routes.post('/notifications',
|
||||
routes.post('/notifications/register',
|
||||
isLoggedUser,
|
||||
SchemaValidator(deviceTokenInputType, true),
|
||||
notificationController.registerUser()
|
||||
notificationController.registerDevice()
|
||||
);
|
||||
|
||||
routes.post('/notifications/devices',
|
||||
isLoggedUser,
|
||||
SchemaValidator(deviceTokenInputType, true),
|
||||
notificationController.registerUser()
|
||||
notificationController.registerDevice()
|
||||
);
|
||||
|
||||
module.exports = routes;
|
||||
@ -67,13 +67,20 @@ const extraMethods = {
|
||||
console.log(receiptIds);
|
||||
console.log(invalidTokens);
|
||||
|
||||
|
||||
let notifications = await _saveNotifications(messages, tickets);
|
||||
return new Promise(function (resolve) { resolve(notifications) });
|
||||
|
||||
},
|
||||
|
||||
getNotificationsWithoutReceipt: async() => {
|
||||
|
||||
},
|
||||
|
||||
updateNotificationsWithReceipts: async (receiptIds) => {
|
||||
|
||||
let receiptIdChunks = expo.chunkPushNotificationReceiptIds(receiptIds);
|
||||
let xxx = await _getPushNotificationsResultAsync(receiptIdChunks);
|
||||
|
||||
let notifications = await _saveNotifications(messages, tickets);
|
||||
|
||||
return new Promise(function (resolve) { resolve(notifications) });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
10
modules/notification/notification_detail.service.js
Normal file
10
modules/notification/notification_detail.service.js
Normal file
@ -0,0 +1,10 @@
|
||||
const moment = require('moment');
|
||||
const { Expo } = require('expo-server-sdk');
|
||||
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
|
||||
const models = require('../../core/models');
|
||||
|
||||
const extraMethods = {
|
||||
|
||||
};
|
||||
|
||||
module.exports = generateService(models.NotificationDetail, extraMethods);
|
||||
Loading…
Reference in New Issue
Block a user