Class: CustomerRelations::Contact

Inherits:
ApplicationRecord show all
Includes:
Gitlab::SQL::Pattern, Sortable, StripAttribute
Defined in:
app/models/customer_relations/contact.rb

Constant Summary collapse

SAFE_ATTRIBUTES =
%w[
  created_at
  description
  first_name
  group_id
  id
  last_name
  organization_id
  state
  updated_at
].freeze

Constants included from Gitlab::SQL::Pattern

Gitlab::SQL::Pattern::MIN_CHARS_FOR_PARTIAL_MATCHING, Gitlab::SQL::Pattern::REGEX_QUOTED_TERM

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StripAttribute

#strip_attributes!

Methods included from Gitlab::SQL::Pattern

split_query_to_search_terms

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

.counts_by_stateObject



149
150
151
# File 'app/models/customer_relations/contact.rb', line 149

def self.counts_by_state
  group(:state).count
end

.exists_for_group?(group) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
# File 'app/models/customer_relations/contact.rb', line 123

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

  exists?(group: group)
end

.find_ids_by_emails(group, emails) ⇒ Object

Raises:

  • (ArgumentError)


117
118
119
120
121
# File 'app/models/customer_relations/contact.rb', line 117

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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/customer_relations/contact.rb', line 129

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



62
63
64
# File 'app/models/customer_relations/contact.rb', line 62

def self.reference_postfix
  ']'
end

.reference_prefixObject



54
55
56
# File 'app/models/customer_relations/contact.rb', line 54

def self.reference_prefix
  '[contact:'
end

.reference_prefix_quotedObject



58
59
60
# File 'app/models/customer_relations/contact.rb', line 58

def self.reference_prefix_quoted
  '["contact:'
end

.search(query) ⇒ Object

Searches for contacts with a matching first name, last name, email or description.

This method uses ILIKE on PostgreSQL

query - The search query as a String

Returns an ActiveRecord::Relation.



73
74
75
# File 'app/models/customer_relations/contact.rb', line 73

def self.search(query)
  fuzzy_search(query, [:first_name, :last_name, :email, :description], use_minimum_char_limit: false)
end

.search_by_state(state) ⇒ Object



77
78
79
# File 'app/models/customer_relations/contact.rb', line 77

def self.search_by_state(state)
  where(state: state)
end

.sort_by_field(field, direction) ⇒ Object



81
82
83
84
85
86
87
# File 'app/models/customer_relations/contact.rb', line 81

def self.sort_by_field(field, direction)
  if direction == :asc
    order_scope_asc(field)
  else
    order_scope_desc(field)
  end
end

.sort_by_nameObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/customer_relations/contact.rb', line 97

def self.sort_by_name
  order(Gitlab::Pagination::Keyset::Order.build(
          [
            Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
              attribute_name: 'last_name',
              order_expression: arel_table[:last_name].asc,
              distinct: false
            ),
            Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
              attribute_name: 'first_name',
              order_expression: arel_table[:first_name].asc,
              distinct: false
            ),
            Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
              attribute_name: 'id',
              order_expression: arel_table[:id].asc
            )
          ]))
end

.sort_by_organization(direction) ⇒ Object



89
90
91
92
93
94
95
# File 'app/models/customer_relations/contact.rb', line 89

def self.sort_by_organization(direction)
  if direction == :asc
    order_by_organization_asc
  else
    order_by_organization_desc
  end
end

Instance Method Details

#hook_attrsObject



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

def hook_attrs
  attributes.slice(*SAFE_ATTRIBUTES)
end