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 @@