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 'i18n'
|
|
|
|
|
|
|
|
module InsertSupporter
|
2021-01-19 20:00:23 +00:00
|
|
|
def self.create_or_update(nonprofit, data, update = false)
|
2019-07-30 21:29:24 +00:00
|
|
|
address_keys = %w[name address city country state_code]
|
2018-03-25 17:30:42 +00:00
|
|
|
custom_fields = data['customFields']
|
|
|
|
data = HashWithIndifferentAccess.new(Format::RemoveDiacritics.from_hash(data, address_keys))
|
2019-07-30 21:29:24 +00:00
|
|
|
.except(:customFields)
|
|
|
|
|
|
|
|
supporter = Qx.select('*').from(:supporters)
|
|
|
|
.where('name = $n AND email = $e', n: data[:name], e: data[:email])
|
2021-01-19 20:00:23 +00:00
|
|
|
.and_where('nonprofit_id=$id', id: nonprofit.id)
|
2019-07-30 21:29:24 +00:00
|
|
|
.and_where('coalesce(deleted, FALSE)=FALSE')
|
|
|
|
.execute.last
|
|
|
|
if supporter && update
|
2018-03-25 17:30:42 +00:00
|
|
|
supporter = Qx.update(:supporters)
|
2019-07-30 21:29:24 +00:00
|
|
|
.set(defaults(data))
|
|
|
|
.where('id=$id', id: supporter['id'])
|
|
|
|
.returning('*')
|
|
|
|
.timestamps
|
|
|
|
.execute.last
|
|
|
|
else
|
2021-01-19 20:00:23 +00:00
|
|
|
supporter = nonprofit.supporters.create(defaults(data))
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
if custom_fields
|
2021-01-19 20:00:23 +00:00
|
|
|
InsertCustomFieldJoins.find_or_create(nonprofit.id, [supporter.id], custom_fields)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
supporter
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.defaults(h)
|
2018-03-25 17:30:42 +00:00
|
|
|
h = h.except('profile_id') unless h['profile_id'].present?
|
2019-07-30 21:29:24 +00:00
|
|
|
if h['first_name'].present? || h['last_name'].present?
|
|
|
|
h['name'] = h['first_name'] || h['last_name']
|
|
|
|
if h['first_name'] && h['last_name']
|
|
|
|
h['name'] = "#{h['first_name'].strip} #{h['last_name'].strip}"
|
|
|
|
end
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
h['email_unsubscribe_uuid'] = SecureRandom.uuid
|
|
|
|
|
|
|
|
if h['address'].present? && h['address_line2'].present?
|
|
|
|
h['address'] += ' ' + h['address_line2']
|
|
|
|
end
|
|
|
|
|
|
|
|
h = h.except('address_line2')
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
h
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
# pass in a hash of supporter info, as well as
|
|
|
|
# any property with tag_x will create a tag with name 'name'
|
|
|
|
# any property with field_x will create a field with name 'x' and value set
|
|
|
|
# eg:
|
|
|
|
# {
|
|
|
|
# 'name' => 'Bob Ross',
|
|
|
|
# 'email' => 'bob@happytrees.org',
|
|
|
|
# 'tag_xy' => true,
|
|
|
|
# 'field_xy' => 420
|
|
|
|
# }
|
|
|
|
# The above will create a supporter with name/email, one tag with name 'xy',
|
|
|
|
# and one field with name 'xy' and value 420
|
|
|
|
def self.with_tags_and_fields(np_id, data)
|
2019-07-30 21:29:24 +00:00
|
|
|
tags = data.select { |key, _val| key.match(/^tag_/) }.map { |key, _val| key.gsub('tag_', '') }
|
|
|
|
fields = data.select { |key, _val| key.match(/^field_/) }.map { |key, val| [key.gsub('field_', ''), val] }
|
|
|
|
supp_cols = data.select { |key, _val| !key.match(/^field_/) && !key.match(/^tag_/) }
|
2018-03-25 17:30:42 +00:00
|
|
|
supporter = create_or_update(np_id, supp_cols)
|
|
|
|
|
2019-11-07 21:48:13 +00:00
|
|
|
InsertTagJoins.find_or_create(np_id, [supporter['id']], tags) if tags.any?
|
|
|
|
InsertCustomFieldJoins.find_or_create(np_id, [supporter['id']], fields) if fields.any?
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
supporter
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|