Skip to content

Commit

Permalink
Make sure there are no duplicate unavailability entries (#1388)
Browse files Browse the repository at this point in the history
  • Loading branch information
alextselegidis committed Jul 17, 2023
1 parent 90c5c02 commit e7ddad5
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions application/controllers/Google.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ public static function sync(string $provider_id = NULL)
$provider_timezone = new DateTimeZone($provider['timezone']);

// Sync each appointment with Google Calendar by following the project's sync protocol (see documentation).
foreach ($appointments as $appointment)
foreach ($appointments as $unavailability)
{
if ( ! $appointment['is_unavailability'])
if ( ! $unavailability['is_unavailability'])
{
$service = $CI->services_model->find($appointment['id_services']);
$customer = $CI->customers_model->find($appointment['id_users_customer']);
$service = $CI->services_model->find($unavailability['id_services']);
$customer = $CI->customers_model->find($unavailability['id_users_customer']);
}
else
{
Expand All @@ -122,55 +122,55 @@ public static function sync(string $provider_id = NULL)
}

// If current appointment not synced yet, add to Google Calendar.
if ($appointment['id_google_calendar'] == NULL)
if ($unavailability['id_google_calendar'] == NULL)
{
$google_event = $CI->google_sync->add_appointment($appointment, $provider, $service, $customer, $settings);
$google_event = $CI->google_sync->add_appointment($unavailability, $provider, $service, $customer, $settings);

$appointment['id_google_calendar'] = $google_event->getId();
$unavailability['id_google_calendar'] = $google_event->getId();

$CI->appointments_model->save($appointment); // Save the Google Calendar ID.
$CI->appointments_model->save($unavailability); // Save the Google Calendar ID.
}
else
{
// Appointment is synced with Google Calendar.
try
{
$google_event = $CI->google_sync->get_event($provider, $appointment['id_google_calendar']);
$google_event = $CI->google_sync->get_event($provider, $unavailability['id_google_calendar']);

if ($google_event->getStatus() == 'cancelled')
{
throw new Exception('Event is cancelled, remove the record from Easy!Appointments.');
}

// If Google Calendar event is different from Easy!Appointments appointment then update Easy!Appointments record.
$appointment_start = strtotime($appointment['start_datetime']);
$appointment_end = strtotime($appointment['end_datetime']);
$appointment_start = strtotime($unavailability['start_datetime']);
$appointment_end = strtotime($unavailability['end_datetime']);
$event_start = new DateTime($google_event->getStart()->getDateTime() ?? $google_event->getEnd()->getDate());
$event_start->setTimezone($provider_timezone);
$event_end = new DateTime($google_event->getEnd()->getDateTime() ?? $google_event->getEnd()->getDate());
$event_end->setTimezone($provider_timezone);

$google_event_notes = $appointment['is_unavailability'] ? $google_event->getSummary() . ' ' . $google_event->getDescription() : $google_event->getDescription();
$google_event_notes = $unavailability['is_unavailability'] ? $google_event->getSummary() . ' ' . $google_event->getDescription() : $google_event->getDescription();

$is_different = $appointment_start !== $event_start->getTimestamp()
|| $appointment_end !== $event_end->getTimestamp()
|| $appointment['notes'] !== $google_event_notes;
|| $unavailability['notes'] !== $google_event_notes;

if ($is_different)
{
$appointment['start_datetime'] = $event_start->format('Y-m-d H:i:s');
$appointment['end_datetime'] = $event_end->format('Y-m-d H:i:s');
$appointment['notes'] = $google_event_notes;
$CI->appointments_model->save($appointment);
$unavailability['start_datetime'] = $event_start->format('Y-m-d H:i:s');
$unavailability['end_datetime'] = $event_end->format('Y-m-d H:i:s');
$unavailability['notes'] = $google_event_notes;
$CI->appointments_model->save($unavailability);
}

}
catch (Throwable)
{
// Appointment not found on Google Calendar, delete from Easy!Appointments.
$CI->appointments_model->delete($appointment['id']);
$CI->appointments_model->delete($unavailability['id']);

$appointment['id_google_calendar'] = NULL;
$unavailability['id_google_calendar'] = NULL;
}
}
}
Expand Down Expand Up @@ -218,15 +218,15 @@ public static function sync(string $provider_id = NULL)
$event_end = new DateTime($google_event->getEnd()->getDateTime());
$event_end->setTimezone($provider_timezone);

$results = $CI->appointments_model->get(['id_google_calendar' => $google_event->getId()]);
$results = $CI->unavailabilities_model->get(['id_google_calendar' => $google_event->getId()]);

if ( ! empty($results))
{
continue;
}

// Record doesn't exist in the Easy!Appointments, so add the event now.
$appointment = [
$unavailability = [
'start_datetime' => $event_start->format('Y-m-d H:i:s'),
'end_datetime' => $event_end->format('Y-m-d H:i:s'),
'is_unavailability' => TRUE,
Expand All @@ -238,7 +238,7 @@ public static function sync(string $provider_id = NULL)
'id_services' => NULL,
];

$CI->unavailabilities_model->save($appointment);
$CI->unavailabilities_model->save($unavailability);
}

json_response([
Expand Down

0 comments on commit e7ddad5

Please sign in to comment.