Skip to content

Commit 6e80a00

Browse files
author
Sachin Maheshwari
committed
adding broadcast processor
1 parent 71bca54 commit 6e80a00

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

config/default.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ module.exports = {
9898
},
9999
},
100100
],
101+
'admin.notification.broadcast' : [{
102+
handleBulkNotification: {}
103+
}
104+
]
101105
//'notifications.community.challenge.created': ['handleChallengeCreated'],
102106
//'notifications.community.challenge.phasewarning': ['handleChallengePhaseWarning'],
103107
},

src/hooks/hookBulkMessage.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ const logger = require('../common/logger')
1111
const models = require('../models')
1212
const logPrefix = "BulkNotificationHook: "
1313

14-
models.BulkMessages.sync()
15-
models.BulkMessageUserRefs.sync()
14+
/**
15+
* CREATE NEW TABLES IF NOT EXISTS
16+
*/
17+
models.BulkMessages.sync().then((t)=> {
18+
models.BulkMessageUserRefs.sync()
19+
})
1620

1721
/**
1822
* Main function
@@ -115,7 +119,7 @@ function createNotificationForUser(userId, bulkMessage) {
115119
type: bulkMessage.type,
116120
contents: {
117121
id: bulkMessage.id, /** broadcast message id */
118-
name: bulkMessage.contents, /** broadcast message */
122+
message: bulkMessage.message, /** broadcast message */
119123
group: 'broadcast',
120124
title: 'Broadcast Message',
121125
},

src/models/BulkMessages.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
module.exports = (sequelize, DataTypes) => sequelize.define('bulk_messages', {
1414
id: { type: DataTypes.BIGINT, primaryKey: true, autoIncrement: true },
1515
type: { type: DataTypes.STRING, allowNull: false },
16-
contents: { type: DataTypes.JSONB, allowNull: false },
17-
recipient_group: { type: DataTypes.STRING, allowNull: false }
16+
message: { type: DataTypes.TEXT, allowNull: false },
17+
recipients: { type: DataTypes.JSONB, allowNull: false },
18+
rules: {type: DataTypes.JSONB, allowNull: true}
1819
}, {});
1920

2021
// sequelize will generate and manage createdAt, updatedAt fields
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Bulk notification handler.
3+
*/
4+
const co = require('co');
5+
const models = require('../../models');
6+
const logger = require('../../common/logger')
7+
8+
/**
9+
* Handle Kafka JSON message of broadcast.
10+
*
11+
* @param {Object} message the Kafka JSON message
12+
* @param {Object} ruleSets
13+
*
14+
* @return {Promise} promise resolved to notifications
15+
*/
16+
const handle = (message, ruleSets) => co(function* () {
17+
return new Promise(function(resolve, reject){
18+
models.BulkMessages.create({
19+
type: message.topic,
20+
message: message.payload.message,
21+
recipients: message.payload.recipients,
22+
rules: message.payload.rules || null,
23+
}).then((bm) => {
24+
logger.info("Broadcast message recieved and inserted in db with id:", bm.id)
25+
resolve([]) // no notification need to insert at this point
26+
}).catch((e) => {
27+
logger.error("Broadcast processor failed in db operation. Error: ", e)
28+
reject(e)
29+
})
30+
})
31+
});
32+
33+
module.exports = {
34+
handle,
35+
};

src/processors/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const ChallengePhaseWarningHandler = require('./challenge/ChallengePhaseWarningH
88
const ChallengeHandler = require('./challenge/ChallengeHandler');
99
const AutoPilotHandler = require('./challenge/AutoPilotHandler');
1010
const SubmissionHandler = require('./challenge/SubmissionHandler');
11+
const BulkNotificationHandler = require('./broadcast/bulkNotificationHandler');
1112

1213
// Exports
1314
module.exports = {
@@ -16,4 +17,5 @@ module.exports = {
1617
handleChallenge: ChallengeHandler.handle,
1718
handleAutoPilot: AutoPilotHandler.handle,
1819
handleSubmission: SubmissionHandler.handle,
20+
handleBulkNotification: BulkNotificationHandler.handle,
1921
};

0 commit comments

Comments
 (0)