-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookingManager.js
More file actions
164 lines (147 loc) · 6.4 KB
/
bookingManager.js
File metadata and controls
164 lines (147 loc) · 6.4 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const Browser = require('./Browser');
const BookingBot = require('./BookingBot');
const { BookingSchedule } = require('./models/bookingSchedules');
const { CancelationMonitoring } = require('./models/cancelationMonitoring');
const { Settings, currentSettings, DateMethods } = require('./models/settings');
const moment = require('moment-timezone');
const refreshList = 300;
module.exports.create = () => new Promise(async (resolve, reject) => {
const bots = [];
let settings = null;
const controller = {
reloadSettings: async function(){
settings = await currentSettings(true);
bots.forEach(bot => bot.settings = settings);
},
deleteBot: function(botId){
let bot = bots.find(item => item.id === botId);
if (bot) {
const index = bots.indexOf(bot);
if (index >= 0) bots.splice(index,1);
bot.destroy().then(()=>{
delete bot;
});
}
},
};
await controller.reloadSettings();
const processQuery = function(records){
records.forEach(record => {
if (!bots.find((item)=> item.id === record._id.toString()))
{
const bot = new BookingBot(record);
bots.push(bot);
bot.on('changeStatus', (newStatus, statusMessage) => {
if (bot.dataSource instanceof BookingSchedule)
var dataSourceQuery = BookingSchedule.findById(bot.id);
else
var dataSourceQuery = CancelationMonitoring.findById(bot.id);
dataSourceQuery.then( dataSource => {
switch (newStatus){
case "new": break;
case "captcha":
dataSource.status = newStatus;
dataSource.statusMessage = "";
dataSource.statusTime = new Date();
dataSource.save();
break;
case "initialized":
case "inprogress":
dataSource.status = "inprogress";
dataSource.statusMessage = "";
dataSource.statusTime = new Date();
dataSource.save();
break;
case "successful":
case "outofdate":
case "failed":
dataSource.status = newStatus;
dataSource.statusMessage = statusMessage;
dataSource.statusTime = new Date();
dataSource.save()
.then( async (result) => {
const index = bots.indexOf(bot);
if (index >= 0) bots.splice(index,1);
return bot.destroy();
})
.then(()=>{
})
.catch(err => console.error(err.message));
break;
}
});
});
}
});
};
Browser.initialize().then(()=>{
const checkingBotsList = function(){
const timeoutTask = new Date();
timeoutTask.setMinutes( timeoutTask.getMinutes() - (settings.bookingInterval || 1) );
const userToday = DateMethods.userToday();
const bookingTargetDateTime = settings.bookingTargetDateTime;
const startBeforeNow = moment(bookingTargetDateTime).add(settings.startBefore * -1, 'm' ).toDate();
const highLimitedHour = moment(bookingTargetDateTime).add(10, 'm' ).toDate();
if (startBeforeNow <= new Date() && highLimitedHour >= new Date())
{
BookingSchedule.find(
{ $or : [
{
$and : [
{ status: 'pending' },
{ launchDate : userToday }
]
},
{
$and : [
{ status: 'inprogress' },
{ statusTime : timeoutTask }
]
}
] }
)
.populate('account')
.then(processQuery)
.catch((error)=>{
console.error(error);
});
}
const today = DateMethods.today();
const bookingLastOpenDate = DateMethods.today();
bookingLastOpenDate.setUTCDate(bookingLastOpenDate.getUTCDate() + settings.openTeeTimes);
CancelationMonitoring.find(
{ $or : [
{
$and : [
{ status: 'active' },
{ fromDate : { $lte: bookingLastOpenDate } },
{ toDate : { $gte: today } }
]
},
{
$and : [
{ status: 'inprogress' },
{ statusTime : { $lt: timeoutTask } }
]
}
] }
)
.populate('account')
.then( records => {
processQuery(records);
setTimeout(checkingBotsList, refreshList);
})
.catch( error => {
setTimeout(checkingBotsList, refreshList);
});
};
setTimeout(checkingBotsList, refreshList);
return resolve(controller);
}).catch((error)=>{
// error
return reject(error);
});
});
/*module.exports.start = () => new Promise(async (resolve, reject) => {
});
*/