-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookingManager.js
More file actions
180 lines (161 loc) · 6.94 KB
/
bookingManager.js
File metadata and controls
180 lines (161 loc) · 6.94 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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 => {
changeStatus(bot, newStatus, statusMessage);
});
});
}
});
};
function changeStatus(bot, newStatus, statusMessage = "") {
const updateStatus = (status, message = "") => {
dataSource.status = status;
dataSource.statusMessage = message;
dataSource.statusTime = new Date();
return dataSource.save();
};
console.log(`Bot ${bot.id} status update initiated: ${newStatus}`);
switch (newStatus) {
case "new":
console.log(`Bot ${bot.id} status remains 'new'.`);
break;
case "captcha":
console.log(`Bot ${bot.id} encountered CAPTCHA. Retrying after delay.`);
setTimeout(() => {
bot.initialize(true); // Retry initialization after CAPTCHA
}, settings.captchaRetryDelay || 30000); // Default delay of 30 seconds
updateStatus("captcha", "Retrying after CAPTCHA detection.")
.then(() => console.log(`Bot ${bot.id} status updated to 'captcha'.`))
.catch(err => console.error(`Error updating bot ${bot.id} status: ${err.message}`));
break;
case "inprogress":
case "initialized":
updateStatus(newStatus === "captcha" ? "captcha" : "inprogress")
.then(() => console.log(`Bot ${bot.id} status updated to ${newStatus}.`))
.catch(err => console.error(`Error updating bot ${bot.id} status: ${err.message}`));
break;
case "successful":
case "outofdate":
case "failed":
updateStatus(newStatus, statusMessage)
.then(() => {
console.log(`Bot ${bot.id} status updated to ${newStatus}.`);
const index = bots.indexOf(bot);
if (index >= 0) bots.splice(index, 1);
console.log(`Bot ${bot.id} removed from active bots.`);
return bot.destroy();
})
.then(() => console.log(`Bot ${bot.id} destroyed.`))
.catch(err => console.error(`Error handling bot ${bot.id} lifecycle: ${err.message}`));
break;
default:
console.error(`Unknown status '${newStatus}' for bot ${bot.id}.`);
}
}
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) => {
});
*/