From 40ae3c6d45433262d6c70d3ad9cab9b688762479 Mon Sep 17 00:00:00 2001 From: mgite Date: Mon, 18 Nov 2024 03:41:13 +0400 Subject: [PATCH] Fix cron exely data --- exely_integration/models/exely_raw_data.py | 43 +++++++++++++--------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/exely_integration/models/exely_raw_data.py b/exely_integration/models/exely_raw_data.py index 8adc44e..3b98b53 100644 --- a/exely_integration/models/exely_raw_data.py +++ b/exely_integration/models/exely_raw_data.py @@ -144,9 +144,12 @@ class ExelyModifiedData(models.Model): hash_object = hashlib.sha256(json_data.encode()) hash_hex = hash_object.hexdigest() - found_raw_record = self.env[model].search([ - ('bid', '=', bid), - ]) + find_records = self.env[model].search([('bid', '=', bid)]) + + if len(find_records) > 1: + found_raw_record = find_records.sorted(key=lambda x: x.id, reverse=True)[0] + else: + found_raw_record = find_records if found_raw_record: if found_raw_record.json_hash_code != hash_hex: @@ -159,9 +162,10 @@ class ExelyModifiedData(models.Model): 'json_hash_code': hash_hex, 'bid': bid, }) - found_raw_record.parent_id = new_created_record.id + for record in find_records: + record.parent_id = new_created_record.id else: - found_raw_record = self.env[model].create({ + self.env[model].create({ 'json_data': json_data, 'json_hash_code': hash_hex, 'bid': bid @@ -372,8 +376,14 @@ class ExelyModifiedData(models.Model): for room in rooms: self.create_or_update_raw_data(room, 'exely.raw.rooms', room['id']) + found_record = self.env['exely.modified.bookings'].search([]) + if found_record: + latest_record = self.env['exely.modified.bookings'].search([], order='mod_period_end desc', limit=1) + max_mod_period_end = latest_record.mod_period_end + max_modified_id = latest_record.id - start_date = datetime(2024, 1, 1) + max_id_to_process = max_modified_id if found_record else 0 + start_date = max_mod_period_end if found_record else datetime(2024, 1, 1) end_date = datetime.now() date_ranges = self.generate_date_ranges(start_date, end_date, 29, "%Y-%m-%dT%H:%M") @@ -381,7 +391,7 @@ class ExelyModifiedData(models.Model): to_create = [] for status in STATES: for start, end in date_ranges: - print(f"{start} to {end}") + print(f"{start} to {end} - {status}") data = self.get_exely_data_api(f"https://connect.hopenapi.com/api/exelypms/v1/bookings?modifiedFrom={start}&modifiedTo={end}&state={status}", headers) if data: @@ -418,15 +428,16 @@ class ExelyModifiedData(models.Model): guest = self.get_exely_data_api(f"https://connect.hopenapi.com/api/exelypms/v1/guests/{bid}", headers) self.create_or_update_raw_data(guest, 'exely.raw.guests', bid) + self.process_exely_data(max_id_to_process) - def process_exely_data(self): - found_booking_record = self.env['hms.bookings'].search([], order="write_date desc", limit=1) - if found_booking_record: - max_date = found_booking_record.write_date - else: - max_date = datetime.strptime('1970-01-01', "%Y-%m-%d").date() + def process_exely_data(self, max_modified_id): + # found_booking_record = self.env['hms.bookings'].search([], order="write_date desc", limit=1) + # if found_booking_record: + # max_date = found_booking_record.write_date + # else: + # max_date = datetime.strptime('1970-01-01', "%Y-%m-%d").date() - to_process_modified_bookings = self.env['exely.raw.bookings'].search([('write_date', '>', max_date), ('parent_id', '=', False)]) + to_process_modified_bookings = self.env['exely.raw.bookings'].search([('id', '>', max_modified_id), ('parent_id', '=', False)]) to_create = [] for modified in to_process_modified_bookings: @@ -513,14 +524,12 @@ class ExelyModifiedData(models.Model): roomstay['roomId'] = room_id - - guests = roomstay.pop('guestsIds', None) if guests: guest_ids = [] for guest in guests: - guest_data = json.loads(self.env['exely.raw.guests'].search([('bid', '=', guest)]).json_data) + guest_data = json.loads(self.env['exely.raw.guests'].search([('bid', '=', guest), ('parent_id', '=', False)]).json_data) guest_data['bid'] = guest_data.pop('id') if guest_data: