2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2018-03-25 17:30:42 +00:00
|
|
|
require 'psql'
|
|
|
|
require 'qexpr'
|
|
|
|
require 'query/query_email_settings'
|
|
|
|
|
|
|
|
module QueryUsers
|
|
|
|
# Return all the nonprofit user emails for a given email notification setting
|
|
|
|
# for notification_type in ['payments', 'campaigns', 'events', 'payouts', 'recurring_donations']
|
|
|
|
def self.nonprofit_user_emails(np_id, notification_type)
|
2019-07-30 21:29:24 +00:00
|
|
|
raise ArgumentError, 'Invalid notification type' unless QueryEmailSettings::Settings.include?(notification_type)
|
|
|
|
|
|
|
|
Qx.select('users.email')
|
|
|
|
.from('users')
|
|
|
|
.join('roles', 'roles.user_id=users.id')
|
|
|
|
.add_join('nonprofits', "roles.host_id=nonprofits.id AND roles.host_type='Nonprofit'")
|
|
|
|
.add_left_join('email_settings', 'email_settings.user_id=users.id')
|
2018-03-25 17:30:42 +00:00
|
|
|
.where("email_settings.user_id IS NULL OR email_settings.#{notification_type}=TRUE")
|
2019-07-30 21:29:24 +00:00
|
|
|
.and_where('nonprofits.id=$id', id: np_id)
|
|
|
|
.group_by('users.email')
|
|
|
|
.execute.map { |h| h['email'] }
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return all nonprofit emails regardless of email settings
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.all_nonprofit_user_emails(np_id, roles = %i[nonprofit_admin nonprofit_user])
|
2018-03-25 17:30:42 +00:00
|
|
|
Qx.select('users.email').from('users')
|
|
|
|
.join('roles', 'roles.user_id = users.id')
|
|
|
|
.add_join('nonprofits', 'nonprofits.id = roles.host_id AND roles.host_type=\'Nonprofit\'')
|
|
|
|
.where('nonprofits.id=$id', id: np_id)
|
|
|
|
.and_where('roles.name IN ($names)', names: roles)
|
2019-07-30 21:29:24 +00:00
|
|
|
.execute.map { |h| h['email'] }
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return an array of email address strings for all users with role of 'super_admin'
|
|
|
|
def self.super_admin_emails
|
2019-07-30 21:29:24 +00:00
|
|
|
Qx.select('users.email').from('users')
|
|
|
|
.join('roles', "roles.user_id=users.id AND roles.name='super_admin'")
|
|
|
|
.ex.map { |h| h['email'] }
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|