1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-21 15:54:22 +01:00

(bug) 2 people can book the same machine slot

Also: fix reservation change behavior
This commit is contained in:
Sylvain 2022-07-20 11:22:00 +02:00
parent d05a6373be
commit 0d072291c7
8 changed files with 21 additions and 19 deletions

View File

@ -480,6 +480,7 @@ Application.Controllers.controller('ReserveMachineController', ['$scope', '$tran
*/ */
$scope.markSlotAsModifying = function () { $scope.markSlotAsModifying = function () {
$scope.selectedEvent.backgroundColor = '#eee'; $scope.selectedEvent.backgroundColor = '#eee';
$scope.selectedEvent.oldTitle = $scope.selectedEvent.title;
$scope.selectedEvent.title = _t('app.logged.machines_reserve.i_change'); $scope.selectedEvent.title = _t('app.logged.machines_reserve.i_change');
updateEvents($scope.selectedEvent); updateEvents($scope.selectedEvent);
}; };
@ -508,8 +509,8 @@ Application.Controllers.controller('ReserveMachineController', ['$scope', '$tran
const save = { const save = {
slotId: $scope.events.modifiable.slot_id, slotId: $scope.events.modifiable.slot_id,
borderColor: $scope.events.modifiable.borderColor, borderColor: $scope.events.modifiable.borderColor,
user: angular.copy($scope.events.modifiable.user), users: angular.copy($scope.events.modifiable.users),
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') 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'; $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.slot_id = save.slotId;
$scope.events.placable.is_reserved = true; $scope.events.placable.is_reserved = true;
$scope.events.placable.can_modify = 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); updateEvents($scope.events.placable);
refetchCalendar(); refetchCalendar();
@ -540,7 +541,7 @@ Application.Controllers.controller('ReserveMachineController', ['$scope', '$tran
$scope.events.placable.backgroundColor = 'white'; $scope.events.placable.backgroundColor = 'white';
$scope.events.placable.title = ''; $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'; $scope.events.modifiable.backgroundColor = 'white';
updateEvents($scope.events.placable, $scope.events.modifiable); updateEvents($scope.events.placable, $scope.events.modifiable);
@ -727,7 +728,7 @@ Application.Controllers.controller('ReserveMachineController', ['$scope', '$tran
angular.forEach(reservation.slots, function (s) { angular.forEach(reservation.slots, function (s) {
if (slot.start.isSame(s.start_at)) { if (slot.start.isSame(s.start_at)) {
slot.slot_id = s.id; slot.slot_id = s.id;
slot.user = user; slot.users = [user];
} }
}); });
updateEvents(slot); updateEvents(slot);

View File

@ -124,16 +124,16 @@ class Availability < ApplicationRecord
end end
end end
def available_places_per_slot def available_places_per_slot(reservable = nil)
case available_type case available_type
when 'training' 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' when 'event'
event.nb_total_places event.nb_total_places
when 'space' when 'space'
nb_total_places || spaces.map(&:default_places).max nb_total_places || reservable&.default_places || spaces.map(&:default_places).max
when 'machines' when 'machines'
machines.count reservable.nil? ? machines.count : 1
else else
raise TypeError raise TypeError
end end

View File

@ -12,8 +12,8 @@ class Slot < ApplicationRecord
attr_accessor :is_reserved, :machine, :space, :title, :can_modify, :current_user_slots_reservations_ids attr_accessor :is_reserved, :machine, :space, :title, :can_modify, :current_user_slots_reservations_ids
def full? def full?(reservable = nil)
availability_places = availability.available_places_per_slot availability_places = availability.available_places_per_slot(reservable)
return false if availability_places.nil? return false if availability_places.nil?
slots_reservations.where(canceled_at: nil).count >= availability_places slots_reservations.where(canceled_at: nil).count >= availability_places

View File

@ -6,7 +6,7 @@ json.title slot.title
json.start slot.start_at.iso8601 json.start slot.start_at.iso8601
json.end slot.end_at.iso8601 json.end slot.end_at.iso8601
json.is_reserved slot.is_reserved json.is_reserved slot.is_reserved
json.is_completed slot.full? json.is_completed slot.full?(reservable)
json.backgroundColor 'white' json.backgroundColor 'white'
json.availability_id slot.availability_id json.availability_id slot.availability_id

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
json.array!(@slots) do |slot| 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.borderColor machines_slot_border_color(slot)
json.machine do json.machine do

View File

@ -40,16 +40,17 @@ json.array!(@availabilities) do |availability|
end end
json.is_reserved availability.is_reserved json.is_reserved availability.is_reserved
json.is_completed availability.full? 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.machine_ids availability.availability.machines.map(&:id)
json.borderColor machines_slot_border_color(availability) json.borderColor machines_slot_border_color(availability)
elsif availability.availability.available_type == 'space' when 'space'
json.space_id availability.availability.space.first.id json.space_id availability.availability.space.first.id
json.borderColor space_slot_border_color(availability) json.borderColor space_slot_border_color(availability)
elsif availability.availability.available_type == 'training' when 'training'
json.training_id availability.availability.trainings.first.id json.training_id availability.availability.trainings.first.id
json.borderColor trainings_events_border_color(availability) json.borderColor trainings_events_border_color(availability)
elsif availability.availability.available_type == 'event' when 'event'
json.event_id availability.availability.event.id json.event_id availability.availability.event.id
json.borderColor trainings_events_border_color(availability) json.borderColor trainings_events_border_color(availability)
else else

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
json.array!(@slots) do |slot| 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.is_completed slot.full?
json.borderColor space_slot_border_color(slot) json.borderColor space_slot_border_color(slot)

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
json.array!(@slots) do |slot| 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.borderColor trainings_events_border_color(slot.availability)
json.is_completed slot.full? json.is_completed slot.full?