Skip to content

Instantly share code, notes, and snippets.

@Luiyit
Created November 19, 2020 17:31
Show Gist options
  • Save Luiyit/ad4558e3cb9f7b20a3c6118f239320e6 to your computer and use it in GitHub Desktop.
Save Luiyit/ad4558e3cb9f7b20a3c6118f239320e6 to your computer and use it in GitHub Desktop.
Create pack_customer, order and order_product for free
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