Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Application.Controllers.controller 'NewRentalController', ['$scope', '$uibModal'
##
# Open a modal dialog allowing the admin to create a new partner user
##
$scope.openPartnerNewModal = (subscription)->
$scope.openPartnerNewModal = (rental_subscription)->
modalInstance = $uibModal.open
animation: true,
templateUrl: '<%= asset_path "shared/_partner_new_modal.html" %>'
Expand Down
92 changes: 71 additions & 21 deletions app/assets/javascripts/controllers/rentals.coffee.erb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class RentalSpacesController
##
# Controller used in the public listing page, allowing everyone to see the list of spaces
##
Application.Controllers.controller 'RentalSpacesController', ['$scope', '$state', 'spacesPromise', '$uibModal', '_t', 'Wallet', 'helpers', ($scope, $state, spacesPromise, $uibModal, _t, Wallet, helpers) ->
Application.Controllers.controller 'RentalSpacesController', ['$scope', '$state', 'rentalsPricesPromise', 'rentalsPromise', 'spacesPromise', '$uibModal', '_t', 'CSRF', 'Wallet', 'helpers', ($scope, $state, rentalsPricesPromise, rentalsPromise, spacesPromise, $uibModal, _t, CSRF, Wallet, helpers) ->

## Retrieve the list of spaces
$scope.spaces = spacesPromise
Expand Down Expand Up @@ -113,20 +113,65 @@ Application.Controllers.controller 'RentalSpacesController', ['$scope', '$state'

$scope.coupon =
applied: null


$scope.rentalsPrices = rentalsPricesPromise
$scope.rentals = rentalsPromise
$scope.selectedRental = null

$scope.selectRental = (rental) ->

$scope.selectRental = (space) ->
if $scope.isAuthenticated()
if $scope.selectedRental != rental
$scope.selectedRental = rental
$scope.selectedRental.amount = 10 ## temporal
if $scope.selectedRental != space
$scope.selectedRental = space
price = findPriceBy($scope.rentalsPrices, $scope.selectedRental.id, $scope.ctrl.member.group_id)
rental = findRentalBy($scope.rentals, $scope.selectedRental.id, $scope.ctrl.member.group_id)
rental = (rental) ? rental : createRentalPlan(space, price)
$scope.selectedRental.amount = price.amount
$scope.selectedRental.rental = rental
updateCartPrice()
else
$scope.selectedRental = null
else
$scope.login()



createRentalPlan = (space, price)->
## form inputs bindings
$scope.rental =
type: null
group_id: price.group_id
interval: "month"
intervalCount: 1
amount: price.amount
is_rolling: false
partnerId: null
partnerContact: null
ui_weight: 0
CSRF.setMetaTags()
url= "/api/plans/"
$.post url, (response)->
console.log("saved")
return response.id

##
# Retrieve a rental from rentals array by a spaceId and a groupId
##
findRentalBy = (rentals, spaceId, groupId)->
for rental in rentals
if rental.space_id == spaceId and rental.group_id == groupId
return rental


##
# Retrieve a price from prices array by a spaceId and a groupId
##
findPriceBy = (prices, spaceId, groupId)->
for price in prices
if price.priceable_id == spaceId and price.group_id == groupId
return price


##
# Callback to trigger the payment process of the subscription
##
Expand Down Expand Up @@ -169,11 +214,12 @@ Application.Controllers.controller 'RentalSpacesController', ['$scope', '$state'
selectedRental: -> $scope.selectedRental
member: -> $scope.ctrl.member
price: -> $scope.cart.total
rental: -> $scope.selectedRental.rental
wallet: ->
Wallet.getWalletByUser({user_id: $scope.ctrl.member.id}).$promise
coupon: -> $scope.coupon.applied
controller: ['$scope', '$uibModalInstance', '$state', 'selectedRental', 'member', 'price', 'Subscription', 'CustomAsset', 'wallet', 'helpers', '$filter', 'coupon',
($scope, $uibModalInstance, $state, selectedRental, member, price, Subscription, CustomAsset, wallet, helpers, $filter, coupon) ->
controller: ['$scope', '$uibModalInstance', '$state', 'selectedRental', 'member', 'price', 'RentalSubscription', 'rental', 'CustomAsset', 'wallet', 'helpers', '$filter', 'coupon',
($scope, $uibModalInstance, $state, selectedRental, member, price, RentalSubscription, rental, CustomAsset, wallet, helpers, $filter, coupon) ->
# User's wallet amount
$scope.walletAmount = wallet.amount

Expand All @@ -185,7 +231,9 @@ Application.Controllers.controller 'RentalSpacesController', ['$scope', '$state'

# Used in wallet info template to interpolate some translations
$scope.numberFilter = $filter('number')
$scope.member = member

$scope.rental = rental
# retrieve the CGV
CustomAsset.get {name: 'cgv-file'}, (cgv) ->
$scope.cgv = cgv.custom_asset
Expand All @@ -200,10 +248,10 @@ Application.Controllers.controller 'RentalSpacesController', ['$scope', '$state'
growl.error(response.error.message)
else
$scope.attempting = true
Subscription.save
RentalSubscription.save
coupon_code: (coupon.code if coupon)
subscription:
plan_id: selectedRental.id
rental_subscription:
rental_id: rental.id
user_id: member.id
card_token: response.id
, (data, status) -> # success
Expand All @@ -213,15 +261,13 @@ Application.Controllers.controller 'RentalSpacesController', ['$scope', '$state'
$scope.alerts.push({msg: _t('an_error_occured_during_the_payment_process_please_try_again_later'), type: 'danger' })
$scope.attempting = false
]
.result['finally'](null).then (subscription)->
.result['finally'](null).then (rental_subscription)->
$scope.ctrl.member.subscribed_plan = angular.copy($scope.selectedRental)
Auth._currentUser.subscribed_plan = angular.copy($scope.selectedRental)
$scope.paid.plan = angular.copy($scope.selectedRental)
$scope.selectedRental = null
$scope.coupon.applied = null



##
# Open a modal window which trigger the local payment process
##
Expand All @@ -233,11 +279,12 @@ Application.Controllers.controller 'RentalSpacesController', ['$scope', '$state'
selectedRental: -> $scope.selectedRental
member: -> $scope.ctrl.member
price: -> $scope.cart.total
rental: -> $scope.selectedRental.rental
wallet: ->
Wallet.getWalletByUser({user_id: $scope.ctrl.member.id}).$promise
coupon: -> $scope.coupon.applied
controller: ['$scope', '$uibModalInstance', '$state', 'selectedRental', 'member', 'price', 'Subscription', 'wallet', 'helpers', '$filter', 'coupon',
($scope, $uibModalInstance, $state, selectedRental, member, price, Subscription, wallet, helpers, $filter, coupon) ->
controller: ['$scope', '$uibModalInstance', '$state', 'selectedRental', 'member', 'price', 'RentalSubscription', 'rental', 'wallet', 'helpers', '$filter', 'coupon',
($scope, $uibModalInstance, $state, selectedRental, member, price, RentalSubscription, rental, wallet, helpers, $filter, coupon) ->
# user wallet amount
$scope.walletAmount = wallet.amount

Expand All @@ -251,11 +298,14 @@ Application.Controllers.controller 'RentalSpacesController', ['$scope', '$state'
$scope.numberFilter = $filter('number')

# The plan that the user is about to subscribe
$scope.plan = selectedRental
$scope.selectedRental = selectedRental

# The member who is subscribing a plan
$scope.member = member

$scope.rental = rental


# Button label
if $scope.amount > 0
$scope.validButtonName = _t('confirm_payment_of_html', {ROLE:$scope.currentUser.role, AMOUNT:$filter('currency')($scope.amount)}, "messageformat")
Expand All @@ -271,10 +321,10 @@ Application.Controllers.controller 'RentalSpacesController', ['$scope', '$state'
##
$scope.ok = ->
$scope.attempting = true
Subscription.save
RentalSubscription.save
coupon_code: (coupon.code if coupon)
subscription:
plan_id: selectedRental.id
rental_subscription:
rental_id: rental.id
user_id: member.id
, (data, status) -> # success
$uibModalInstance.close(data)
Expand Down Expand Up @@ -307,7 +357,7 @@ Application.Controllers.controller 'RentalSpacesController', ['$scope', '$state'
if $scope.currentUser
if $scope.currentUser.role isnt 'admin'
$scope.ctrl.member = $scope.currentUser
##$scope.paid.plan = $scope.currentUser.subscribed_plan
## $scope.paid.plan = $scope.currentUser.subscribed_plan
##$scope.group.id = $scope.currentUser.group_id

$scope.$on 'devise:new-session', (event, user)->
Expand Down
10 changes: 8 additions & 2 deletions app/assets/javascripts/router.coffee.erb
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,12 @@ angular.module('application.router', ['ui.router']).
spacesPromise: ['Space', (Space)->
Space.query({is_rental: true}).$promise
]
rentalsPricesPromise: ['Price', (Price)->
Price.query(priceable_type: 'Space', plan_id: 'null').$promise
]
rentalsPromise: ['Rental', (Rental)->
Rental.query().$promise
]
translations: [ 'Translations', (Translations) ->
Translations.query(['app.public.rentals_list', 'app.shared.member_select', 'app.shared.stripe', 'app.shared.wallet', 'app.shared.coupon_input', 'app.shared.cart']).$promise
]
Expand Down Expand Up @@ -570,8 +576,8 @@ angular.module('application.router', ['ui.router']).
availabilitySpacesPromise: ['Availability', '$stateParams', (Availability, $stateParams)->
Availability.spaces({spaceId: $stateParams.id}).$promise
]
plansPromise: ['Plan', (Plan)->
Plan.query().$promise
rentalsPromise: ['Rental', (Rental)->
Rental.query().$promise
]
groupsPromise: ['Group', (Group)->
Group.query().$promise
Expand Down
11 changes: 11 additions & 0 deletions app/assets/javascripts/services/rental_subscription.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

Application.Services.factory 'RentalSubscription', ["$resource", ($resource)->
$resource "/api/rental_subscriptions/:id",
{id: "@id"},
update:
method: 'PUT'
cancel:
method: 'PUT'
url: '/api/rental_subscriptions/:id/cancel'
]
6 changes: 3 additions & 3 deletions app/assets/templates/rentals/payment_modal.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div class="modal-header">
<img ng-src="{{logoBlack.custom_asset_file_attributes.attachment_url}}" alt="{{logo.custom_asset_file_attributes.attachment}}" class="modal-logo"/>
<h1 translate>{{ 'rental_confirmation' }}</h1>
<h1 translate>{{ 'rentals_list.rental_confirmation' }}</h1>
</div>
<div class="modal-body">
<uib-alert ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</uib-alert>
<ng-include src="'<%= asset_path 'shared/_wallet_amount_info.html' %>'"></ng-include>

<p>{{ 'here_is_the_NAME_rental_summary' | translate:{NAME:member.name} }}</p>
<p>{{ selectedRental.name }}</p>
<p>{{ 'rentals_list.here_is_the_NAME_rental_summary' | translate:{NAME:member.name} }} </p>
<p>{{ plan.name }}</p>
</div>
<div class="modal-footer">
<button class="btn btn-info" ng-click="ok()" ng-disabled="attempting" ng-bind-html="validButtonName"></button>
Expand Down
81 changes: 81 additions & 0 deletions app/controllers/api/rental_subscriptions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
class API::RentalSubscriptionsController < ApplicationController
include FablabConfiguration

before_action :set_rental_subscription, only: [:show, :edit, :update, :cancel, :destroy]
before_action :authenticate_user!

def show
authorize @subscription
end

def create
if fablab_plans_deactivated?
head 403
else
if current_user.is_admin?
@subscription = RentalSubscription.find_or_initialize_by(user_id: rental_subscription_params[:user_id])
@subscription.attributes = rental_subscription_params
is_subscribe = @subscription.save_with_local_payment(true, coupon_params[:coupon_code])
else
@subscription = RentalSubscription.find_or_initialize_by(user_id: current_user.id)
@subscription.attributes = rental_subscription_params
is_subscribe = @subscription.save_with_payment(true, coupon_params[:coupon_code])
end
if is_subscribe
render :show, status: :created, location: @subscription
else
render json: @subscription.errors, status: :unprocessable_entity
end
end
end

def update
authorize @subscription

free_days = params[:rental_subscription][:free] == true

if @subscription.extend_expired_date(rental_subscription_update_params[:expired_at], free_days)
ex_expired_at = @subscription.previous_changes[:expired_at].first
@subscription.user.generate_admin_invoice(free_days, ex_expired_at)
render status: :ok
else
render status: :unprocessable_entity
end
end

def cancel

@subscription.cancel

render :show, status: :ok, location: @subscription
end

private
# Use callbacks to share common setup or constraints between actions.
def set_rental_subscription
@subscription = Subscription.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def rental_subscription_params
params.require(:rental_subscription).permit(:rental_id, :user_id, :card_token)
end

def coupon_params
params.permit(:coupon_code)
end

def rental_subscription_update_params
params.require(:rental_subscription).permit(:expired_at)
end

# TODO refactor subscriptions logic and move this in model/validator
def valid_card_token?(token)
begin
Stripe::Token.retrieve(token)
rescue Stripe::InvalidRequestError => e
@subscription.errors[:card_token] << e.message
false
end
end
end
6 changes: 3 additions & 3 deletions app/controllers/api/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class API::RentalsController < API::ApiController
before_action :authenticate_user!, except: [:index]

def index
@rentals = Rental.includes(:plan_file)
@rentals = Rental.all
@rentals = @rentals.where(group_id: params[:group_id]) if params[:group_id]
render :index
end
Expand Down Expand Up @@ -69,7 +69,7 @@ def update
if @rental.update(rental_params)
render :show, status: :ok
else
render json: @rental.errors, status: :unprocessable_entity
render json: @rental.errors, status: :unprocessablse_entity
end
end

Expand All @@ -92,7 +92,7 @@ def rental_params
end if @parameters[:rental][:prices_attributes]

@parameters = @parameters.require(:rental).permit(:base_name, :type, :group_id, :amount, :interval, :interval_count, :is_rolling,
:training_credit_nb, :ui_weight, :disabled,
:training_credit_nb, :ui_weight, :disabled, :space_id,
rental_file_attributes: [:id, :attachment, :_destroy],
prices_attributes: [:id, :amount]
)
Expand Down
Loading