|
| 1 | +class Signup |
| 2 | + include ActiveModel::Model |
| 3 | + include ActiveModel::Attributes |
| 4 | + include ActiveModel::Validations |
| 5 | + |
| 6 | + attr_accessor :full_name, :email_address, :identity |
| 7 | + attr_reader :account, :user |
| 8 | + |
| 9 | + with_options on: :completion do |
| 10 | + validates_presence_of :full_name, :identity |
| 11 | + end |
| 12 | + |
| 13 | + def initialize(...) |
| 14 | + super |
| 15 | + |
| 16 | + @email_address = @identity.email_address if @identity |
| 17 | + end |
| 18 | + |
| 19 | + def create_identity |
| 20 | + @identity = Identity.find_or_create_by!(email_address: email_address) |
| 21 | + @identity.send_magic_link |
| 22 | + end |
| 23 | + |
| 24 | + def complete |
| 25 | + if valid?(:completion) |
| 26 | + begin |
| 27 | + @tenant = create_tenant |
| 28 | + create_account |
| 29 | + true |
| 30 | + rescue => error |
| 31 | + destroy_account |
| 32 | + handle_account_creation_error(error) |
| 33 | + |
| 34 | + errors.add(:base, "Something went wrong, and we couldn't create your account. Please give it another try.") |
| 35 | + Rails.error.report(error, severity: :error) |
| 36 | + Rails.logger.error error |
| 37 | + Rails.logger.error error.backtrace.join("\n") |
| 38 | + |
| 39 | + false |
| 40 | + end |
| 41 | + else |
| 42 | + false |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + private |
| 47 | + # Override to customize the handling of external accounts associated to the account. |
| 48 | + def create_tenant |
| 49 | + nil |
| 50 | + end |
| 51 | + |
| 52 | + # Override to inject custom handling for account creation errors |
| 53 | + def handle_account_creation_error(error) |
| 54 | + end |
| 55 | + |
| 56 | + def create_account |
| 57 | + @account = Account.create_with_admin_user( |
| 58 | + account: { |
| 59 | + external_account_id: @tenant, |
| 60 | + name: generate_account_name |
| 61 | + }, |
| 62 | + owner: { |
| 63 | + name: full_name, |
| 64 | + identity: identity |
| 65 | + } |
| 66 | + ) |
| 67 | + @user = @account.users.find_by!(role: :admin) |
| 68 | + @account.setup_customer_template |
| 69 | + end |
| 70 | + |
| 71 | + def generate_account_name |
| 72 | + AccountNameGenerator.new(identity: identity, name: full_name).generate |
| 73 | + end |
| 74 | + |
| 75 | + |
| 76 | + def destroy_account |
| 77 | + @account&.destroy! |
| 78 | + |
| 79 | + @user = nil |
| 80 | + @account = nil |
| 81 | + @tenant = nil |
| 82 | + end |
| 83 | + |
| 84 | + def subscription_attributes |
| 85 | + subscription = FreeV1Subscription |
| 86 | + |
| 87 | + {}.tap do |attributes| |
| 88 | + attributes[:name] = subscription.to_param |
| 89 | + attributes[:price] = subscription.price |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + def request_attributes |
| 94 | + {}.tap do |attributes| |
| 95 | + attributes[:remote_address] = Current.ip_address |
| 96 | + attributes[:user_agent] = Current.user_agent |
| 97 | + attributes[:referrer] = Current.referrer |
| 98 | + end |
| 99 | + end |
| 100 | +end |
0 commit comments