diff --git a/app/graphql/mutations/organizations/create.rb b/app/graphql/mutations/organizations/create.rb new file mode 100644 index 000000000000..9168098f5433 --- /dev/null +++ b/app/graphql/mutations/organizations/create.rb @@ -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 diff --git a/app/graphql/types/mutation_type.rb b/app/graphql/types/mutation_type.rb index e836ce54eec1..b40778f3f7bd 100644 --- a/app/graphql/types/mutation_type.rb +++ b/app/graphql/types/mutation_type.rb @@ -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