feat: Add the support for custom attributes in message variables (#8511)

This commit is contained in:
Muhsin Keloth
2023-12-09 03:43:35 +05:30
committed by GitHub
parent bf49a17efa
commit b1a68759cf
4 changed files with 36 additions and 3 deletions

View File

@@ -18,4 +18,9 @@ class ContactDrop < BaseDrop
def last_name
@obj.try(:name).try(:split).try(:last).try(:capitalize) if @obj.try(:name).try(:split).try(:size) > 1
end
def custom_attribute
custom_attributes = @obj.try(:custom_attributes) || {}
custom_attributes.transform_keys(&:to_s)
end
end

View File

@@ -19,6 +19,11 @@ class ConversationDrop < BaseDrop
end
end
def custom_attribute
custom_attributes = @obj.try(:custom_attributes) || {}
custom_attributes.transform_keys(&:to_s)
end
private
def message_sender_name(sender)

View File

@@ -3,7 +3,7 @@ require 'rails_helper'
describe ContactDrop do
subject(:contact_drop) { described_class.new(contact) }
let!(:contact) { create(:contact) }
let!(:contact) { create(:contact, custom_attributes: { car_model: 'Tesla Model S', car_year: '2022' }) }
context 'when first name' do
it 'returns first name' do
@@ -38,4 +38,19 @@ describe ContactDrop do
expect(subject.last_name).to eq 'Doe'
end
end
context 'when accessing custom attributes' do
it 'returns the correct car model from custom attributes' do
expect(contact_drop.custom_attribute['car_model']).to eq 'Tesla Model S'
end
it 'returns the correct car year from custom attributes' do
expect(contact_drop.custom_attribute['car_year']).to eq '2022'
end
it 'returns empty hash when there are no custom attributes' do
contact.update!(custom_attributes: nil)
expect(contact_drop.custom_attribute).to eq({})
end
end
end

View File

@@ -2,8 +2,8 @@ require 'rails_helper'
shared_examples_for 'liqudable' do
context 'when liquid is present in content' do
let(:contact) { create(:contact, name: 'john', phone_number: '+912883') }
let(:conversation) { create(:conversation, id: 1, contact: contact) }
let(:contact) { create(:contact, name: 'john', phone_number: '+912883', custom_attributes: { customer_type: 'platinum' }) }
let(:conversation) { create(:conversation, id: 1, contact: contact, custom_attributes: { priority: 'high' }) }
context 'when message is incoming' do
let(:message) { build(:message, conversation: conversation, message_type: 'incoming') }
@@ -24,6 +24,14 @@ shared_examples_for 'liqudable' do
expect(message.content).to eq 'hey John how are you?'
end
it 'set replaces liquid custom attributes in message' do
message.content = 'Are you a {{contact.custom_attribute.customer_type}} customer,
If yes then the priority is {{conversation.custom_attribute.priority}}'
message.save!
expect(message.content).to eq 'Are you a platinum customer,
If yes then the priority is high'
end
it 'process liquid operators like default value' do
message.content = 'Can we send you an email at {{ contact.email | default: "default" }} ?'
message.save!