From 94c0dc3732422d56f4ba6265cb16e825d98fcb05 Mon Sep 17 00:00:00 2001 From: bilal Date: Tue, 2 Jun 2020 22:23:19 +0200 Subject: [PATCH] Handle QrMatching response - mock --- app/jobs/match_appearance_releases_job.rb | 18 +++++------ .../appearance_release_matching.rb | 31 ++++++++++++------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/app/jobs/match_appearance_releases_job.rb b/app/jobs/match_appearance_releases_job.rb index b3fdd71..e087277 100644 --- a/app/jobs/match_appearance_releases_job.rb +++ b/app/jobs/match_appearance_releases_job.rb @@ -8,22 +8,25 @@ class MatchAppearanceReleasesJob < ApplicationJob payload = { request_id: matching_request.id, bucket: aws_bucket_name, files: attachments} response = BrayniacAI::AppearanceReleaseMatching.match_attachments payload + # BrayniacAI::QrMatching.enable_logging + # response = BrayniacAI::QrMatching.create! payload matches = response[:matches] || [] - errors = response[:errors] || [] handle_matches matches, project - handle_errors errors + matching_request.destroy end private def handle_matches(matches, project) matches.each do |match| - contract = match[:contract] - headshot = match[:headshot] + contract = match[:contracts].blank? ? nil : match[:contracts].first + headshot = match[:headshots].blank? ? nil : match[:headshots].first identifier = match[:identifier] + next if contract.nil? && headshot.nil? + identified_release = identifier.blank? ? nil : AppearanceRelease.find_by(identifier: identifier) if identified_release.nil? create_release project, contract, headshot, identifier @@ -33,13 +36,6 @@ class MatchAppearanceReleasesJob < ApplicationJob 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? diff --git a/lib/brayniac_ai/appearance_release_matching.rb b/lib/brayniac_ai/appearance_release_matching.rb index 4f89c69..4c19339 100644 --- a/lib/brayniac_ai/appearance_release_matching.rb +++ b/lib/brayniac_ai/appearance_release_matching.rb @@ -15,12 +15,17 @@ module BrayniacAI pdfs = [] images = [] matches = [] - errors = [] # Use first file for the error entry first_attachment = attachments.shift - unless first_attachment.nil? - errors << { file: first_attachment, error_code: 1 } + + if first_attachment.present? + matches << { + headshots: [], + contracts: [], + unknowns: [first_attachment], + identifier: '' + } end attachments.each do |attachment| @@ -45,18 +50,20 @@ module BrayniacAI pdf = pair_element2 image = pair_element1 end - if pdf.nil? || image.nil? - matches << { headshot: image } if pdf.nil? - matches << { contract: pdf } if image.nil? - else - matches << { headshot: image, contract: pdf, identifier: '' } - end + headshots = image.present? ? [image] : [] + contracts = pdf.present? ? [pdf] : [] + + matches << { + headshots: headshots, + contracts: contracts, + unknowns: [], + identifier: '' + } end { - request_id: request_data[:request_id], - matches: matches, - errors: errors + request_id: request_data[:request_id], + matches: matches } end end