forked from fcamblor/weezevent-slack-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.js
62 lines (51 loc) · 1.61 KB
/
store.js
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
62
var Promise = require('bluebird');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var _ = require('lodash');
function Store(opts){
var self = this;
self.mongo_url = opts.mongo_url;
};
Store.prototype.fetchPersistedParticipants = function(storeName){
var self = this;
return new Promise(function(resolve, reject) {
MongoClient.connect(self.mongo_url, function(err, db){
if(err) {
reject(err);
return;
}
db.collection('participants').find({ name: storeName }).limit(1).next(function(err, storeParticipants){
if(err) {
reject(err);
return;
}
resolve(storeParticipants);
db.close();
});
});
});
};
Store.prototype.persistParticipantsIn = function(storeName, participants) {
var self = this;
return new Promise(function(resolve, reject) {
MongoClient.connect(self.mongo_url, function(err, db){
if(err) {
reject(err);
return;
}
db.collection('participants').updateOne({ name: storeName }, { $set: {
name: storeName,
participants: participants,
latest_creation: _.max(_.map(participants, 'create_date'))
} }, function(err, r){
if(err) {
reject(err);
return;
}
resolve();
db.close();
});
});
});
};
module.exports = Store;