-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.mem.js
124 lines (104 loc) · 2.73 KB
/
sessions.mem.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var _sessions = [];
function findSessions(username) {
for(var i=0; i<_sessions.length; i++)
if (_sessions[i].username == username)
return {valid:true, sessions:_sessions[i].sessions};
return {valid:false, sessions:[]};
}
function isIdUsed(id) {
for(var i=0; i<_sessions.length; i++)
{
var sessions = _sessions[i].sessions;
if (sessions.some(function(el,i,a) { return el.id == id; }))
return true;
}
return false;
}
function createNewSessionId() {
var id;
var counter = 0;
var maxCounter = 100;
do {
id = Math.floor( Math.random() * 10000 );
counter++;
} while( counter < maxCounter && isIdUsed(id) );
console.log("createNewSessionId: counter = "+counter);
if (counter >= maxCounter)
return null;
return id;
}
module.exports = {
init: function() {
if (process.env.NODE_ENV == 'development') {
_sessions = [
{username:'admin', sessions:[
{id:1,open:true, createdOn:new Date(2011,1,10), candidateName:'joe', interviewerText:'Some interviewer text', candidateText:'some candidate text...', interviewerTextLastUpdateTime:new Date(), candidateTextLastUpdateTime:new Date()},
{id:2,open:false, createdOn:new Date(2010,11,22), candidateName:'albert', interviewerText:'', candidateText:'', interviewerTextLastUpdateTime:new Date(), candidateTextLastUpdateTime:new Date()},
]},
];
}
},
all: function(username) {
return findSessions(username).sessions;
},
allOpen: function(username) {
return findSessions(username).sessions.filter(function(el,i,a) { return el.open; });
},
allClosed: function(username) {
return findSessions(username).sessions.filter(function(el,i,a) { return !el.open; });
},
get: function(id) {
for(var i=0; i<_sessions.length; i++)
{
var sessions = _sessions[i].sessions;
if (sessions != null)
{
for(var j=0; j<sessions.length; j++)
if (sessions[j].id == id)
return sessions[j];
}
}
return null;
},
createNew: function(username) {
var newId = createNewSessionId();
if (newId == null)
return null;
var elt = {
id:newId,
open:true,
createdOn: new Date(),
comments:null,
interviewerText:'',
candidateText:'',
interviewerTextLastUpdateTime:new Date(),
candidateTextLastUpdateTime:new Date()
};
var entry = findSessions(username);
if (!entry.valid)
{
entry = {username:username, sessions:[]};
_sessions.push(entry);
}
entry.sessions.push(elt);
return elt;
},
update: function(aSession) {
// nothing to do here
},
remove: function(id) {
for(var i=0; i<_sessions.length; i++)
{
var sessions = _sessions[i].sessions;
if (sessions != null)
{
for(var j=0; j<sessions.length; j++)
if (sessions[j].id == id)
{
sessions.splice(j,1);
return;
}
}
}
}
};