Snippets

ESTI design alfaportal notification observers

Created by Robert Poz last modified
class NotificationsObserver < ActiveRecord::Observer

    observe :session, :code

    def after_save(obj)
        # session changed from active to failed and client side error was reported
        # send notification about broken code
        if obj.is_a?(Session) && obj.code_based?
            if  obj.state_change == [Session::ACTIVE, Session::FAILED] && 
                obj.error_code == Session::ERROR_CLIENT_SIDE && obj.code.working == false
                    ApiMailer.delay.broken_code_alert(obj)
            end
            if obj.state_change == [nil, Session::ACTIVE] && obj.subscription
                ApiMailer.delay.add_codes_alert(obj.code.product)
            end
        end
    rescue 
        nil
    end

    def before_update(obj)
        # code instances after save observer
        if obj.is_a?(Code)
            # check if product related to code has valid duration
            # code is valid if expiration date of the code - now = code product duration
            if obj.expire_date_was == nil && !obj.expire_date.nil?
                # round up durations to hours
                if obj.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)
                end
            end
        end
    rescue
        nil
    end

end

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.