From dea9663f375b6da61d374a89f6e4f91b87a22099 Mon Sep 17 00:00:00 2001 From: Guilherme Chaguri Date: Tue, 29 Nov 2022 17:02:01 -0300 Subject: [PATCH] Added reservation deadline parameter --- app/frontend/src/javascript/models/setting.ts | 1 + app/frontend/src/javascript/router.js | 2 +- .../templates/admin/settings/reservations.html | 12 ++++++++++++ app/models/cart_item/reservation.rb | 9 +++++++++ app/models/setting.rb | 1 + .../availabilities/availabilities_service.rb | 3 ++- config/locales/app.admin.en.yml | 3 +++ db/seeds.rb | 2 ++ 8 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app/frontend/src/javascript/models/setting.ts b/app/frontend/src/javascript/models/setting.ts index 1f81a30e0..95e6d54b7 100644 --- a/app/frontend/src/javascript/models/setting.ts +++ b/app/frontend/src/javascript/models/setting.ts @@ -78,6 +78,7 @@ export const bookingSettings = [ 'reminder_delay', 'visibility_yearly', 'visibility_others', + 'reservation_deadline', 'display_name_enable', 'book_overlapping_slots', 'slot_duration', diff --git a/app/frontend/src/javascript/router.js b/app/frontend/src/javascript/router.js index 2e8593899..973a47c54 100644 --- a/app/frontend/src/javascript/router.js +++ b/app/frontend/src/javascript/router.js @@ -1178,7 +1178,7 @@ angular.module('application.router', ['ui.router']) "'renew_pack_threshold', 'pack_only_for_subscription', 'overlapping_categories', 'public_registrations'," + "'extended_prices_in_same_day', 'recaptcha_site_key', 'recaptcha_secret_key', 'user_validation_required', " + "'user_validation_required_list', 'machines_module', 'user_change_group', 'show_username_in_admin_list', " + - "'store_module']" + "'store_module', 'reservation_deadline']" }).$promise; }], privacyDraftsPromise: ['Setting', function (Setting) { return Setting.get({ name: 'privacy_draft', history: true }).$promise; }], diff --git a/app/frontend/templates/admin/settings/reservations.html b/app/frontend/templates/admin/settings/reservations.html index 2d99e7ae8..106d1b463 100644 --- a/app/frontend/templates/admin/settings/reservations.html +++ b/app/frontend/templates/admin/settings/reservations.html @@ -105,6 +105,18 @@
+
+

{{ 'app.admin.settings.reservation_deadline' }}

+ + +
+

{{ 'app.admin.settings.default_slot_duration' }}

{{ 'app.admin.settings.default_slot_duration_info' }}

diff --git a/app/models/cart_item/reservation.rb b/app/models/cart_item/reservation.rb index 2c86364ee..d8489e555 100644 --- a/app/models/cart_item/reservation.rb +++ b/app/models/cart_item/reservation.rb @@ -42,6 +42,10 @@ class CartItem::Reservation < CartItem::BaseItem def valid?(all_items) pending_subscription = all_items.find { |i| i.is_a?(CartItem::Subscription) } + + reservation_deadline_minutes = Setting.get('reservation_deadline').to_i + reservation_deadline = reservation_deadline_minutes.minutes.since + @slots.each do |slot| slot_db = Slot.find(slot[:slot_id]) if slot_db.nil? @@ -60,6 +64,11 @@ class CartItem::Reservation < CartItem::BaseItem return false end + if slot_db.start_at < reservation_deadline + @errors[:slot] = 'cannot reserve a slot ' + reservation_deadline_minutes.to_s + ' minutes prior to its start' + return false + end + next if availability.plan_ids.empty? next if required_subscription?(availability, pending_subscription) diff --git a/app/models/setting.rb b/app/models/setting.rb index 29e6ab7c4..e61e7e90c 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -53,6 +53,7 @@ class Setting < ApplicationRecord space_explications_alert visibility_yearly visibility_others + reservation_deadline display_name_enable machines_sort_by accounting_journal_code diff --git a/app/services/availabilities/availabilities_service.rb b/app/services/availabilities/availabilities_service.rb index 656e76c0e..837a51fd0 100644 --- a/app/services/availabilities/availabilities_service.rb +++ b/app/services/availabilities/availabilities_service.rb @@ -9,6 +9,7 @@ class Availabilities::AvailabilitiesService year: Setting.get('visibility_yearly').to_i.months.since, other: Setting.get('visibility_others').to_i.months.since } + @minimum_visibility = Setting.get('reservation_deadline').to_i.minutes.since @service = Availabilities::StatusService.new(current_user&.role) @level = level end @@ -93,7 +94,7 @@ class Availabilities::AvailabilitiesService end_at = @maximum_visibility[:year] if subscription_year?(user) && type != 'training' end_at = @maximum_visibility[:year] if show_more_trainings?(user) && type == 'training' window_end = [end_at, range_end].min - window_start = [range_start, DateTime.current].max + window_start = [range_start, @minimum_visibility].max availabilities.includes(:tags, :plans, :slots) .joins(:slots) .where('availabilities.start_at <= ? AND availabilities.end_at >= ? AND available_type = ?', window_end, window_start, type) diff --git a/config/locales/app.admin.en.yml b/config/locales/app.admin.en.yml index 7419041eb..f671325dd 100644 --- a/config/locales/app.admin.en.yml +++ b/config/locales/app.admin.en.yml @@ -1350,6 +1350,8 @@ en: max_visibility: "Maximum visibility (in months)" visibility_for_yearly_members: "For currently running subscriptions, at least 1 year long" visibility_for_other_members: "For all other members" + reservation_deadline: "Prevent last minute booking" + deadline_minutes: "Prior period (minutes)" ability_for_the_users_to_move_their_reservations: "Ability for the users to move their reservations" reservations_shifting: "Reservations shifting" prior_period_hours: "Prior period (hours)" @@ -1405,6 +1407,7 @@ en: default_value_is_24_hours: "If the field is leaved empty: 24 hours." visibility_yearly: "maximum visibility for annual subscribers" visibility_others: "maximum visibility for other members" + reservation_deadline: "reservation deadline" display: "Display" display_name_info_html: "When enabled, connected members browsing the calendar or booking a resource will see the name of the members who has already booked some slots. When disabled, only administrators and managers will view the names.
Warning: if you enable this feature, please write it down in your privacy policy." display_reservation_user_name: "Display the full name of the user(s) who booked a slots" diff --git a/db/seeds.rb b/db/seeds.rb index f34daaf50..0fa2660cd 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -729,6 +729,8 @@ Setting.set('visibility_yearly', 3) unless Setting.find_by(name: 'visibility_yea Setting.set('visibility_others', 1) unless Setting.find_by(name: 'visibility_others').try(:value) +Setting.set('reservation_deadline', 0) unless Setting.find_by(name: 'reservation_deadline').try(:value) + Setting.set('display_name_enable', false) unless Setting.find_by(name: 'display_name_enable').try(:value) Setting.set('machines_sort_by', 'default') unless Setting.find_by(name: 'machines_sort_by').try(:value)