-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathdataStore.js
56 lines (49 loc) · 1.74 KB
/
dataStore.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
var _ = require('lodash');
function DataStore() {
var botVariables = [];
}
DataStore.prototype.saveAllVariables = function(response, langArr) {
this.botVariables = [];
for (var i = 0; i < langArr.length; i++) {
if (response[i].errors) {
console.log(response[i].errors[0].msg);
var obj = {
"variables": [],
"error": response[i].errors[0].msg
};
this.botVariables.push(obj);
} else {
this.botVariables.push(response[i]);
}
}
};
DataStore.prototype.addVariable = function(variable, arrIndex) {
if (!this.botVariables[arrIndex].error) {
this.botVariables[arrIndex].variables.push(variable);
this.botVariables[arrIndex].count = this.botVariables[arrIndex].count + 1;
}
};
DataStore.prototype.updateVariable = function(variable, langArr, index) {
var eleIndex = _.findIndex(this.botVariables[index].variables, ['_id', variable._id]);
if (eleIndex > -1) {
if (variable.variableType === "env") {
for (var i = 0; i < langArr.length; i++) {
this.botVariables[i].variables[eleIndex] = variable;
}
} else {
this.botVariables[index].variables[eleIndex] = variable;
}
}
};
DataStore.prototype.deleteVariable = function(variable, langArr) {
for (var i = 0; i < langArr.length; i++) {
if (!this.botVariables[i].error) {
var eleIndex = _.findIndex(this.botVariables[i].variables, ['_id', variable._id]);
this.botVariables[i].variables.splice(eleIndex, 1);
this.botVariables[i].count = this.botVariables[i].count - 1;
}
}
};
module.exports.getInst = function() {
return new DataStore();
};