forked from opentok/opentok-video-embed-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
72 lines (63 loc) · 2.11 KB
/
db.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
// This is our simple DB in memory. A real-world use case would use an actual database.
let DB = {
// Used to store meeting information
meetings: [],
// Used to store embed code
embed_code: ""
};
let sort = m_list => {
return m_list.sort(function (a, b) {
return a.start_time > b.start_time;
});
}
/**
* Filters through given list of meetings and split it into upcoming and current meetings based on time.
*
* @param {null|boolean} is_booked - If `null` or `undefined`, filter on all items in `mlist`. If true, filter only on
* meetings that are booked. If false, filter only on meetings that have not been booked.
* @return {object} Object containing `upcoming` and `current` meetings
*/
DB.meetings_filter = function (is_booked=null) {
const currtime = Date.now();
let mlist;
if (is_booked != null) {
if (is_booked) {
mlist = () => this.meetings.filter(m => m.booked);
} else {
mlist = () => this.meetings.filter(m => !m.booked);
}
} else {
mlist = () => this.meetings;
}
return {
// Starting after 5 minutes
upcoming: sort(mlist().filter(i => i.start_time.getTime() >= currtime + 300000 )),
// Starting in 5 minutes or has already started but not ended
current: sort(mlist().filter(i => i.start_time.getTime() < currtime + 300000 && i.end_time.getTime() >= currtime))
}
};
/**
* Find a meeting by its id
*
* @param {number} id - Meeting id
* @return {null|object} - Null if meeting id is not found, else return the meeting object
*/
DB.meetings_get = function (id) {
return this.meetings.find(m => m.id === id) || null;
}
/**
* Add/Update a meeting entry
*
* @param {object} new_meeting - Meeting object to replace. If `new_meeting.id` exists, existing entry is updated. Else,
* a new entry is pushed to `DB.meetings`
* @return {null|object} - Null if meeting id is not found, else return the meeting object
*/
DB.meetings_put = function (new_meeting) {
const key = this.meetings.findIndex(m => m.id === new_meeting.id);
if (key < 0) {
this.meetings.push(new_meeting);
} else {
this.meetings[key] = new_meeting;
}
}
module.exports = DB;