From 07bc7b3812d3f2ad7990173de75ce335856dcb13 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Fri, 24 May 2024 19:59:53 +0200 Subject: [PATCH] Make sure that editing a working plan exception and changing the date will not keep the previous entry in place (#1515) --- application/controllers/Calendar.php | 6 ++++++ assets/js/http/calendar_http_client.js | 11 ++++++++++- assets/js/utils/calendar_default_view.js | 9 +++++++-- assets/js/utils/calendar_table_view.js | 9 +++++++-- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/application/controllers/Calendar.php b/application/controllers/Calendar.php index 246be6cecc..54003c4295 100644 --- a/application/controllers/Calendar.php +++ b/application/controllers/Calendar.php @@ -468,6 +468,8 @@ public function save_working_plan_exception(): void $date = request('date'); + $original_date = request('original_date'); + $working_plan_exception = request('working_plan_exception'); if (!$working_plan_exception) { @@ -478,6 +480,10 @@ public function save_working_plan_exception(): void $this->providers_model->save_working_plan_exception($provider_id, $date, $working_plan_exception); + if ($date !== $original_date) { + $this->providers_model->delete_working_plan_exception($provider_id, $original_date); + } + json_response([ 'success' => true, ]); diff --git a/assets/js/http/calendar_http_client.js b/assets/js/http/calendar_http_client.js index b0db1c51e1..fe9dd61db3 100755 --- a/assets/js/http/calendar_http_client.js +++ b/assets/js/http/calendar_http_client.js @@ -131,10 +131,18 @@ App.Http.Calendar = (function () { * @param {Number} providerId Contains the working plan exceptions data. * @param {Function} successCallback The ajax success callback function. * @param {Function} errorCallback The ajax failure callback function. + * @param {Date} [originalDate] On edit, provide the original date. * * @return {*|jQuery} */ - function saveWorkingPlanException(date, workingPlanException, providerId, successCallback, errorCallback) { + function saveWorkingPlanException( + date, + workingPlanException, + providerId, + successCallback, + errorCallback, + originalDate, + ) { const url = App.Utils.Url.siteUrl('calendar/save_working_plan_exception'); const data = { @@ -142,6 +150,7 @@ App.Http.Calendar = (function () { date: date, working_plan_exception: workingPlanException, provider_id: providerId, + original_date: originalDate, }; return $.post(url, data) diff --git a/assets/js/utils/calendar_default_view.js b/assets/js/utils/calendar_default_view.js index 36857e9a68..4220e2dbaa 100755 --- a/assets/js/utils/calendar_default_view.js +++ b/assets/js/utils/calendar_default_view.js @@ -90,11 +90,11 @@ App.Utils.CalendarDefaultView = (function () { const data = lastFocusedEventData.extendedProps.data; if (data.hasOwnProperty('workingPlanException')) { - const date = lastFocusedEventData.extendedProps.data.date; + const originalDate = lastFocusedEventData.extendedProps.data.date; const workingPlanException = lastFocusedEventData.extendedProps.data.workingPlanException; const provider = lastFocusedEventData.extendedProps.data.provider; - App.Components.WorkingPlanExceptionsModal.edit(date, workingPlanException).done( + App.Components.WorkingPlanExceptionsModal.edit(originalDate, workingPlanException).done( (date, workingPlanException) => { const successCallback = () => { App.Layouts.Backend.displayNotification(lang('working_plan_exception_saved')); @@ -103,6 +103,10 @@ App.Utils.CalendarDefaultView = (function () { workingPlanExceptions[date] = workingPlanException; + if (date !== originalDate) { + delete workingPlanExceptions[originalDate]; + } + for (const index in vars('available_providers')) { const availableProvider = vars('available_providers')[index]; @@ -122,6 +126,7 @@ App.Utils.CalendarDefaultView = (function () { provider.id, successCallback, null, + originalDate, ); }, ); diff --git a/assets/js/utils/calendar_table_view.js b/assets/js/utils/calendar_table_view.js index 485e8c532e..a05ee12093 100755 --- a/assets/js/utils/calendar_table_view.js +++ b/assets/js/utils/calendar_table_view.js @@ -165,11 +165,11 @@ App.Utils.CalendarTableView = (function () { let endMoment; if (lastFocusedEventData.extendedProps.data.workingPlanException) { - const date = lastFocusedEventData.extendedProps.data.date; + const originalDate = lastFocusedEventData.extendedProps.data.date; const workingPlanException = lastFocusedEventData.extendedProps.data.workingPlanException; const provider = lastFocusedEventData.extendedProps.data.provider; - App.Components.WorkingPlanExceptionsModal.edit(date, workingPlanException).done( + App.Components.WorkingPlanExceptionsModal.edit(originalDate, workingPlanException).done( (date, workingPlanException) => { const successCallback = () => { App.Layouts.Backend.displayNotification(lang('working_plan_exception_saved')); @@ -178,6 +178,10 @@ App.Utils.CalendarTableView = (function () { workingPlanExceptions[date] = workingPlanException; + if (date !== originalDate) { + delete workingPlanExceptions[originalDate]; + } + for (const index in vars('available_providers')) { const availableProvider = vars('available_providers')[index]; @@ -197,6 +201,7 @@ App.Utils.CalendarTableView = (function () { provider.id, successCallback, null, + originalDate, ); }, );