1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-11 22:24:21 +01:00
fab-manager/app/assets/javascripts/controllers/events.js.erb

816 lines
30 KiB
Plaintext
Raw Normal View History

/* eslint-disable
camelcase,
handle-callback-err,
no-return-assign,
no-undef,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
2015-05-05 03:10:25 +02:00
Application.Controllers.controller('EventsController', ['$scope', '$state', 'Event', 'categoriesPromise', 'themesPromise', 'ageRangesPromise',
function ($scope, $state, Event, categoriesPromise, themesPromise, ageRangesPromise) {
/* PUBLIC SCOPE */
2015-05-05 03:10:25 +02:00
// The events displayed on the page
2018-11-21 11:32:50 +01:00
$scope.events = [];
// The currently displayed page number
2018-11-21 11:32:50 +01:00
$scope.page = 1;
// List of categories for the events
2018-11-21 11:32:50 +01:00
$scope.categories = categoriesPromise;
2016-06-30 15:26:34 +02:00
// List of events themes
2018-11-21 11:32:50 +01:00
$scope.themes = themesPromise;
// List of age ranges
2018-11-21 11:32:50 +01:00
$scope.ageRanges = ageRangesPromise;
// Hide or show the 'load more' button
2018-11-21 11:32:50 +01:00
$scope.noMoreResults = false;
2015-05-05 03:10:25 +02:00
// Active filters for the events list
$scope.filters = {
category_id: null,
theme_id: null,
age_range_id: null
2018-11-21 11:32:50 +01:00
};
2015-05-05 03:10:25 +02:00
2018-11-21 11:32:50 +01:00
$scope.monthNames = [<%= t('date.month_names')[1..-1].map { |m| "\"#{m}\"" }.join(', ') %>];
2015-05-05 03:10:25 +02:00
/**
* Adds a resultset of events to the bottom of the page, grouped by month
*/
$scope.loadMoreEvents = function () {
2018-11-21 11:32:50 +01:00
$scope.page += 1;
return Event.query(Object.assign({ page: $scope.page }, $scope.filters), function (data) {
2018-11-21 11:32:50 +01:00
$scope.events = $scope.events.concat(data);
groupEvents($scope.events);
2015-05-05 03:10:25 +02:00
if (!data[0] || (data[0].nb_total_events <= $scope.events.length)) {
2018-11-21 11:32:50 +01:00
return $scope.noMoreResults = true;
}
2018-11-21 11:32:50 +01:00
});
};
2015-05-05 03:10:25 +02:00
/**
* Callback to redirect the user to the specified event page
* @param event {{id:number}}
*/
2018-11-21 11:32:50 +01:00
$scope.showEvent = function (event) { $state.go('app.public.events_show', { id: event.id }); };
2015-05-05 03:10:25 +02:00
/**
* Callback to refresh the events list according to the filters set
*/
$scope.filterEvents = function () {
// reinitialize results datasets
2018-11-21 11:32:50 +01:00
$scope.page = 1;
$scope.eventsGroupByMonth = {};
$scope.events = [];
$scope.monthOrder = [];
$scope.noMoreResults = false;
// run a search query
return Event.query(Object.assign({ page: $scope.page }, $scope.filters), function (data) {
2018-11-21 11:32:50 +01:00
$scope.events = data;
groupEvents(data);
if (!data[0] || (data[0].nb_total_events <= $scope.events.length)) {
2018-11-21 11:32:50 +01:00
return $scope.noMoreResults = true;
}
2018-11-21 11:32:50 +01:00
});
};
2015-05-05 03:10:25 +02:00
/**
* Test if the provided event occurs on a single day or on many days
* @param event {{start_date:Date, end_date:Date}} Event object as retreived from the API
* @return {boolean} false if the event occurs on many days
*/
2018-11-21 11:32:50 +01:00
$scope.onSingleDay = function (event) { moment(event.start_date).isSame(event.end_date, 'day'); };
2018-11-21 11:32:50 +01:00
/* PRIVATE SCOPE */
/**
* Kind of constructor: these actions will be realized first when the controller is loaded
*/
2018-11-21 11:32:50 +01:00
const initialize = function () {
$scope.filterEvents();
};
/**
* Group the provided events by month/year and concat them with existing results
* Then compute the ordered list of months for the complete resultset.
* Affect the resulting events groups in $scope.eventsGroupByMonth and the ordered month keys in $scope.monthOrder.
* @param events {Array} Events retrieved from the API
*/
const groupEvents = function (events) {
if (events.length > 0) {
2018-11-21 11:32:50 +01:00
const eventsGroupedByMonth = _.groupBy(events, function (obj) {
return _.map(['month_id', 'year'], function (key) {
return obj[key];
});
2018-11-21 11:32:50 +01:00
});
$scope.eventsGroupByMonth = Object.assign($scope.eventsGroupByMonth, eventsGroupedByMonth);
return $scope.monthOrder = Object.keys($scope.eventsGroupByMonth);
}
2018-11-21 11:32:50 +01:00
};
2015-05-05 03:10:25 +02:00
// # !!! MUST BE CALLED AT THE END of the controller
2018-11-21 11:32:50 +01:00
initialize();
}
2018-11-21 11:32:50 +01:00
]);
2015-05-05 03:10:25 +02:00
2019-11-20 10:27:56 +01:00
Application.Controllers.controller('ShowEventController', ['$scope', '$state', '$stateParams', '$rootScope', 'Event', '$uibModal', 'Member', 'Reservation', 'Price', 'CustomAsset', 'Slot', 'eventPromise', 'growl', '_t', 'Wallet', 'helpers', 'dialogs', 'priceCategoriesPromise', 'settingsPromise',
function ($scope, $state, $stateParams, $rootScope, Event, $uibModal, Member, Reservation, Price, CustomAsset, Slot, eventPromise, growl, _t, Wallet, helpers, dialogs, priceCategoriesPromise, settingsPromise) {
/* PUBLIC SCOPE */
2015-05-05 03:10:25 +02:00
// reservations for the currently shown event
2018-11-21 11:32:50 +01:00
$scope.reservations = [];
2015-05-05 03:10:25 +02:00
// user to deal with
$scope.ctrl =
2018-11-21 11:32:50 +01:00
{ member: {} };
2015-05-05 03:10:25 +02:00
// parameters for a new reservation
$scope.reserve = {
nbPlaces: {
normal: []
},
nbReservePlaces: 0,
tickets: {},
toReserve: false,
amountTotal: 0,
totalNoCoupon: 0,
totalSeats: 0
2018-11-21 11:32:50 +01:00
};
2015-05-05 03:10:25 +02:00
// Discount coupon to apply to the basket, if any
$scope.coupon =
2018-11-21 11:32:50 +01:00
{ applied: null };
2015-05-05 03:10:25 +02:00
// Get the details for the current event (event's id is recovered from the current URL)
2018-11-21 11:32:50 +01:00
$scope.event = eventPromise;
2015-05-05 03:10:25 +02:00
// List of price categories for the events
2018-11-21 11:32:50 +01:00
$scope.priceCategories = priceCategoriesPromise;
2015-05-05 03:10:25 +02:00
// Global config: is the user authorized to change his bookings slots?
2018-11-21 11:32:50 +01:00
$scope.enableBookingMove = (settingsPromise.booking_move_enable === 'true');
2015-05-05 03:10:25 +02:00
// Global config: delay in hours before a booking while changing the booking slot is forbidden
2018-11-21 11:32:50 +01:00
$scope.moveBookingDelay = parseInt(settingsPromise.booking_move_delay);
2015-05-05 03:10:25 +02:00
2019-11-20 10:27:56 +01:00
// Global config: is the user authorized to cancel his booking slots?
$scope.enableBookingCancel = settingsPromise.booking_cancel_enable === 'true';
// Global config: delay in hours from now when restrictions occurs about cancelling reservations
$scope.cancelBookingDelay = parseInt(settingsPromise.booking_cancel_delay);
// Message displayed to the end user about rules that applies to events reservations
2018-11-21 11:32:50 +01:00
$scope.eventExplicationsAlert = settingsPromise.event_explications_alert;
2015-05-05 03:10:25 +02:00
2018-11-19 16:17:49 +01:00
/**
* Callback to delete the provided event (admins only)
* @param event {$resource} angular's Event $resource
*/
2018-11-19 16:52:48 +01:00
$scope.deleteEvent = function (event) {
dialogs.confirm({
resolve: {
object () {
return {
title: _t('confirmation_required'),
msg: _t('do_you_really_want_to_delete_this_event')
2018-11-21 11:32:50 +01:00
};
}
}
2018-11-21 11:32:50 +01:00
}, function () {
// the admin has confirmed, delete
2018-11-21 11:32:50 +01:00
event.$delete(function () {
$state.go('app.public.events_list');
return growl.info(_t('event_successfully_deleted'));
}, function (error) {
console.error(error);
growl.error(_t('unable_to_delete_the_event_because_some_users_alredy_booked_it'));
2018-11-21 11:32:50 +01:00
});
}
);
};
2018-11-19 16:17:49 +01:00
/**
* Callback to call when the number of tickets to book changes in the current booking
*/
$scope.changeNbPlaces = function () {
// compute the total remaning places
2018-11-21 11:32:50 +01:00
let remain = $scope.event.nb_free_places - $scope.reserve.nbReservePlaces;
for (let ticket in $scope.reserve.tickets) {
2018-11-21 11:32:50 +01:00
remain -= $scope.reserve.tickets[ticket];
}
// we store the total number of seats booked, this is used to know if the 'pay' button must be shown
2018-11-21 11:32:50 +01:00
$scope.reserve.totalSeats = $scope.event.nb_free_places - remain;
// update the available seats for full price tickets
2018-11-21 11:32:50 +01:00
const fullPriceRemains = $scope.reserve.nbReservePlaces + remain;
$scope.reserve.nbPlaces.normal = __range__(0, fullPriceRemains, true);
// update the available seats for other prices tickets
for (let key in $scope.reserve.nbPlaces) {
if (key !== 'normal') {
2018-11-21 11:32:50 +01:00
const priceRemain = $scope.reserve.tickets[key] + remain;
$scope.reserve.nbPlaces[key] = __range__(0, priceRemain, true);
}
}
// recompute the total price
2018-11-21 11:32:50 +01:00
return $scope.computeEventAmount();
};
2018-11-19 16:17:49 +01:00
/**
* Callback to reset the current reservation parameters
* @param e {Object} see https://docs.angularjs.org/guide/expression#-event-
*/
$scope.cancelReserve = function (e) {
2018-11-21 11:32:50 +01:00
e.preventDefault();
return resetEventReserve();
};
2018-11-19 16:17:49 +01:00
/**
* Callback to allow the user to set the details for his reservation
*/
$scope.reserveEvent = function () {
if ($scope.event.nb_total_places > 0) {
2018-11-21 11:32:50 +01:00
$scope.reserveSuccess = false;
if (!$scope.isAuthenticated()) {
return $scope.login(null, function (user) {
2018-11-21 11:32:50 +01:00
$scope.reserve.toReserve = !$scope.reserve.toReserve;
if (user.role !== 'admin') {
2018-11-21 11:32:50 +01:00
return $scope.ctrl.member = user;
}
2018-11-21 11:32:50 +01:00
});
} else {
2018-11-21 11:32:50 +01:00
return $scope.reserve.toReserve = !$scope.reserve.toReserve;
}
}
2018-11-21 11:32:50 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Callback to deal with the reservations of the user selected in the dropdown list instead of the current user's
* reservations. (admins only)
*/
$scope.updateMember = function () {
2018-11-21 11:32:50 +01:00
resetEventReserve();
$scope.reserveSuccess = false;
if ($scope.ctrl.member) {
return Member.get({ id: $scope.ctrl.member.id }, function (member) {
2018-11-21 11:32:50 +01:00
$scope.ctrl.member = member;
return getReservations($scope.event.id, 'Event', $scope.ctrl.member.id);
});
}
2018-11-21 11:32:50 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Callback to trigger the payment process of the current reservation
*/
$scope.payEvent = function () {
// first, we check that a user was selected
if (Object.keys($scope.ctrl.member).length > 0) {
2018-11-21 11:32:50 +01:00
const reservation = mkReservation($scope.ctrl.member, $scope.reserve, $scope.event);
return Wallet.getWalletByUser({ user_id: $scope.ctrl.member.id }, function (wallet) {
2018-11-21 11:32:50 +01:00
const amountToPay = helpers.getAmountToPay($scope.reserve.amountTotal, wallet.amount);
if (($scope.currentUser.role !== 'admin') && (amountToPay > 0)) {
if ($rootScope.fablabWithoutOnlinePayment) {
growl.error(_t('online_payment_disabled'));
} else {
return payByStripe(reservation);
}
} else {
if (($scope.currentUser.role === 'admin') || (amountToPay === 0)) {
2018-11-21 11:32:50 +01:00
return payOnSite(reservation);
}
}
2018-11-21 11:32:50 +01:00
});
} else {
// otherwise we alert, this error musn't occur when the current user is not admin
2018-11-21 11:32:50 +01:00
return growl.error(_t('please_select_a_member_first'));
}
2018-11-21 11:32:50 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Callback to validate the booking of a free event
*/
$scope.validReserveEvent = function () {
const reservation = {
user_id: $scope.ctrl.member.id,
reservable_id: $scope.event.id,
reservable_type: 'Event',
slots_attributes: [],
nb_reserve_places: $scope.reserve.nbReservePlaces,
tickets_attributes: []
2018-11-21 11:32:50 +01:00
};
// a single slot is used for events
reservation.slots_attributes.push({
start_at: $scope.event.start_date,
end_at: $scope.event.end_date,
availability_id: $scope.event.availability.id
2018-11-21 11:32:50 +01:00
});
// iterate over reservations per prices
for (let price_id in $scope.reserve.tickets) {
2018-11-21 11:32:50 +01:00
const seats = $scope.reserve.tickets[price_id];
reservation.tickets_attributes.push({
event_price_category_id: price_id,
booked: seats
2018-11-21 11:32:50 +01:00
});
}
// set the attempting marker
2018-11-21 11:32:50 +01:00
$scope.attempting = true;
// save the reservation to the API
return Reservation.save({ reservation }, function (reservation) {
// reservation successfull
2018-11-21 11:32:50 +01:00
afterPayment(reservation);
return $scope.attempting = false;
}
, function (response) {
// reservation failed
2018-11-21 11:32:50 +01:00
$scope.alerts = [];
$scope.alerts.push({
msg: response.data.card[0],
type: 'danger'
2018-11-21 11:32:50 +01:00
});
// unset the attempting marker
2018-11-21 11:32:50 +01:00
return $scope.attempting = false;
});
};
2019-11-20 10:27:56 +01:00
/**
* Callback to cancel a reservation
* @param reservation {{id:number, reservable_id:number, nb_reserve_places:number}}
*/
$scope.cancelReservation = function(reservation) {
dialogs.confirm({
2019-11-20 10:27:56 +01:00
resolve: {
object: function() {
return {
title: _t('cancel_the_reservation'),
msg: _t('do_you_really_want_to_cancel_this_reservation_this_apply_to_all_booked_tickets')
};
}
}
}, function() { // cancel confirmed
Slot.cancel({
2019-11-20 10:27:56 +01:00
id: reservation.slots[0].id
}, function() { // successfully canceled
let index;
2019-11-20 10:27:56 +01:00
growl.success(_t('reservation_was_successfully_cancelled'));
index = $scope.reservations.indexOf(reservation);
$scope.event.nb_free_places = $scope.event.nb_free_places + reservation.total_booked_seats;
$scope.reservations[index].slots[0].canceled_at = new Date();
2019-11-20 10:27:56 +01:00
}, function(error) {
growl.warning(_t('cancellation_failed'));
2019-11-20 10:27:56 +01:00
});
});
};
/**
* Test if the provided reservation has been cancelled
* @param reservation {Reservation}
* @returns {boolean}
*/
$scope.isCancelled = function(reservation) {
return !!(reservation.slots[0].canceled_at);
}
2018-11-19 16:17:49 +01:00
/**
* Callback to alter an already booked reservation date. A modal window will be opened to allow the user to choose
* a new date for his reservation (if any available)
* @param reservation {{id:number, reservable_id:number, nb_reserve_places:number}}
*/
2019-11-20 10:27:56 +01:00
$scope.modifyReservation = function (reservation) {
2018-11-21 11:32:50 +01:00
const index = $scope.reservations.indexOf(reservation);
return $uibModal.open({
templateUrl: '<%= asset_path "events/modify_event_reservation_modal.html" %>',
resolve: {
2018-11-21 11:32:50 +01:00
event () { return $scope.event; },
reservation () { return reservation; }
},
controller: ['$scope', '$uibModalInstance', 'event', 'reservation', 'Reservation', function ($scope, $uibModalInstance, event, reservation, Reservation) {
// we copy the controller's resolved parameters into the scope
2018-11-21 11:32:50 +01:00
$scope.event = event;
$scope.reservation = angular.copy(reservation);
// set the reservable_id to the first available event
2019-11-20 10:27:56 +01:00
for (evt of Array.from(event.recurrence_events)) {
if (evt.nb_free_places > reservation.total_booked_seats) {
$scope.reservation.reservable_id = evt.id;
2018-11-21 11:32:50 +01:00
break;
}
}
// Callback to validate the new reservation's date
$scope.ok = function () {
2018-11-21 11:32:50 +01:00
let eventToPlace = null;
angular.forEach(event.recurrence_events, function (e) {
if (e.id === parseInt($scope.reservation.reservable_id, 10)) {
2018-11-21 11:32:50 +01:00
return eventToPlace = e;
}
2018-11-21 11:32:50 +01:00
});
$scope.reservation.slots[0].start_at = eventToPlace.start_date;
$scope.reservation.slots[0].end_at = eventToPlace.end_date;
$scope.reservation.slots[0].availability_id = eventToPlace.availability_id;
$scope.reservation.slots_attributes = $scope.reservation.slots;
$scope.attempting = true;
Reservation.update({ id: reservation.id }, { reservation: $scope.reservation }, function (reservation) {
2018-11-21 11:32:50 +01:00
$uibModalInstance.close(reservation);
$scope.attempting = true;
}
, function (response) {
2018-11-21 11:32:50 +01:00
$scope.alerts = [];
angular.forEach(response, function (v, k) {
angular.forEach(v, function (err) {
$scope.alerts.push({ msg: k + ': ' + err, type: 'danger' });
});
});
$scope.attempting = false;
2018-11-21 11:32:50 +01:00
});
};
// Callback to cancel the modification
2018-11-21 11:32:50 +01:00
$scope.cancel = function () { $uibModalInstance.dismiss('cancel'); };
}
] })
.result['finally'](null).then(function (reservation) {
// remove the reservation from the user's reservations list for this event (occurrence)
2018-11-21 11:32:50 +01:00
$scope.reservations.splice(index, 1);
// add the number of places transfered (to the new date) to the total of free places for this event
2018-11-21 11:32:50 +01:00
$scope.event.nb_free_places = $scope.event.nb_free_places + reservation.total_booked_seats;
// remove the number of places transfered from the total of free places of the receiving occurrance
angular.forEach($scope.event.recurrence_events, function (e) {
if (e.id === parseInt(reservation.reservable.id, 10)) {
2018-11-21 11:32:50 +01:00
return e.nb_free_places = e.nb_free_places - reservation.total_booked_seats;
}
2018-11-21 11:32:50 +01:00
});
});
};
2018-11-19 16:17:49 +01:00
/**
* Checks if the provided reservation is able to be moved (date change)
2019-11-20 10:27:56 +01:00
* @param reservation {{slots:[], total_booked_seats:number}}
2018-11-19 16:17:49 +01:00
*/
$scope.reservationCanModify = function (reservation) {
2018-11-21 11:32:50 +01:00
const slotStart = moment(reservation.slots[0].start_at);
const now = moment();
2018-11-21 11:32:50 +01:00
let isAble = false;
angular.forEach($scope.event.recurrence_events, function (e) {
2018-11-21 11:32:50 +01:00
if (e.nb_free_places >= reservation.total_booked_seats) { return isAble = true; }
});
return (isAble && $scope.enableBookingMove && (slotStart.diff(now, 'hours') >= $scope.moveBookingDelay));
};
2019-11-20 10:27:56 +01:00
/**
* Checks if the provided reservation is able to be cancelled
* @param reservation {{slots:[]}}
*/
$scope.reservationCanCancel = function(reservation) {
var now, slotStart;
slotStart = moment(reservation.slots[0].start_at);
now = moment();
return $scope.enableBookingCancel && slotStart.diff(now, "hours") >= $scope.cancelBookingDelay;
};
2018-11-19 16:17:49 +01:00
/**
* Compute the total amount for the current reservation according to the previously set parameters
* and assign the result in $scope.reserve.amountTotal
*/
$scope.computeEventAmount = function () {
// first we check that a user was selected
if (Object.keys($scope.ctrl.member).length > 0) {
2018-11-21 11:32:50 +01:00
const r = mkReservation($scope.ctrl.member, $scope.reserve, $scope.event);
return Price.compute(mkRequestParams(r, $scope.coupon.applied), function (res) {
2018-11-21 11:32:50 +01:00
$scope.reserve.amountTotal = res.price;
return $scope.reserve.totalNoCoupon = res.price_without_coupon;
});
} else {
2018-11-21 11:32:50 +01:00
return $scope.reserve.amountTotal = null;
}
2018-11-21 11:32:50 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Return the URL allowing to share the current project on the Facebook social network
*/
2018-11-21 11:32:50 +01:00
$scope.shareOnFacebook = function () { return `https://www.facebook.com/share.php?u=${$state.href('app.public.events_show', { id: $scope.event.id }, { absolute: true }).replace('#', '%23')}`; };
2018-11-19 16:17:49 +01:00
/**
* Return the URL allowing to share the current project on the Twitter social network
*/
2018-11-21 11:32:50 +01:00
$scope.shareOnTwitter = function () { return `https://twitter.com/intent/tweet?url=${encodeURIComponent($state.href('app.public.events_show', { id: $scope.event.id }, { absolute: true }))}&text=${encodeURIComponent($scope.event.title)}`; };
2018-11-19 16:17:49 +01:00
/**
* Return the textual description of the conditions applyable to the given price's category
* @param category_id {number} ID of the price's category
*/
$scope.getPriceCategoryConditions = function (category_id) {
for (let cat of Array.from($scope.priceCategories)) {
if (cat.id === category_id) {
2018-11-21 11:32:50 +01:00
return cat.conditions;
}
}
2018-11-21 11:32:50 +01:00
};
2018-11-21 11:32:50 +01:00
/* PRIVATE SCOPE */
2018-11-19 16:17:49 +01:00
/**
* Kind of constructor: these actions will be realized first when the controller is loaded
*/
const initialize = function () {
// set the controlled user as the current user if the current user is not an admin
if ($scope.currentUser) {
if ($scope.currentUser.role !== 'admin') {
2018-11-21 11:32:50 +01:00
$scope.ctrl.member = $scope.currentUser;
}
}
// initialize the "reserve" object with the event's data
2018-11-21 11:32:50 +01:00
resetEventReserve();
// if non-admin, get the current user's reservations into $scope.reservations
if ($scope.currentUser) {
2018-11-21 11:32:50 +01:00
getReservations($scope.event.id, 'Event', $scope.currentUser.id);
}
// watch when a coupon is applied to re-compute the total price
return $scope.$watch('coupon.applied', function (newValue, oldValue) {
if ((newValue !== null) || (oldValue !== null)) {
2018-11-21 11:32:50 +01:00
return $scope.computeEventAmount();
}
2018-11-21 11:32:50 +01:00
});
};
2018-11-19 16:17:49 +01:00
/**
* Retrieve the reservations for the couple event / user
* @param reservable_id {number} the current event id
* @param reservable_type {string} 'Event'
* @param user_id {number} the user's id (current or managed)
*/
2018-11-19 16:52:48 +01:00
var getReservations = function (reservable_id, reservable_type, user_id) {
Reservation.query({
reservable_id,
reservable_type,
user_id
2018-11-21 11:32:50 +01:00
}).$promise.then(function (reservations) { $scope.reservations = reservations; });
2018-11-19 16:52:48 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Create an hash map implementing the Reservation specs
* @param member {Object} User as retreived from the API: current user / selected user if current is admin
* @param reserve {Object} Reservation parameters (places...)
* @param event {Object} Current event
* @return {{user_id:number, reservable_id:number, reservable_type:string, slots_attributes:Array<Object>, nb_reserve_places:number}}
*/
var mkReservation = function (member, reserve, event) {
const reservation = {
user_id: member.id,
reservable_id: event.id,
reservable_type: 'Event',
slots_attributes: [],
nb_reserve_places: reserve.nbReservePlaces,
tickets_attributes: []
2018-11-21 11:32:50 +01:00
};
reservation.slots_attributes.push({
start_at: event.start_date,
end_at: event.end_date,
availability_id: event.availability.id,
offered: event.offered || false
2018-11-21 11:32:50 +01:00
});
for (let evt_px_cat of Array.from(event.prices)) {
2018-11-21 11:32:50 +01:00
const booked = reserve.tickets[evt_px_cat.id];
if (booked > 0) {
reservation.tickets_attributes.push({
event_price_category_id: evt_px_cat.id,
booked
2018-11-21 11:32:50 +01:00
});
}
}
2018-11-21 11:32:50 +01:00
return reservation;
};
2018-11-19 16:17:49 +01:00
/**
* Format the parameters expected by /api/prices/compute or /api/reservations and return the resulting object
* @param reservation {Object} as returned by mkReservation()
* @param coupon {Object} Coupon as returned from the API
* @return {{reservation:Object, coupon_code:string}}
*/
var mkRequestParams = function (reservation, coupon) {
const params = {
reservation,
coupon_code: ((coupon ? coupon.code : undefined))
2018-11-21 11:32:50 +01:00
};
2018-11-21 11:32:50 +01:00
return params;
};
2018-11-19 16:17:49 +01:00
/**
2019-11-20 10:27:56 +01:00
* Set the current reservation to the default values. This implies the reservation form to be hidden.
2018-11-19 16:17:49 +01:00
*/
var resetEventReserve = function () {
if ($scope.event) {
$scope.reserve = {
nbPlaces: {
normal: __range__(0, $scope.event.nb_free_places, true)
},
nbReservePlaces: 0,
tickets: {},
toReserve: false,
amountTotal: 0,
totalSeats: 0
2018-11-21 11:32:50 +01:00
};
for (let evt_px_cat of Array.from($scope.event.prices)) {
2018-11-21 11:32:50 +01:00
$scope.reserve.nbPlaces[evt_px_cat.id] = __range__(0, $scope.event.nb_free_places, true);
$scope.reserve.tickets[evt_px_cat.id] = 0;
}
2018-11-21 11:32:50 +01:00
return $scope.event.offered = false;
}
2018-11-21 11:32:50 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Open a modal window which trigger the stripe payment process
* @param reservation {Object} to book
*/
2018-11-19 16:52:48 +01:00
var payByStripe = function (reservation) {
$uibModal.open({
templateUrl: '<%= asset_path "stripe/payment_modal.html" %>',
size: 'md',
resolve: {
reservation () {
2018-11-21 11:32:50 +01:00
return reservation;
},
price () {
2018-11-21 11:32:50 +01:00
return Price.compute(mkRequestParams(reservation, $scope.coupon.applied)).$promise;
},
wallet () {
2018-11-21 11:32:50 +01:00
return Wallet.getWalletByUser({ user_id: reservation.user_id }).$promise;
},
cgv () {
2018-11-21 11:32:50 +01:00
return CustomAsset.get({ name: 'cgv-file' }).$promise;
},
objectToPay () {
return {
eventToReserve: $scope.event,
reserve: $scope.reserve,
member: $scope.ctrl.member
2018-11-21 11:32:50 +01:00
};
},
coupon () {
2018-11-21 11:32:50 +01:00
return $scope.coupon.applied;
},
cartItems () {
return mkRequestParams(reservation, $scope.coupon.applied);
}
},
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'cgv', 'Auth', 'Reservation', 'growl', 'wallet', 'helpers', '$filter', 'coupon', 'cartItems',
function ($scope, $uibModalInstance, $state, reservation, price, cgv, Auth, Reservation, growl, wallet, helpers, $filter, coupon, cartItems) {
// User's wallet amount
2018-11-21 11:32:50 +01:00
$scope.walletAmount = wallet.amount;
// Price
2018-11-21 11:32:50 +01:00
$scope.amount = helpers.getAmountToPay(price.price, wallet.amount);
// Cart items
$scope.cartItems = cartItems;
// CGV
2018-11-21 11:32:50 +01:00
$scope.cgv = cgv.custom_asset;
// Reservation
2018-11-21 11:32:50 +01:00
$scope.reservation = reservation;
// Used in wallet info template to interpolate some translations
2018-11-21 11:32:50 +01:00
$scope.numberFilter = $filter('number');
// Callback to handle the post-payment and reservation
return $scope.onPaymentSuccess = function (reservation) {
$uibModalInstance.close(reservation);
2018-11-21 11:32:50 +01:00
};
}
]
2018-11-21 11:32:50 +01:00
}).result['finally'](null).then(function (reservation) { afterPayment(reservation); });
};
2018-11-19 16:17:49 +01:00
/**
* Open a modal window which trigger the local payment process
* @param reservation {Object} to book
*/
2018-11-19 16:52:48 +01:00
var payOnSite = function (reservation) {
$uibModal.open({
templateUrl: '<%= asset_path "shared/valid_reservation_modal.html" %>',
size: 'sm',
resolve: {
reservation () {
2018-11-21 11:32:50 +01:00
return reservation;
},
price () {
2018-11-21 11:32:50 +01:00
return Price.compute(mkRequestParams(reservation, $scope.coupon.applied)).$promise;
},
wallet () {
2018-11-21 11:32:50 +01:00
return Wallet.getWalletByUser({ user_id: reservation.user_id }).$promise;
},
coupon () {
2018-11-21 11:32:50 +01:00
return $scope.coupon.applied;
}
},
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'Auth', 'Reservation', 'wallet', 'helpers', '$filter', 'coupon',
function ($scope, $uibModalInstance, $state, reservation, price, Auth, Reservation, wallet, helpers, $filter, coupon) {
// User's wallet amount
2018-11-21 11:32:50 +01:00
$scope.walletAmount = wallet.amount;
// Price
2018-11-21 11:32:50 +01:00
$scope.price = price.price;
// price to pay
2018-11-21 11:32:50 +01:00
$scope.amount = helpers.getAmountToPay(price.price, wallet.amount);
// Reservation
2018-11-21 11:32:50 +01:00
$scope.reservation = reservation;
// Used in wallet info template to interpolate some translations
2018-11-21 11:32:50 +01:00
$scope.numberFilter = $filter('number');
// Button label
if ($scope.amount > 0) {
2018-11-21 11:32:50 +01:00
$scope.validButtonName = _t('confirm_payment_of_html', { ROLE: $scope.currentUser.role, AMOUNT: $filter('currency')($scope.amount) }, 'messageformat');
} else {
if ((price.price > 0) && ($scope.walletAmount === 0)) {
2018-11-21 11:32:50 +01:00
$scope.validButtonName = _t('confirm_payment_of_html', { ROLE: $scope.currentUser.role, AMOUNT: $filter('currency')(price.price) }, 'messageformat');
} else {
2018-11-21 11:32:50 +01:00
$scope.validButtonName = _t('confirm');
}
}
// Callback to validate the payment
$scope.ok = function () {
2018-11-21 11:32:50 +01:00
$scope.attempting = true;
return Reservation.save(mkRequestParams($scope.reservation, coupon), function (reservation) {
2018-11-21 11:32:50 +01:00
$uibModalInstance.close(reservation);
return $scope.attempting = true;
}
, function (response) {
2018-11-21 11:32:50 +01:00
$scope.alerts = [];
angular.forEach(response, function (v, k) {
angular.forEach(v, function (err) {
$scope.alerts.push({
msg: k + ': ' + err,
type: 'danger'
2018-11-21 11:32:50 +01:00
});
});
});
return $scope.attempting = false;
});
};
// Callback to cancel the payment
2018-11-21 11:32:50 +01:00
return $scope.cancel = function () { $uibModalInstance.dismiss('cancel'); };
}
] })
2018-11-21 11:32:50 +01:00
.result['finally'](null).then(function (reservation) { afterPayment(reservation); });
};
2018-11-19 16:17:49 +01:00
/**
* What to do after the payment was successful
* @param reservation {Object} booked reservation
2018-11-19 16:17:49 +01:00
*/
var afterPayment = function (reservation) {
2018-11-21 11:32:50 +01:00
$scope.event.nb_free_places = $scope.event.nb_free_places - reservation.total_booked_seats;
resetEventReserve();
$scope.reserveSuccess = true;
$scope.coupon.applied = null;
$scope.reservations.push(reservation);
if ($scope.currentUser.role === 'admin') {
2018-11-21 11:32:50 +01:00
return $scope.ctrl.member = null;
}
2018-11-21 11:32:50 +01:00
};
2018-11-19 16:17:49 +01:00
// !!! MUST BE CALLED AT THE END of the controller
2018-11-21 11:32:50 +01:00
return initialize();
}
2018-11-21 11:32:50 +01:00
]);
function __range__ (left, right, inclusive) {
2018-11-21 11:32:50 +01:00
let range = [];
let ascending = left < right;
let end = !inclusive ? right : ascending ? right + 1 : right - 1;
for (let i = left; ascending ? i < end : i > end; ascending ? i++ : i--) {
2018-11-21 11:32:50 +01:00
range.push(i);
}
2018-11-21 11:32:50 +01:00
return range;
}