classNotificationsObserver<ActiveRecord::Observerobserve:session,:codedefafter_save(obj)# session changed from active to failed and client side error was reported# send notification about broken codeifobj.is_a?(Session)&&obj.code_based?ifobj.state_change==[Session::ACTIVE,Session::FAILED]&&obj.error_code==Session::ERROR_CLIENT_SIDE&&obj.code.working==falseApiMailer.delay.broken_code_alert(obj)endifobj.state_change==[nil,Session::ACTIVE]&&obj.subscriptionApiMailer.delay.add_codes_alert(obj.code.product)endendrescuenilenddefbefore_update(obj)# code instances after save observerifobj.is_a?(Code)# check if product related to code has valid duration# code is valid if expiration date of the code - now = code product durationifobj.expire_date_was==nil&&!obj.expire_date.nil?# round up durations to hoursifobj.product.duration.to_i.hours!=((obj.expire_date-Time.now).to_i/1.hour).to_i# obj.not_working!(false)ApiMailer.delay.invalid_duration_code_alert(obj)endendendrescuenilendend
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.