-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeslots.js
118 lines (106 loc) · 3.21 KB
/
Timeslots.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
const mqtt = require("./Mqtt");
module.exports.mqtt = mqtt;
const Booking = require("./Models/booking.js");
const mongoose = require("mongoose");
// Published topic
const timeslotsValidatedTopic = "Team5/Dentistimo/Timeslots/Validated"; // Forward to Frontend
/********************************************************** */
/* Check and update timeslots availability */
/********************************************************** */
/**
* Starting point for checking timeslots availabilty
* @param {*} message timeslots as JSON
*/
module.exports.checkTimeslots = function (message) {
console.log("Validating timeslots");
let timeslots = message.timeSlots;
const result = updateBreaks(timeslots);
checkBookings(result, message.clinicId);
}
/**
* Removes breaks from timeslots so that the breaks are kept free
* @param {*} timeslots
* @returns updated timeslots
*/
function updateBreaks(timeslots) {
const LUNCH_1 = "12:00";
const LUNCH_2 = "12:30";
const FIKA_1 = "10:00";
const FIKA_2 = "15:00";
return (result = timeslots.filter(
(item) =>
item.start !== LUNCH_1 &&
item.start !== LUNCH_2 &&
item.start !== FIKA_1 &&
item.start !== FIKA_2
));
}
/**
* Finds all bookings made to the clinic at the date. Then calls
* methods filterAvailablity and forwardTimeslots
* to filter and finally to forward timeslots.
* @param {*} timeslots timeslots with updated breaks
* @param {*} clinicID
*/
function checkBookings(timeslots, clinicID) {
const promises = [];
for (let i = 0; i < timeslots.length; i++) {
promises.push(
Booking.find({
clinic: mongoose.Types.ObjectId(clinicID),
date: new Date(convertDate(timeslots[i].date))
.toISOString()
.slice(0, 10),
startTime: timeslots[i].start + "-" + timeslots[i].end,
})
);
}
Promise.all(promises).then((bookings) => {
timeslots = filterAvailabilty(timeslots, bookings);
forwardTimeslots(timeslots, clinicID);
});
}
/**
* Ensures that the correct date format is used
* @param {*} date
* @returns
*/
const convertDate = (date) => {
const parts = date.split(" ");
if (parts.length != 4) {
console.error("Date from timeslot generator has invalid format");
} else {
return parts[2] + " " + parts[1] + " " + parts[3] + " 00:00:00 UTC";
}
};
/**
* Filters availabilty based on how many bookings have already been made
* at a timeslot.
* @param {*} timeslots
* @param {*} bookings
* @returns updated timeslots with available timeslots.
*/
function filterAvailabilty(timeslots, bookings) {
return (result = timeslots
.map((timeslot, index) => {
timeslot.available = timeslot.available - bookings[index].length;
return timeslot;
})
.filter((item) => item.available > 0));
}
/**
* Forwards valid timeslots to frontend via MQTT
* @param {*} timeslots
* @param {*} clinicId
*/
function forwardTimeslots(timeslots, clinicId) {
mqtt.publishToTopic(
timeslotsValidatedTopic,
JSON.stringify({
timeSlots: timeslots,
clinicId: clinicId,
}),
{qos:1}
);
console.log("Timeslots have been validated");
}