From 898057c80909fcfdbd4bf996695332374bc9bba6 Mon Sep 17 00:00:00 2001 From: Ben R Date: Sun, 15 Oct 2023 15:43:53 -0700 Subject: [PATCH 1/3] allow blank phone numbers on org profile, and add creation of location and profile in create org service. --- app/models/organization_profile.rb | 2 +- app/services/organizations/create_service.rb | 61 +++++++++++++++----- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/app/models/organization_profile.rb b/app/models/organization_profile.rb index 1a276a8bc..f693657b6 100644 --- a/app/models/organization_profile.rb +++ b/app/models/organization_profile.rb @@ -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 diff --git a/app/services/organizations/create_service.rb b/app/services/organizations/create_service.rb index e693aea19..1a31c8504 100644 --- a/app/services/organizations/create_service.rb +++ b/app/services/organizations/create_service.rb @@ -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: -# { -# organization_name: 'value', -# organization_slug: 'value', -# user_email: 'value', -# user_first_name: 'value', -# user_last_name: 'value' -# } +# sample args: +{ + location: { + country: 'Mexico', + city_town: 'La Ventana', + province_state: 'Baja' + }, + organization: { + name: 'Baja Pet Rescue', + slug: 'baja' + }, + user: { + email: 'test@test.lol', + 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 @@ -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 @@ -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" From c17ed8c488c4be578857a924b490b48507d89065 Mon Sep 17 00:00:00 2001 From: Ben R Date: Sun, 15 Oct 2023 15:47:19 -0700 Subject: [PATCH 2/3] comment out sample args --- app/services/organizations/create_service.rb | 32 ++++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/app/services/organizations/create_service.rb b/app/services/organizations/create_service.rb index 1a31c8504..f3c379077 100644 --- a/app/services/organizations/create_service.rb +++ b/app/services/organizations/create_service.rb @@ -2,22 +2,22 @@ # email is sent to admin user if all steps are successful # call with Organizations::CreateService.new.signal(args) # sample args: -{ - location: { - country: 'Mexico', - city_town: 'La Ventana', - province_state: 'Baja' - }, - organization: { - name: 'Baja Pet Rescue', - slug: 'baja' - }, - user: { - email: 'test@test.lol', - first_name: 'Jimmy', - last_name: 'Hendrix' - } -} +# { +# location: { +# country: 'Mexico', +# city_town: 'La Ventana', +# province_state: 'Baja' +# }, +# organization: { +# name: 'Baja Pet Rescue', +# slug: 'baja' +# }, +# user: { +# email: 'test@test.lol', +# first_name: 'Jimmy', +# last_name: 'Hendrix' +# } +# } class Organizations::CreateService def signal(args) From 27695ed157be3d800cd110aeaa79bb6b36819803 Mon Sep 17 00:00:00 2001 From: Ben R Date: Sun, 15 Oct 2023 16:27:26 -0700 Subject: [PATCH 3/3] lint fix --- app/models/organization_profile.rb | 2 +- app/services/organizations/create_service.rb | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/models/organization_profile.rb b/app/models/organization_profile.rb index f693657b6..61c25db9a 100644 --- a/app/models/organization_profile.rb +++ b/app/models/organization_profile.rb @@ -29,7 +29,7 @@ class OrganizationProfile < ApplicationRecord before_save :normalize_phone - validates :phone_number, phone: { possible: true, allow_blank: true } + validates :phone_number, phone: {possible: true, allow_blank: true} private diff --git a/app/services/organizations/create_service.rb b/app/services/organizations/create_service.rb index f3c379077..76a045385 100644 --- a/app/services/organizations/create_service.rb +++ b/app/services/organizations/create_service.rb @@ -15,7 +15,7 @@ # user: { # email: 'test@test.lol', # first_name: 'Jimmy', -# last_name: 'Hendrix' +# last_name: 'Hendrix' # } # } @@ -28,12 +28,12 @@ def signal(args) args[:location][:province_state] ) create_organization_and_profile( - args[:organization][:name], + args[:organization][:name], args[:organization][:slug] ) create_user( - args[:user][:email], - args[:user][:first_name], + args[:user][:email], + args[:user][:first_name], args[:user][:last_name] ) create_staff_account @@ -48,7 +48,7 @@ def signal(args) def create_location(country, city_town, province_state) @location = Location.create!( - country: country, + country: country, city_town: city_town, province_state: province_state ) @@ -59,7 +59,7 @@ def create_organization_and_profile(name, slug) name: name, slug: slug, profile: OrganizationProfile.new( - location_id: @location.id, + location_id: @location.id ) ) end