90 lines
2.6 KiB
Ruby
90 lines
2.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class AppearanceReleaseImportsController < ApplicationController
|
|
include AppearanceReleaseContext
|
|
include ProjectContext
|
|
include CreateReleasableJobs
|
|
|
|
before_action :set_project, only: [:create]
|
|
|
|
include ProjectLayout
|
|
|
|
def create
|
|
authorize AppearanceRelease
|
|
@failed_files = []
|
|
attachments = appearance_release_params
|
|
if attachments.nil?
|
|
alert_message = t 'appearance_releases.create.no_attachments'
|
|
else
|
|
attachments.each do |attachment|
|
|
create_imported_appearance_release attachment
|
|
end
|
|
end
|
|
|
|
unless @failed_files.empty?
|
|
alert_message = t 'appearance_releases.create.failed_import'
|
|
alert_message += '<br><ul>'
|
|
@failed_files.each { |file_name| alert_message += "<li>#{file_name}</li>" }
|
|
alert_message += '</ul>'
|
|
end
|
|
|
|
redirect_to [@project, :appearance_releases], alert: alert_message
|
|
end
|
|
|
|
private
|
|
|
|
def appearance_releases
|
|
if @project
|
|
policy_scope(@project.appearance_releases)
|
|
else
|
|
super
|
|
end
|
|
end
|
|
|
|
def appearance_release_params
|
|
params.require(:attachments)
|
|
end
|
|
|
|
def build_appearance_release(params = {})
|
|
authorize appearance_releases.build(params)
|
|
end
|
|
|
|
def log_create_analytics
|
|
TrackAnalyticsJob.perform_later(Current.user, Current.account, :track_create_non_native_release, release_type: AppearanceRelease.to_s, user_agent: request.user_agent, user_ip: request.remote_ip)
|
|
end
|
|
|
|
def create_imported_appearance_release(attachment)
|
|
blob = ActiveStorage::Blob.find_signed(attachment)
|
|
return if blob.nil?
|
|
|
|
extension = blob.filename.extension_with_delimiter
|
|
unless AppearanceRelease.acceptable_import_file_extensions.include? extension
|
|
blob.purge
|
|
@failed_files << blob.filename
|
|
return
|
|
end
|
|
|
|
random_contract_no = AppearanceRelease.random_contract_number.to_s
|
|
appearance_release_params = {
|
|
person_last_name: random_contract_no
|
|
}
|
|
|
|
if blob.image?
|
|
appearance_release_params[:person_photo] = attachment
|
|
appearance_release_params[:person_first_name] = I18n.t('appearance_releases.shared.imported_appearance_release_headshot_name')
|
|
elsif extension == '.pdf'
|
|
appearance_release_params[:contract] = attachment
|
|
appearance_release_params[:person_first_name] = I18n.t('appearance_releases.shared.imported_appearance_release_contract_name')
|
|
end
|
|
|
|
appearance_release = build_appearance_release(appearance_release_params)
|
|
|
|
if appearance_release.save(context: :non_native)
|
|
log_create_analytics
|
|
after_create appearance_release
|
|
else
|
|
@failed_files << blob.filename
|
|
end
|
|
end
|
|
end
|