diff --git a/app/models/account.rb b/app/models/account.rb index 95c826779..85754a395 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -85,6 +85,7 @@ class Account < ApplicationRecord validates_with JsonSchemaValidator, schema: SETTINGS_PARAMS_SCHEMA, attribute_resolver: ->(record) { record.settings } + validate :validate_reporting_timezone store_accessor :settings, :auto_resolve_after, :auto_resolve_message, :auto_resolve_ignore_waiting @@ -215,6 +216,12 @@ class Account < ApplicationRecord # method overridden in enterprise module end + def validate_reporting_timezone + return if reporting_timezone.blank? || ActiveSupport::TimeZone[reporting_timezone].present? + + errors.add(:reporting_timezone, I18n.t('errors.account.reporting_timezone.invalid')) + end + def remove_account_sequences ActiveRecord::Base.connection.exec_query("drop sequence IF EXISTS camp_dpid_seq_#{id}") ActiveRecord::Base.connection.exec_query("drop sequence IF EXISTS conv_dpid_seq_#{id}") diff --git a/config/locales/en.yml b/config/locales/en.yml index 0c89f3e7d..5034f5567 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -48,6 +48,9 @@ en: inbox_deletetion_response: Your inbox deletion request will be processed in some time. errors: + account: + reporting_timezone: + invalid: is not a valid timezone validations: presence: must not be blank webhook: diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 5ccff6517..3da958188 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -217,6 +217,21 @@ RSpec.describe Account do expect(described_class.with_auto_resolve.pluck(:id)).not_to include(account.id) end end + + context 'when reporting_timezone is set' do + it 'allows valid timezone names' do + account.reporting_timezone = 'America/New_York' + + expect(account).to be_valid + end + + it 'rejects invalid timezone names' do + account.reporting_timezone = 'Invalid/Timezone' + + expect(account).not_to be_valid + expect(account.errors[:reporting_timezone]).to include(I18n.t('errors.account.reporting_timezone.invalid')) + end + end end describe 'captain_preferences' do