feat(lifecycle): add Captain::Lifecycle::Config model
This commit is contained in:
parent
1c89ef73ff
commit
6ee3fcd4ef
4
enterprise/app/models/captain/lifecycle.rb
Normal file
4
enterprise/app/models/captain/lifecycle.rb
Normal file
@ -0,0 +1,4 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Captain::Lifecycle
|
||||
end
|
||||
16
enterprise/app/models/captain/lifecycle/config.rb
Normal file
16
enterprise/app/models/captain/lifecycle/config.rb
Normal file
@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Captain::Lifecycle::Config < ApplicationRecord
|
||||
self.table_name = 'captain_lifecycle_configs'
|
||||
|
||||
belongs_to :account
|
||||
belongs_to :opt_out_label, class_name: 'Label', optional: true
|
||||
|
||||
validates :account_id, uniqueness: true
|
||||
validates :min_interval_minutes, numericality: { greater_than_or_equal_to: 0 }
|
||||
validates :pause_on_customer_reply_within_minutes, numericality: { greater_than_or_equal_to: 0 }
|
||||
|
||||
def self.for_account(account)
|
||||
find_or_create_by!(account_id: account.id)
|
||||
end
|
||||
end
|
||||
37
spec/enterprise/models/captain/lifecycle/config_spec.rb
Normal file
37
spec/enterprise/models/captain/lifecycle/config_spec.rb
Normal file
@ -0,0 +1,37 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Captain::Lifecycle::Config, type: :model do
|
||||
describe 'associations' do
|
||||
it { is_expected.to belong_to(:account) }
|
||||
it { is_expected.to belong_to(:opt_out_label).class_name('Label').optional }
|
||||
end
|
||||
|
||||
describe 'validations' do
|
||||
subject { build(:captain_lifecycle_config) }
|
||||
|
||||
it { is_expected.to validate_uniqueness_of(:account_id) }
|
||||
it { is_expected.to validate_numericality_of(:min_interval_minutes).is_greater_than_or_equal_to(0) }
|
||||
it { is_expected.to validate_numericality_of(:pause_on_customer_reply_within_minutes).is_greater_than_or_equal_to(0) }
|
||||
end
|
||||
|
||||
describe '.for_account' do
|
||||
let(:account) { create(:account) }
|
||||
|
||||
it 'returns existing config when present' do
|
||||
config = create(:captain_lifecycle_config, account: account)
|
||||
expect(described_class.for_account(account)).to eq(config)
|
||||
end
|
||||
|
||||
it 'creates default config when missing' do
|
||||
expect { described_class.for_account(account) }
|
||||
.to change(described_class, :count).by(1)
|
||||
end
|
||||
|
||||
it 'defaults have quiet_hours disabled' do
|
||||
config = described_class.for_account(account)
|
||||
expect(config.quiet_hours_enabled).to be(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
13
spec/factories/captain_lifecycle_configs.rb
Normal file
13
spec/factories/captain_lifecycle_configs.rb
Normal file
@ -0,0 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
FactoryBot.define do
|
||||
factory :captain_lifecycle_config, class: 'Captain::Lifecycle::Config' do
|
||||
account
|
||||
quiet_hours_enabled { false }
|
||||
quiet_hours_from { '23:00' }
|
||||
quiet_hours_to { '08:00' }
|
||||
min_interval_minutes { 30 }
|
||||
pause_on_customer_reply { false }
|
||||
pause_on_customer_reply_within_minutes { 60 }
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user