Sha256: 2d269c9a712f8d7e12b3e51f4e5de2fd9e21e4be4feb37ff69949d69a514815b

Contents?: true

Size: 1.68 KB

Versions: 27

Compression:

Stored size: 1.68 KB

Contents

# <%= autogenerated_event_warning %>

module LiveEvents
  class EnrollmentEvent < LiveEvents::BaseEvent
    # The following is provided in the live events call:
    # {
    #
    #     enrollment_id: enrollment.global_id,
    #     course_id: enrollment.global_course_id,
    #     user_id: enrollment.global_user_id,
    #     user_name: enrollment.user_name,
    #     type: enrollment.type,
    #     created_at: enrollment.created_at,
    #     updated_at: enrollment.updated_at,
    #     limit_privileges_to_course_section: enrollment.limit_privileges_to_course_section,
    #     course_section_id: enrollment.global_course_section_id,
    #     workflow_state: enrollment.workflow_state
    # }

    def perform(payload)
      super
      return if course.nil? || section.nil? || user.nil?
      attrs = {
        canvas_enrollment_id: local_canvas_id(payload[:enrollment_id]),
        canvas_course_id: course.try(:id),
        canvas_user_id: user.try(:id),
        role: payload[:type],
        canvas_section_id: section.try(:id),
        status: payload[:workflow_state],
      }
      create_or_update(attrs)
    end

    private

    def create_or_update(attrs)
      enrollment = Enrollment.where(canvas_enrollment_id: attrs[:canvas_enrollment_id]).first_or_initialize
      enrollment.assign_attributes(attrs)
      enrollment.save!
      enrollment
    end

    def course
      @course ||= Course.find_by(canvas_course_id: local_canvas_id(payload[:course_id]))
    end

    def section
      @section ||= Section.find_by(canvas_section_id: local_canvas_id(payload[:section_id]))
    end

    def user
      @user ||= User.find_by(canvas_user_id: local_canvas_id(payload[:user_id]))
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
canvas_sync-0.10.2 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.10.0 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.9.4 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.9.3 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.9.2 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.9.1 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.9.0 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.8.4 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.8.3 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.8.2 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.8.1 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.8.0 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.7.3 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.7.2 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.7.1 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.7.0 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.6.4 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.6.3 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.6.2 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb
canvas_sync-0.6.1 lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb