forked from fcamblor/weezevent-slack-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
73 lines (66 loc) · 2.3 KB
/
app.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
var restify = require('restify');
var Promise = require('bluebird');
var _ = require('lodash');
var WeezEvent = require('./wz');
var Slack = require('./slk');
var Store = require('./store');
var SlackMessageProducer = require('./msgProducer');
var requiredEnvKeysFilled = true;
_.each(['WZ_USER', 'WZ_PWD', 'WZ_API', 'WZ_EVT_ID', 'SLK_HOOK_URL', 'SLK_USERNAME', 'SLK_ICON', 'MNG_URL'], function(requiredEnvKey){
if(!process.env[requiredEnvKey]) {
console.error("Missing mandatory environment key : %s", requiredEnvKey);
requiredEnvKeysFilled = false;
}
});
if(!requiredEnvKeysFilled) {
process.exit();
}
var we = new WeezEvent({
wz_user: process.env.WZ_USER,
wz_pwd: process.env.WZ_PWD,
wz_api: process.env.WZ_API,
wz_event_id: process.env.WZ_EVT_ID
});
var slk = new Slack({
slk_hook_url: process.env.SLK_HOOK_URL,
slk_channel: process.env.SLK_CHANNEL,
slk_user_name: process.env.SLK_USERNAME,
slk_icon: process.env.SLK_ICON
});
var store = new Store({
mongo_url: process.env.MNG_URL
});
var msgProducer = new SlackMessageProducer();
var server = restify.createServer({
name: 'weezevent-slack-notifier',
version: '1.0.0'
});
server.get('/checkParticipants', function (req, res, next) {
Promise.all([
we.fetchWZParticipants(),
we.fetchWZEventTickets(),
store.fetchPersistedParticipants("bdxio")
]).then(function(promResults) {
var wzParticipants = promResults[0];
var wzTickets = promResults[1];
var bdxioParticipants = msgProducer.convertWZParticipantsToBDXIOParticipants(wzParticipants, wzTickets);
var persistedParticipants = promResults[2] || [];
console.log("wzParticipants: %s", JSON.stringify(bdxioParticipants));
console.log("persistedParticipants : %s", JSON.stringify(persistedParticipants));
var slackMsg = msgProducer.produceMessageFrom(persistedParticipants, bdxioParticipants);
if(slackMsg){
slk.sendMessage(slackMsg);
store.persistParticipantsIn("bdxio", bdxioParticipants);
}
});
res.send(200)
return next();
});
we.init().then(function(){
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.listen(parseInt(process.env.PORT, 10) || 8080, function () {
console.log('%s listening at %s', server.name, server.url);
});
});