diff --git a/src/appointment.js b/src/appointment.js index c9f393b..b36b9a0 100644 --- a/src/appointment.js +++ b/src/appointment.js @@ -14,7 +14,19 @@ async function filterAlreadyBookedContacts(contactList) { let alreadyBookedContacts = await mongoManager.getAllSuccessfulItemsForDay(collectionName); let contactsToBook = []; contactList.forEach((contact) => { - if (alreadyBookedContacts.find((bookedContact) => bookedContact.email === contact.mail) === undefined) { + 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) } }) @@ -25,20 +37,21 @@ async function filterBlacklistedContacts(contactList) { let blackListedContacts = await mongoManager.getAllBlackedListItems(); let contactsToBook = []; contactList.forEach((contact) => { - let blackListedItem = blackListedContacts.find((bookedContact) => bookedContact.email === contact.mail) - if (blackListedItem === undefined) { - contactsToBook.push(contact) - } else { - console.log("=====handle blacklist item before booking===="); - console.log("update_at_s is " + blackListedItem.update_at_in_s) - console.log("black list email is " + blackListedItem.mail) - let needToBook = blackListedItem.update_at_in_s + SEVEN_DAYS_IN_S <= (new Date()) / 1000; - if (!needToBook) { - console.log("contact in blacklist -->skip"); - } else { + 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; }