iachat/app/services/whatsapp/zapi_handlers/message_status_callback.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

41 lines
1007 B
Ruby

module Whatsapp::ZapiHandlers::MessageStatusCallback
include Whatsapp::ZapiHandlers::Helpers
private
def process_message_status_callback
status = map_zapi_status_to_chatwoot(processed_params[:status])
return unless status
processed_params[:ids].each do |message_id|
message = inbox.messages.find_by(source_id: message_id)
next unless message
message.update!(status: status) if status_transition_allowed?(message, status.to_s)
end
end
def map_zapi_status_to_chatwoot(zapi_status)
case zapi_status.upcase
when 'SENT'
:sent
when 'DELIVERED', 'RECEIVED'
:delivered
when 'READ', 'READ_BY_ME', 'PLAYED'
:read
when 'FAILED'
:failed
else
Rails.logger.warn "Unknown ZAPI status: #{zapi_status}"
nil
end
end
def status_transition_allowed?(message, new_status)
return false if message.status == 'read'
return false if message.status == 'delivered' && new_status == 'sent'
true
end
end