diff --git a/README.md b/README.md index aed4da610..e24561d42 100644 --- a/README.md +++ b/README.md @@ -294,7 +294,7 @@ You can start your local development environment by running the following comman docker compose up ``` -Refer to `db/seeds.rb` for credentials on how to login using the seed users. +Refer to `lib/seed/constants.rb` for credentials on how to login using the seed users. Below are some common commands you can use via `just`: diff --git a/db/seeds.rb b/db/seeds.rb index c069b4606..58581b0ba 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,94 +1,3 @@ # rubocop:disable Rails/Output -puts "Seeding database..." - -# Admin user -# ---------- -ADMIN_FULL_NAME = "Admin User" -ADMIN_PREFERRED_NAME = "Admin" -ADMIN_EMAIL = "admin@tramline.app" -ADMIN_PASSWORD = "why aroma enclose startup" - -admin_user = lambda do - email_authentication = Accounts::EmailAuthentication.find_or_initialize_by(email: ADMIN_EMAIL) - admin = true - - unless email_authentication.persisted? - user = Accounts::User.find_or_create_by!(full_name: ADMIN_FULL_NAME, preferred_name: ADMIN_PREFERRED_NAME, admin:, unique_authn_id: ADMIN_EMAIL) - email_authentication.update!(password: ADMIN_PASSWORD, confirmed_at: DateTime.now, user:) - end - - puts "Added/updated admin user." -end - -# Owner user -# -------------- -OWNER_FULL_NAME = "Owner User" -OWNER_PREFERRED_NAME = "Owner" -OWNER_EMAIL = "owner@tramline.app" -OWNER_PASSWORD = "why aroma enclose startup" - -owner_user = lambda do - email_authentication = Accounts::EmailAuthentication.find_or_initialize_by(email: OWNER_EMAIL) - - if email_authentication.persisted? - user = email_authentication.user - else - user = Accounts::User.find_or_create_by!(full_name: OWNER_FULL_NAME, preferred_name: OWNER_PREFERRED_NAME, unique_authn_id: OWNER_EMAIL) - email_authentication.update!(password: OWNER_PASSWORD, confirmed_at: DateTime.now, user:) - email_authentication.reload - end - - organization = Accounts::Organization.find_or_create_by!( - name: "Tramline Test 1 (Owner)", - status: Accounts::Organization.statuses[:active], - created_by: email_authentication.email - ) - - Accounts::Membership.find_or_create_by!( - user:, - organization:, - role: Accounts::Membership.roles[:owner] - ) - - puts "Added/updated owner user." -end - -# Developer user -# -------------- -DEVELOPER_FULL_NAME = "Developer User" -DEVELOPER_PREFERRED_NAME = "Developer" -DEVELOPER_EMAIL = "developer@tramline.app" -DEVELOPER_PASSWORD = "why aroma enclose startup" - -developer_user = lambda do - email_authentication = Accounts::EmailAuthentication.find_or_initialize_by(email: DEVELOPER_EMAIL) - - if email_authentication.persisted? - user = email_authentication.user - else - user = Accounts::User.find_or_create_by!(full_name: DEVELOPER_FULL_NAME, preferred_name: DEVELOPER_PREFERRED_NAME, unique_authn_id: DEVELOPER_EMAIL) - email_authentication.update!(password: DEVELOPER_PASSWORD, confirmed_at: DateTime.now, user:) - email_authentication.reload - end - - organization = Accounts::Organization.find_or_create_by!( - name: "Tramline Test 1 (Developer)", - status: Accounts::Organization.statuses[:active], - created_by: email_authentication.email - ) - - Accounts::Membership.find_or_create_by!( - user:, - organization:, - role: Accounts::Membership.roles[:developer] - ) - - puts "Added/updated developer user." -end - -ActiveRecord::Base.transaction do - admin_user.call - owner_user.call - developer_user.call -end +Seed::DevStarter.call # rubocop:enable Rails/Output diff --git a/lib/seed/constants.rb b/lib/seed/constants.rb new file mode 100644 index 000000000..cbd647dc3 --- /dev/null +++ b/lib/seed/constants.rb @@ -0,0 +1,21 @@ +module Seed + module Constants + # admin constants + ADMIN_FULL_NAME = "Admin User" + ADMIN_PREFERRED_NAME = "Admin" + ADMIN_EMAIL = "admin@tramline.app" + ADMIN_PASSWORD = "why aroma enclose startup" + + # owner constants + OWNER_FULL_NAME = "Owner User" + OWNER_PREFERRED_NAME = "Owner" + OWNER_EMAIL = "owner@tramline.app" + OWNER_PASSWORD = "why aroma enclose startup" + + # developer constants + DEVELOPER_FULL_NAME = "Developer User" + DEVELOPER_PREFERRED_NAME = "Developer" + DEVELOPER_EMAIL = "developer@tramline.app" + DEVELOPER_PASSWORD = "why aroma enclose startup" + end +end diff --git a/lib/seed/dev_starter.rb b/lib/seed/dev_starter.rb new file mode 100644 index 000000000..1a5d53aba --- /dev/null +++ b/lib/seed/dev_starter.rb @@ -0,0 +1,89 @@ +# rubocop:disable Rails/Output + +module Seed + class DevStarter + include Seed::Constants + + def call + puts "Seeding database..." + + ActiveRecord::Base.transaction do + self.class.send(:create_admin_user) + self.class.send(:create_owner_user) + self.class.send(:create_developer_user) + end + + puts "Completed seeding database" + end + + def self.create_admin_user + email_authentication = Accounts::EmailAuthentication.find_or_initialize_by(email: ADMIN_EMAIL) + admin = true + + unless email_authentication.persisted? + user = Accounts::User.find_or_create_by!(full_name: ADMIN_FULL_NAME, preferred_name: ADMIN_PREFERRED_NAME, admin:, unique_authn_id: ADMIN_EMAIL) + email_authentication.update!(password: ADMIN_PASSWORD, confirmed_at: DateTime.now, user:) + end + + puts "Added/updated admin user." + end + + def self.create_owner_user + email_authentication = Accounts::EmailAuthentication.find_or_initialize_by(email: OWNER_EMAIL) + + if email_authentication.persisted? + user = email_authentication.user + else + user = Accounts::User.find_or_create_by!(full_name: OWNER_FULL_NAME, preferred_name: OWNER_PREFERRED_NAME, unique_authn_id: OWNER_EMAIL) + email_authentication.update!(password: OWNER_PASSWORD, confirmed_at: DateTime.now, user:) + email_authentication.reload + end + + organization = Accounts::Organization.find_or_create_by!( + name: "Tramline Test 1 (Owner)", + status: Accounts::Organization.statuses[:active], + created_by: email_authentication.email + ) + + Accounts::Membership.find_or_create_by!( + user:, + organization:, + role: Accounts::Membership.roles[:owner] + ) + + puts "Added/updated owner user." + end + + def self.create_developer_user + email_authentication = Accounts::EmailAuthentication.find_or_initialize_by(email: DEVELOPER_EMAIL) + + if email_authentication.persisted? + user = email_authentication.user + else + user = Accounts::User.find_or_create_by!(full_name: DEVELOPER_FULL_NAME, preferred_name: DEVELOPER_PREFERRED_NAME, unique_authn_id: DEVELOPER_EMAIL) + email_authentication.update!(password: DEVELOPER_PASSWORD, confirmed_at: DateTime.now, user:) + email_authentication.reload + end + + organization = Accounts::Organization.find_or_create_by!( + name: "Tramline Test 1 (Developer)", + status: Accounts::Organization.statuses[:active], + created_by: email_authentication.email + ) + + Accounts::Membership.find_or_create_by!( + user:, + organization:, + role: Accounts::Membership.roles[:developer] + ) + + puts "Added/updated developer user." + end + + private_class_method :create_admin_user + private_class_method :create_owner_user + private_class_method :create_developer_user + end +end + +# rubocop:enable Rails/Output