aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/requests.js
blob: 2152ae0e92756c88b10ec697e88f757d2c1f179a (plain) (blame)
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
'use strict'

const mongoose = require('mongoose')

const logger = require('../helpers/logger')

// ---------------------------------------------------------------------------

const requestsSchema = mongoose.Schema({
  type: String,
  id: String, // Special id to find duplicates (video created we want to remove...)
  request: mongoose.Schema.Types.Mixed
})
const RequestsDB = mongoose.model('requests', requestsSchema)

// ---------------------------------------------------------------------------

const Requests = {
  create: create,
  findById: findById,
  list: list,
  removeRequestById: removeRequestById,
  removeRequests: removeRequests
}

function create (id, type, request, callback) {
  RequestsDB.create({ id: id, type: type, request: request }, callback)
}

function findById (id, callback) {
  RequestsDB.findOne({ id: id }, callback)
}

function list (callback) {
  RequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback)
}

function removeRequestById (id, callback) {
  RequestsDB.remove({ id: id }, callback)
}

function removeRequests (ids) {
  RequestsDB.remove({ _id: { $in: ids } }, function (err) {
    if (err) {
      logger.error('Cannot remove requests from the requests database.', { error: err })
      return // Abort
    }

    logger.info('Pool requests flushed.')
  })
}

// ---------------------------------------------------------------------------

module.exports = Requests