.
This commit is contained in:
parent
2035343245
commit
47b1f87a79
@ -4,6 +4,7 @@ const httpStatus = require('http-status');
|
|||||||
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 userDeviceService = require('./user_device.service');
|
const userDeviceService = require('./user_device.service');
|
||||||
|
|
||||||
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
|
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
|
||||||
@ -15,6 +16,10 @@ const controllerOptions = { MODULE_NAME };
|
|||||||
const extraControllers = {
|
const extraControllers = {
|
||||||
sendNotification: (config) => {
|
sendNotification: (config) => {
|
||||||
return async (req, res, next) => {
|
return async (req, res, next) => {
|
||||||
|
config = config || {
|
||||||
|
scopes: [],
|
||||||
|
};
|
||||||
|
|
||||||
let receipt = undefined;
|
let receipt = undefined;
|
||||||
const context = buildContext(req, config);
|
const context = buildContext(req, config);
|
||||||
let params = extractParamsFromRequest(req, res);
|
let params = extractParamsFromRequest(req, res);
|
||||||
@ -34,22 +39,33 @@ const extraControllers = {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
let getUserDevicesPromise = (userId) => userDeviceService.fetchAll({ params: { userId: userId }}, context);
|
let getUserDevicesPromise = (userId) => userDeviceService.fetchAll({ params: { userId: userId }}, context);
|
||||||
|
let saveNotificationPromise = (notification) => notificationService.create(notification, context);
|
||||||
let sendNotificationsPromise = (messages) => notificationService.sendNotification(messages);
|
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;
|
let message = undefined;
|
||||||
userDevices.rows.forEach(function (userDevice) {
|
userDevices.rows.forEach(async function (userDevice) {
|
||||||
if (notificationService.isValidPushToken(userDevice.token)) {
|
if (notificationService.isValidPushToken(userDevice.token)) {
|
||||||
message = {
|
message = {
|
||||||
|
...notificationRecord,
|
||||||
userId: userDevice.userId,
|
userId: userDevice.userId,
|
||||||
to: userDevice.token,
|
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',
|
sound: 'default',
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
|
await disableUserDevicePromise(userDevice);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return new Promise(function(resolve) { resolve(message) });
|
return new Promise(function(resolve) { resolve(message) });
|
||||||
@ -61,11 +77,13 @@ const extraControllers = {
|
|||||||
getUserDevicesList.push(getUserDevicesPromise(userId));
|
getUserDevicesList.push(getUserDevicesPromise(userId));
|
||||||
});
|
});
|
||||||
|
|
||||||
receipt = await Promise.all(getUserDevicesList).then(
|
receipt = await saveNotificationPromise(notificationRecord)
|
||||||
function(userDeviceList) {
|
.then(function() {
|
||||||
return Promise.all(userDeviceList.map(buildMessagePromise))
|
return Promise.all(getUserDevicesList)
|
||||||
})
|
}).then(function(userDeviceList) {
|
||||||
.then(sendNotificationsPromise)
|
return Promise.all(userDeviceList.map(buildMessagePromise))
|
||||||
|
}).then(sendNotificationsPromise)
|
||||||
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', error, res)
|
return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', error, res)
|
||||||
@ -75,8 +93,36 @@ const extraControllers = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
registerUser: (config) => {
|
updateNotificationsWithReceipts: (config) => {
|
||||||
return async (req, res, next) => {
|
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 {
|
try {
|
||||||
const context = buildContext(req, config);
|
const context = buildContext(req, config);
|
||||||
var data = {
|
var data = {
|
||||||
|
|||||||
@ -26,11 +26,6 @@ module.exports = function (sequelize, DataTypes) {
|
|||||||
type: DataTypes.JSON,
|
type: DataTypes.JSON,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
},
|
},
|
||||||
userId: {
|
|
||||||
type: DataTypes.UUID,
|
|
||||||
allowNull: false,
|
|
||||||
foreignKey: true
|
|
||||||
}
|
|
||||||
}, {
|
}, {
|
||||||
tableName: 'notifications',
|
tableName: 'notifications',
|
||||||
freezeTableName: true,
|
freezeTableName: true,
|
||||||
|
|||||||
@ -44,13 +44,13 @@ routes.post('/notifications',
|
|||||||
routes.post('/notifications/register',
|
routes.post('/notifications/register',
|
||||||
isLoggedUser,
|
isLoggedUser,
|
||||||
SchemaValidator(deviceTokenInputType, true),
|
SchemaValidator(deviceTokenInputType, true),
|
||||||
notificationController.registerUser()
|
notificationController.registerDevice()
|
||||||
);
|
);
|
||||||
|
|
||||||
routes.post('/notifications/devices',
|
routes.post('/notifications/devices',
|
||||||
isLoggedUser,
|
isLoggedUser,
|
||||||
SchemaValidator(deviceTokenInputType, true),
|
SchemaValidator(deviceTokenInputType, true),
|
||||||
notificationController.registerUser()
|
notificationController.registerDevice()
|
||||||
);
|
);
|
||||||
|
|
||||||
module.exports = routes;
|
module.exports = routes;
|
||||||
@ -67,13 +67,20 @@ const extraMethods = {
|
|||||||
console.log(receiptIds);
|
console.log(receiptIds);
|
||||||
console.log(invalidTokens);
|
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 receiptIdChunks = expo.chunkPushNotificationReceiptIds(receiptIds);
|
||||||
let xxx = await _getPushNotificationsResultAsync(receiptIdChunks);
|
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