1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-30 19:52:20 +01:00

(feat) user cannot reserve 2 times for event family

This commit is contained in:
Du Peng 2023-07-11 19:13:38 +02:00
parent 9dc85ee80b
commit 67f5e68fd7
3 changed files with 25 additions and 13 deletions

View File

@ -644,7 +644,7 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
if (!user.booked) {
return false;
}
if ($scope.enableChildValidationRequired && user.booked.type === 'Child' && !user.booked.is_valid) {
if ($scope.enableChildValidationRequired && user.booked.type === 'Child' && !user.booked.validated_at) {
return false;
}
}
@ -675,9 +675,10 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
// get the current user's reservations into $scope.reservations
if ($scope.currentUser) {
getReservations($scope.event.id, 'Event', $scope.currentUser.id);
getChildren($scope.currentUser.id).then(function (children) {
updateNbReservePlaces();
getReservations($scope.event.id, 'Event', $scope.currentUser.id).then(function (reservations) {
getChildren($scope.currentUser.id).then(function (children) {
updateNbReservePlaces();
});
});
}
@ -696,7 +697,7 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
* @param user_id {number} the user's id (current or managed)
*/
const getReservations = function (reservable_id, reservable_type, user_id) {
Reservation.query({
return Reservation.query({
reservable_id,
reservable_type,
user_id
@ -727,6 +728,14 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
}
}
}
for (const r of $scope.reservations) {
for (const user of r.booking_users_attributes) {
const key = user.booked_type === 'User' ? `user_${user.booked_id}` : `child_${user.booked_id}`;
if (key === userKey) {
return true;
}
}
}
return false;
};
@ -748,7 +757,7 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
name: child.first_name + ' ' + child.last_name,
id: child.id,
type: 'Child',
is_valid: child.is_valid,
validated_at: child.validated_at,
birthday: child.birthday
});
}
@ -761,7 +770,10 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
*/
const updateNbReservePlaces = function () {
if ($scope.event.event_type === 'family') {
const maxPlaces = $scope.children.length + 1;
const reservedPlaces = $scope.reservations.reduce((sum, reservation) => {
return sum + reservation.booking_users_attributes.length;
}, 0);
const maxPlaces = $scope.children.length + 1 - reservedPlaces;
if ($scope.event.nb_free_places > maxPlaces) {
$scope.reserve.nbPlaces.normal = __range__(0, maxPlaces, true);
for (let evt_px_cat of Array.from($scope.event.event_price_categories_attributes)) {
@ -976,11 +988,11 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
Reservation.get({ id: invoice.main_object.id }, function (reservation) {
$scope.event.nb_free_places = $scope.event.nb_free_places - reservation.total_booked_seats;
$scope.reservations.push(reservation);
resetEventReserve();
updateNbReservePlaces();
$scope.reserveSuccess = true;
$scope.coupon.applied = null;
});
resetEventReserve();
updateNbReservePlaces();
$scope.reserveSuccess = true;
$scope.coupon.applied = null;
if ($scope.currentUser.role === 'admin') {
return $scope.ctrl.member = null;
}

View File

@ -142,7 +142,7 @@
class="form-control">
<option value=""></option>
</select>
<uib-alert type="danger" ng-if="enableChildValidationRequired && user.booked && user.booked.type === 'Child' && !user.booked.is_valid" style="margin-bottom: 0.8rem;">
<uib-alert type="danger" ng-if="enableChildValidationRequired && user.booked && user.booked.type === 'Child' && !user.booked.validated_at" style="margin-bottom: 0.8rem;">
<span class="text-sm">
<i class="fa fa-warning"></i>
<span translate>{{ 'app.shared.cart.child_validation_required_alert' }}</span>

View File

@ -11,7 +11,7 @@ class Child < ApplicationRecord
validates :first_name, presence: true
validates :last_name, presence: true
# validates :email, presence: true, format: { with: Devise.email_regexp }
validate :validate_age
# validate :validate_age
# birthday should less than 18 years ago
def validate_age