-
Notifications
You must be signed in to change notification settings - Fork 4
/
notifier.js
executable file
·80 lines (74 loc) · 2.91 KB
/
notifier.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
var http = require('http'),
info = require('./HTTP_Client/info.json'),
config = require('./config'),
winston = require('winston')
var db = require(config.database);
var logger = new winston.Logger({
transports: [
new winston.transports.File({
level: 'debug',
filename: './log/all-log',
colorize: false
}),
new winston.transports.Console({
level: 'info',
colorize: true
})
],
exitOnError: false
});
// Send notifications to the WStore
exports.notify = function(accounting_info) {
if (accounting_info.num === 0){
logger.log('debug', 'No request needed.');
} else {
logger.log('debug', 'Request needed.');
db.getAccountingInfo(accounting_info.publicPath, {
organization: accounting_info.organization,
name: accounting_info.name,
version: accounting_info.version
}, function(err, acc) {
if (err || acc === null) {
logger.warn('Error while notifying')
} else {
info.offering = {
organization: accounting_info.organization,
name: accounting_info.name,
version: accounting_info.version
};
info.customer = accounting_info.actorID;
info.time_stamp = (new Date()).toISOString();
info.value = accounting_info.num.toString();
info.correlation_number = accounting_info.correlation_number;
info.record_type = acc.record_type;
info.unit = acc.unit;
info.component_label = acc.component_label;
var body = JSON.stringify(info);
var options = {
host: config.WStore.accounting_host,
port: config.WStore.accounting_port,
path: config.WStore.accounting_path + accounting_info.reference + '/accounting',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': body.length
}
};
var request = http.request(options, function(res) {
if (200 <= res.statusCode && res.statusCode <= 299) {
logger.log('Resquest worked!');
db.resetCount(accounting_info.actorID, accounting_info.API_KEY, accounting_info.publicPath, function(err) {
if (err) {
logger.warn('Error while reseting the account')
}
});
} else {
logger.warn('Resquest failed!');
}
});
request.write(body);
request.end();
}
});
}
};