chore: Add custom attributes in campaign.triggered event (#4463)

This commit is contained in:
Muhsin Keloth 2022-04-20 10:49:52 +05:30 committed by GitHub
parent 5b9c4bf7f1
commit 4f3a271355
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 34 additions and 16 deletions

View file

@ -1,5 +1,5 @@
class Campaigns::CampaignConversationBuilder
pattr_initialize [:contact_inbox_id!, :campaign_display_id!, :conversation_additional_attributes]
pattr_initialize [:contact_inbox_id!, :campaign_display_id!, :conversation_additional_attributes, :custom_attributes]
def perform
@contact_inbox = ContactInbox.find(@contact_inbox_id)
@ -32,7 +32,8 @@ class Campaigns::CampaignConversationBuilder
contact_id: @contact_inbox.contact_id,
contact_inbox_id: @contact_inbox.id,
campaign_id: @campaign.id,
additional_attributes: conversation_additional_attributes
additional_attributes: conversation_additional_attributes,
custom_attributes: custom_attributes || {}
}
end
end

View file

@ -155,13 +155,14 @@ export default {
this.replaceRoute('prechat-form');
} else {
this.replaceRoute('messages');
bus.$emit('execute-campaign', this.activeCampaign.id);
bus.$emit('execute-campaign', { campaignId: this.activeCampaign.id });
}
this.unsetUnreadView();
});
bus.$on('execute-campaign', campaignId => {
bus.$on('execute-campaign', campaignDetails => {
const { customAttributes, campaignId } = campaignDetails;
const { websiteToken } = window.chatwootWebChannel;
this.executeCampaign({ campaignId, websiteToken });
this.executeCampaign({ campaignId, websiteToken, customAttributes });
this.replaceRoute('messages');
});
},

View file

@ -7,9 +7,16 @@ const getCampaigns = async websiteToken => {
return result;
};
const triggerCampaign = async ({ campaignId, websiteToken }) => {
const urlData = endPoints.triggerCampaign({ websiteToken, campaignId });
const triggerCampaign = async ({
campaignId,
websiteToken,
customAttributes,
}) => {
const urlData = endPoints.triggerCampaign({
websiteToken,
campaignId,
customAttributes,
});
await API.post(
urlData.url,
{ ...urlData.data },
@ -18,5 +25,4 @@ const triggerCampaign = async ({ campaignId, websiteToken }) => {
}
);
};
export { getCampaigns, triggerCampaign };

View file

@ -78,12 +78,13 @@ const getCampaigns = token => ({
website_token: token,
},
});
const triggerCampaign = ({ websiteToken, campaignId }) => ({
const triggerCampaign = ({ websiteToken, campaignId, customAttributes }) => ({
url: '/api/v1/widget/events',
data: {
name: 'campaign.triggered',
event_info: {
campaign_id: campaignId,
custom_attributes: customAttributes,
...generateEventParams(),
},
},

View file

@ -92,14 +92,17 @@ export const actions = {
}
},
executeCampaign: async ({ commit }, { campaignId, websiteToken }) => {
executeCampaign: async (
{ commit },
{ campaignId, websiteToken, customAttributes }
) => {
try {
commit(
'conversation/setConversationUIFlag',
{ isCreating: true },
{ root: true }
);
await triggerCampaign({ campaignId, websiteToken });
await triggerCampaign({ campaignId, websiteToken, customAttributes });
commit('setCampaignExecuted', true);
commit('setActiveCampaign', {});
} catch (error) {

View file

@ -46,7 +46,10 @@ export default {
conversationCustomAttributes,
}) {
if (activeCampaignId) {
bus.$emit('execute-campaign', activeCampaignId);
bus.$emit('execute-campaign', {
campaignId: activeCampaignId,
customAttributes: conversationCustomAttributes,
});
this.$store.dispatch('contacts/update', {
user: {
email: emailAddress,

View file

@ -2,13 +2,15 @@ class CampaignListener < BaseListener
def campaign_triggered(event)
contact_inbox = event.data[:contact_inbox]
campaign_display_id = event.data[:event_info][:campaign_id]
custom_attributes = event.data[:event_info][:custom_attributes]
return if campaign_display_id.blank?
::Campaigns::CampaignConversationBuilder.new(
contact_inbox_id: contact_inbox.id,
campaign_display_id: campaign_display_id,
conversation_additional_attributes: event.data[:event_info].except(:campaign_id)
conversation_additional_attributes: event.data[:event_info].except(:campaign_id, :custom_attributes),
custom_attributes: custom_attributes
).perform
end
end

View file

@ -9,7 +9,7 @@ describe CampaignListener do
let!(:event) do
Events::Base.new('campaign_triggered', Time.zone.now,
contact_inbox: contact_inbox, event_info: { campaign_id: campaign.display_id })
contact_inbox: contact_inbox, event_info: { campaign_id: campaign.display_id, custom_attributes: { order_id: 321 } })
end
describe '#campaign_triggered' do
@ -23,7 +23,8 @@ describe CampaignListener do
context 'when params contain campaign id' do
it 'triggers campaign conversation builder' do
expect(Campaigns::CampaignConversationBuilder).to receive(:new)
.with({ contact_inbox_id: contact_inbox.id, campaign_display_id: campaign.display_id, conversation_additional_attributes: {} }).once
.with({ contact_inbox_id: contact_inbox.id, campaign_display_id: campaign.display_id, conversation_additional_attributes: {},
custom_attributes: { order_id: 321 } }).once
listener.campaign_triggered(event)
end
end