|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Admin |
| 4 | + class SectionsController < AdminController |
| 5 | + include GoogleCredentials |
| 6 | + include Reimportable |
| 7 | + include Queryable |
| 8 | + |
| 9 | + before_action :find_selected, only: %i(destroy_selected reimport_selected) |
| 10 | + before_action :set_query_params |
| 11 | + |
| 12 | + QUERY_ATTRS = %i( |
| 13 | + grade |
| 14 | + search_term |
| 15 | + section_number |
| 16 | + sort_by |
| 17 | + subject |
| 18 | + unit_id |
| 19 | + ).freeze |
| 20 | + QUERY_ATTRS_NESTED = { |
| 21 | + grades: [] |
| 22 | + }.freeze |
| 23 | + QUERY_ATTRS_KEYS = QUERY_ATTRS + QUERY_ATTRS_NESTED.keys |
| 24 | + |
| 25 | + def index |
| 26 | + @query = query_struct(@query_params) |
| 27 | + @sections = Admin::SectionsQuery.call(@query, page: params[:page]) |
| 28 | + render_customized_view |
| 29 | + end |
| 30 | + |
| 31 | + def create |
| 32 | + @section_form = SectionForm.new(form_params.except(:async).to_h) |
| 33 | + |
| 34 | + return create_multiple if form_params[:link].match?(RE_GOOGLE_FOLDER) |
| 35 | + |
| 36 | + form_params[:async].to_i.zero? ? create_sync : create_async |
| 37 | + end |
| 38 | + |
| 39 | + def destroy |
| 40 | + section = Resource.sections.find(params[:id].to_i) |
| 41 | + section.destroy |
| 42 | + redirect_to admin_sections_path(query: @query_params), notice: t(".success") |
| 43 | + end |
| 44 | + |
| 45 | + def destroy_selected |
| 46 | + count = @sections.destroy_all.count |
| 47 | + redirect_to admin_sections_path(query: @query_params), notice: t(".success", count:) |
| 48 | + end |
| 49 | + |
| 50 | + def import_status |
| 51 | + data = import_status_for(SectionParseJob) |
| 52 | + render json: data, status: :ok |
| 53 | + end |
| 54 | + |
| 55 | + def new |
| 56 | + @section_form = SectionForm.new |
| 57 | + end |
| 58 | + |
| 59 | + def reimport_selected |
| 60 | + urls = [] |
| 61 | + skipped = 0 |
| 62 | + @sections.each do |section| |
| 63 | + if (url = section.links.dig("source", "gdoc", "url")).present? |
| 64 | + urls << url |
| 65 | + else |
| 66 | + skipped += 1 |
| 67 | + end |
| 68 | + end |
| 69 | + flash.now[:alert] = t(".skipped", count: skipped) if skipped.positive? |
| 70 | + bulk_import urls |
| 71 | + render :import |
| 72 | + end |
| 73 | + |
| 74 | + private |
| 75 | + |
| 76 | + def bulk_import(file_urls) |
| 77 | + jobs = |
| 78 | + file_urls.each_with_object({}) do |url, jobs_| |
| 79 | + job_id = SectionParseJob.perform_later(url).job_id |
| 80 | + jobs_[job_id] = { link: url, status: "waiting" } |
| 81 | + end |
| 82 | + polling_path = import_status_admin_sections_path |
| 83 | + @props = { jobs:, links: view_links, polling_path:, type: :sections } |
| 84 | + .transform_keys! { _1.to_s.camelize(:lower).to_sym } |
| 85 | + end |
| 86 | + |
| 87 | + def collect_errors |
| 88 | + @collect_errors ||= |
| 89 | + if @section_form.service_errors.empty? |
| 90 | + [] |
| 91 | + else |
| 92 | + @section_form.service_errors.map { "<li>#{_1}</li>" }.join |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + def create_async |
| 97 | + bulk_import Array.wrap(form_params[:link]) |
| 98 | + render :import |
| 99 | + end |
| 100 | + |
| 101 | + def create_sync |
| 102 | + if @section_form.save |
| 103 | + flash_message = |
| 104 | + if collect_errors.empty? |
| 105 | + { notice: t("admin.sections.create.success", name: @section_form.section.title) } |
| 106 | + else |
| 107 | + { alert: t("admin.sections.create.error", name: @section_form.section.title, errors: collect_errors) } |
| 108 | + end |
| 109 | + redirect_to admin_sections_path(anchor: "section_#{@section_form.section.id}", query: @query_params), **flash_message |
| 110 | + else |
| 111 | + render :new |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + def find_selected |
| 116 | + return head(:bad_request) unless params[:selected_ids].present? |
| 117 | + |
| 118 | + ids = params[:selected_ids].split(",") |
| 119 | + @sections = Resource.sections.where(id: ids) |
| 120 | + end |
| 121 | + |
| 122 | + def form_params |
| 123 | + @form_params ||= params.require(:section_form).permit(:async, :link) |
| 124 | + end |
| 125 | + |
| 126 | + def gdoc_files_from(url) |
| 127 | + folder_id = ::Lt::Google::Api::Drive.folder_id_for(url) |
| 128 | + |
| 129 | + ::Lt::Google::Api::Drive.new(google_credentials) |
| 130 | + .list_file_ids_in(folder_id) |
| 131 | + .map { |id| ::Lt::Lcms::Lesson::Downloader::Gdoc.gdoc_file_url(id) } |
| 132 | + end |
| 133 | + end |
| 134 | +end |
0 commit comments