From 3160fefdab4a40ecb9f71c0b04dc33a1a4d1e06a Mon Sep 17 00:00:00 2001 From: PAN Lei Date: Mon, 25 Jul 2022 10:53:29 +0200 Subject: [PATCH] added check accepted or not before sending request --- src/db/mongo_manager.py | 11 ++++++++ src/pojo/accepted_appointment_pojo.py | 40 +++++++++++++++++++++++++-- src/utils/black_list_checker.py | 20 +++++++++++++- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/db/mongo_manager.py b/src/db/mongo_manager.py index 238d9a6..aa95578 100644 --- a/src/db/mongo_manager.py +++ b/src/db/mongo_manager.py @@ -41,6 +41,17 @@ class MongoDbManager: except Exception as Error: self.logger.info(Error) + def get_all_accepted_appointments(self) -> list: + collection_name = ACCEPTED_APPOINTMENT_LIST + appointment_list_contacts = [] + try: + collection_to_use = self.db[collection_name] + for document in collection_to_use.find(): + appointment_list_contacts.append(AcceptedAppointmentPojo.from_firestore_dict(document)) + except Exception as error: + self.logger.info(error) + return appointment_list_contacts + def insert_captcha_error_contact(self, contact: ContactPojo): day = str(datetime.date.today()) collection_name = CAPTCHA_ERROR_COLLECTION_PREFIX + day diff --git a/src/pojo/accepted_appointment_pojo.py b/src/pojo/accepted_appointment_pojo.py index b8f5d45..9b752b1 100644 --- a/src/pojo/accepted_appointment_pojo.py +++ b/src/pojo/accepted_appointment_pojo.py @@ -1,6 +1,6 @@ import time -from src.pojo.ReserveResultPojo import ReserveResultPojo +from src.pojo.ReserveResultPojo import ReserveResultPojo, PublishType class AcceptedAppointmentPojo(ReserveResultPojo): @@ -46,7 +46,43 @@ class AcceptedAppointmentPojo(ReserveResultPojo): @staticmethod def from_firestore_dict(source): - result = ReserveResultPojo.from_firestore_dict(source) + if 'type' in source: + publish_type = source['type'] + if publish_type: + publish_type = PublishType[publish_type] + else: + publish_type = PublishType.SUCCESS + phone = source['phone'] + url = source['url'] + id = source['id'] + email = source['email'] + lastName = source['lastName'] + firstName = source['firstName'] + result = ReserveResultPojo(type=publish_type, phone=phone, + message="", url=url, email=email, + firstName=firstName, lastName=lastName) + if 'accepted' in source: + accepted = source['accepted'] + result.accepted = accepted + if 'source' in source: + source_from = source['source'] + result.source_from = source_from + if 'sim_position' in source: + sim_position = source['sim_position'] + result.sim_position = sim_position + if 'slot_position' in source: + slot_position = source['slot_position'] + result.slot_position = slot_position + if 'passport' in source: + passport = source['passport'] + result.passport = passport + if 'ccid' in source: + ccid = source['ccid'] + result.ccid = ccid + if 'store_type' in source: + store_type = source['store_type'] + result.store_type = store_type + result.id = id accepted_pojo = AcceptedAppointmentPojo.from_reserve(result) if 'accepted_at' in source: accepted_at = source['accepted_at'] diff --git a/src/utils/black_list_checker.py b/src/utils/black_list_checker.py index 10e5ebf..7a637ae 100644 --- a/src/utils/black_list_checker.py +++ b/src/utils/black_list_checker.py @@ -4,6 +4,7 @@ from src.db.mongo_manager import MONGO_STORE_MANAGER from src.pojo.contact_pojo import ContactPojo SEVEN_DAYS_IN_S = 7 * 24 * 3600 +TWO_WEEKS_IN_S = 2 * SEVEN_DAYS_IN_S def is_already_sent(contact: ContactPojo) -> bool: @@ -24,5 +25,22 @@ def is_in_white_list(contact: ContactPojo) -> bool: return True +def is_in_accepted_list(contact: ContactPojo) -> bool: + accepted_appointment_list = MONGO_STORE_MANAGER.get_all_accepted_appointments() + for accepted_contact in accepted_appointment_list: + if contact.mail == accepted_contact.email: + # check date + return accepted_contact.accepted_at + TWO_WEEKS_IN_S > time.time() + + return False + + def can_send_request(contact: ContactPojo) -> bool: - return is_in_white_list(contact) and (not is_already_sent(contact=contact)) + return is_in_white_list(contact) and (not is_already_sent(contact=contact)) and ( + not is_in_accepted_list(contact=contact)) + + +if __name__ == '__main__': + contact = ContactPojo(phone_number='605764105', passport_number='23023672', last_name='Jia', first_name='Roujin', + mail='alanobierx@hotmail.com') + print(can_send_request(contact))