• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

telefonicaid / perseo-fe / 18558650619

16 Oct 2025 10:43AM UTC coverage: 78.143% (+0.1%) from 78.011%
18558650619

Pull #817

github

web-flow
Merge f387d3457 into a71a30851
Pull Request #817: [WIP] upgrade dep driver mongodb to 4.17.2

797 of 1149 branches covered (69.36%)

Branch coverage included in aggregate %.

107 of 123 new or added lines in 6 files covered. (86.99%)

1 existing line in 1 file now uncovered.

2242 of 2740 relevant lines covered (81.82%)

71.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.76
/lib/models/rulesStore.js
1
/*
2
 * Copyright 2015 Telefonica Investigación y Desarrollo, S.A.U
3
 *
4
 * This file is part of perseo-fe
5
 *
6
 * perseo-fe is free software: you can redistribute it and/or
7
 * modify it under the terms of the GNU Affero General Public License as
8
 * published by the Free Software Foundation, either version 3 of the License,
9
 * or (at your option) any later version.
10
 *
11
 * perseo-fe is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 * See the GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public
17
 * License along with perseo-fe.
18
 * If not, see http://www.gnu.org/licenses/.
19
 *
20
 * For those usages not covered by the GNU Affero General Public License
21
 * please contact with::[contacto@tid.es]
22
 */
23
'use strict';
24

25
var util = require('util'),
1✔
26
    appContext = require('../appContext'),
1✔
27
    rulesCollectionName = require('../../config').collections.rules,
1✔
28
    myutils = require('../myutils'),
1✔
29
    logger = require('logops'),
1✔
30
    errors = {};
1✔
31

32
function parsePostAxnParams(rule) {
33
    if (rule && rule.action && rule.action.type === 'post') {
56✔
34
        if (rule.action.parameters.json) {
8!
35
            try {
×
36
                rule.action.parameters.json = JSON.parse(rule.action.parameters.json);
×
37
            } catch (ex) {
38
                myutils.logErrorIf(ex);
×
39
            }
40
        }
41
        if (rule.action.parameters.qs) {
8!
42
            try {
×
43
                rule.action.parameters.qs = JSON.parse(rule.action.parameters.qs);
×
44
            } catch (ex) {
45
                myutils.logErrorIf(ex);
×
46
            }
47
        }
48
        if (rule.action.parameters.headers) {
8!
49
            try {
×
50
                rule.action.parameters.headers = JSON.parse(rule.action.parameters.headers);
×
51
            } catch (ex) {
52
                myutils.logErrorIf(ex);
×
53
            }
54
        }
55
    }
56
}
57

58
function stringifyPostAxnParams(rule) {
59
    if (rule && rule.action && rule.action.type === 'post') {
146✔
60
        if (rule.action.parameters.json) {
22!
61
            try {
×
62
                rule.action.parameters.json = JSON.stringify(rule.action.parameters.json);
×
63
            } catch (e) {
64
                myutils.logErrorIf(e);
×
65
            }
66
        }
67
        if (rule.action.parameters.qs) {
22!
68
            try {
×
69
                rule.action.parameters.qs = JSON.stringify(rule.action.parameters.qs);
×
70
            } catch (e) {
71
                myutils.logErrorIf(e);
×
72
            }
73
        }
74
        if (rule.action.parameters.headers) {
22!
75
            try {
×
76
                rule.action.parameters.headers = JSON.stringify(rule.action.parameters.headers);
×
77
            } catch (e) {
78
                myutils.logErrorIf(e);
×
79
            }
80
        }
81
    }
82
}
83

84
function search(rule, callback) {
85
    myutils.collectionExists(appContext.Db(), rulesCollectionName, function(exists) {
264✔
86
        if (!exists) {
264✔
87
            return callback('collection ' + rulesCollectionName + ' does not exist');
46✔
88
        }
89
        var col = appContext.Db().collection(rulesCollectionName);
218✔
90
        col.findOne({ name: rule.name, subservice: rule.subservice, service: rule.service }, callback);
218✔
91
    });
92
}
93

94
function findAll(service, subservice, callback) {
95
    var criterion = {};
105✔
96

97
    if (typeof service === 'function') {
105✔
98
        callback = service;
100✔
99
    } else {
100
        criterion.service = service;
5✔
101
        criterion.subservice = subservice;
5✔
102
    }
103
    var db = appContext.Db();
105✔
104
    myutils.collectionExists(db, rulesCollectionName, function(exists) {
105✔
105
        if (!exists) {
105✔
106
            return callback('collection ' + rulesCollectionName + ' does not exist');
2✔
107
        }
108
        const col = db.collection(rulesCollectionName);
103✔
109
        col.find(criterion).toArray(function(err, rules) {
103✔
110
            if (rules && util.isArray(rules)) {
103!
111
                rules.forEach(function(r) {
103✔
112
                    parsePostAxnParams(r);
13✔
113
                });
114
            }
115
            myutils.logErrorIf(err);
103✔
116
            logger.info('rulesStore.FindAll %s', myutils.firstChars(rules));
103✔
117
            return callback(err, rules);
103✔
118
        });
119
    });
120
}
121

122
module.exports = {
1✔
123
    Find: function Find(rule, callback) {
124
        search(rule, function(err, result) {
84✔
125
            myutils.logErrorIf(err);
84✔
126
            if (err) {
84✔
127
                return callback(err, null);
23✔
128
            }
129
            if (!result) {
61✔
130
                return callback(new errors.NotFoundRule(rule.name), null);
19✔
131
            }
132
            parsePostAxnParams(result);
42✔
133
            logger.info('rulesStore.Find %s', myutils.firstChars(result));
42✔
134
            return callback(err, result);
42✔
135
        });
136
    },
137
    Exists: function Exists(rule, callback) {
138
        search(rule, function(err, result) {
180✔
139
            myutils.logErrorIf(err);
180✔
140
            callback(err, Boolean(result));
180✔
141
        });
142
    },
143
    FindAll: findAll,
144
    Remove: function Remove(rule, callback) {
145
        myutils.collectionExists(appContext.Db(), rulesCollectionName, function(exists) {
31✔
146
            if (!exists) {
31✔
147
                return callback('collection ' + rulesCollectionName + ' does not exist');
23✔
148
            }
149

150
            var col = appContext.Db().collection(rulesCollectionName);
8✔
151
            col.remove(
8✔
152
                {
153
                    name: rule.name,
154
                    subservice: rule.subservice,
155
                    service: rule.service
156
                },
157
                function(err, result) {
158
                    myutils.logErrorIf(err);
8✔
159
                    logger.info('rulesStore.Remove %j', myutils.firstChars(result));
8✔
160
                    return callback(err, result);
8✔
161
                }
162
            );
163
        });
164
    },
165
    Save: function Save(r, callback) {
166
        var cb = function(err, result) {
144✔
167
            myutils.logErrorIf(err);
144✔
168
            logger.info('rulesStore.Save %j', myutils.firstChars(result));
144✔
169
            return callback(err, result);
144✔
170
        };
171

172
        myutils.collectionExists(appContext.Db(), rulesCollectionName, function(exists) {
144✔
173
            if (!exists) {
144!
NEW
174
                return cb('collection ' + rulesCollectionName + ' does not exist');
×
175
            }
176

177
            var col = appContext.Db().collection(rulesCollectionName);
144✔
178
            stringifyPostAxnParams(r);
144✔
179
            // Depending if r has _id or not we use updateOne or insertOne
180
            if (r._id) {
144!
NEW
181
                col.updateOne({ _id: r._id }, r, cb);
×
182
            } else {
183
                col.insertOne(r, cb);
144✔
184
            }
185
        });
186
    },
187
    Update: function Update(id, r, callback) {
188
        var cb = function(err, result) {
13✔
189
            myutils.logErrorIf(err);
13✔
190
            logger.info('rulesStore.Update %j', myutils.firstChars(result));
13✔
191
            return callback(err, result);
13✔
192
        };
193

194
        myutils.collectionExists(appContext.Db(), rulesCollectionName, function(exists) {
13✔
195
            if (!exists) {
13✔
196
                return cb('collection ' + rulesCollectionName + ' does not exist');
11✔
197
            }
198
            var col = appContext.Db().collection(rulesCollectionName);
2✔
199
            stringifyPostAxnParams(r);
2✔
200

201
            col.findOneAndUpdate(
2✔
202
                { name: id },
203
                { $set: r },
204
                {
205
                    upsert: false,
206
                    returnNewDocument: true
207
                },
208
                function(err, result) {
209
                    if (result && result.lastErrorObject && result.lastErrorObject.updatedExisting === false) {
2✔
210
                        return cb(new errors.NotFoundRule(id), null);
1✔
211
                    }
212
                    parsePostAxnParams(r);
1✔
213
                    logger.debug('rulesStore.findOneAndUpdate %s', myutils.firstChars(r));
1✔
214
                    return cb(err, r);
1✔
215
                }
216
            );
217
        });
218
    }
219
};
220
/**
221
 * Constructors for possible errors from this module
222
 *
223
 * @type {Object}
224
 */
225
module.exports.errors = errors;
1✔
226

227
(function() {
1✔
228
    errors.NotFoundRule = function NotFoundRule(msg) {
1✔
229
        this.name = 'RULE_NOTFOUND';
20✔
230
        this.message = 'rule not found ' + msg;
20✔
231
        this.httpCode = 404;
20✔
232
    };
233
    Object.keys(errors).forEach(function(element) {
1✔
234
        util.inherits(errors[element], Error);
1✔
235
    });
236
})();
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc