feat: Add additional attributes in setUser method (#3958)

This commit is contained in:
Muhsin Keloth
2022-02-28 12:10:55 +05:30
committed by GitHub
parent 9b615f11f1
commit 46c1734ba1
4 changed files with 55 additions and 10 deletions

View File

@@ -52,12 +52,11 @@ class ContactIdentifyAction
end
def update_contact
custom_attributes = params[:custom_attributes] ? @contact.custom_attributes.merge(params[:custom_attributes]) : @contact.custom_attributes
# blank identifier or email will throw unique index error
# TODO: replace reject { |_k, v| v.blank? } with compact_blank when rails is upgraded
@contact.update!(params.slice(:name, :email, :identifier, :phone_number).reject do |_k, v|
v.blank?
end.merge({ custom_attributes: custom_attributes }))
end.merge({ custom_attributes: custom_attributes, additional_attributes: additional_attributes }))
ContactAvatarJob.perform_later(@contact, params[:avatar_url]) if params[:avatar_url].present?
end
@@ -68,4 +67,16 @@ class ContactIdentifyAction
mergee_contact: merge_contact
).perform
end
def custom_attributes
params[:custom_attributes] ? @contact.custom_attributes.deep_merge(params[:custom_attributes].stringify_keys) : @contact.custom_attributes
end
def additional_attributes
if params[:additional_attributes]
@contact.additional_attributes.deep_merge(params[:additional_attributes].stringify_keys)
else
@contact.additional_attributes
end
end
end

View File

@@ -46,6 +46,7 @@ class Api::V1::Widget::ContactsController < Api::V1::Widget::BaseController
end
def permitted_params
params.permit(:website_token, :identifier, :identifier_hash, :email, :name, :avatar_url, :phone_number, custom_attributes: {})
params.permit(:website_token, :identifier, :identifier_hash, :email, :name, :avatar_url, :phone_number, custom_attributes: {},
additional_attributes: {})
end
end

View File

@@ -24,17 +24,38 @@ export const actions = {
},
update: async ({ dispatch }, { identifier, user: userObject }) => {
try {
const {
email,
name,
avatar_url,
identifier_hash,
phone_number,
company_name,
city,
country_code,
description,
custom_attributes,
social_profiles,
} = userObject;
const user = {
email: userObject.email,
name: userObject.name,
avatar_url: userObject.avatar_url,
identifier_hash: userObject.identifier_hash,
phone_number: userObject.phone_number,
email,
name,
avatar_url,
identifier_hash,
phone_number,
additional_attributes: {
company_name,
city,
description,
country_code,
social_profiles,
},
custom_attributes,
};
await ContactsAPI.update(identifier, user);
dispatch('get');
if (userObject.identifier_hash) {
if (identifier_hash) {
dispatch('conversation/clearConversations', {}, { root: true });
dispatch('conversation/fetchOldConversations', {}, { root: true });
}