module CanvasSync module Jobs # ActiveJob class that starts a Canvas provisioning report class SyncProvisioningReportJob < CanvasSync::Job # @param job_chain [Hash] # @param options [Hash] If options contains a :term_scope a seperate provisioning report # will be started for each term in that scope. :models should be an array of # models to sync. def perform(job_chain, options) if options[:term_scope] sub_reports = CanvasSync.fork(@job_log, job_chain, keys: [:canvas_term_id]) do |job_chain| Term.send(options[:term_scope]).find_each.map do |term| # Deep copy the job_chain so each report gets the correct term id passed into # its options with no side effects term_id = get_term_id(term) duped_job_chain = Marshal.load(Marshal.dump(job_chain)) duped_job_chain[:global_options][:canvas_term_id] = term_id { job_chain: duped_job_chain, params: report_params(options, term_id), options: options, } end end sub_reports.each do |r| start_report(r[:params], r[:job_chain], r[:options]) end else start_report(report_params(options), job_chain, options) end end private def start_report(report_params, job_chain, options) CanvasSync::Jobs::ReportStarter.perform_later( job_chain, "proservices_provisioning_csv", report_params, CanvasSync::Processors::ProvisioningReportProcessor.to_s, options, ) end def report_params(options, canvas_term_id=nil) params = { include_deleted: true, } options[:models].each do |model| # group_membership is the only model param that is singular :( model = 'group_membership' if model == 'group_memberships' params[model] = true end params[:enrollment_term_id] = canvas_term_id if canvas_term_id params.merge!(options[:report_parameters]) if options[:report_parameters].present? { parameters: params } end def get_term_id(term) term.try(:canvas_id) || term.canvas_term_id end end end end