-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.js
52 lines (46 loc) · 1.27 KB
/
history.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
'use strict';
const Promise = require('bluebird');
const debug = require('debug')('history');
const moment = require('moment');
const _ = require('lodash');
const Word = require('./word');
const Storage = require('./storage');
const collectionName = 'history';
/**
* Stores last state, word, message and whatever for each chat
* @param {Object} data
* @param {Number} chatId
* @returns {Promise}
*/
const save = function(data, chatId) {
return Storage
.remove(collectionName, {chatId: chatId})
.then(function() {
return Storage.insert(collectionName, {
data: data,
date: moment().toDate(),
chatId: chatId
});
});
};
/**
* Retrieves last state, word and message for each chat
* @param {Number} chatId
* @returns {Promise}
*/
const get = function(chatId) {
return Storage
.find(collectionName, {chatId: chatId})
.then(function(entries) {
let data = entries.length ? entries[0].data : {};
// Hydrating the word object
if (data.word && data.word.term) {
data.word = new Word(data.word.term, data.word.translation);
}
return data;
});
};
module.exports = {
save: save,
get: get
};