Skip to content
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
43 changes: 43 additions & 0 deletions app/graphql/mutations/organizations/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module Mutations
module Organizations
# Creates a brand-new organization and makes the current user its first
# admin. Unlike most org mutations this does NOT `include RequiredOrganization`
# — there is no current org yet; the logged-in user is the only requirement.
#
# Mirrors the org bootstrap performed by `Mutations::RegisterUser` /
# `UsersService#register` (CreateService -> Membership -> admin MembershipRole)
# so a self-serve "Create organization" button behaves exactly like signup.
class Create < BaseMutation
include AuthenticableApiUser

graphql_name "CreateOrganization"
description "Creates a new organization and adds the current user as an admin member"

argument :name, String, required: true

type Types::Organizations::CurrentOrganizationType

def resolve(name:)
result = ::Organizations::CreateService.call(
name:,
document_numbering: "per_organization"
)
return result_error(result) unless result.success?

organization = result.organization

ActiveRecord::Base.transaction do
membership = Membership.create!(user: context[:current_user], organization:)
MembershipRole.create!(organization:, membership:, role: Role.admins.first!)
end

organization
rescue ActiveRecord::RecordInvalid => e
result.record_validation_failure!(record: e.record)
result_error(result)
end
end
end
end
1 change: 1 addition & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class MutationType < Types::BaseObject
field :login_user, mutation: Mutations::LoginUser
field :register_user, mutation: Mutations::RegisterUser

field :create_organization, mutation: Mutations::Organizations::Create
field :update_organization, mutation: Mutations::Organizations::Update

field :create_billable_metric, mutation: Mutations::BillableMetrics::Create
Expand Down
Loading