diff --git a/app/frontend/src/javascript/controllers/machines.js.erb b/app/frontend/src/javascript/controllers/machines.js.erb index deb7a41ed..c7d026334 100644 --- a/app/frontend/src/javascript/controllers/machines.js.erb +++ b/app/frontend/src/javascript/controllers/machines.js.erb @@ -480,6 +480,7 @@ Application.Controllers.controller('ReserveMachineController', ['$scope', '$tran */ $scope.markSlotAsModifying = function () { $scope.selectedEvent.backgroundColor = '#eee'; + $scope.selectedEvent.oldTitle = $scope.selectedEvent.title; $scope.selectedEvent.title = _t('app.logged.machines_reserve.i_change'); updateEvents($scope.selectedEvent); }; @@ -508,8 +509,8 @@ Application.Controllers.controller('ReserveMachineController', ['$scope', '$tran const save = { slotId: $scope.events.modifiable.slot_id, borderColor: $scope.events.modifiable.borderColor, - user: angular.copy($scope.events.modifiable.user), - title: (!$scope.events.modifiable.user || $scope.currentUser.id === $scope.events.modifiable.user.id) ? _t('app.logged.machines_reserve.i_ve_reserved') : _t('app.logged.machines_reserve.not_available') + users: angular.copy($scope.events.modifiable.users), + title: (!$scope.events.modifiable.users || $scope.events.modifiable.users.map(u => u.id).includes($scope.currentUser.id)) ? _t('app.logged.machines_reserve.i_ve_reserved') : _t('app.logged.machines_reserve.not_available') }; $scope.events.modifiable.backgroundColor = 'white'; @@ -526,7 +527,7 @@ Application.Controllers.controller('ReserveMachineController', ['$scope', '$tran $scope.events.placable.slot_id = save.slotId; $scope.events.placable.is_reserved = true; $scope.events.placable.can_modify = true; - $scope.events.placable.user = angular.copy(save.user); + $scope.events.placable.users = angular.copy(save.users); updateEvents($scope.events.placable); refetchCalendar(); @@ -540,7 +541,7 @@ Application.Controllers.controller('ReserveMachineController', ['$scope', '$tran $scope.events.placable.backgroundColor = 'white'; $scope.events.placable.title = ''; } - $scope.events.modifiable.title = $scope.currentUser.id === $scope.events.modifiable.user.id ? _t('app.logged.machines_reserve.i_ve_reserved') : _t('app.logged.machines_reserve.not_available'); + $scope.events.modifiable.title = $scope.events.modifiable.oldTitle; $scope.events.modifiable.backgroundColor = 'white'; updateEvents($scope.events.placable, $scope.events.modifiable); @@ -727,7 +728,7 @@ Application.Controllers.controller('ReserveMachineController', ['$scope', '$tran angular.forEach(reservation.slots, function (s) { if (slot.start.isSame(s.start_at)) { slot.slot_id = s.id; - slot.user = user; + slot.users = [user]; } }); updateEvents(slot); diff --git a/app/models/availability.rb b/app/models/availability.rb index a794c9a50..e737c6d03 100644 --- a/app/models/availability.rb +++ b/app/models/availability.rb @@ -124,16 +124,16 @@ class Availability < ApplicationRecord end end - def available_places_per_slot + def available_places_per_slot(reservable = nil) case available_type when 'training' - nb_total_places || trainings.map(&:nb_total_places).max + nb_total_places || reservable&.nb_total_places || trainings.map(&:nb_total_places).max when 'event' event.nb_total_places when 'space' - nb_total_places || spaces.map(&:default_places).max + nb_total_places || reservable&.default_places || spaces.map(&:default_places).max when 'machines' - machines.count + reservable.nil? ? machines.count : 1 else raise TypeError end diff --git a/app/models/slot.rb b/app/models/slot.rb index 38eb8d2b6..32334d2ba 100644 --- a/app/models/slot.rb +++ b/app/models/slot.rb @@ -12,8 +12,8 @@ class Slot < ApplicationRecord attr_accessor :is_reserved, :machine, :space, :title, :can_modify, :current_user_slots_reservations_ids - def full? - availability_places = availability.available_places_per_slot + def full?(reservable = nil) + availability_places = availability.available_places_per_slot(reservable) return false if availability_places.nil? slots_reservations.where(canceled_at: nil).count >= availability_places diff --git a/app/views/api/availabilities/_slot.json.jbuilder b/app/views/api/availabilities/_slot.json.jbuilder index 0f7730ed2..45635e41f 100644 --- a/app/views/api/availabilities/_slot.json.jbuilder +++ b/app/views/api/availabilities/_slot.json.jbuilder @@ -6,7 +6,7 @@ json.title slot.title json.start slot.start_at.iso8601 json.end slot.end_at.iso8601 json.is_reserved slot.is_reserved -json.is_completed slot.full? +json.is_completed slot.full?(reservable) json.backgroundColor 'white' json.availability_id slot.availability_id diff --git a/app/views/api/availabilities/machine.json.jbuilder b/app/views/api/availabilities/machine.json.jbuilder index 95646ba52..6296ee385 100644 --- a/app/views/api/availabilities/machine.json.jbuilder +++ b/app/views/api/availabilities/machine.json.jbuilder @@ -1,7 +1,7 @@ # frozen_string_literal: true json.array!(@slots) do |slot| - json.partial! 'api/availabilities/slot', slot: slot, operator_role: @operator_role + json.partial! 'api/availabilities/slot', slot: slot, operator_role: @operator_role, reservable: @machine json.borderColor machines_slot_border_color(slot) json.machine do diff --git a/app/views/api/availabilities/public.json.jbuilder b/app/views/api/availabilities/public.json.jbuilder index 23c65b64f..c5ac42899 100644 --- a/app/views/api/availabilities/public.json.jbuilder +++ b/app/views/api/availabilities/public.json.jbuilder @@ -40,16 +40,17 @@ json.array!(@availabilities) do |availability| end json.is_reserved availability.is_reserved json.is_completed availability.full? - if availability.availability.available_type == 'machines' + case availability.availability.available_type + when 'machines' json.machine_ids availability.availability.machines.map(&:id) json.borderColor machines_slot_border_color(availability) - elsif availability.availability.available_type == 'space' + when 'space' json.space_id availability.availability.space.first.id json.borderColor space_slot_border_color(availability) - elsif availability.availability.available_type == 'training' + when 'training' json.training_id availability.availability.trainings.first.id json.borderColor trainings_events_border_color(availability) - elsif availability.availability.available_type == 'event' + when 'event' json.event_id availability.availability.event.id json.borderColor trainings_events_border_color(availability) else diff --git a/app/views/api/availabilities/spaces.json.jbuilder b/app/views/api/availabilities/spaces.json.jbuilder index 45cb23d0a..2200fb689 100644 --- a/app/views/api/availabilities/spaces.json.jbuilder +++ b/app/views/api/availabilities/spaces.json.jbuilder @@ -1,7 +1,7 @@ # frozen_string_literal: true json.array!(@slots) do |slot| - json.partial! 'api/availabilities/slot', slot: slot, operator_role: @operator_role + json.partial! 'api/availabilities/slot', slot: slot, operator_role: @operator_role, reservable: @space json.is_completed slot.full? json.borderColor space_slot_border_color(slot) diff --git a/app/views/api/availabilities/trainings.json.jbuilder b/app/views/api/availabilities/trainings.json.jbuilder index 5fc9b27b9..b4efdce10 100644 --- a/app/views/api/availabilities/trainings.json.jbuilder +++ b/app/views/api/availabilities/trainings.json.jbuilder @@ -1,7 +1,7 @@ # frozen_string_literal: true json.array!(@slots) do |slot| - json.partial! 'api/availabilities/slot', slot: slot, operator_role: @operator_role + json.partial! 'api/availabilities/slot', slot: slot, operator_role: @operator_role, reservable: slot.availability.trainings.first json.borderColor trainings_events_border_color(slot.availability) json.is_completed slot.full?