Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add creation of location and org profile to create org service #277

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/organization_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class OrganizationProfile < ApplicationRecord

before_save :normalize_phone

validates :phone_number, phone: {possible: true}
validates :phone_number, phone: {possible: true, allow_blank: true}

private

Expand Down
57 changes: 45 additions & 12 deletions app/services/organizations/create_service.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
# class to create a new organization, user, staff account with role admin, and send email via the console
# class to create a new location, organization, organization profile, user, and staff account with role admin
# email is sent to admin user if all steps are successful
# call with Organizations::CreateService.new.signal(args)
# args:
# sample args:
# {
# organization_name: 'value',
# organization_slug: 'value',
# user_email: 'value',
# user_first_name: 'value',
# user_last_name: 'value'
# location: {
# country: 'Mexico',
# city_town: 'La Ventana',
# province_state: 'Baja'
# },
# organization: {
# name: 'Baja Pet Rescue',
# slug: 'baja'
# },
# user: {
# email: '[email protected]',
# first_name: 'Jimmy',
# last_name: 'Hendrix'
# }
# }

class Organizations::CreateService
def signal(args)
ActiveRecord::Base.transaction do
create_organization(args[:organization_name], args[:organization_slug])
create_user(args[:user_email], args[:user_first_name], args[:user_last_name])
create_location(
args[:location][:country],
args[:location][:city_town],
args[:location][:province_state]
)
create_organization_and_profile(
args[:organization][:name],
args[:organization][:slug]
)
create_user(
args[:user][:email],
args[:user][:first_name],
args[:user][:last_name]
)
create_staff_account
add_admin_role_to_staff_account
send_email
Expand All @@ -24,10 +46,21 @@ def signal(args)

private

def create_organization(name, slug)
def create_location(country, city_town, province_state)
@location = Location.create!(
country: country,
city_town: city_town,
province_state: province_state
)
end

def create_organization_and_profile(name, slug)
@organization = Organization.create!(
name: name,
slug: slug
slug: slug,
profile: OrganizationProfile.new(
location_id: @location.id
)
)
end

Expand All @@ -54,7 +87,7 @@ def create_staff_account
end

def add_admin_role_to_staff_account
# @staff_account.add_role(:admin)
@staff_account.add_role(:admin)

if !@staff_account.has_role?(:admin)
raise StandardError, "Failed to add admin role"
Expand Down