/
notification.create.js
70 lines (56 loc) · 2.43 KB
/
notification.create.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
module.exports = create;
var Promise = require('bluebird');
var queries = require('./notification.queries.js');
function create(options) {
// handle scenarios
var notification;
if(options && options.params) notification = options.params;
else notification = options;
return Notification.create(notification)
.then(function(notification) {
return [notification,
gladys.utils.sql(queries.getNotificationTypes, [notification.user]),
gladys.user.getById({id: notification.user})
];
})
.spread(function(notification, types, user) {
sails.log.info(`Notification : create : Notification saved with success. Trying to send notification to user ID ${notification.user}`);
return Promise.mapSeries(types, function(type) {
return startService(notification, type, user);
});
})
.catch(function(err) {
if (err.message !== 'ok') {
sails.log.error(err);
}
});
}
/**
* Call the service related to the notification
*/
function startService(notification, type, user) {
var notify = null;
if(gladys[type.service] && typeof gladys[type.service].notify === "function"){
notify = gladys[type.service].notify;
} else if (gladys.modules[type.service] || typeof gladys.modules[type.service].notify === "function") {
notify = gladys.modules[type.service].notify;
} else {
return Promise.reject(new Error(`${type.service} is not a valid service`));
}
sails.log.info(`Notification : create : Trying to contact ${type.service}`);
return notify(notification, user)
.then(function(result) {
sails.log.info(`Notification threw ${type.service} sent with success. Aborting the chain.`);
// if module resolved, we stop the promise chain
// it means one notification worked!
return Promise.reject(new Error('ok'));
})
.catch(function(e){
// if the error is because we want to exist the promise chain,
// we need to propagate the error
if(e.message === 'ok') return Promise.reject(e);
// if notification does not work, we resolve
// it means that we need to continue the flow
return Promise.resolve();
});
}