1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-17 06:52:27 +01:00

users_credits/manager service, handle the case if user does not have a plan

This commit is contained in:
Nicolas Florentin 2016-04-07 16:01:31 +02:00
parent 2ab8d04a68
commit 02b56ed6a7
2 changed files with 32 additions and 0 deletions

View File

@ -89,6 +89,8 @@ module UsersCredits
private
def _will_use_credits?
return false, 0 unless plan
if machine_credit = plan.machine_credits.find_by(creditable_id: reservation.reservable_id)
users_credit = user.users_credits.find_by(credit_id: machine_credit.id)
already_used_hours = users_credit ? users_credit.hours_used : 0
@ -122,6 +124,8 @@ module UsersCredits
private
def _will_use_credits?
return false, nil unless plan
# if there is a training_credit defined for this plan and this training
if training_credit = plan.training_credits.find_by(creditable_id: reservation.reservable_id)
# if user has not used all the plan credits

View File

@ -6,12 +6,28 @@ class UsersCreditsManagerTest < ActiveSupport::TestCase
@training = Training.find(2)
@plan = Plan.find(3)
@user = User.joins(:subscriptions).find_by(subscriptions: { plan: @plan })
@user.users_credits.destroy_all
@availability = @machine.availabilities.first
@reservation_machine = Reservation.new(user: @user, reservable: @machine)
@reservation_training = Reservation.new(user: @user, reservable: @training)
end
## context machine reservation
test "machine reservation from user without subscribed plan" do
@user.subscriptions.destroy_all
@reservation_machine.assign_attributes(slots_attributes: [{
start_at: @availability.start_at, end_at: @availability.start_at + 1.hour, availability_id: @availability.id
}])
manager = UsersCredits::Manager.new(reservation: @reservation_machine)
assert_equal false, manager.will_use_credits?
assert_equal 0, manager.free_hours_count
assert_no_difference 'UsersCredit.count' do
manager.update_credits
end
end
test "machine reservation without credit associated" do
Credit.where(creditable: @machine).destroy_all
@ -101,6 +117,18 @@ class UsersCreditsManagerTest < ActiveSupport::TestCase
# context training reservation
test "training reservation from user without subscribed plan" do
@user.subscriptions.destroy_all
manager = UsersCredits::Manager.new(reservation: @reservation_training)
assert_equal false, manager.will_use_credits?
assert_no_difference 'UsersCredit.count' do
manager.update_credits
end
end
test "training reservation without credit associated" do
Credit.where(creditable: @training).destroy_all