Skip to content

Instantly share code, notes, and snippets.

@Luiyit
Last active March 3, 2021 16:57
Show Gist options
  • Save Luiyit/29abdf2640f73a2ea2184444e674dea8 to your computer and use it in GitHub Desktop.
Save Luiyit/29abdf2640f73a2ea2184444e674dea8 to your computer and use it in GitHub Desktop.
Gives free access to the On Demand collection. Each customer must include the email and the end date.
def free_on_demand_access (on_demand_collection_id, customers)
collection = OnDemandCollection.find(on_demand_collection_id)
return "Invalid collection" if collection.nil?
ActiveRecord::Base.transaction do
customers.each do |customer|
user = User.find_by(email: customer[:email])
return "Invalid user email #{customer[:email]}" if user.nil?
return "Invalid finish date for #{customer[:email]}" if customer[:finish_date].nil?
OnDemandCollectionCustomer.create!(
user_id: user.id,
on_demand_collection_id: collection.id,
source: user,
start_date: DateTime.now,
finish_date: customer[:finish_date]
)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment