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 'param_validation'
|
|
|
|
require 'qx'
|
|
|
|
|
|
|
|
module OnboardAccounts
|
|
|
|
def self.create_org(params)
|
|
|
|
nonprofit_data = set_nonprofit_defaults(params['nonprofit'])
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new(nonprofit_data,
|
|
|
|
name: { required: true },
|
|
|
|
# email: {required: true},
|
|
|
|
# phone: {required: true},
|
|
|
|
city: { required: true },
|
|
|
|
state_code: { required: true })
|
2018-03-25 17:30:42 +00:00
|
|
|
user_data = set_user_defaults(params['user'])
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new(user_data,
|
|
|
|
name: { required: true },
|
|
|
|
email: { required: true },
|
|
|
|
password: { required: true },
|
|
|
|
phone: { required: true })
|
2018-03-25 17:30:42 +00:00
|
|
|
extra_info = params['extraInfo']
|
|
|
|
|
|
|
|
nonprofit = Qx.insert_into(:nonprofits)
|
2019-07-30 21:29:24 +00:00
|
|
|
.values(nonprofit_data).timestamps
|
|
|
|
.returning('*')
|
|
|
|
.execute.last
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2020-06-10 22:31:47 +00:00
|
|
|
billing_plan_id = Houdini.default_bp.id
|
2018-03-25 17:30:42 +00:00
|
|
|
billing_subscription = Qx.insert_into(:billing_subscriptions)
|
2019-07-30 21:29:24 +00:00
|
|
|
.values(
|
|
|
|
nonprofit_id: nonprofit['id'],
|
|
|
|
billing_plan_id: billing_plan_id,
|
|
|
|
status: 'active'
|
|
|
|
)
|
|
|
|
.timestamps.execute.last
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
# Create the user using the User and Role models (since we have to use Devise)
|
|
|
|
user = User.create!(user_data)
|
|
|
|
role = Qx.insert_into(:roles)
|
2019-07-30 21:29:24 +00:00
|
|
|
.values(user_id: user.id, name: 'nonprofit_admin', host_id: nonprofit['id'], host_type: 'Nonprofit')
|
|
|
|
.timestamps
|
|
|
|
.execute.last
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
delay.send_onboard_email(nonprofit, nonprofit_data, user_data, extra_info)
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
{
|
2018-03-25 17:30:42 +00:00
|
|
|
nonprofit: nonprofit,
|
|
|
|
user: user,
|
|
|
|
role: role,
|
|
|
|
billing_subscription: billing_subscription
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
### ethis is a one time method in order to add a user without testing for the method. Do not use this long term
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.create_org_with_user(params, user = nil)
|
2018-03-25 17:30:42 +00:00
|
|
|
nonprofit_data = set_nonprofit_defaults(params['nonprofit'])
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new(nonprofit_data,
|
|
|
|
name: { required: true },
|
|
|
|
# email: {required: true},
|
|
|
|
# phone: {required: true},
|
|
|
|
city: { required: true },
|
|
|
|
state_code: { required: true })
|
|
|
|
unless user
|
2018-03-25 17:30:42 +00:00
|
|
|
user_data = set_user_defaults(params['user'])
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new(user_data,
|
|
|
|
name: { required: true },
|
|
|
|
email: { required: true },
|
|
|
|
password: { required: true },
|
|
|
|
phone: { required: true })
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
extra_info = params['extraInfo']
|
|
|
|
|
|
|
|
nonprofit = Qx.insert_into(:nonprofits)
|
2019-07-30 21:29:24 +00:00
|
|
|
.values(nonprofit_data).timestamps
|
|
|
|
.returning('*')
|
|
|
|
.execute.last
|
2018-03-25 17:30:42 +00:00
|
|
|
# Create a billing subscription for the 6% fee tier
|
2020-06-10 22:31:47 +00:00
|
|
|
billing_plan_id = Houdini.default_bp.id
|
2018-03-25 17:30:42 +00:00
|
|
|
billing_subscription = Qx.insert_into(:billing_subscriptions)
|
2019-07-30 21:29:24 +00:00
|
|
|
.values(
|
|
|
|
nonprofit_id: nonprofit['id'],
|
|
|
|
billing_plan_id: billing_plan_id,
|
|
|
|
status: 'active'
|
|
|
|
)
|
|
|
|
.timestamps.execute.last
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
# Create the user using the User and Role models (since we have to use Devise)
|
|
|
|
user = !user ? User.create!(user_data) : user
|
|
|
|
role = Qx.insert_into(:roles)
|
2019-07-30 21:29:24 +00:00
|
|
|
.values(user_id: user.id, name: 'nonprofit_admin', host_id: nonprofit['id'], host_type: 'Nonprofit')
|
|
|
|
.timestamps
|
|
|
|
.execute.last
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
delay.send_onboard_email(nonprofit, nonprofit_data, user_data, extra_info)
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
{
|
|
|
|
nonprofit: nonprofit,
|
|
|
|
user: user,
|
|
|
|
role: role,
|
|
|
|
billing_subscription: billing_subscription
|
2018-03-25 17:30:42 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_nonprofit_defaults(data)
|
2019-07-30 21:29:24 +00:00
|
|
|
data = data.merge(
|
2018-03-25 17:30:42 +00:00
|
|
|
verification_status: 'unverified',
|
|
|
|
published: true,
|
2020-06-10 22:31:47 +00:00
|
|
|
vetted: Houdini.nonprofits_must_be_vetted ? false : true,
|
2018-03-25 17:30:42 +00:00
|
|
|
statement: data[:name][0..16],
|
|
|
|
city_slug: Format::Url.convert_to_slug(data[:city]),
|
|
|
|
state_code_slug: Format::Url.convert_to_slug(data[:state_code]),
|
|
|
|
slug: Format::Url.convert_to_slug(data[:name])
|
2019-07-30 21:29:24 +00:00
|
|
|
)
|
2018-03-25 17:30:42 +00:00
|
|
|
data
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_user_defaults(data)
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
|
|
|
# np is the created nonprofit row in the database
|
|
|
|
# nonprofit_data is the data sent from the onboarding form, and may include extra stuff not in the db
|
|
|
|
# user_data and extra_info are additional data hashes sent from the onboarding form
|
|
|
|
def self.send_onboard_email(np, nonprofit_data, user_data, extra_info)
|
|
|
|
# Send the welcome email to the nonprofit
|
2019-11-07 19:03:04 +00:00
|
|
|
NonprofitCreateJob.perform_later(Nonprofit.find(np['id']))
|
2018-03-25 17:30:42 +00:00
|
|
|
# Send an email notifying people internal folks of the new nonporfit, with the above info and extra_info
|
|
|
|
to_emails = ['support@commitchange.com']
|
2019-07-30 21:29:24 +00:00
|
|
|
message = %(
|
2018-03-25 17:30:42 +00:00
|
|
|
New signup on CommitChange for an organization with the name "#{np['name']}"
|
|
|
|
Location: #{np['city']} #{np['state_code']}, #{np['zip_code']}
|
|
|
|
Org Email: #{nonprofit_data['email']}
|
|
|
|
Org Phone: #{nonprofit_data['phone']}
|
|
|
|
User Email: #{user_data['email']}
|
|
|
|
User Name: #{user_data['name']}
|
|
|
|
User Phone: #{user_data['phone']}
|
|
|
|
Entity Type: #{extra_info['entity_type']}
|
|
|
|
How they heard about us: #{extra_info['how_they_heard']}
|
2019-07-30 21:29:24 +00:00
|
|
|
What they want to use: #{%w[use_donations use_crm use_campaigns use_events].select { |x| extra_info[x] == 'on' }.join(', ')}
|
2018-03-25 17:30:42 +00:00
|
|
|
)
|
|
|
|
subject = "New Account Signup: #{np['name']}"
|
|
|
|
GenericMailer.generic_mail('support@commitchange.com', 'CC Bot', message, subject, to_emails, '').deliver
|
|
|
|
end
|
|
|
|
end
|