Feat: Add Null values at the last while sorting (#3292)
* Add Null values at the last while sorting * Add contacts at last with special character in it * Optimize SQL order and direction
This commit is contained in:
@@ -50,6 +50,59 @@ class Contact < ApplicationRecord
|
||||
after_update_commit :dispatch_update_event
|
||||
after_destroy_commit :dispatch_destroy_event
|
||||
|
||||
scope :order_on_last_activity_at, lambda { |direction|
|
||||
order(
|
||||
Arel::Nodes::SqlLiteral.new(
|
||||
sanitize_sql_for_order("\"contacts\".\"last_activity_at\" #{direction}
|
||||
NULLS LAST")
|
||||
)
|
||||
)
|
||||
}
|
||||
scope :order_on_company_name, lambda { |direction|
|
||||
order(
|
||||
Arel::Nodes::SqlLiteral.new(
|
||||
sanitize_sql_for_order(
|
||||
"\"contacts\".\"additional_attributes\"->>'company_name' #{direction}
|
||||
NULLS LAST"
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
scope :order_on_city, lambda { |direction|
|
||||
order(
|
||||
Arel::Nodes::SqlLiteral.new(
|
||||
sanitize_sql_for_order(
|
||||
"\"contacts\".\"additional_attributes\"->>'city' #{direction}
|
||||
NULLS LAST"
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
scope :order_on_country_name, lambda { |direction|
|
||||
order(
|
||||
Arel::Nodes::SqlLiteral.new(
|
||||
sanitize_sql_for_order(
|
||||
"\"contacts\".\"additional_attributes\"->>'country' #{direction}
|
||||
NULLS LAST"
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
scope :order_on_name, lambda { |direction|
|
||||
order(
|
||||
Arel::Nodes::SqlLiteral.new(
|
||||
sanitize_sql_for_order(
|
||||
"CASE
|
||||
WHEN \"contacts\".\"name\" ~~* '^+\d*' THEN 'z'
|
||||
WHEN \"contacts\".\"name\" ~~* '^\b*' THEN 'z'
|
||||
ELSE LOWER(\"contacts\".\"name\")
|
||||
END #{direction}"
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
def get_source_id(inbox_id)
|
||||
contact_inboxes.find_by!(inbox_id: inbox_id).source_id
|
||||
end
|
||||
@@ -79,6 +132,12 @@ class Contact < ApplicationRecord
|
||||
}
|
||||
end
|
||||
|
||||
def self.resolved_contacts
|
||||
where.not(email: [nil, '']).or(
|
||||
Current.account.contacts.where.not(phone_number: [nil, ''])
|
||||
).or(Current.account.contacts.where.not(identifier: [nil, '']))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ip_lookup
|
||||
|
||||
Reference in New Issue
Block a user