From 05ab0db079d188323f2ca98ea0ac97a312eb179c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Sun, 3 May 2020 22:17:31 +0200 Subject: [PATCH] Automatically populate the appointment end datetime based on service duration in API. --- application/controllers/api/v1/Appointments.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/application/controllers/api/v1/Appointments.php b/application/controllers/api/v1/Appointments.php index c2f0fa54..c7953947 100644 --- a/application/controllers/api/v1/Appointments.php +++ b/application/controllers/api/v1/Appointments.php @@ -110,6 +110,8 @@ class Appointments extends API_V1_Controller { */ public function post() { + $this->load->model('services_model'); + try { // Insert the appointment to the database. @@ -122,6 +124,18 @@ class Appointments extends API_V1_Controller { unset($appointment['id']); } + // Generate end_datetime based on service duration if this field is not defined + if (!isset($appointment['end_datetime'])) + { + $service = $this->services_model->get_row($appointment['id_services']); + if (isset($service['duration'])) + { + $endTime = new DateTime($appointment['start_datetime']); + $endTime->add(new DateInterval('PT' . $service['duration'] . 'M')); + $appointment['end_datetime'] = $endTime->format('Y-m-d H:i:s'); + } + } + $id = $this->appointments_model->add($appointment); // Fetch the new object from the database and return it to the client.