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

99 lines
2.9 KiB
Ruby
Raw Normal View History

2021-10-27 15:23:28 +05:30
# frozen_string_literal: true
class CustomerRelations::Organization < ApplicationRecord
2022-07-23 23:45:48 +05:30
include Gitlab::SQL::Pattern
include Sortable
2021-11-11 11:23:49 +05:30
include StripAttribute
2021-10-27 15:23:28 +05:30
self.table_name = "customer_relations_organizations"
2021-11-18 22:05:49 +05:30
belongs_to :group, -> { where(type: Group.sti_name) }, foreign_key: 'group_id'
2021-10-27 15:23:28 +05:30
2021-11-11 11:23:49 +05:30
strip_attributes! :name
2021-10-27 15:23:28 +05:30
enum state: {
inactive: 0,
active: 1
}
validates :group, presence: true
validates :name, presence: true
validates :name, uniqueness: { case_sensitive: false, scope: [:group_id] }
validates :name, length: { maximum: 255 }
validates :description, length: { maximum: 1024 }
2022-05-07 20:08:51 +05:30
validate :validate_root_group
2021-10-27 15:23:28 +05:30
2022-10-11 01:57:18 +05:30
scope :order_scope_asc, ->(field) { order(arel_table[field].asc.nulls_last) }
scope :order_scope_desc, ->(field) { order(arel_table[field].desc.nulls_last) }
2022-07-23 23:45:48 +05:30
# Searches for organizations with a matching name or description.
#
# This method uses ILIKE on PostgreSQL
#
# query - The search query as a String
#
# Returns an ActiveRecord::Relation.
def self.search(query)
fuzzy_search(query, [:name, :description], use_minimum_char_limit: false)
end
def self.search_by_state(state)
where(state: state)
end
2022-10-11 01:57:18 +05:30
def self.sort_by_field(field, direction)
if direction == :asc
order_scope_asc(field)
else
order_scope_desc(field)
end
end
2022-07-23 23:45:48 +05:30
def self.sort_by_name
order(name: :asc)
end
2021-10-27 15:23:28 +05:30
def self.find_by_name(group_id, name)
where(group: group_id)
.where('LOWER(name) = LOWER(?)', name)
end
2022-05-07 20:08:51 +05:30
2022-06-21 17:19:12 +05:30
def self.move_to_root_group(group)
update_query = <<~SQL
UPDATE #{CustomerRelations::Contact.table_name}
SET organization_id = new_organizations.id
FROM #{table_name} AS existing_organizations
JOIN #{table_name} AS new_organizations ON new_organizations.group_id = :old_group_id AND LOWER(new_organizations.name) = LOWER(existing_organizations.name)
WHERE existing_organizations.group_id = :new_group_id AND organization_id = existing_organizations.id
SQL
2022-10-11 01:57:18 +05:30
connection.execute(sanitize_sql([update_query, old_group_id: group.root_ancestor.id, new_group_id: group.id]))
2022-06-21 17:19:12 +05:30
dupes_query = <<~SQL
DELETE FROM #{table_name} AS existing_organizations
USING #{table_name} AS new_organizations
WHERE existing_organizations.group_id = :new_group_id AND new_organizations.group_id = :old_group_id AND LOWER(new_organizations.name) = LOWER(existing_organizations.name)
SQL
2022-10-11 01:57:18 +05:30
connection.execute(sanitize_sql([dupes_query, old_group_id: group.root_ancestor.id, new_group_id: group.id]))
2022-06-21 17:19:12 +05:30
where(group: group).update_all(group_id: group.root_ancestor.id)
end
2022-10-11 01:57:18 +05:30
def self.counts_by_state
default_state_counts.merge(group(:state).count)
end
2022-05-07 20:08:51 +05:30
private
2022-10-11 01:57:18 +05:30
def self.default_state_counts
2023-03-04 22:38:38 +05:30
states.keys.index_with do |key|
0
2022-10-11 01:57:18 +05:30
end
end
2022-05-07 20:08:51 +05:30
def validate_root_group
return if group&.root?
self.errors.add(:base, _('organizations can only be added to root groups'))
end
2021-10-27 15:23:28 +05:30
end