-
Notifications
You must be signed in to change notification settings - Fork 2
/
getSlots.js
91 lines (74 loc) · 2.27 KB
/
getSlots.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
var moment = require('moment');
moment().format();
var getWeekArray = function(date, time){
console.log("entered getWeekArray");
var startString = date + time;
var a = moment(date);
var b = a.add(7, 'day'); //make this shit a moment god dammit
var c = b.format().substring(0,19);
var endString = c.split('T').join(' ');
var start = moment(startString, 'YYYY-MM-DD hh:mm a');
var end = moment(endString, 'YYYY-MM-DD hh:mm a');
var result = [];
var current = moment(start);
while (current <= end) {
result.push(current.format('YYYY-MM-DD HH:mm'));
current.add(30, 'minutes');
}
result = result.filter(function(item){
var item = item.split(' ');
var time = parseInt(item[1].substring(0,2));
return (time>=9 && time<=18);
})
return result;
} //returns week array
var limitWeekArray = function(weekArray){
console.log("entered limitWeekArray");
var finalArray = [];
for(var i = 1; i < 8 ; i++){
finalArray.push([]);
}
var j = 0 ;
for(var i=0;i<weekArray.length; i++){
if(finalArray[j].length===3){
j++;
var date = parseInt(weekArray[i].substring(8,10));
var target = date===30 || date===31 ? 1 : date+1;
for(var z=0;z<weekArray.length;z++){
var look = parseInt(weekArray[z].substring(8,10));
if(target === look){
i=z;
break;
}
}
if(j===7)break;
}
finalArray[j].push(weekArray[i]);
}
var mainArray = [];
var k=0;
while(mainArray.length!==10){
if(finalArray[k].length===0)k++;
mainArray.push(finalArray[k].shift());
}
console.log("mainArray", mainArray);
return mainArray;
} // cuts down weekArray to 10 slots;
var cutWeekArray = function(busyArray, state){
console.log("entered function cutWeekArray");
var weekArray = getWeekArray(state.date, state.time);
for(var i=0;i<busyArray.length;i+=2){
var x = weekArray.indexOf(busyArray[i]);
var y = weekArray.indexOf(busyArray[i+1]);
if(x!==-1)weekArray.splice(x,y-x);
}
return limitWeekArray(weekArray);
} // returns week array with available time slots
var getSlots = function(busyArray, user){
var result = cutWeekArray(busyArray, user.pendingState);
console.log("after cutting and limiting", result);
return result;
}
module.exports = {
getSlots
}