feat: validate sender before creating campaign (#10934)

This commit is contained in:
Shivam Mishra
2025-02-20 16:15:03 +05:30
committed by GitHub
parent 9631b0b929
commit 7fcb0d6880
2 changed files with 32 additions and 0 deletions

View File

@@ -37,6 +37,8 @@ class Campaign < ApplicationRecord
validate :validate_campaign_inbox
validate :validate_url
validate :prevent_completed_campaign_from_update, on: :update
validate :sender_must_belong_to_account
belongs_to :account
belongs_to :inbox
belongs_to :sender, class_name: 'User', optional: true
@@ -90,6 +92,14 @@ class Campaign < ApplicationRecord
errors.add(:url, 'invalid') if inbox.inbox_type == 'Website' && !use_http_protocol
end
def sender_must_belong_to_account
return unless sender
return if account.users.exists?(id: sender.id)
errors.add(:sender_id, 'must belong to the same account as the campaign')
end
def prevent_completed_campaign_from_update
errors.add :status, 'The campaign is already completed' if !campaign_status_changed? && completed?
end