diff options
Diffstat (limited to 'models/poolRequests.js')
-rw-r--r-- | models/poolRequests.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/models/poolRequests.js b/models/poolRequests.js new file mode 100644 index 000000000..0f488ef04 --- /dev/null +++ b/models/poolRequests.js | |||
@@ -0,0 +1,67 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var mongoose = require('mongoose') | ||
5 | |||
6 | var logger = require('../helpers/logger') | ||
7 | |||
8 | // --------------------------------------------------------------------------- | ||
9 | |||
10 | var poolRequestsSchema = mongoose.Schema({ | ||
11 | type: String, | ||
12 | id: String, // Special id to find duplicates (video created we want to remove...) | ||
13 | request: mongoose.Schema.Types.Mixed | ||
14 | }) | ||
15 | var PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema) | ||
16 | |||
17 | // --------------------------------------------------------------------------- | ||
18 | |||
19 | var PoolRequests = { | ||
20 | addRequest: addRequest, | ||
21 | list: list, | ||
22 | removeRequests: removeRequests | ||
23 | } | ||
24 | |||
25 | function addRequest (id, type, request) { | ||
26 | logger.debug('Add request to the pool requests.', { id: id, type: type, request: request }) | ||
27 | |||
28 | PoolRequestsDB.findOne({ id: id }, function (err, entity) { | ||
29 | if (err) logger.error(err) | ||
30 | |||
31 | if (entity) { | ||
32 | if (entity.type === type) { | ||
33 | logger.error(new Error('Cannot insert two same requests.')) | ||
34 | return | ||
35 | } | ||
36 | |||
37 | // Remove the request of the other type | ||
38 | PoolRequestsDB.remove({ id: id }, function (err) { | ||
39 | if (err) logger.error(err) | ||
40 | }) | ||
41 | } else { | ||
42 | PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) { | ||
43 | if (err) logger.error(err) | ||
44 | }) | ||
45 | } | ||
46 | }) | ||
47 | } | ||
48 | |||
49 | function list (callback) { | ||
50 | PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback) | ||
51 | } | ||
52 | |||
53 | function removeRequests (ids) { | ||
54 | PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { | ||
55 | if (err) { | ||
56 | logger.error('Cannot remove requests from the pool requests database.', { error: err }) | ||
57 | return | ||
58 | } | ||
59 | |||
60 | logger.info('Pool requests flushed.') | ||
61 | }) | ||
62 | } | ||
63 | |||
64 | // --------------------------------------------------------------------------- | ||
65 | |||
66 | module.exports = PoolRequests | ||
67 | })() | ||