-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·125 lines (111 loc) · 3.31 KB
/
index.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
const find = require('lodash/find');
require('dotenv').config();
const useUser = require('./models/user');
const { getNextReservationDay } = require('./helpers/date');
const { BOOKING, COURTS } = require('./constants');
const run = async () => {
try {
// init users:
// - get their php session id
// - get their pending bookings
const doubleUser = await useUser(
process.env.DOUBLE_USER_EMAIL,
process.env.DOUBLE_USER_PASSWORD,
);
const simpleUser = await useUser(
process.env.SIMPLE_USER_EMAIL,
process.env.SIMPLE_USER_PASSWORD,
);
await Promise.all([doubleUser, simpleUser]);
// define the next reservation day
const nextResevationDay = getNextReservationDay();
// launch the magic :-)
// get next pending resa for double
const nextPendingDoubleBooking = doubleUser.getNextPendingBooking(
nextResevationDay,
BOOKING.beginHour,
BOOKING.endHour,
);
const nextPendingSimpleBooking = simpleUser.getNextPendingBooking(
nextResevationDay,
BOOKING.beginHour,
BOOKING.endHour,
false,
);
// if pending double resa
if (nextPendingDoubleBooking) {
// this mean there is a pending resa in double
// check if there is a better court
const courtIndex = COURTS.indexOf(nextPendingDoubleBooking.courtId);
if (courtIndex > 0) {
await doubleUser.makeBestBooking(
{
momentDay: nextResevationDay,
beginHour: BOOKING.beginHour,
endHour: BOOKING.endHour,
},
COURTS.slice(0, courtIndex),
true,
nextPendingDoubleBooking,
);
}
} else {
// no pending double
// check if simple is booked
if (nextPendingSimpleBooking) {
// cancel it
await simpleUser.cancelPendingBooking(nextPendingSimpleBooking);
// sleep during 5 sec
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
await delay(5000);
}
// make the best book for double
await doubleUser.makeBestBooking(
{
momentDay: nextResevationDay,
beginHour: BOOKING.beginHour,
endHour: BOOKING.endHour,
},
COURTS,
);
// and book in simple for next week
await simpleUser.makeBestBooking(
{
momentDay: nextResevationDay.clone().add(1, 'week'),
beginHour: BOOKING.beginHour,
endHour: BOOKING.endHour,
},
COURTS,
false,
);
return true;
}
const nextWeekReservationDay = nextResevationDay.clone().add(1, 'week');
const nextWeekPendingSimpleBooking = simpleUser.getNextPendingBooking(
nextWeekReservationDay,
BOOKING.beginHour,
BOOKING.endHour,
false,
);
if (nextWeekPendingSimpleBooking) {
const nextCourtIndex = COURTS.indexOf(
nextWeekPendingSimpleBooking.courtId,
);
if (nextCourtIndex > 0) {
await simpleUser.makeBestBooking(
{
momentDay: nextWeekReservationDay,
beginHour: BOOKING.beginHour,
endHour: BOOKING.endHour,
},
COURTS.slice(0, nextCourtIndex),
false,
nextWeekPendingSimpleBooking,
);
}
}
} catch (error) {
console.log(error);
}
};
run();