]>
Commit | Line | Data |
---|---|---|
c173e565 C |
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) { | |
8425cb89 C |
29 | if (err) { |
30 | logger.error('Cannot find one pool request.', { error: err }) | |
31 | return // Abort | |
32 | } | |
c173e565 C |
33 | |
34 | if (entity) { | |
35 | if (entity.type === type) { | |
8425cb89 C |
36 | logger.error('Cannot insert two same requests.') |
37 | return // Abort | |
c173e565 C |
38 | } |
39 | ||
40 | // Remove the request of the other type | |
41 | PoolRequestsDB.remove({ id: id }, function (err) { | |
8425cb89 C |
42 | if (err) { |
43 | logger.error('Cannot remove a pool request.', { error: err }) | |
44 | return // Abort | |
45 | } | |
c173e565 C |
46 | }) |
47 | } else { | |
48 | PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) { | |
8425cb89 C |
49 | logger.error('Cannot create a pool request.', { error: err }) |
50 | return // Abort | |
c173e565 C |
51 | }) |
52 | } | |
53 | }) | |
54 | } | |
55 | ||
56 | function list (callback) { | |
57 | PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback) | |
58 | } | |
59 | ||
60 | function removeRequests (ids) { | |
61 | PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { | |
62 | if (err) { | |
63 | logger.error('Cannot remove requests from the pool requests database.', { error: err }) | |
8425cb89 | 64 | return // Abort |
c173e565 C |
65 | } |
66 | ||
67 | logger.info('Pool requests flushed.') | |
68 | }) | |
69 | } | |
70 | ||
71 | // --------------------------------------------------------------------------- | |
72 | ||
73 | module.exports = PoolRequests | |
74 | })() |