From 4d9bf3d54e3f0c81ed7c00854a2132c635c5ca37 Mon Sep 17 00:00:00 2001 From: icyleaf Date: Mon, 23 Sep 2024 02:13:33 +0000 Subject: [PATCH] fix: can not register iOS device to apple developer (#1662) --- app/controllers/udid_controller.rb | 8 ++++---- app/models/apple_key.rb | 20 +++++++++++--------- app/views/udid/_register_form.html.slim | 2 ++ config/locales/zealot/en.yml | 6 ++++++ config/locales/zealot/zh-CN.yml | 6 ++++++ 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/app/controllers/udid_controller.rb b/app/controllers/udid_controller.rb index dd8c453e1..40bc6a312 100644 --- a/app/controllers/udid_controller.rb +++ b/app/controllers/udid_controller.rb @@ -54,10 +54,10 @@ def register name = [ 'Zealot', platform, SecureRandom.hex(4) ].compact.join('-') if name.blank? # Max 50 chars new_device = apple_key.register_device(udid, name, platform) - unless new_device.valid? - logger.debug "Register failed with errors: #{new_device.errors}" - error_message = new_device.errors.messages[:devices][0] - flash[:alter] = error_message if error_message.present? + if new_device.errors.present? + logger.debug "Register failed with errors: #{new_device.errors.full_messages}" + error_message = new_device.errors.messages[:name].join(' / ') + flash[:warn] = error_message if error_message.present? return render :show, status: :unprocessable_entity end diff --git a/app/models/apple_key.rb b/app/models/apple_key.rb index ffdfc5929..d27341e7d 100644 --- a/app/models/apple_key.rb +++ b/app/models/apple_key.rb @@ -39,11 +39,11 @@ def sync_devices end def register_device(udid, name = nil, platform = 'IOS') - remote_device = client.device(uuid: uuid) + remote_device = client.device(udid: udid) db_device = Device.find_by(udid: udid) return db_device if remote_device && db_device - if remtoe_device && !db_device + if remote_device && !db_device devices << remote_device return remote_device end @@ -62,33 +62,35 @@ def register_device(udid, name = nil, platform = 'IOS') message = e.errors[0]['detail'] is_exists = message.include?('already exists') - # udid had registered, force sync device + # udid had registered, but not exists in zealot system, needs to force sync device if is_exists sync_devices return self end + invaild_device = Device.new # invaild udid if message.include?('invalid value') # This is never happen, never ever! - errors.add(:devices, :invalid_value, value: udid) + invaild_device.errors.add(:name, :invalid_value, value: udid) else - errors.add(:devices, :api, message: message) + invaild_device.errors.add(:name, :api, message: message) end - self + invaild_device rescue => e logger.error "Register device raise an exception: #{e}" logger.error e.backtrace.join("\n") message = e.respond_to?(:errors) ? errors[0]['detail'] : e.message - errors.add(:devices, :unknown, message: message) - self + invaild_device = Device.new + invaild_device.errors.add(:name, :unknown, message: message) + invaild_device end def update_device_name(device) - response_device = client.rename_device(device.name, id: device.device_id, udid: device.udid) + client.rename_device(device.name, id: device.device_id, udid: device.udid) rescue TinyAppstoreConnect::InvalidEntityError => e logger.error "Device may not exists or the other error in apple key #{id}: #{e}" end diff --git a/app/views/udid/_register_form.html.slim b/app/views/udid/_register_form.html.slim index 432677045..39fb64152 100644 --- a/app/views/udid/_register_form.html.slim +++ b/app/views/udid/_register_form.html.slim @@ -7,6 +7,8 @@ .card-tools .card-body = f.error_notification + - unless f.object.errors.empty? + = debug f.object.errors = f.input :name = f.input :udid, as: :hidden, input_html: { value: udid } = f.input :platform, as: :hidden, input_html: { value: product } diff --git a/config/locales/zealot/en.yml b/config/locales/zealot/en.yml index 0c37e8ca8..40015d7a6 100644 --- a/config/locales/zealot/en.yml +++ b/config/locales/zealot/en.yml @@ -1018,6 +1018,12 @@ en: invalid_value: Register device %{value} was invalid. api: 'Apple return error: %{message}' unknown: 'Unknown error: %{message}' + udid: + attributes: + name: + invalid_value: Register device %{value} was invalid. + api: 'Apple return error: %{message}' + unknown: 'Unknown error: %{message}' debug_file: attributes: app_id: diff --git a/config/locales/zealot/zh-CN.yml b/config/locales/zealot/zh-CN.yml index dbdab4713..23f385336 100644 --- a/config/locales/zealot/zh-CN.yml +++ b/config/locales/zealot/zh-CN.yml @@ -1018,6 +1018,12 @@ zh-CN: invalid_value: 注册设备 %{value} 是无效的请检测后重新尝试 api: '苹果返回错误:%{message}' unknown: '未知错误: %{message}' + udid: + attributes: + name: + invalid_value: 注册设备 %{value} 是无效的请检测后重新尝试 + api: '苹果返回错误:%{message}' + unknown: '未知错误: %{message}' debug_file: attributes: app_id: