-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
144 lines (131 loc) · 3.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* This is entry point of the TopCoder notification server module.
*/
'use strict';
const config = require('config');
const _ = require('lodash');
const errors = require('./src/common/errors');
const tcCoreLibAuth = require('tc-core-library-js').auth;
// some useful components to exposure
const logger = require('./src/common/logger');
const busService = require('./src/services/BusAPI');
const eventScheduler = require('./src/services/EventScheduler');
const notificationService = require('./src/services/NotificationService');
global.M2m = tcCoreLibAuth.m2m(config);
// key is topic name, e.g. 'notifications.connect.project.created';
// value is handler for the topic to find user ids that should receive notifications for a message,
// it is defined as: function(topic, message, callback),
// the topic is topic name,
// the message is JSON event message,
// the callback is function(error, userIds), where userIds is an array of user ids to receive notifications
const handlers = {};
/**
* List of notification service handlers which will process notifications
*
* Each item is the function of the next signature
* function(topicName, messageJSON, notification)
* - {String} topicName topic name (event type)
* - {Object} messageJSON message raw JSON
* - {Object} notification pre-processed notification object
*/
const notificationServiceHandlers = [];
/**
* Set configuration, the default config will be overridden by the given config,
* unspecified config parameters will not be changed, i.e. still using default values.
*
* Note that setConfig should be called before the initDatabase and start functions.
*
* @param {Object} cfg the configuration to set
*/
function setConfig(cfg) {
if (!cfg) {
throw new errors.ValidationError('Missing configuration.');
}
_.extend(config, cfg);
}
/**
* Add topic handler for topic, override existing one if any.
* @param {String} topic the topic name
* @param {Function} handler the handler
*/
function addTopicHandler(topic, handler) {
if (!topic) {
throw new errors.ValidationError('Missing topic.');
}
if (!handler) {
throw new errors.ValidationError('Missing handler.');
}
handlers[topic] = handler;
}
/**
* Adds notification service handler
*
* @param {Function} handler notification service handler
*/
function addNotificationServiceHandler(handler) {
if (!handler) {
throw new errors.ValidationError('Missing notification service handler.');
}
notificationServiceHandlers.push(handler);
}
/**
* Remove topic handler for topic.
* @param {String} topic the topic name
*/
function removeTopicHandler(topic) {
if (!topic) {
throw new errors.ValidationError('Missing topic.');
}
delete handlers[topic];
}
/**
* Get all topic handlers.
* @returns {Object} all topic handlers, key is topic name, value is handler
*/
function getAllHandlers() {
return handlers;
}
/**
* Start the notification API server.
*/
function startAPI() {
// load app only after config is set
const app = require('./src/app');
app.start();
}
/**
* Start the event bus consumer.
*/
function startKafkaConsumers() {
if (_.isEmpty(handlers)) {
throw new errors.ValidationError('Missing handler(s).');
}
// load app only after config is set
const app = require('./src/app');
app.startKafkaConsumer(handlers, notificationServiceHandlers);
}
/**
* Initialize database. All tables are cleared and re-created.
* @returns {Promise} promise to init db
*/
function initDatabase() {
// load models only after config is set
const models = require('./src/models');
return models.init(true);
}
// Exports
module.exports = {
setConfig,
addTopicHandler,
removeTopicHandler,
getAllHandlers,
startAPI,
startKafkaConsumers,
initDatabase,
addNotificationServiceHandler,
// exposure some useful components
logger,
busService,
eventScheduler,
notificationService,
};