diff --git a/app/drops/contact_drop.rb b/app/drops/contact_drop.rb index 6611ecdc3..1d450adb7 100644 --- a/app/drops/contact_drop.rb +++ b/app/drops/contact_drop.rb @@ -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 diff --git a/app/drops/conversation_drop.rb b/app/drops/conversation_drop.rb index d77e150c2..d62642885 100644 --- a/app/drops/conversation_drop.rb +++ b/app/drops/conversation_drop.rb @@ -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) diff --git a/spec/drops/contact_drop_spec.rb b/spec/drops/contact_drop_spec.rb index e83ebb377..d00a0924d 100644 --- a/spec/drops/contact_drop_spec.rb +++ b/spec/drops/contact_drop_spec.rb @@ -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 diff --git a/spec/models/concerns/liquidable_shared.rb b/spec/models/concerns/liquidable_shared.rb index 7648bd15c..8df526a2f 100644 --- a/spec/models/concerns/liquidable_shared.rb +++ b/spec/models/concerns/liquidable_shared.rb @@ -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!