can start booking with gui

This commit is contained in:
2022-09-15 00:17:13 +02:00
parent abc8de7933
commit 8d0df978f7
5 changed files with 29 additions and 20 deletions
+82
View File
@@ -0,0 +1,82 @@
const {_android: android} = require('playwright');
const ExcelUtil = require("./excel/ExcelUtil");
const CommandorPage = require("./workers/CommandorPage");
const {MongoManager, formatDate} = require("./workers/mongo_manager");
const mongoManager = new MongoManager();
let excelUtil = new ExcelUtil();
let collectionName = formatDate(new Date())
async function filterAlreadyBookedContacts(contactList) {
let alreadyBookedContacts = await mongoManager.getAllSuccessfulItemsForDay(collectionName);
let contactsToBook = [];
contactList.forEach((contact) => {
if (alreadyBookedContacts.find((bookedContact) => bookedContact.email === contact.mail) === undefined) {
contactsToBook.push(contact)
}
})
return contactsToBook;
}
// Connect to the device.
async function needToBook(contact, mongoManager) {
let alreadyBooked = await mongoManager.getAllSuccessfulItemsForDay(collectionName)
let toReturn = true;
await alreadyBooked.forEach((bookedItem) => {
if (bookedItem.email === contact.mail) {
toReturn = false;
}
}
)
return toReturn
}
async function startBook(contactPojo, device, selectedStore) {
console.log(`Model: ${device.model()}`);
console.log(`Serial: ${device.serial()}`);
if (await needToBook(contactPojo, mongoManager)) {
let commandor = new CommandorPage(contactPojo, device, mongoManager, selectedStore);
//read contacts form excel
return await commandor.loadPage();
} else {
console.log("do not send request --> skip")
}
}
async function startWithList(contacts, device, selectedStore) {
await contacts.reduce(async (promise, contactPojo) => {
// This line will wait for the last async function to finish.
// The first iteration uses an already resolved Promise
// so, it will immediately continue.
await promise;
const contents = await startBook(contactPojo, device, selectedStore);
console.log(contents);
}, Promise.resolve());
}
async function startBookWithNumbers(startNumber, endNumber, selectedStore) {
let allContactList = excelUtil.readContacts();
let contactList;
if (endNumber <= allContactList.length) {
contactList = allContactList.slice(startNumber, endNumber);
} else {
contactList = allContactList;
}
mongoManager.connect().then(r => {
filterAlreadyBookedContacts(contactList).then(listToBook => {
console.log("number of contacts to book:" + listToBook.length)
android.devices().then((devices) => {
let segmentNumber = listToBook.length / devices.length;
for (let i = 0; i < devices.length; i++) {
startWithList(listToBook.slice(i * segmentNumber, segmentNumber * (i + 1)), devices[i], selectedStore);
}
})
})
}
)
}
module.exports = startBookWithNumbers
+4 -2
View File
@@ -30,6 +30,7 @@ const TOO_MANY_REQUEST_ERROR_MESSAGE_FR = "Suite à un trop grand nombre de dema
const CAPTCHA_ERROR_MESSAGE = "Error verifying captcha, please try again"
const CAPTCHA_ERROR_MESSAGE_FR = "La vérification du captcha a échoué"
REGEX_RDV_URL = "https:\/\/rendezvousparis\.hermes\.com\/client\/register\/[A-Z0-9]+"
const DEFAULT_STORE = 'faubourg';
function delay(delayInms) {
return new Promise(resolve => {
@@ -48,10 +49,11 @@ function getRandomWaitTime() {
}
class CommandorPage {
constructor(contact, device, mongoManager) {
constructor(contact, device, mongoManager, selectedStore = DEFAULT_STORE) {
this.contact = contact;
this.device = device;
this.mongoManager = mongoManager;
this.selectedStore = selectedStore;
this.isFillingFields = false;
this.isTerminated = false;
}
@@ -198,7 +200,7 @@ class CommandorPage {
await page.locator(PREFER_STORE).focus()
await delay(1000)
await page.click(PREFER_STORE);
await page.selectOption(PREFER_STORE, "faubourg");
await page.selectOption(PREFER_STORE, this.selectedStore);
}
} catch (e) {
console.log(e);