diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-08-18 08:17:02 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-08-18 08:17:02 +0000 |
commit | b39512ed755239198a9c294b6a45e65c05900235 (patch) | |
tree | d234a3efade1de67c46b9e5a38ce813627726aa7 /app/models/customer_relations | |
parent | d31474cf3b17ece37939d20082b07f6657cc79a9 (diff) | |
download | gitlab-ce-b39512ed755239198a9c294b6a45e65c05900235.tar.gz |
Add latest changes from gitlab-org/gitlab@15-3-stable-eev15.3.0-rc42
Diffstat (limited to 'app/models/customer_relations')
-rw-r--r-- | app/models/customer_relations/contact.rb | 26 | ||||
-rw-r--r-- | app/models/customer_relations/contact_state_counts.rb | 42 |
2 files changed, 68 insertions, 0 deletions
diff --git a/app/models/customer_relations/contact.rb b/app/models/customer_relations/contact.rb index 0f13c45b84d..f6455da890b 100644 --- a/app/models/customer_relations/contact.rb +++ b/app/models/customer_relations/contact.rb @@ -29,6 +29,12 @@ class CustomerRelations::Contact < ApplicationRecord validate :validate_email_format validate :validate_root_group + scope :order_scope_asc, ->(field) { order(arel_table[field].asc.nulls_last) } + scope :order_scope_desc, ->(field) { order(arel_table[field].desc.nulls_last) } + + scope :order_by_organization_asc, -> { includes(:organization).order("customer_relations_organizations.name ASC NULLS LAST") } + scope :order_by_organization_desc, -> { includes(:organization).order("customer_relations_organizations.name DESC NULLS LAST") } + def self.reference_prefix '[contact:' end @@ -56,6 +62,22 @@ class CustomerRelations::Contact < ApplicationRecord where(state: state) end + def self.sort_by_field(field, direction) + if direction == :asc + order_scope_asc(field) + else + order_scope_desc(field) + end + end + + def self.sort_by_organization(direction) + if direction == :asc + order_by_organization_asc + else + order_by_organization_desc + end + end + def self.sort_by_name order(Gitlab::Pagination::Keyset::Order.build([ Gitlab::Pagination::Keyset::ColumnOrderDefinition.new( @@ -115,6 +137,10 @@ class CustomerRelations::Contact < ApplicationRecord where(group: group).update_all(group_id: group.root_ancestor.id) end + def self.counts_by_state + group(:state).count + end + private def validate_email_format diff --git a/app/models/customer_relations/contact_state_counts.rb b/app/models/customer_relations/contact_state_counts.rb new file mode 100644 index 00000000000..31c95e166bb --- /dev/null +++ b/app/models/customer_relations/contact_state_counts.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +module CustomerRelations + class ContactStateCounts + include Gitlab::Utils::StrongMemoize + + attr_reader :group + + def self.declarative_policy_class + 'CustomerRelations::ContactPolicy' + end + + def initialize(current_user, group, params) + @current_user = current_user + @group = group + @params = params + end + + # Define method for each state + ::CustomerRelations::Contact.states.each_key do |state| + define_method(state) { counts[state] } + end + + def all + counts.values.sum + end + + private + + attr_reader :current_user, :params + + def counts + strong_memoize(:counts) do + Hash.new(0).merge(counts_by_state) + end + end + + def counts_by_state + ::Crm::ContactsFinder.counts_by_state(current_user, params.merge({ group: group })) + end + end +end |