iachat/app/jobs/channels/whatsapp/zapi_qr_code_job.rb
Gabriel Jablonski 4fc80ba4ee
feat(zapi): Z-API integration (#115)
* feat(zapi): connect flow and UI updates

* fix(zapi): re-add manage connection section

* feat(zapi): reply

* feat: send message

* fix: qrcode job logic

* test: qr code job specs

* chore: concurrent index

* chore: whatsapp model minor

* test: message window service specs

* chore: service refactor

* test: zapi service

* chore: zapi beta

* chore: minor fixes

* test: incoming message specs

* chore: minor fixes

* feat: handle status transitions

* test: refactor spec

* test: refactor spec

* chore(z-api): use feature flag

* chore: fix migration name
2025-10-15 16:23:04 -03:00

33 lines
1.0 KiB
Ruby

class Channels::Whatsapp::ZapiQrCodeJob < ApplicationJob
queue_as :default
def perform(whatsapp_channel, attempt = 1)
return if attempt == 1 && whatsapp_channel.provider_connection['connection'] != 'close'
return if attempt > 1 && whatsapp_channel.provider_connection['connection'] != 'connecting'
if attempt > 3
whatsapp_channel.update_provider_connection!(connection: 'close')
return
end
fetch_and_update_qr_code(whatsapp_channel)
self.class.set(wait: 30.seconds).perform_later(whatsapp_channel, attempt + 1)
end
private
def fetch_and_update_qr_code(whatsapp_channel)
service = Whatsapp::Providers::WhatsappZapiService.new(whatsapp_channel: whatsapp_channel)
qr_code = service.qr_code_image
return if qr_code.blank?
# NOTE: Avoid race condition.
return if whatsapp_channel.reload.provider_connection['connection'] == 'open'
whatsapp_channel.update_provider_connection!(
connection: 'connecting',
qr_data_url: qr_code
)
end
end