Class: Panda::Core::Admin::ImportSessionsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/panda/core/admin/import_sessions_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#add_breadcrumb, #authenticate_admin_user!, #authenticate_user!, #breadcrumbs, #current_user, #set_current_request_details, #user_signed_in?

Methods included from Panda::Core::Authorizable

#authorize!, #authorized_for?, #authorized_for_admin_access?, #can?

Instance Method Details

#column_mapObject



66
67
68
69
# File 'app/controllers/panda/core/admin/import_sessions_controller.rb', line 66

def column_map
  @headers = @import_session.file_headers
  @field_options = @import_session.column_options
end

#createObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/panda/core/admin/import_sessions_controller.rb', line 19

def create
  tenant = resolve_tenant
  uploaded_file = params[:import_file]

  unless uploaded_file.present?
    @importable_type = params[:importable_type]
    flash.now[:error] = "Please select a file to upload."
    render :new, status: :unprocessable_entity
    return
  end

  if FileParser.xls?(uploaded_file.original_filename)
    @importable_type = params[:importable_type]
    flash.now[:error] = "XLS format is not supported. Please save your spreadsheet as XLSX, CSV, or TSV and try again."
    render :new, status: :unprocessable_entity
    return
  end

  unless FileParser.supported?(uploaded_file.original_filename)
    @importable_type = params[:importable_type]
    flash.now[:error] = "Unsupported file format. Please upload a CSV, TSV, or XLSX file."
    render :new, status: :unprocessable_entity
    return
  end

  @import_session = ImportSession.new(
    importable_type: params[:importable_type],
    user: current_user,
    tenant: tenant,
    status: "mapping"
  )

  if uploaded_file.present?
    @import_session.import_file.attach(uploaded_file)
  end

  if @import_session.save
    redirect_to column_map_admin_import_session_path(@import_session)
  else
    @importable_type = params[:importable_type]
    render :new, status: :unprocessable_entity
  end
end

#importObject



87
88
89
90
91
92
93
94
95
96
# File 'app/controllers/panda/core/admin/import_sessions_controller.rb', line 87

def import
  @import_session.update!(status: "importing")

  # Run synchronously for now — swap to CSVImportJob.perform_later for async
  CSVImportService.new(@import_session).call

  redirect_to admin_import_session_path(@import_session), success: "Import complete."
rescue => e
  redirect_to admin_import_session_path(@import_session), error: "Import failed: #{e.message}"
end

#indexObject



10
11
12
13
# File 'app/controllers/panda/core/admin/import_sessions_controller.rb', line 10

def index
  @import_sessions = scoped_import_sessions.recent
  @import_sessions = @import_sessions.where(importable_type: params[:type]) if params[:type].present?
end

#newObject



15
16
17
# File 'app/controllers/panda/core/admin/import_sessions_controller.rb', line 15

def new
  @importable_type = params[:importable_type]
end

#previewObject



81
82
83
84
85
# File 'app/controllers/panda/core/admin/import_sessions_controller.rb', line 81

def preview
  @preview_rows = @import_session.preview_rows(limit: 5)
  @mapping = @import_session.column_mapping
  @field_definitions = @import_session.column_options
end

#showObject



63
64
# File 'app/controllers/panda/core/admin/import_sessions_controller.rb', line 63

def show
end

#update_mappingObject



71
72
73
74
75
76
77
78
79
# File 'app/controllers/panda/core/admin/import_sessions_controller.rb', line 71

def update_mapping
  mapping = {}
  (params[:mapping] || {}).each do |csv_col, field_name|
    mapping[csv_col] = field_name if field_name.present?
  end

  @import_session.update!(column_mapping: mapping, status: "previewing")
  redirect_to preview_admin_import_session_path(@import_session)
end