require"csv"require"activerecord-import"require"zip"require_relative"./report_processor"moduleCanvasSyncmoduleProcessorsclassProvisioningReportProcessor<ReportProcessor# Processes a provisioning report using the bulk importer.## options must contain a models key. If there is only one model# Canvas downloads the single report directly as a CSV. If it's# more than one model Canvas downloads a ZIP file, so we have to# extract that and iterate through it for processing.## @param report_file_path [String]# @param options [Hash]defself.process(report_file_path,options,report_id)new(report_file_path,options)enddefinitialize(report_file_path,options)# rubocop:disable Metrics/AbcSize@options=optionsifoptions[:models].length==1run_import(options[:models][0],report_file_path)elseunzipped_file_path=extract(report_file_path)Dir[unzipped_file_path+"/*.csv"].sort.eachdo|file_path|model_name=file_path.split("/").last.split(".").firstrun_import(model_name,file_path)endendendprivatedefextract(file_path)unzipped_file_path="#{file_path}_unzipped"Zip::File.open(file_path)do|zip_file|zip_file.eachdo|f|f_path=File.join(unzipped_file_path,f.name)FileUtils.mkdir_p(File.dirname(f_path))zip_file.extract(f,f_path)unlessFile.exist?(f_path)endendunzipped_file_pathenddefrun_import(model_name,report_file_path)iflegacy?(model_name)CanvasSync::Importers::LegacyImporter.import(report_file_path,model_name.singularize.capitalize.constantize,@options[:account_id],@options,)elsesend("bulk_process_#{model_name}",report_file_path)endenddeflegacy?(model_name)opt=@options[:legacy_support]returnfalseifopt==false||opt.nil?returntrueifopt==truereturnopt.include?(model_name)enddefbulk_process_users(report_file_path)do_bulk_import(report_file_path,User,options: @options)enddefbulk_process_user_observers(report_file_path)do_bulk_import(report_file_path,UserObserver,options: @options)enddefbulk_process_pseudonyms(report_file_path)do_bulk_import(report_file_path,Pseudonym,options: @options)enddefbulk_process_accounts(report_file_path)do_bulk_import(report_file_path,Account,options: @options)enddefbulk_process_courses(report_file_path)do_bulk_import(report_file_path,Course,options: @options)enddefbulk_process_enrollments(report_file_path)do_bulk_import(report_file_path,Enrollment,options: @options)enddefbulk_process_sections(report_file_path)do_bulk_import(report_file_path,Section,options: @options)enddefbulk_process_xlist(report_file_path)do_bulk_import(report_file_path,Section,options: @options,mapping_key: :xlist)enddefbulk_process_groups(report_file_path)do_bulk_import(report_file_path,Group,options: @options)enddefbulk_process_grading_periods(report_file_path)do_bulk_import(report_file_path,GradingPeriod,options: @options)enddefbulk_process_grading_period_groups(report_file_path)do_bulk_import(report_file_path,GradingPeriodGroup,options: @options)end# Note that group membership is singular because we override the model name param in sync_provisioning_report_jobdefbulk_process_group_membership(report_file_path)do_bulk_import(report_file_path,GroupMembership,options: @options)enddefbulk_process_learning_outcomes(report_file_path)do_bulk_import(report_file_path,LearningOutcome,options: @options)do|row|row[:root_account_ids]=JSON.parserow[:root_account_ids]rowendendendendend