2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-25 16:15:39 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
2018-03-25 17:30:42 +00:00
|
|
|
require 'psql'
|
|
|
|
require 'qexpr'
|
|
|
|
|
|
|
|
module QueryCustomFields
|
|
|
|
# Find all duplicate custom field joins on supporters
|
|
|
|
# Returns an array of groups of duplicate custom_field_join_ids
|
|
|
|
def self.find_dupes(np_id)
|
2019-07-30 21:29:24 +00:00
|
|
|
Qx.select('ARRAY_AGG(custom_field_joins.id)')
|
2018-03-25 17:30:42 +00:00
|
|
|
.from(:custom_field_joins)
|
2019-07-30 21:29:24 +00:00
|
|
|
.join(:custom_field_masters, 'custom_field_masters.id=custom_field_joins.custom_field_master_id')
|
|
|
|
.where('custom_field_masters.nonprofit_id=$id', id: np_id)
|
|
|
|
.group_by('custom_field_joins.custom_field_master_id', 'custom_field_joins.value', 'custom_field_joins.supporter_id')
|
|
|
|
.having('COUNT(custom_field_joins.id) > 1')
|
2018-03-25 17:30:42 +00:00
|
|
|
.execute(format: 'csv')[1..-1]
|
|
|
|
end
|
|
|
|
end
|