Created
November 19, 2020 17:31
-
-
Save Luiyit/ad4558e3cb9f7b20a3c6118f239320e6 to your computer and use it in GitHub Desktop.
Create pack_customer, order and order_product for free
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def free_pack_access(pack_id, users_emails, finish_date=nil) | |
pack = Pack.find(pack_id) | |
users_emails.each do |email| | |
current_user = User.find_by(email: email) | |
ActiveRecord::Base.transaction do | |
order = Order.create!( | |
user_id: current_user.id, | |
date: Time.now, | |
status: 'success', | |
total_price: pack.price.to_f, | |
discounted_total: 0, | |
note: "Manual order for #{current_user.email}" | |
) | |
OrderProduct.create!( | |
order_id: order.id, | |
product_type: pack.class.name.underscore, | |
product_id: pack.id, | |
name: pack.title, | |
description: pack.description, | |
price: pack.price, | |
quanty: 1, | |
classification: pack.class.name.underscore, | |
discount_id: nil | |
) | |
bundle_customer = pack.create_customer!(current_user) | |
bundle_customer.update({finish_date: finish_date}) if finish_date | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment