2024-03-15 18:27:31 -04:00
|
|
|
class ImportsController < ApplicationController
|
|
|
|
|
before_action :authenticate_user!
|
|
|
|
|
before_action :set_import, only: %i[ show destroy ]
|
|
|
|
|
|
|
|
|
|
def index
|
|
|
|
|
@imports = current_user.imports
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def show
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def new
|
|
|
|
|
@import = Import.new
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
|
|
|
|
files = import_params[:files].reject(&:blank?)
|
2024-04-25 16:28:34 -04:00
|
|
|
|
2024-04-06 16:31:33 -04:00
|
|
|
import_ids = files.map do |file|
|
2024-03-24 13:05:39 -04:00
|
|
|
import = current_user.imports.create(
|
|
|
|
|
name: file.original_filename,
|
2024-03-24 13:55:35 -04:00
|
|
|
source: params[:import][:source]
|
2024-03-24 13:05:39 -04:00
|
|
|
)
|
2024-03-15 18:27:31 -04:00
|
|
|
|
2024-04-25 16:28:34 -04:00
|
|
|
import.update(raw_data: JSON.parse(File.read(file)))
|
2024-04-06 16:31:33 -04:00
|
|
|
import.id
|
|
|
|
|
end
|
|
|
|
|
|
2024-04-25 16:53:55 -04:00
|
|
|
import_ids.each { ImportJob.perform_later(current_user.id, _1) }
|
2024-03-15 18:27:31 -04:00
|
|
|
|
2024-03-24 13:05:39 -04:00
|
|
|
redirect_to imports_url, notice: "#{files.size} files are queued to be imported in background", status: :see_other
|
2024-03-15 18:27:31 -04:00
|
|
|
rescue StandardError => e
|
2024-03-24 13:05:39 -04:00
|
|
|
Import.where(user: current_user, name: files.map(&:original_filename)).destroy_all
|
2024-03-24 13:55:35 -04:00
|
|
|
|
2024-03-15 18:27:31 -04:00
|
|
|
flash.now[:error] = e.message
|
|
|
|
|
|
|
|
|
|
redirect_to new_import_path, notice: e.message, status: :unprocessable_entity
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
|
@import.destroy!
|
|
|
|
|
redirect_to imports_url, notice: "Import was successfully destroyed.", status: :see_other
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def set_import
|
|
|
|
|
@import = Import.find(params[:id])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def import_params
|
|
|
|
|
params.require(:import).permit(:source, files: [])
|
|
|
|
|
end
|
|
|
|
|
end
|