From f89fdb23c07763872b22e1053f4ad6ca02666851 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Tue, 26 Jul 2022 16:43:37 +0300 Subject: [PATCH] Make sure that synced appointments are removed from Google Calendar if the provider changes after an appointment update action --- application/controllers/Calendar.php | 5 +++++ application/libraries/Synchronization.php | 24 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/application/controllers/Calendar.php b/application/controllers/Calendar.php index d53ee156..594c6d47 100644 --- a/application/controllers/Calendar.php +++ b/application/controllers/Calendar.php @@ -235,6 +235,11 @@ class Calendar extends EA_Controller { $appointment['id_users_customer'] = $customer['id'] ?? $customer_data['id']; } + if ($manage_mode && ! empty($appointment['id'])) + { + $this->synchronization->remove_appointment_on_provider_change($appointment['id']); + } + $this->appointments_model->only($appointment, [ 'id', 'start_datetime', diff --git a/application/libraries/Synchronization.php b/application/libraries/Synchronization.php index 3094a810..a99ac860 100644 --- a/application/libraries/Synchronization.php +++ b/application/libraries/Synchronization.php @@ -207,4 +207,28 @@ class Synchronization { log_message('error', $e->getTraceAsString()); } } + + /** + * Make sure a synced appointment is removed from Google Calendar, if its provider is changed. + * + * @throws Exception + */ + public function remove_appointment_on_provider_change($appointment_id) + { + $existing_appointment = $this->CI->appointments_model->get_row($appointment_id); + + $existing_google_id = $existing_appointment['id_google_calendar']; + + $existing_provider_id = $existing_appointment['id_users_provider']; + + if ( ! empty($existing_google_id) && (int)$existing_provider_id !== (int)$existing_appointment['id_users_provider']) + { + $existing_provider = $this->CI->providers_model->get_row($existing_provider_id); + + if ($existing_provider['settings']['google_sync']) + { + $this->sync_appointment_deleted($existing_appointment, $existing_provider); + } + } + } }