diff --git a/app/controllers/api/v1/accounts/custom_filters_controller.rb b/app/controllers/api/v1/accounts/custom_filters_controller.rb index 188f0e623..6cd70b07d 100644 --- a/app/controllers/api/v1/accounts/custom_filters_controller.rb +++ b/app/controllers/api/v1/accounts/custom_filters_controller.rb @@ -11,6 +11,7 @@ class Api::V1::Accounts::CustomFiltersController < Api::V1::Accounts::BaseContro @custom_filter = current_user.custom_filters.create!( permitted_payload.merge(account_id: Current.account.id) ) + render json: { error: @custom_filter.errors.messages }, status: :unprocessable_entity and return unless @custom_filter.valid? end def update diff --git a/app/javascript/dashboard/routes/dashboard/customviews/AddCustomViews.vue b/app/javascript/dashboard/routes/dashboard/customviews/AddCustomViews.vue index c87c85883..1736cecad 100644 --- a/app/javascript/dashboard/routes/dashboard/customviews/AddCustomViews.vue +++ b/app/javascript/dashboard/routes/dashboard/customviews/AddCustomViews.vue @@ -98,7 +98,7 @@ export default { const errorMessage = error?.message; this.alertMessage = errorMessage || this.filterType === 0 - ? this.$t('FILTER.CUSTOM_VIEWS.ADD.API_FOLDERS.ERROR_MESSAGE') + ? errorMessage : this.$t('FILTER.CUSTOM_VIEWS.ADD.API_SEGMENTS.ERROR_MESSAGE'); } finally { this.showAlert(this.alertMessage); diff --git a/app/models/custom_filter.rb b/app/models/custom_filter.rb index e506edc5a..b3d58fd17 100644 --- a/app/models/custom_filter.rb +++ b/app/models/custom_filter.rb @@ -17,8 +17,16 @@ # index_custom_filters_on_user_id (user_id) # class CustomFilter < ApplicationRecord + MAX_FILTER_PER_USER = 50 belongs_to :user belongs_to :account enum filter_type: { conversation: 0, contact: 1, report: 2 } + validate :validate_number_of_filters + + def validate_number_of_filters + return true if account.custom_filters.where(user_id: user_id).size < MAX_FILTER_PER_USER + + errors.add :account_id, I18n.t('errors.custom_filters.number_of_records') + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 2a2437566..daa5a295c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -70,6 +70,8 @@ en: connection_closed_error: Connection closed. validations: name: should not start or end with symbols, and it should not have < > / \ @ characters. + custom_filters: + number_of_records: Limit reached. The maximum number of allowed custom filters for a user per account is 50. reports: period: Reporting period %{since} to %{until} diff --git a/spec/controllers/api/v1/accounts/custom_filters_controller_spec.rb b/spec/controllers/api/v1/accounts/custom_filters_controller_spec.rb index 73ba61859..2b0815add 100644 --- a/spec/controllers/api/v1/accounts/custom_filters_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/custom_filters_controller_spec.rb @@ -66,6 +66,8 @@ RSpec.describe 'Custom Filters API', type: :request do context 'when it is an authenticated user' do let(:user) { create(:user, account: account) } + let(:new_account) { create(:account) } + let(:new_user) { create(:user, account: new_account) } it 'creates the filter' do expect do @@ -77,6 +79,23 @@ RSpec.describe 'Custom Filters API', type: :request do json_response = response.parsed_body expect(json_response['name']).to eq 'vip-customers' end + + it 'gives the error for 51st record' do + CustomFilter::MAX_FILTER_PER_USER.times do + create(:custom_filter, user: user, account: account) + end + + expect do + post "/api/v1/accounts/#{account.id}/custom_filters", headers: user.create_new_auth_token, + params: payload + end.not_to change(CustomFilter, :count) + + expect(response).to have_http_status(:unprocessable_entity) + json_response = response.parsed_body + expect(json_response['message']).to include( + 'Account Limit reached. The maximum number of allowed custom filters for a user per account is 50.' + ) + end end end