forked from moffatman/qu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.js
128 lines (118 loc) · 2.83 KB
/
queue.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
124
125
126
127
128
//Internal Dependencies
var db = require('./db.js');
function Queue(locationId, concurrency) {
this.locationId = locationId;
this.queue = [];
var hours = db.getHours(locationId);
for (var i = hours.open; i < hours.close; i++) {
this.queue[i] = [[], [], [], []];
}
this.hours = Object.keys(this.queue);
this.concurrency = concurrency;
this.nextId = 0;
}
Queue.prototype.iterate = function(cb) {
for (var i in this.hours) {
var hour = this.hours[i];
for (var quarterHour = 0; quarterHour < 4; quarterHour++) {
if (!cb(this.queue[hour][quarterHour], hour, quarterHour)) break;
}
}
}
Queue.prototype.getAppointments = function() {
var ret = [];
this.iterate(function(q) {
for (var key in q) {
ret.push(q[key]);
}
return true;
});
return ret;
}
Queue.prototype.getAvailableTimes = function() {
var ret = [];
var queue = this;
this.iterate(function(q, hour, quarterHour) {
var time = 0;
for (var key in q) {
var appointment = q[key];
if (appointment.status != "done") {
time += (appointment.task.time_unprepared + appointment.task.time_prepared) / 2;
}
}
if (time < 15 * queue.concurrency) {
ret.push(hour + ":" + (quarterHour == 0 ? "00" : (quarterHour * 15)));
}
return true;
});
return ret;
}
Queue.prototype.getAllTimes = function() {
var ret = [];
var queue = this;
this.iterate(function(q, hour, quarterHour) {
var time = 0;
for (var key in q) {
var appointment = q[key];
if (appointment.status != "done") {
time += (appointment.task.time_unprepared + appointment.task.time_prepared) / 2;
}
}
ret.push({
string: hour + ":" + (quarterHour == 0 ? "00" : (quarterHour * 15)),
available: time < 15 * queue.concurrency
});
return true;
});
return ret;
}
Queue.prototype.getQ = function(timeString) {
var split = timeString.split(":");
var hour = parseInt(split[0]);
var quarterHour = parseInt(split[1]) / 15;
return this.queue[hour][quarterHour];
}
Queue.prototype.getAppointment = function(id) {
var appointment = false;
this.iterate(function(q) {
for (var key in q) {
if (q[key].id == id) {
appointment = q[key];
return false;
}
}
return true;
});
if (appointment) {
return appointment;
}
throw "Appointment not found";
}
Queue.prototype.add = function(personId, selectedTime, task) {
if (this.getAvailableTimes().includes(selectedTime)) {
this.getQ(selectedTime).push({
person: db.getPerson(personId),
task: task,
time: selectedTime,
id: this.nextId,
forms: "Status Unknown",
status: "On time"
});
this.nextId++;
return this.nextId - 1;
}
else {
throw "Time not available";
}
}
Queue.prototype.getPosition = function(appointmentId) {
var position = 0;
for (var item in this.queue) {
if (item.id == appointmentId) {
return position;
}
position++;
}
throw "Appointment not in queue";
}
module.exports = Queue;