Skip to content

Instantly share code, notes, and snippets.

@Luiyit
Created November 19, 2020 21:52
Show Gist options
  • Save Luiyit/44fb8df205acae8288fe91874bc001bc to your computer and use it in GitHub Desktop.
Save Luiyit/44fb8df205acae8288fe91874bc001bc to your computer and use it in GitHub Desktop.
Use pack to book class
def redeem_class_credit(customer_user_id, class_id)
# Validate customer
current_user = User.find(customer_user_id)
return "Invalide customer user id" if current_user.nil?
# Validate class
klass = TalentClass.find(class_id);
return "Invalide class id" if klass.nil?
# Validate credits
credits = SpacesHelper.redeemable_credits_for(klass, current_user, 'talent_class')
return "You don't have credits available that apply to redeem this resource." if credits[:available].zero?
ActiveRecord::Base.transaction do
order = Order.create!(
user_id: current_user.id,
date: Time.now,
status: 'success',
total_price: klass.price.to_f,
discounted_total: 0,
)
order_product = OrderProduct.create!(
order_id: order.id,
product_type: klass.class.name.underscore,
product_id: klass.id,
name: klass.title,
description: klass.description,
price: klass.price,
quanty: 1,
classification: klass.class.name.underscore,
discount_id: nil
)
TalentClassCustomer.create!(user_id: current_user.id, talent_class_id: klass.id)
PackRedemption.create!(pack_id: credits[:pack_id] , order_product_id: order_product.id , user_id: current_user.id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment