Skip to content

Instantly share code, notes, and snippets.

@Luiyit
Last active April 2, 2021 21:12
Show Gist options
  • Save Luiyit/d794d525740668b3ef56243c3f1d6462 to your computer and use it in GitHub Desktop.
Save Luiyit/d794d525740668b3ef56243c3f1d6462 to your computer and use it in GitHub Desktop.
Remove pass customer and products
# TODO: include sessions
def remove_customer_pass(client_user_id, pass_customer_id, delete_pass_customer = false, expire_pass_customer = false)
user_client = User.find(client_user_id)
if user_client
pass_customer_classes = []
customer_collection = nil
pass_customer = PassCustomer.find(pass_customer_id)
pass_applies_to = pass_customer.pass.pass_products.pluck(:applies_to)
talent = pass_customer.pass.user.talent
ActiveRecord::Base.transaction do
# Remove classes
if pass_applies_to.include?("talent_class")
talent_classes_ids = TalentClass.where(talent_id: talent.id).where('start_time >= ? AND start_time >= ?', Time.now.utc, pass_customer.created_at).pluck(:id)
pass_customer_classes = TalentClassCustomer.where(talent_class_id: talent_classes_ids).where(user_id: user_client.id).order("id asc")
pass_customer_classes.delete_all
end
# Remove on demand
if pass_applies_to.include?("on_demand_collection")
on_demand_collection = OnDemandCollection.where(talent_id: talent.id).first
customer_collection = OnDemandCollectionCustomer.where(on_demand_collection_id: on_demand_collection.id, user_id: user_client.id).first
customer_collection.delete
end
# Expire pass customer
pass_customer.update(finish_date: DateTime.now) if expire_pass_customer
# Remove pass customer
pass_customer.delete if delete_pass_customer
rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordNotDestroyed => ex
return { errors: ex.message }
end
return { pass_customer_classes: pass_customer_classes, customer_collection: customer_collection, success:true }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment