180 lines
7.9 KiB
JavaScript
180 lines
7.9 KiB
JavaScript
const {_android: android} = require('playwright');
|
|
const ExcelUtil = require("./excel/ExcelUtil");
|
|
const CommandorPage = require("./workers/CommandorPage");
|
|
const {MongoManager, formatDate} = require("./workers/mongo_manager");
|
|
const alert = require('alert');
|
|
const schedule = require("node-schedule");
|
|
|
|
const mongoManager = new MongoManager();
|
|
const SEVEN_DAYS_IN_S = 3600 * 24 * 7;
|
|
const THIRTY_DAYS_IN_S = 3600 * 24 * 30;
|
|
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;
|
|
}
|
|
|
|
async function filterAlreadyAccepteddContacts(contactList) {
|
|
let alreadyBookedContacts = await mongoManager.getAllAcceptedAppointments();
|
|
let contactsToBook = [];
|
|
contactList.forEach((contact) => {
|
|
let needToBook = true;
|
|
alreadyBookedContacts.forEach((acceptedItem) => {
|
|
if (acceptedItem.email === contact.mail) {
|
|
console.log("=====handle already accepted item before booking====");
|
|
console.log("accepted_at is " + acceptedItem.accepted_at);
|
|
console.log("accepted email is " + acceptedItem.email);
|
|
needToBook = acceptedItem.accepted_at + THIRTY_DAYS_IN_S <= (new Date()) / 1000;
|
|
if (!needToBook) {
|
|
console.log("already accepted appointment --> skip");
|
|
}
|
|
}
|
|
})
|
|
if (needToBook) {
|
|
contactsToBook.push(contact)
|
|
}
|
|
})
|
|
return contactsToBook;
|
|
}
|
|
|
|
async function filterBlacklistedContacts(contactList) {
|
|
let blackListedContacts = await mongoManager.getAllBlackedListItems();
|
|
let contactsToBook = [];
|
|
contactList.forEach((contact) => {
|
|
let needToBook = true;
|
|
blackListedContacts.forEach((blackListItem) => {
|
|
if (blackListItem.mail === contact.mail) {
|
|
console.log("=====handle blacklist item before booking=====");
|
|
console.log("update_at_s is " + blackListItem.update_at_in_s)
|
|
console.log("black list email is " + blackListItem.mail)
|
|
needToBook = blackListItem.update_at_in_s + SEVEN_DAYS_IN_S <= (new Date()) / 1000;
|
|
if (!needToBook) {
|
|
console.log("contact in blacklist -->skip");
|
|
}
|
|
}
|
|
})
|
|
if (needToBook) {
|
|
contactsToBook.push(contact)
|
|
}
|
|
})
|
|
return contactsToBook;
|
|
}
|
|
|
|
async function needToBook(contact, mongoManager) {
|
|
console.log("check contact with email " + contact.mail)
|
|
let alreadyBooked = await mongoManager.getAllSuccessfulItemsForDay(collectionName);
|
|
let blackListItems = await mongoManager.getAllBlackedListItems();
|
|
let alreadyAcceptedItems = await mongoManager.getAllAcceptedAppointments();
|
|
let needToBook = true;
|
|
await alreadyBooked.forEach((bookedItem) => {
|
|
if (bookedItem.email === contact.mail) {
|
|
needToBook = false;
|
|
}
|
|
}
|
|
)
|
|
if (needToBook) {
|
|
await blackListItems.forEach((blackListItem) => {
|
|
if (blackListItem.mail === contact.mail) {
|
|
console.log("=====handle blacklist item====");
|
|
console.log("update_at_s is " + blackListItem.update_at_in_s)
|
|
console.log("black list email is " + blackListItem.mail)
|
|
needToBook = blackListItem.update_at_in_s + SEVEN_DAYS_IN_S <= (new Date()) / 1000;
|
|
if (!needToBook) {
|
|
console.log("contact in blacklist -->skip");
|
|
}
|
|
}
|
|
})
|
|
}
|
|
if (needToBook) {
|
|
await alreadyAcceptedItems.forEach((acceptedItem) => {
|
|
if (acceptedItem.email === contact.mail) {
|
|
console.log("=====handle already accepted item====");
|
|
console.log("accepted_at is " + acceptedItem.accepted_at);
|
|
console.log("accepted email is " + acceptedItem.email);
|
|
needToBook = acceptedItem.accepted_at + THIRTY_DAYS_IN_S <= (new Date()) / 1000;
|
|
if (!needToBook) {
|
|
console.log("already accepted appointment --> skip");
|
|
}
|
|
}
|
|
})
|
|
}
|
|
return needToBook
|
|
}
|
|
|
|
async function startBook(contactPojo, device, selectedStore, audioAnalyse, alertBeep) {
|
|
console.log(`model: ${device.model()}`);
|
|
console.log(`serial: ${device.serial()}`);
|
|
if (await needToBook(contactPojo, mongoManager)) {
|
|
let commandor = new CommandorPage(contactPojo, device, mongoManager, selectedStore, audioAnalyse, alertBeep);
|
|
//read contacts form excel
|
|
return await commandor.loadPage();
|
|
} else {
|
|
console.log("do not send request --> skip")
|
|
}
|
|
}
|
|
|
|
async function startWithList(contacts, device, selectedStore, audioAnalyse, alertBeep) {
|
|
let duplicatedList = [].concat(contacts).concat(contacts).concat(contacts)
|
|
await duplicatedList.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, audioAnalyse, alertBeep);
|
|
console.log(contents);
|
|
}, Promise.resolve());
|
|
}
|
|
|
|
async function scheduleBookWithNumbers(startNumber, endNumber, selectedStore, pathToExcelFile = '/Users/lpan/Desktop/contact_all.xlsx', audioAnalyse = false, alertBeep = false) {
|
|
console.log("scheduleBookWithNumbers() called")
|
|
schedule.scheduleJob('30 10 * * *', function () {
|
|
console.log("start startBookWithNumbers")
|
|
startBookWithNumbers(startNumber, endNumber, selectedStore, pathToExcelFile, audioAnalyse = false, alertBeep = false)
|
|
})
|
|
}
|
|
|
|
async function startBookWithNumbers(startNumber, endNumber, selectedStore, pathToExcelFile = '/Users/lpan/Desktop/contact_all.xlsx', audioAnalyse = false, alertBeep = false) {
|
|
let allContactList = excelUtil.readContacts(pathToExcelFile);
|
|
let contactList;
|
|
if (endNumber <= allContactList.length) {
|
|
contactList = allContactList.slice(startNumber, endNumber);
|
|
} else {
|
|
contactList = allContactList.slice(startNumber, allContactList.length);
|
|
}
|
|
if (contactList.length === 0) {
|
|
alert("联系人数为0")
|
|
return
|
|
}
|
|
mongoManager.connect().then(r => {
|
|
filterAlreadyBookedContacts(contactList).then((listToBook) => {
|
|
filterAlreadyAccepteddContacts(listToBook).then(notAcceptedContacts => {
|
|
filterBlacklistedContacts(notAcceptedContacts).then(listWithoutBlackContact => {
|
|
console.log("number of contacts to book:" + listWithoutBlackContact.length)
|
|
android.devices().then((devices) => {
|
|
if (devices.length === 0) {
|
|
alert("未找到连接的设备");
|
|
return
|
|
}
|
|
let segmentNumber = listWithoutBlackContact.length / devices.length;
|
|
console.log("connected device number:" + devices.length)
|
|
console.log("segmentNumber:" + segmentNumber)
|
|
for (let i = 0; i < devices.length; i++) {
|
|
startWithList(listWithoutBlackContact.slice(i * segmentNumber, segmentNumber * (i + 1)), devices[i], selectedStore, audioAnalyse, alertBeep);
|
|
}
|
|
})
|
|
})
|
|
}
|
|
)
|
|
})
|
|
});
|
|
}
|
|
|
|
module.exports = {startBookWithNumbers, scheduleBookWithNumbers} |