1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-29 18:52:22 +01:00

migrate js clients to new architecture

This commit is contained in:
Sylvain 2021-04-26 11:41:02 +02:00
parent b9e8c8867c
commit 6b8f7da98c
3 changed files with 60 additions and 75 deletions

View File

@ -299,7 +299,7 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
$scope.payEvent = function () {
// first, we check that a user was selected
if (Object.keys($scope.ctrl.member).length > 0) {
const reservation = mkReservation($scope.ctrl.member, $scope.reserve, $scope.event);
const reservation = mkReservation($scope.reserve, $scope.event);
return Wallet.getWalletByUser({ user_id: $scope.ctrl.member.id }, function (wallet) {
const amountToPay = helpers.getAmountToPay($scope.reserve.amountTotal, wallet.amount);
@ -328,32 +328,36 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
* 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: []
};
const cartItems = {
customer_id: $scope.ctrl.member.id,
reservation: {
reservable_id: $scope.event.id,
reservable_type: 'Event',
slots_attributes: [],
nb_reserve_places: $scope.reserve.nbReservePlaces,
tickets_attributes: []
}
}
// a single slot is used for events
reservation.slots_attributes.push({
cartItems.reservation.slots_attributes.push({
start_at: $scope.event.start_date,
end_at: $scope.event.end_date,
availability_id: $scope.event.availability.id
});
// iterate over reservations per prices
for (let price_id in $scope.reserve.tickets) {
const seats = $scope.reserve.tickets[price_id];
reservation.tickets_attributes.push({
event_price_category_id: price_id,
booked: seats
});
if (Object.prototype.hasOwnProperty.call($scope.reserve.tickets, price_id)) {
const seats = $scope.reserve.tickets[price_id];
cartItems.reservation.tickets_attributes.push({
event_price_category_id: price_id,
booked: seats
});
}
}
// set the attempting marker
$scope.attempting = true;
// save the reservation to the API
return Reservation.save({ reservation }, function (reservation) {
return Reservation.save(cartItems, function (reservation) {
// reservation successfull
afterPayment(reservation);
return $scope.attempting = false;
@ -513,8 +517,8 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
$scope.computeEventAmount = function () {
// first we check that a user was selected
if (Object.keys($scope.ctrl.member).length > 0) {
const r = mkReservation($scope.ctrl.member, $scope.reserve, $scope.event);
return Price.compute(mkRequestParams(r, $scope.coupon.applied), function (res) {
const r = mkReservation($scope.reserve, $scope.event);
return Price.compute(mkCartItems(r, $scope.coupon.applied), function (res) {
$scope.reserve.amountTotal = res.price;
return $scope.reserve.totalNoCoupon = res.price_without_coupon;
});
@ -589,15 +593,13 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
};
/**
* 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
* Create a hash map implementing the Reservation specs
* @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}}
* @return {{reservable_id:number, reservable_type:string, slots_attributes:Array<Object>, nb_reserve_places:number}}
*/
const mkReservation = function (member, reserve, event) {
const mkReservation = function (reserve, event) {
const reservation = {
user_id: member.id,
reservable_id: event.id,
reservable_type: 'Event',
slots_attributes: [],
@ -629,13 +631,15 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
* 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
* @param paymentMethod {string} 'card' | ''
* @return {{reservation:Object, coupon_code:string}}
*/
const mkRequestParams = function (reservation, coupon) {
const mkCartItems = function (reservation, coupon, paymentMethod = '') {
return {
customer_id: reservation.user_id,
reservation,
coupon_code: ((coupon ? coupon.code : undefined))
coupon_code: ((coupon ? coupon.code : undefined)),
payment_method: paymentMethod,
};
};
@ -677,7 +681,7 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
return reservation;
},
price () {
return Price.compute(mkRequestParams(reservation, $scope.coupon.applied)).$promise;
return Price.compute(mkCartItems(reservation, $scope.coupon.applied, 'card')).$promise;
},
wallet () {
return Wallet.getWalletByUser({ user_id: reservation.user_id }).$promise;
@ -696,7 +700,7 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
return $scope.coupon.applied;
},
cartItems () {
return mkRequestParams(reservation, $scope.coupon.applied);
return mkCartItems(reservation, $scope.coupon.applied, 'card');
},
stripeKey: ['Setting', function (Setting) { return Setting.get({ name: 'stripe_public_key' }).$promise; }]
},
@ -748,7 +752,7 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
return reservation;
},
price () {
return Price.compute(mkRequestParams(reservation, $scope.coupon.applied)).$promise;
return Price.compute(mkCartItems(reservation, $scope.coupon.applied)).$promise;
},
wallet () {
return Wallet.getWalletByUser({ user_id: reservation.user_id }).$promise;
@ -757,7 +761,7 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
return $scope.coupon.applied;
},
cartItems () {
return mkRequestParams(reservation, $scope.coupon.applied);
return mkCartItems(reservation, $scope.coupon.applied);
},
},
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'Auth', 'Reservation', 'wallet', 'helpers', '$filter', 'coupon', 'cartItems',
@ -794,7 +798,7 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
// Callback to validate the payment
$scope.ok = function () {
$scope.attempting = true;
return Reservation.save(mkRequestParams($scope.reservation, coupon), function (reservation) {
return Reservation.save(mkCartItems($scope.reservation, coupon), function (reservation) {
$uibModalInstance.close(reservation);
return $scope.attempting = true;
}

View File

@ -612,8 +612,8 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
*/
const updateCartPrice = function () {
if (Object.keys($scope.user).length > 0) {
const r = mkReservation($scope.user, $scope.events.reserved, $scope.selectedPlan);
return Price.compute(mkRequestParams({ reservation: r }, $scope.coupon.applied), function (res) {
const r = mkReservation($scope.events.reserved);
return Price.compute(mkCartItems([r], ''), function (res) {
$scope.amountTotal = res.price;
$scope.schedule.payment_schedule = res.schedule;
$scope.totalNoCoupon = res.price_without_coupon;
@ -637,33 +637,16 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
});
};
/**
* Format the parameters expected by /api/prices/compute or /api/reservations and return the resulting object
* @param request {{reservation: *}|{subscription: *}} as returned by mkReservation()
* @param coupon {{code: string}} Coupon as returned from the API
* @return {CartItems}
*/
const mkRequestParams = function (request, coupon) {
return Object.assign({
coupon_code: ((coupon ? coupon.code : undefined))
}, request);
};
/**
* Create a hash map implementing the Reservation specs
* @param member {Object} User as retrieved from the API: current user / selected user if current is admin
* @param slots {Array<Object>} Array of fullCalendar events: slots selected on the calendar
* @param [plan] {Object} Plan as retrieved from the API: plan to buy with the current reservation
* @return {{reservable_type: string, payment_schedule: boolean, user_id: *, reservable_id: string, slots_attributes: [], plan_id: (*|undefined)}}
* @return {{reservation: {reservable_type: string, reservable_id: string, slots_attributes: []}}}
*/
const mkReservation = function (member, slots, plan) {
const mkReservation = function (slots) {
const reservation = {
user_id: member.id,
reservable_id: $scope.reservableId,
reservable_type: $scope.reservableType,
slots_attributes: [],
plan_id: ((plan ? plan.id : undefined)),
payment_schedule: $scope.schedule.requested_schedule
slots_attributes: []
};
angular.forEach(slots, function (slot) {
reservation.slots_attributes.push({
@ -674,7 +657,7 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
});
});
return reservation;
return { reservation };
};
/**
@ -692,22 +675,21 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
/**
* Build the CartItems object, from the current reservation
* @param reservation {*}
* @param items {Array<{reservation:{reservable_type: string, reservable_id: string, slots_attributes: []}}|{subscription: {plan_id: number}}>}
* @param paymentMethod {string}
* @return {CartItems}
*/
const mkCartItems = function (reservation, paymentMethod) {
const request = {
customer_id: reservation.user_id,
const mkCartItems = function (items, paymentMethod) {
const cartItems = {
customer_id: $scope.user.id,
payment_schedule: $scope.schedule.requested_schedule,
payment_method: paymentMethod
payment_method: paymentMethod,
coupon_code: (($scope.coupon.applied ? $scope.coupon.applied.code : undefined))
};
if (reservation.slots_attributes.length === 0 && reservation.plan_id) {
Object.assign(request, mkSubscription($scope.selectedPlan.id));
} else {
Object.assign(request, { reservation });
for (const item of items) {
Object.assign(cartItems, item);
}
return mkRequestParams(request, $scope.coupon.applied);
return cartItems;
};
/**
@ -719,7 +701,7 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
growl.error(_t('app.shared.cart.online_payment_disabled'));
} else {
$scope.toggleOnlinePaymentModal(() => {
$scope.onlinePayment.cartItems = mkCartItems(reservation, 'card');
$scope.onlinePayment.cartItems = mkCartItems([reservation], 'card');
});
}
};
@ -735,11 +717,10 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
return reservation;
},
price () {
return Price.compute(mkRequestParams({ reservation }, $scope.coupon.applied)).$promise;
return Price.compute(mkCartItems([reservation], '')).$promise;
},
cartItems () {
// TODO: why 'card' despite we pay on site?
return mkCartItems(reservation, 'card');
return mkCartItems([reservation], $scope.method.payment_method);
},
wallet () {
return Wallet.getWalletByUser({ user_id: reservation.user_id }).$promise;
@ -814,9 +795,9 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
$scope.attempting = true;
// save subscription (if there's only a subscription selected)
if ($scope.reservation.slots_attributes.length === 0 && selectedPlan) {
const sub = mkSubscription(selectedPlan.id, $scope.reservation.user_id, schedule.requested_schedule, $scope.method.payment_method);
const sub = mkSubscription(selectedPlan.id);
return Subscription.save(mkRequestParams(sub, coupon),
return Subscription.save(mkCartItems([sub], $scope.method.payment_method),
function (subscription) {
$uibModalInstance.close(subscription);
$scope.attempting = true;
@ -827,8 +808,8 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
});
}
// otherwise, save the reservation (may include a subscription)
const rsrv = Object.assign({}, $scope.reservation, { payment_method: $scope.method.payment_method });
Reservation.save(mkRequestParams({ reservation: rsrv }, coupon), function (reservation) {
const sub = selectedPlan ? mkSubscription(selectedPlan.id) : undefined;
Reservation.save(mkCartItems([$scope.reservation, sub], coupon), function (reservation) {
$uibModalInstance.close(reservation);
$scope.attempting = true;
}, function (response) {
@ -869,7 +850,7 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
const initialize = function () {
$scope.$watch('method.payment_method', function (newValue) {
$scope.validButtonName = computeValidButtonName();
$scope.cartItems = mkCartItems($scope.reservation, newValue);
$scope.cartItems = mkCartItems([$scope.reservation], newValue);
});
};
@ -933,7 +914,7 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
* Actions to pay slots (or subscription)
*/
const paySlots = function () {
const reservation = mkReservation($scope.user, $scope.events.reserved, $scope.selectedPlan);
const reservation = mkReservation($scope.events.reserved);
return Wallet.getWalletByUser({ user_id: $scope.user.id }, function (wallet) {
const amountToPay = helpers.getAmountToPay($scope.amountTotal, wallet.amount);

View File

@ -15,9 +15,9 @@ class Subscriptions::CreateAsAdminTest < ActionDispatch::IntegrationTest
VCR.use_cassette('subscriptions_admin_create_success') do
post '/api/subscriptions',
params: {
customer_id: user.id,
subscription: {
plan_id: plan.id,
user_id: user.id
plan_id: plan.id
}
}.to_json, headers: default_headers
end