From 76370267f3cc7a9dbef1066239210915fd733bd1 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Thu, 11 Nov 2021 15:23:33 +0530 Subject: [PATCH] feat: Render contact custom attributes in contact/conversation sidebar (#3310) --- .rubocop.yml | 1 + .../api/v1/accounts/contacts_controller.rb | 8 +- ...custom_attribute_definitions_controller.rb | 4 +- app/javascript/dashboard/api/attributes.js | 4 +- app/javascript/dashboard/api/contacts.js | 6 + .../dashboard/api/specs/contacts.spec.js | 10 ++ .../dashboard/components/CustomAttribute.vue | 28 ++++- .../dashboard/components/layout/Sidebar.vue | 1 + .../i18n/locale/en/attributesMgmt.json | 38 ++++--- .../dashboard/i18n/locale/en/contact.json | 2 +- .../dashboard/i18n/locale/en/settings.json | 2 +- .../dashboard/i18n/sidebarItems/settings.js | 6 +- .../dashboard/mixins/attributeMixin.js | 43 +++++-- .../mixins/specs/attributeMixin.spec.js | 107 +++++++++++++++--- .../contacts/components/ContactInfoPanel.vue | 19 +++- .../dashboard/conversation/ContactPanel.vue | 22 +++- .../CustomAttributeDropDown.vue | 7 +- .../CustomAttributeSelector.vue | 28 +++-- .../customAttributes/CustomAttributes.vue | 36 ++++-- .../settings/attributes/AddAttribute.vue | 44 ++++--- .../settings/attributes/CustomAttribute.vue | 4 + .../settings/attributes/EditAttribute.vue | 32 +++--- .../settings/attributes/attributes.routes.js | 2 +- .../settings/attributes/constants.js | 4 + .../dashboard/store/modules/attributes.js | 4 +- .../store/modules/contacts/actions.js | 12 ++ .../modules/specs/contacts/actions.spec.js | 20 ++++ app/models/custom_attribute_definition.rb | 1 + app/policies/contact_policy.rb | 4 + .../destroy_custom_attributes.json.jbuilder | 3 + config/routes.rb | 7 +- .../v1/accounts/contacts_controller_spec.rb | 29 +++++ ...m_attribute_definitions_controller_spec.rb | 2 +- 33 files changed, 416 insertions(+), 124 deletions(-) create mode 100644 app/views/api/v1/accounts/contacts/destroy_custom_attributes.json.jbuilder diff --git a/.rubocop.yml b/.rubocop.yml index 76ef5fcfc..b1c69ec70 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -14,6 +14,7 @@ Metrics/ClassLength: - 'app/mailers/conversation_reply_mailer.rb' - 'app/models/message.rb' - 'app/builders/messages/facebook/message_builder.rb' + - 'app/controllers/api/v1/accounts/contacts_controller.rb' RSpec/ExampleLength: Max: 25 Style/Documentation: diff --git a/app/controllers/api/v1/accounts/contacts_controller.rb b/app/controllers/api/v1/accounts/contacts_controller.rb index b795a8417..e22341d90 100644 --- a/app/controllers/api/v1/accounts/contacts_controller.rb +++ b/app/controllers/api/v1/accounts/contacts_controller.rb @@ -12,7 +12,7 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController before_action :check_authorization before_action :set_current_page, only: [:index, :active, :search] - before_action :fetch_contact, only: [:show, :update, :destroy, :contactable_inboxes] + before_action :fetch_contact, only: [:show, :update, :destroy, :contactable_inboxes, :destroy_custom_attributes] before_action :set_include_contact_inboxes, only: [:index, :search] def index @@ -64,6 +64,12 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController @contactable_inboxes = @all_contactable_inboxes.select { |contactable_inbox| policy(contactable_inbox[:inbox]).show? } end + # TODO : refactor this method into dedicated contacts/custom_attributes controller class and routes + def destroy_custom_attributes + @contact.custom_attributes = @contact.custom_attributes.excluding(params[:custom_attributes]) + @contact.save! + end + def create ActiveRecord::Base.transaction do @contact = Current.account.contacts.new(contact_params) diff --git a/app/controllers/api/v1/accounts/custom_attribute_definitions_controller.rb b/app/controllers/api/v1/accounts/custom_attribute_definitions_controller.rb index e02c090db..13cba46ea 100644 --- a/app/controllers/api/v1/accounts/custom_attribute_definitions_controller.rb +++ b/app/controllers/api/v1/accounts/custom_attribute_definitions_controller.rb @@ -25,9 +25,7 @@ class Api::V1::Accounts::CustomAttributeDefinitionsController < Api::V1::Account private def fetch_custom_attributes_definitions - @custom_attribute_definitions = Current.account.custom_attribute_definitions.where( - attribute_model: permitted_params[:attribute_model] || DEFAULT_ATTRIBUTE_MODEL - ) + @custom_attribute_definitions = Current.account.custom_attribute_definitions.with_attribute_model(permitted_params[:attribute_model]) end def fetch_custom_attribute_definition diff --git a/app/javascript/dashboard/api/attributes.js b/app/javascript/dashboard/api/attributes.js index 56eb5da76..3552bb909 100644 --- a/app/javascript/dashboard/api/attributes.js +++ b/app/javascript/dashboard/api/attributes.js @@ -6,8 +6,8 @@ class AttributeAPI extends ApiClient { super('custom_attribute_definitions', { accountScoped: true }); } - getAttributesByModel(modelId) { - return axios.get(`${this.url}?attribute_model=${modelId}`); + getAttributesByModel() { + return axios.get(this.url); } } diff --git a/app/javascript/dashboard/api/contacts.js b/app/javascript/dashboard/api/contacts.js index a6415cb37..08c969248 100644 --- a/app/javascript/dashboard/api/contacts.js +++ b/app/javascript/dashboard/api/contacts.js @@ -60,6 +60,12 @@ class ContactAPI extends ApiClient { headers: { 'Content-Type': 'multipart/form-data' }, }); } + + destroyCustomAttributes(contactId, customAttributes) { + return axios.post(`${this.url}/${contactId}/destroy_custom_attributes`, { + custom_attributes: customAttributes, + }); + } } export default new ContactAPI(); diff --git a/app/javascript/dashboard/api/specs/contacts.spec.js b/app/javascript/dashboard/api/specs/contacts.spec.js index 03a71ca11..08e6720d7 100644 --- a/app/javascript/dashboard/api/specs/contacts.spec.js +++ b/app/javascript/dashboard/api/specs/contacts.spec.js @@ -60,6 +60,16 @@ describe('#ContactsAPI', () => { ); }); + it('#destroyCustomAttributes', () => { + contactAPI.destroyCustomAttributes(1, ['cloudCustomer']); + expect(context.axiosMock.post).toHaveBeenCalledWith( + '/api/v1/contacts/1/destroy_custom_attributes', + { + custom_attributes: ['cloudCustomer'], + } + ); + }); + it('#importContacts', () => { const file = 'file'; contactAPI.importContacts(file); diff --git a/app/javascript/dashboard/components/CustomAttribute.vue b/app/javascript/dashboard/components/CustomAttribute.vue index 0f5afbb88..5fe724342 100644 --- a/app/javascript/dashboard/components/CustomAttribute.vue +++ b/app/javascript/dashboard/components/CustomAttribute.vue @@ -1,5 +1,5 @@