iachat/spec/factories/channel/channel_whatsapp.rb
Cayo P. R. Oliveira a3effacc21
Feat: improve read receipts (#56)
* feat: add store in additional_attributes to conversation model

* feat: set default mark_as_read to true in additional_attributes for conversation params

* fix: remove mark_as_read from additional_attributes in conversation

* refactor: move message read logic to mark_messages_as_read method in ConversationsController

* feat: add 'Mark messages as read' option to WhatsApp channel settings

* feat: add 'mark_as_read' option in factorie  for WhatsApp channel

* feat: integrate Checkbox component for 'mark as read' option in WhatsApp settings

* fix: ensure 'mark_as_read' option is included in channel creation payload

* feat: add 'Mark as read' settings for WhatsApp inbox, including UI and state management

* chore: remove redundant content update test from IncomingMessageBaileysService spec

* feat: update 'Mark as read' label to 'Read receipts' and replace Checkbox with Switch component in WhatsApp settings

* fix: handle potential nil value for 'mark_as_read' in provider config

* feat: refactor provider config to streamline channel creation with 'mark_as_read' option

* feat: add test for MESSAGE_READ event dispatch in update_last_seen action

* fix: update subheader for 'Mark as read' setting to read receipt behavior in WhatsApp

* feat: update mark_messages_as_read behavior to handle false value in provider config

* chore: update label for 'Mark as read' option

* feat: update update_last_seen to send WhatsApp read receipt for WhatsApp channels

* test: remove MESSAGE_READ event dispatch tests in wrong file

* feat: enhance update_last_seen behavior for WhatsApp channel to conditionally dispatch messages.read event

* feat: update update_last_seen to dispatch messages.read event

* test: refactor update_last_seen tests

* chore: refactor to ensure provider_service responds to read_messages before ensure the mark_as_read provider config

* test: enhance #read_messages with provider config mark_as_read expected behaviors

* chore: clarify test names and remove useless expect

---------

Co-authored-by: gabrieljablonski <contact@gabrieljablonski.com>
2025-06-03 23:28:15 -03:00

92 lines
4.4 KiB
Ruby

FactoryBot.define do
factory :channel_whatsapp, class: 'Channel::Whatsapp' do
sequence(:phone_number) { |n| "+123456789#{n}1" }
account
provider_config { { 'api_key' => 'test_key', 'phone_number_id' => 'random_id' } }
message_templates do
[{ 'name' => 'sample_shipping_confirmation',
'status' => 'approved',
'category' => 'SHIPPING_UPDATE',
'language' => 'id',
'namespace' => '2342384942_32423423_23423fdsdaf',
'components' =>
[{ 'text' => 'Paket Anda sudah dikirim. Paket akan sampai dalam {{1}} hari kerja.', 'type' => 'BODY' },
{ 'text' => 'Pesan ini berasal dari bisnis yang tidak terverifikasi.', 'type' => 'FOOTER' }],
'rejected_reason' => 'NONE' },
{ 'name' => 'customer_yes_no',
'status' => 'approved',
'category' => 'SHIPPING_UPDATE',
'language' => 'ar',
'namespace' => '2342384942_32423423_23423fdsdaf23',
'components' =>
[{ 'text' => 'عميلنا العزيز الرجاء الرد على هذه الرسالة بكلمة *نعم* للرد على إستفساركم من قبل خدمة العملاء.',
'type' => 'BODY' }],
'rejected_reason' => 'NONE' },
{ 'name' => 'sample_shipping_confirmation',
'status' => 'approved',
'category' => 'SHIPPING_UPDATE',
'language' => 'en_US',
'namespace' => '23423423_2342423_324234234_2343224',
'components' =>
[{ 'text' => 'Your package has been shipped. It will be delivered in {{1}} business days.', 'type' => 'BODY' },
{ 'text' => 'This message is from an unverified business.', 'type' => 'FOOTER' }],
'rejected_reason' => 'NONE' },
{
'name' => 'ticket_status_updated',
'status' => 'APPROVED',
'category' => 'UTILITY',
'language' => 'en',
'components' => [
{ 'text' => "Hello {{name}}, Your support ticket with ID: \#{{ticket_id}} has been updated by the support agent.",
'type' => 'BODY',
'example' => { 'body_text_named_params' => [
{ 'example' => 'John', 'param_name' => 'name' },
{ 'example' => '2332', 'param_name' => 'ticket_id' }
] } }
],
'sub_category' => 'CUSTOM',
'parameter_format' => 'NAMED'
},
{
'name' => 'ticket_status_updated',
'status' => 'APPROVED',
'category' => 'UTILITY',
'language' => 'en_US',
'components' => [
{ 'text' => "Hello {{last_name}}, Your support ticket with ID: \#{{ticket_id}} has been updated by the support agent.",
'type' => 'BODY',
'example' => { 'body_text_named_params' => [
{ 'example' => 'Dale', 'param_name' => 'last_name' },
{ 'example' => '2332', 'param_name' => 'ticket_id' }
] } }
],
'sub_category' => 'CUSTOM',
'parameter_format' => 'NAMED'
}]
end
message_templates_last_updated { Time.now.utc }
transient do
sync_templates { true }
validate_provider_config { true }
end
before(:create) do |channel_whatsapp, options|
# since factory already has the required message templates, we just need to bypass it getting updated
channel_whatsapp.define_singleton_method(:sync_templates) { nil } unless options.sync_templates
channel_whatsapp.define_singleton_method(:validate_provider_config) { nil } unless options.validate_provider_config
if channel_whatsapp.provider == 'baileys'
channel_whatsapp.provider_config = channel_whatsapp.provider_config.merge({ 'api_key' => 'test_key', 'provider_url' => 'https://baileys.api',
'phone_number_id' => '123456789', 'mark_as_read' => true })
elsif channel_whatsapp.provider == 'whatsapp_cloud'
channel_whatsapp.provider_config = channel_whatsapp.provider_config.merge({ 'api_key' => 'test_key', 'phone_number_id' => '123456789',
'business_account_id' => '123456789' })
end
end
after(:create) do |channel_whatsapp|
create(:inbox, channel: channel_whatsapp, account: channel_whatsapp.account)
end
end
end