using System; using System.Threading; using System.Threading.Tasks; using HotChocolate; using HotChocolate.Types; using HotChocolate.Types.Relay; using Tgstation.Server.Host.Authority; using Tgstation.Server.Host.GraphQL.Mutations.Payloads; using Tgstation.Server.Host.GraphQL.Types; using Tgstation.Server.Host.Models.Transformers; namespace Tgstation.Server.Host.GraphQL.Mutations { /// /// related s. /// [ExtendObjectType(typeof(Mutation))] [GraphQLDescription(Mutation.GraphQLDescription)] public sealed class UserGroupMutations { /// /// Transform a into a . /// /// The to transform. /// The transformed . static Models.PermissionSet? TransformApiPermissionSet(PermissionSetInput? permissionSet) => permissionSet != null ? new Models.PermissionSet { InstanceManagerRights = permissionSet?.InstanceManagerRights, AdministrationRights = permissionSet?.AdministrationRights, } : null; /// /// Creates a . /// /// The of the . /// The initial permission set for the . /// The for the . /// The for the operation. /// The created . [Error(typeof(ErrorMessageException))] public ValueTask CreateUserGroup( string name, PermissionSetInput? permissionSet, [Service] IGraphQLAuthorityInvoker userGroupAuthority, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(name); ArgumentNullException.ThrowIfNull(userGroupAuthority); return userGroupAuthority.InvokeTransformable( authority => authority.Create(name, TransformApiPermissionSet(permissionSet), cancellationToken)); } /// /// Updates a . /// /// The of the to update. /// Optional new for the . /// Optional new permission set for the . /// The for the . /// The for the operation. /// The updated . [Error(typeof(ErrorMessageException))] public ValueTask UpdateUserGroup( [ID(nameof(UserGroup))] long id, string? newName, PermissionSetInput? newPermissionSet, [Service] IGraphQLAuthorityInvoker userGroupAuthority, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(userGroupAuthority); return userGroupAuthority.InvokeTransformable( authority => authority.Update(id, newName, TransformApiPermissionSet(newPermissionSet), cancellationToken)); } /// /// Deletes a . /// /// The of the to update. /// The for the . /// The for the operation. /// The root. [Error(typeof(ErrorMessageException))] public async ValueTask DeleteEmptyUserGroup( [ID(nameof(UserGroup))] long id, [Service] IGraphQLAuthorityInvoker userGroupAuthority, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(userGroupAuthority); await userGroupAuthority.Invoke( authority => authority.DeleteEmpty(id, cancellationToken)); return new Query(); } } }