debian-mirror-gitlab/app/models/customer_relations/issue_contact.rb

45 lines
1.4 KiB
Ruby
Raw Normal View History

2021-12-11 22:18:48 +05:30
# frozen_string_literal: true
class CustomerRelations::IssueContact < ApplicationRecord
self.table_name = "issue_customer_relations_contacts"
belongs_to :issue, optional: false, inverse_of: :customer_relations_contacts
belongs_to :contact, optional: false, inverse_of: :issue_contacts
2022-05-07 20:08:51 +05:30
validate :contact_belongs_to_root_group
2021-12-11 22:18:48 +05:30
2022-06-21 17:19:12 +05:30
BATCH_DELETE_SIZE = 1_000
2022-01-26 12:08:38 +05:30
def self.find_contact_ids_by_emails(issue_id, emails)
raise ArgumentError, "Cannot lookup more than #{MAX_PLUCK} emails" if emails.length > MAX_PLUCK
joins(:contact)
.where(issue_id: issue_id, customer_relations_contacts: { email: emails })
.pluck(:contact_id)
end
2022-04-04 11:22:00 +05:30
def self.delete_for_project(project_id)
2022-06-21 17:19:12 +05:30
loop do
deleted_records = joins(:issue).where(issues: { project_id: project_id }).limit(BATCH_DELETE_SIZE).delete_all
break if deleted_records == 0
end
end
def self.delete_for_group(group)
loop do
deleted_records = joins(issue: :project).where(projects: { namespace: group.self_and_descendants }).limit(BATCH_DELETE_SIZE).delete_all
break if deleted_records == 0
end
2022-04-04 11:22:00 +05:30
end
2021-12-11 22:18:48 +05:30
private
2022-05-07 20:08:51 +05:30
def contact_belongs_to_root_group
2021-12-11 22:18:48 +05:30
return unless contact&.group_id
return unless issue&.project&.namespace_id
2022-05-07 20:08:51 +05:30
return if issue.project.root_ancestor&.id == contact.group_id
2021-12-11 22:18:48 +05:30
2022-05-07 20:08:51 +05:30
errors.add(:base, _("The contact does not belong to the issue group's root ancestor"))
2021-12-11 22:18:48 +05:30
end
end