mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-30 19:52:20 +01:00
migrate js clients to new architecture
This commit is contained in:
parent
b9e8c8867c
commit
6b8f7da98c
@ -299,7 +299,7 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
|
|||||||
$scope.payEvent = function () {
|
$scope.payEvent = function () {
|
||||||
// first, we check that a user was selected
|
// first, we check that a user was selected
|
||||||
if (Object.keys($scope.ctrl.member).length > 0) {
|
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) {
|
return Wallet.getWalletByUser({ user_id: $scope.ctrl.member.id }, function (wallet) {
|
||||||
const amountToPay = helpers.getAmountToPay($scope.reserve.amountTotal, wallet.amount);
|
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
|
* Callback to validate the booking of a free event
|
||||||
*/
|
*/
|
||||||
$scope.validReserveEvent = function () {
|
$scope.validReserveEvent = function () {
|
||||||
const reservation = {
|
const cartItems = {
|
||||||
user_id: $scope.ctrl.member.id,
|
customer_id: $scope.ctrl.member.id,
|
||||||
reservable_id: $scope.event.id,
|
reservation: {
|
||||||
reservable_type: 'Event',
|
reservable_id: $scope.event.id,
|
||||||
slots_attributes: [],
|
reservable_type: 'Event',
|
||||||
nb_reserve_places: $scope.reserve.nbReservePlaces,
|
slots_attributes: [],
|
||||||
tickets_attributes: []
|
nb_reserve_places: $scope.reserve.nbReservePlaces,
|
||||||
};
|
tickets_attributes: []
|
||||||
|
}
|
||||||
|
}
|
||||||
// a single slot is used for events
|
// a single slot is used for events
|
||||||
reservation.slots_attributes.push({
|
cartItems.reservation.slots_attributes.push({
|
||||||
start_at: $scope.event.start_date,
|
start_at: $scope.event.start_date,
|
||||||
end_at: $scope.event.end_date,
|
end_at: $scope.event.end_date,
|
||||||
availability_id: $scope.event.availability.id
|
availability_id: $scope.event.availability.id
|
||||||
});
|
});
|
||||||
// iterate over reservations per prices
|
// iterate over reservations per prices
|
||||||
for (let price_id in $scope.reserve.tickets) {
|
for (let price_id in $scope.reserve.tickets) {
|
||||||
const seats = $scope.reserve.tickets[price_id];
|
if (Object.prototype.hasOwnProperty.call($scope.reserve.tickets, price_id)) {
|
||||||
reservation.tickets_attributes.push({
|
const seats = $scope.reserve.tickets[price_id];
|
||||||
event_price_category_id: price_id,
|
cartItems.reservation.tickets_attributes.push({
|
||||||
booked: seats
|
event_price_category_id: price_id,
|
||||||
});
|
booked: seats
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// set the attempting marker
|
// set the attempting marker
|
||||||
$scope.attempting = true;
|
$scope.attempting = true;
|
||||||
// save the reservation to the API
|
// save the reservation to the API
|
||||||
return Reservation.save({ reservation }, function (reservation) {
|
return Reservation.save(cartItems, function (reservation) {
|
||||||
// reservation successfull
|
// reservation successfull
|
||||||
afterPayment(reservation);
|
afterPayment(reservation);
|
||||||
return $scope.attempting = false;
|
return $scope.attempting = false;
|
||||||
@ -513,8 +517,8 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
|
|||||||
$scope.computeEventAmount = function () {
|
$scope.computeEventAmount = function () {
|
||||||
// first we check that a user was selected
|
// first we check that a user was selected
|
||||||
if (Object.keys($scope.ctrl.member).length > 0) {
|
if (Object.keys($scope.ctrl.member).length > 0) {
|
||||||
const r = mkReservation($scope.ctrl.member, $scope.reserve, $scope.event);
|
const r = mkReservation($scope.reserve, $scope.event);
|
||||||
return Price.compute(mkRequestParams(r, $scope.coupon.applied), function (res) {
|
return Price.compute(mkCartItems(r, $scope.coupon.applied), function (res) {
|
||||||
$scope.reserve.amountTotal = res.price;
|
$scope.reserve.amountTotal = res.price;
|
||||||
return $scope.reserve.totalNoCoupon = res.price_without_coupon;
|
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
|
* Create a 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 reserve {Object} Reservation parameters (places...)
|
||||||
* @param event {Object} Current event
|
* @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 = {
|
const reservation = {
|
||||||
user_id: member.id,
|
|
||||||
reservable_id: event.id,
|
reservable_id: event.id,
|
||||||
reservable_type: 'Event',
|
reservable_type: 'Event',
|
||||||
slots_attributes: [],
|
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
|
* Format the parameters expected by /api/prices/compute or /api/reservations and return the resulting object
|
||||||
* @param reservation {Object} as returned by mkReservation()
|
* @param reservation {Object} as returned by mkReservation()
|
||||||
* @param coupon {Object} Coupon as returned from the API
|
* @param coupon {Object} Coupon as returned from the API
|
||||||
|
* @param paymentMethod {string} 'card' | ''
|
||||||
* @return {{reservation:Object, coupon_code:string}}
|
* @return {{reservation:Object, coupon_code:string}}
|
||||||
*/
|
*/
|
||||||
const mkRequestParams = function (reservation, coupon) {
|
const mkCartItems = function (reservation, coupon, paymentMethod = '') {
|
||||||
return {
|
return {
|
||||||
customer_id: reservation.user_id,
|
customer_id: reservation.user_id,
|
||||||
reservation,
|
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;
|
return reservation;
|
||||||
},
|
},
|
||||||
price () {
|
price () {
|
||||||
return Price.compute(mkRequestParams(reservation, $scope.coupon.applied)).$promise;
|
return Price.compute(mkCartItems(reservation, $scope.coupon.applied, 'card')).$promise;
|
||||||
},
|
},
|
||||||
wallet () {
|
wallet () {
|
||||||
return Wallet.getWalletByUser({ user_id: reservation.user_id }).$promise;
|
return Wallet.getWalletByUser({ user_id: reservation.user_id }).$promise;
|
||||||
@ -696,7 +700,7 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
|
|||||||
return $scope.coupon.applied;
|
return $scope.coupon.applied;
|
||||||
},
|
},
|
||||||
cartItems () {
|
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; }]
|
stripeKey: ['Setting', function (Setting) { return Setting.get({ name: 'stripe_public_key' }).$promise; }]
|
||||||
},
|
},
|
||||||
@ -748,7 +752,7 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
|
|||||||
return reservation;
|
return reservation;
|
||||||
},
|
},
|
||||||
price () {
|
price () {
|
||||||
return Price.compute(mkRequestParams(reservation, $scope.coupon.applied)).$promise;
|
return Price.compute(mkCartItems(reservation, $scope.coupon.applied)).$promise;
|
||||||
},
|
},
|
||||||
wallet () {
|
wallet () {
|
||||||
return Wallet.getWalletByUser({ user_id: reservation.user_id }).$promise;
|
return Wallet.getWalletByUser({ user_id: reservation.user_id }).$promise;
|
||||||
@ -757,7 +761,7 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', '
|
|||||||
return $scope.coupon.applied;
|
return $scope.coupon.applied;
|
||||||
},
|
},
|
||||||
cartItems () {
|
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',
|
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
|
// Callback to validate the payment
|
||||||
$scope.ok = function () {
|
$scope.ok = function () {
|
||||||
$scope.attempting = true;
|
$scope.attempting = true;
|
||||||
return Reservation.save(mkRequestParams($scope.reservation, coupon), function (reservation) {
|
return Reservation.save(mkCartItems($scope.reservation, coupon), function (reservation) {
|
||||||
$uibModalInstance.close(reservation);
|
$uibModalInstance.close(reservation);
|
||||||
return $scope.attempting = true;
|
return $scope.attempting = true;
|
||||||
}
|
}
|
||||||
|
@ -612,8 +612,8 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
|
|||||||
*/
|
*/
|
||||||
const updateCartPrice = function () {
|
const updateCartPrice = function () {
|
||||||
if (Object.keys($scope.user).length > 0) {
|
if (Object.keys($scope.user).length > 0) {
|
||||||
const r = mkReservation($scope.user, $scope.events.reserved, $scope.selectedPlan);
|
const r = mkReservation($scope.events.reserved);
|
||||||
return Price.compute(mkRequestParams({ reservation: r }, $scope.coupon.applied), function (res) {
|
return Price.compute(mkCartItems([r], ''), function (res) {
|
||||||
$scope.amountTotal = res.price;
|
$scope.amountTotal = res.price;
|
||||||
$scope.schedule.payment_schedule = res.schedule;
|
$scope.schedule.payment_schedule = res.schedule;
|
||||||
$scope.totalNoCoupon = res.price_without_coupon;
|
$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
|
* 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 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 {{reservation: {reservable_type: string, reservable_id: string, slots_attributes: []}}}
|
||||||
* @return {{reservable_type: string, payment_schedule: boolean, user_id: *, reservable_id: string, slots_attributes: [], plan_id: (*|undefined)}}
|
|
||||||
*/
|
*/
|
||||||
const mkReservation = function (member, slots, plan) {
|
const mkReservation = function (slots) {
|
||||||
const reservation = {
|
const reservation = {
|
||||||
user_id: member.id,
|
|
||||||
reservable_id: $scope.reservableId,
|
reservable_id: $scope.reservableId,
|
||||||
reservable_type: $scope.reservableType,
|
reservable_type: $scope.reservableType,
|
||||||
slots_attributes: [],
|
slots_attributes: []
|
||||||
plan_id: ((plan ? plan.id : undefined)),
|
|
||||||
payment_schedule: $scope.schedule.requested_schedule
|
|
||||||
};
|
};
|
||||||
angular.forEach(slots, function (slot) {
|
angular.forEach(slots, function (slot) {
|
||||||
reservation.slots_attributes.push({
|
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
|
* 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}
|
* @param paymentMethod {string}
|
||||||
* @return {CartItems}
|
* @return {CartItems}
|
||||||
*/
|
*/
|
||||||
const mkCartItems = function (reservation, paymentMethod) {
|
const mkCartItems = function (items, paymentMethod) {
|
||||||
const request = {
|
const cartItems = {
|
||||||
customer_id: reservation.user_id,
|
customer_id: $scope.user.id,
|
||||||
payment_schedule: $scope.schedule.requested_schedule,
|
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) {
|
for (const item of items) {
|
||||||
Object.assign(request, mkSubscription($scope.selectedPlan.id));
|
Object.assign(cartItems, item);
|
||||||
} else {
|
|
||||||
Object.assign(request, { reservation });
|
|
||||||
}
|
}
|
||||||
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'));
|
growl.error(_t('app.shared.cart.online_payment_disabled'));
|
||||||
} else {
|
} else {
|
||||||
$scope.toggleOnlinePaymentModal(() => {
|
$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;
|
return reservation;
|
||||||
},
|
},
|
||||||
price () {
|
price () {
|
||||||
return Price.compute(mkRequestParams({ reservation }, $scope.coupon.applied)).$promise;
|
return Price.compute(mkCartItems([reservation], '')).$promise;
|
||||||
},
|
},
|
||||||
cartItems () {
|
cartItems () {
|
||||||
// TODO: why 'card' despite we pay on site?
|
return mkCartItems([reservation], $scope.method.payment_method);
|
||||||
return mkCartItems(reservation, 'card');
|
|
||||||
},
|
},
|
||||||
wallet () {
|
wallet () {
|
||||||
return Wallet.getWalletByUser({ user_id: reservation.user_id }).$promise;
|
return Wallet.getWalletByUser({ user_id: reservation.user_id }).$promise;
|
||||||
@ -814,9 +795,9 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
|
|||||||
$scope.attempting = true;
|
$scope.attempting = true;
|
||||||
// save subscription (if there's only a subscription selected)
|
// save subscription (if there's only a subscription selected)
|
||||||
if ($scope.reservation.slots_attributes.length === 0 && selectedPlan) {
|
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) {
|
function (subscription) {
|
||||||
$uibModalInstance.close(subscription);
|
$uibModalInstance.close(subscription);
|
||||||
$scope.attempting = true;
|
$scope.attempting = true;
|
||||||
@ -827,8 +808,8 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
// otherwise, save the reservation (may include a subscription)
|
// otherwise, save the reservation (may include a subscription)
|
||||||
const rsrv = Object.assign({}, $scope.reservation, { payment_method: $scope.method.payment_method });
|
const sub = selectedPlan ? mkSubscription(selectedPlan.id) : undefined;
|
||||||
Reservation.save(mkRequestParams({ reservation: rsrv }, coupon), function (reservation) {
|
Reservation.save(mkCartItems([$scope.reservation, sub], coupon), function (reservation) {
|
||||||
$uibModalInstance.close(reservation);
|
$uibModalInstance.close(reservation);
|
||||||
$scope.attempting = true;
|
$scope.attempting = true;
|
||||||
}, function (response) {
|
}, function (response) {
|
||||||
@ -869,7 +850,7 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
|
|||||||
const initialize = function () {
|
const initialize = function () {
|
||||||
$scope.$watch('method.payment_method', function (newValue) {
|
$scope.$watch('method.payment_method', function (newValue) {
|
||||||
$scope.validButtonName = computeValidButtonName();
|
$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)
|
* Actions to pay slots (or subscription)
|
||||||
*/
|
*/
|
||||||
const paySlots = function () {
|
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) {
|
return Wallet.getWalletByUser({ user_id: $scope.user.id }, function (wallet) {
|
||||||
const amountToPay = helpers.getAmountToPay($scope.amountTotal, wallet.amount);
|
const amountToPay = helpers.getAmountToPay($scope.amountTotal, wallet.amount);
|
||||||
|
@ -15,9 +15,9 @@ class Subscriptions::CreateAsAdminTest < ActionDispatch::IntegrationTest
|
|||||||
VCR.use_cassette('subscriptions_admin_create_success') do
|
VCR.use_cassette('subscriptions_admin_create_success') do
|
||||||
post '/api/subscriptions',
|
post '/api/subscriptions',
|
||||||
params: {
|
params: {
|
||||||
|
customer_id: user.id,
|
||||||
subscription: {
|
subscription: {
|
||||||
plan_id: plan.id,
|
plan_id: plan.id
|
||||||
user_id: user.id
|
|
||||||
}
|
}
|
||||||
}.to_json, headers: default_headers
|
}.to_json, headers: default_headers
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user