Sync of the branch
This commit is contained in:
79
app/jobs/match_appearance_releases_job.rb
Normal file
79
app/jobs/match_appearance_releases_job.rb
Normal file
@@ -0,0 +1,79 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class MatchAppearanceReleasesJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
def perform(project, attachments)
|
||||
matching_request = MatchingRequest.create project: project, attachments: attachments
|
||||
|
||||
payload = { request_id: matching_request.id, bucket: aws_bucket_name, files: attachments}
|
||||
response = BrayniacAI::AppearanceReleaseMatching.match_attachments payload
|
||||
|
||||
matches = response[:matches] || []
|
||||
errors = response[:errors] || []
|
||||
|
||||
handle_matches matches, project
|
||||
handle_errors errors
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def handle_matches(matches, project)
|
||||
matches.each do |match|
|
||||
contract = match[:contract]
|
||||
headshot = match[:headshot]
|
||||
identifier = match[:identifier]
|
||||
|
||||
identified_release = identifier.blank? ? nil : AppearanceRelease.find_by(identifier: identifier)
|
||||
if identified_release.nil?
|
||||
create_release project, contract, headshot, identifier
|
||||
else
|
||||
update_release identified_release, contract, headshot
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def handle_errors(errors)
|
||||
logger.error "== MATCHING ERRORS ==" unless errors.empty?
|
||||
errors.each do |error|
|
||||
logger.error "[#{error[:error_code]}] #{error[:file]}"
|
||||
end
|
||||
end
|
||||
|
||||
def create_release(project, contract, headshot, identifier)
|
||||
random_contract_no = AppearanceRelease.random_contract_number.to_s
|
||||
is_incomplete = contract.nil? || headshot.nil?
|
||||
params = {
|
||||
project: project,
|
||||
person_first_name: appearance_first_name(is_incomplete),
|
||||
person_last_name: random_contract_no
|
||||
}
|
||||
|
||||
params[:person_photo] = headshot unless headshot.nil?
|
||||
params[:contract] = contract unless contract.nil?
|
||||
params[:identifier] = identifier unless blank?
|
||||
|
||||
return if AppearanceRelease.create(params)
|
||||
|
||||
logger.error "Failed to create AppearanceRelease with params : \r\n#{params}"
|
||||
end
|
||||
|
||||
def update_release(release, contract, headshot)
|
||||
release.contract = contract unless contract.nil?
|
||||
release.person_photo = headshot unless headshot.nil?
|
||||
|
||||
release.save
|
||||
end
|
||||
|
||||
def appearance_first_name(incomplete)
|
||||
if incomplete
|
||||
I18n.t('appearance_releases.shared.incomplete_match')
|
||||
else
|
||||
I18n.t('appearance_releases.shared.matched_import')
|
||||
end
|
||||
end
|
||||
|
||||
def aws_bucket_name
|
||||
ENV.fetch 'AWS_BUCKET'
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user