-
Notifications
You must be signed in to change notification settings - Fork 29
/
rulesStore.js
234 lines (226 loc) · 7.63 KB
/
rulesStore.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright 2015 Telefonica Investigación y Desarrollo, S.A.U
*
* This file is part of perseo-fe
*
* perseo-fe is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* perseo-fe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with perseo-fe.
* If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License
* please contact with::[[email protected]]
*/
'use strict';
var util = require('util'),
async = require('async'),
appContext = require('../appContext'),
rulesCollectionName = require('../../config').collections.rules,
myutils = require('../myutils'),
logger = require('logops'),
errors = {};
function parsePostAxnParams(rule) {
if (rule && rule.action && rule.action.type === 'post') {
if (rule.action.parameters.json) {
try {
rule.action.parameters.json = JSON.parse(rule.action.parameters.json);
} catch (ex) {
myutils.logErrorIf(ex);
}
}
if (rule.action.parameters.qs) {
try {
rule.action.parameters.qs = JSON.parse(rule.action.parameters.qs);
} catch (ex) {
myutils.logErrorIf(ex);
}
}
if (rule.action.parameters.headers) {
try {
rule.action.parameters.headers = JSON.parse(rule.action.parameters.headers);
} catch (ex) {
myutils.logErrorIf(ex);
}
}
}
}
function stringifyPostAxnParams(rule) {
if (rule && rule.action && rule.action.type === 'post') {
if (rule.action.parameters.json) {
try {
rule.action.parameters.json = JSON.stringify(rule.action.parameters.json);
} catch (e) {
myutils.logErrorIf(e);
}
}
if (rule.action.parameters.qs) {
try {
rule.action.parameters.qs = JSON.stringify(rule.action.parameters.qs);
} catch (e) {
myutils.logErrorIf(e);
}
}
if (rule.action.parameters.headers) {
try {
rule.action.parameters.headers = JSON.stringify(rule.action.parameters.headers);
} catch (e) {
myutils.logErrorIf(e);
}
}
}
}
function search(rule, callback) {
var db = appContext.Db();
async.waterfall(
[
db.collection.bind(db, rulesCollectionName, { strict: true }),
function(col, cb) {
col.findOne({ name: rule.name, subservice: rule.subservice, service: rule.service }, cb);
}
],
callback
);
}
function findAll(service, subservice, callback) {
var criterion = {};
if (typeof service === 'function') {
callback = service;
} else {
criterion.service = service;
criterion.subservice = subservice;
}
var db = appContext.Db();
async.waterfall(
[
db.collection.bind(db, rulesCollectionName, { strict: true }),
function(col, cb) {
col.find(criterion).toArray(function(err, rules) {
if (rules && util.isArray(rules)) {
rules.forEach(function(r) {
parsePostAxnParams(r);
});
}
cb(err, rules);
});
}
],
function(err, result) {
myutils.logErrorIf(err);
logger.info('rulesStore.FindAll %s', myutils.firstChars(result));
return callback(err, result);
}
);
}
module.exports = {
Find: function Find(rule, callback) {
search(rule, function(err, result) {
myutils.logErrorIf(err);
if (err) {
return callback(err, null);
}
if (!result) {
return callback(new errors.NotFoundRule(rule.name), null);
}
parsePostAxnParams(result);
logger.info('rulesStore.Find %s', myutils.firstChars(result));
return callback(err, result);
});
},
Exists: function Exists(rule, callback) {
search(rule, function(err, result) {
myutils.logErrorIf(err);
callback(err, Boolean(result));
});
},
FindAll: findAll,
Remove: function Remove(rule, callback) {
var db = appContext.Db();
async.waterfall(
[
db.collection.bind(db, rulesCollectionName, { strict: true }),
function(col, cb) {
col.remove({ name: rule.name, subservice: rule.subservice, service: rule.service }, cb);
}
],
function(err, result) {
myutils.logErrorIf(err);
logger.info('rulesStore.Remove %j', myutils.firstChars(result));
return callback(err, result);
}
);
},
Save: function Save(r, callback) {
var db = appContext.Db();
stringifyPostAxnParams(r);
async.waterfall(
[
db.collection.bind(db, rulesCollectionName, { strict: true }),
function(col, cb) {
col.save(r, cb);
}
],
function(err, result) {
myutils.logErrorIf(err);
logger.info('rulesStore.Save %j', myutils.firstChars(result));
return callback(err, result);
}
);
},
Update: function Update(id, r, callback) {
var db = appContext.Db();
stringifyPostAxnParams(r);
async.waterfall(
[
db.collection.bind(db, rulesCollectionName, { strict: true }),
function(col, cb) {
col.findOneAndUpdate(
{ name: id },
{ $set: r },
{
upsert: false,
returnOriginal: false
},
function(err, result) {
if (result && result.lastErrorObject && result.lastErrorObject.updatedExisting === false) {
return cb(new errors.NotFoundRule(id), null);
}
parsePostAxnParams(r);
logger.debug('rulesStore.findOneAndUpdate %s', myutils.firstChars(r));
return cb(err, r);
}
);
}
],
function(err, result) {
myutils.logErrorIf(err);
logger.info('rulesStore.Update %j', myutils.firstChars(result));
return callback(err, result);
}
);
}
};
/**
* Constructors for possible errors from this module
*
* @type {Object}
*/
module.exports.errors = errors;
(function() {
errors.NotFoundRule = function NotFoundRule(msg) {
this.name = 'RULE_NOTFOUND';
this.message = 'rule not found ' + msg;
this.httpCode = 404;
};
Object.keys(errors).forEach(function(element) {
util.inherits(errors[element], Error);
});
})();