Class: CustomerRelations::Contact

Inherits:
ApplicationRecord show all
Includes:
StripAttribute
Defined in:
app/models/customer_relations/contact.rb

Constant Summary

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Class Method Summary collapse

Methods included from StripAttribute

#strip_attributes!

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.exists_for_group?(group) ⇒ Boolean

Returns:

  • (Boolean)

48
49
50
51
52
# File 'app/models/customer_relations/contact.rb', line 48

def self.exists_for_group?(group)
  return false unless group

  exists?(group: group)
end

.find_ids_by_emails(group, emails) ⇒ Object

Raises:

  • (ArgumentError)

42
43
44
45
46
# File 'app/models/customer_relations/contact.rb', line 42

def self.find_ids_by_emails(group, emails)
  raise ArgumentError, "Cannot lookup more than #{MAX_PLUCK} emails" if emails.length > MAX_PLUCK

  where(group: group).where('lower(email) in (?)', emails.map(&:downcase)).pluck(:id)
end

.move_to_root_group(group) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/customer_relations/contact.rb', line 54

def self.move_to_root_group(group)
  update_query = <<~SQL
    UPDATE #{CustomerRelations::IssueContact.table_name}
    SET contact_id = new_contacts.id
    FROM #{table_name} AS existing_contacts
    JOIN #{table_name} AS new_contacts ON new_contacts.group_id = :old_group_id AND LOWER(new_contacts.email) = LOWER(existing_contacts.email)
    WHERE existing_contacts.group_id = :new_group_id AND contact_id = existing_contacts.id
  SQL
  connection.execute(sanitize_sql([
    update_query,
    old_group_id: group.root_ancestor.id,
    new_group_id: group.id
    ]))

  dupes_query = <<~SQL
    DELETE FROM #{table_name} AS existing_contacts
    USING #{table_name} AS new_contacts
    WHERE existing_contacts.group_id = :new_group_id AND new_contacts.group_id = :old_group_id AND LOWER(new_contacts.email) = LOWER(existing_contacts.email)
  SQL
  connection.execute(sanitize_sql([
    dupes_query,
    old_group_id: group.root_ancestor.id,
    new_group_id: group.id
    ]))

  where(group: group).update_all(group_id: group.root_ancestor.id)
end

.reference_postfixObject


38
39
40
# File 'app/models/customer_relations/contact.rb', line 38

def self.reference_postfix
  ']'
end

.reference_prefixObject


30
31
32
# File 'app/models/customer_relations/contact.rb', line 30

def self.reference_prefix
  '[contact:'
end

.reference_prefix_quotedObject


34
35
36
# File 'app/models/customer_relations/contact.rb', line 34

def self.reference_prefix_quoted
  '["contact:'
end