From e4e285931f5fea24fb775b1b530251cef85da97a Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Fri, 25 Mar 2022 12:44:05 +0100 Subject: [PATCH] Move the consent creation process to when the appointment gets created and not earlier --- application/controllers/Booking.php | 36 ++++++++++++++++++--- application/controllers/Consents.php | 24 +++++++++++++- assets/js/http/booking_http_client.js | 17 ---------- assets/js/pages/booking.js | 46 --------------------------- 4 files changed, 54 insertions(+), 69 deletions(-) diff --git a/application/controllers/Booking.php b/application/controllers/Booking.php index a37bac1d..7cbe4a25 100755 --- a/application/controllers/Booking.php +++ b/application/controllers/Booking.php @@ -37,6 +37,7 @@ class Booking extends EA_Controller { $this->load->model('services_model'); $this->load->model('customers_model'); $this->load->model('settings_model'); + $this->load->model('consents_model'); $this->load->library('timezones'); $this->load->library('synchronization'); @@ -66,7 +67,7 @@ class Booking extends EA_Controller { $disable_booking = setting('disable_booking'); $google_analytics_code = setting('google_analytics_code'); $matomo_analytics_url = setting('matomo_analytics_url'); - + if ($disable_booking) { $disable_booking_message = setting('disable_booking_message'); @@ -134,7 +135,7 @@ class Booking extends EA_Controller { $timezones = $this->timezones->to_array(); $grouped_timezones = $this->timezones->to_grouped_array(); - + $appointment_hash = html_vars('appointment_hash'); if ( ! empty($appointment_hash)) @@ -271,13 +272,13 @@ class Booking extends EA_Controller { * Render the booking page and display the selected appointment. * * This method will call the "index" callback to handle the page rendering. - * + * * @param string $appointment_hash */ public function reschedule($appointment_hash) { html_vars(['appointment_hash' => $appointment_hash]); - + $this->index(); } @@ -474,6 +475,30 @@ class Booking extends EA_Controller { $appointment['color'] = $service['color']; } + $customer_ip = $this->input->ip_address(); + + // Create the consents (if needed). + $consent = [ + 'first_name' => $customer['first_name'] ?? '-', + 'last_name' => $customer['last_name'] ?? '-', + 'email' => $customer['email'] ?? '-', + 'ip' => $customer_ip, + ]; + + if (setting('display_terms_and_conditions')) + { + $consent['type'] = 'terms-and-conditions'; + + $this->consents_model->save($consent); + } + + if (setting('display_privacy_policy')) + { + $consent['type'] = 'privacy-policy'; + + $this->consents_model->save($consent); + } + // Save customer language (the language which is used to render the booking page). $customer['language'] = session('language') ?? config('language'); @@ -495,6 +520,7 @@ class Booking extends EA_Controller { $customer = $this->customers_model->find($customer_id); $appointment['id_users_customer'] = $customer_id; + $appointment['is_unavailability'] = FALSE; $this->appointments_model->only($appointment, [ 'start_datetime', @@ -506,7 +532,7 @@ class Booking extends EA_Controller { 'id_users_customer', 'id_services', ]); - + $appointment_id = $this->appointments_model->save($appointment); $appointment = $this->appointments_model->find($appointment_id); diff --git a/application/controllers/Consents.php b/application/controllers/Consents.php index 54d2de76..f7b42947 100644 --- a/application/controllers/Consents.php +++ b/application/controllers/Consents.php @@ -32,7 +32,7 @@ class Consents extends EA_Controller { /** * Save (insert or update) the consent */ - public function save_consent() + public function save() { try { @@ -40,6 +40,28 @@ class Consents extends EA_Controller { $consent['ip'] = $this->input->ip_address(); + $occurrences = $this->consents_model->get(['ip' => $consent['ip']], 1, 0, 'create_datetime DESC'); + + if ( ! empty($occurrences)) + { + $last_consent = $occurrences[0]; + + $last_consent_create_datetime_instance = new DateTime($last_consent['create_datetime']); + + $threshold_datetime_instance = new DateTime('-24 hours'); + + if ($last_consent_create_datetime_instance > $threshold_datetime_instance) + { + // Do not create a new consent. + + json_response([ + 'success' => TRUE, + ]); + + return; + } + } + $consent['id'] = $this->consents_model->save($consent); json_response([ diff --git a/assets/js/http/booking_http_client.js b/assets/js/http/booking_http_client.js index bd86913c..f944e1d7 100755 --- a/assets/js/http/booking_http_client.js +++ b/assets/js/http/booking_http_client.js @@ -302,22 +302,6 @@ App.Http.Booking = (function () { processingUnavailabilities = false; } - /** - * Save the user's consent. - * - * @param {Object} consent Contains user's consents. - */ - function saveConsent(consent) { - const url = App.Utils.Url.siteUrl('consents/save_consent'); - - const data = { - csrf_token: vars('csrf_token'), - consent: consent - }; - - $.post(url, data); - } - /** * Delete personal information. * @@ -341,7 +325,6 @@ App.Http.Booking = (function () { getAvailableHours, getUnavailabilityDates, applyPreviousUnavailabilityDates, - saveConsent, deletePersonalInformation }; })(); diff --git a/assets/js/pages/booking.js b/assets/js/pages/booking.js index 654db3d8..27adceb2 100644 --- a/assets/js/pages/booking.js +++ b/assets/js/pages/booking.js @@ -35,20 +35,6 @@ App.Pages.Booking = (function () { const $bookAppointmentSubmit = $('#book-appointment-submit'); const $deletePersonalInformation = $('#delete-personal-information'); - /** - * Contains terms and conditions consent. - * - * @type {Object} - */ - let termsAndConditionsConsent; - - /** - * Contains privacy policy consent. - * - * @type {Object} - */ - let privacyPolicyConsent; - /** * Determines the functionality of the page. * @@ -365,38 +351,6 @@ App.Pages.Booking = (function () { return; // Validation failed, do not continue. } else { updateConfirmFrame(); - - const $acceptToTermsAndConditions = $('#accept-to-terms-and-conditions'); - if ($acceptToTermsAndConditions.length && $acceptToTermsAndConditions.prop('checked') === true) { - const newTermsAndConditionsConsent = { - first_name: $firstName.val(), - last_name: $lastName.val(), - email: $email.val(), - type: 'terms-and-conditions' - }; - - if ( - JSON.stringify(newTermsAndConditionsConsent) !== JSON.stringify(termsAndConditionsConsent) - ) { - termsAndConditionsConsent = newTermsAndConditionsConsent; - App.Http.Booking.saveConsent(termsAndConditionsConsent); - } - } - - const $acceptToPrivacyPolicy = $('#accept-to-privacy-policy'); - if ($acceptToPrivacyPolicy.length && $acceptToPrivacyPolicy.prop('checked') === true) { - const newPrivacyPolicyConsent = { - first_name: $firstName.val(), - last_name: $lastName.val(), - email: $email.val(), - type: 'privacy-policy' - }; - - if (JSON.stringify(newPrivacyPolicyConsent) !== JSON.stringify(privacyPolicyConsent)) { - privacyPolicyConsent = newPrivacyPolicyConsent; - App.Http.Booking.saveConsent(privacyPolicyConsent); - } - } } }