This repository was archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmissionStore.js
More file actions
61 lines (57 loc) · 1.95 KB
/
Copy pathsubmissionStore.js
File metadata and controls
61 lines (57 loc) · 1.95 KB
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
// An object for interacting with the submission collection.
// Submissions are kept in 2 stores:
// 1. A 'Cold Store' for past submissions,
// 2. A queue that feeds the poster object with new things to post.
const assert = require('assert');
const config = require('./config');
const mongoUtil = require('./mongoUtil');
let SubmissionStore = function() {
db = mongoUtil.getDb();
};
SubmissionStore.prototype = {
addToColdStore: function (objToAdd) {
assert.notEqual(null, objToAdd);
db.collection(config.mongodb.coldStoreName)
.updateOne(objToAdd, objToAdd, {upsert: true}, function (err) {
if (err) { return console.dir(err) }
});
},
ifSubmissionNew: function(item, callback) {
assert.notEqual(null, item);
db.collection(config.mongodb.coldStoreName)
.find({_id: item._id}).limit(1).count()
.then(function (count) {
if (count === 0) {
callback();
}
}).catch(function(err) {
if (err) { return console.dir(err); }
console.log("Promise Rejected: " + err);
});
},
queuePush: function (item) {
mongoUtil.getQueue().add(item, function (err, id) {
if (err) { return console.dir(err); }
console.log(`Adding obj id ${id} to submission-queue.`);
});
},
queuePop: function () {
return new Promise(function (resolve, reject) {
mongoUtil.getQueue().get(function (err, msg) {
if (err) reject(err);
resolve(msg);
})
})
},
queueAck: function (msg) {
mongoUtil.getQueue().ack(msg, function (err, id) {
if (err) { console.dir(err); }
});
},
queueClean: function () {
mongoUtil.getQueue().clean(new function (err) {
if (err) { console.dir(err); }
});
}
};
module.exports = SubmissionStore;