54 lines
1.8 KiB
C#
54 lines
1.8 KiB
C#
using System.Security.Claims;
|
|
using FictionArchive.Service.UserService.Models.DTOs;
|
|
using FictionArchive.Service.UserService.Services;
|
|
using HotChocolate.Authorization;
|
|
using HotChocolate.Types;
|
|
|
|
namespace FictionArchive.Service.UserService.GraphQL;
|
|
|
|
public class Mutation
|
|
{
|
|
[Authorize]
|
|
[Error<InvalidOperationException>]
|
|
public async Task<UserDto> InviteUser(
|
|
string email,
|
|
string username,
|
|
UserManagementService userManagementService,
|
|
ClaimsPrincipal claimsPrincipal)
|
|
{
|
|
// Get the current user's OAuth provider ID from claims
|
|
var oAuthProviderId = claimsPrincipal.FindFirst("sub")?.Value;
|
|
if (string.IsNullOrEmpty(oAuthProviderId))
|
|
{
|
|
throw new InvalidOperationException("Unable to determine current user identity");
|
|
}
|
|
|
|
// Get the inviter from the database
|
|
var inviter = await userManagementService.GetUserByOAuthProviderIdAsync(oAuthProviderId);
|
|
if (inviter == null)
|
|
{
|
|
throw new InvalidOperationException("Current user not found in the system");
|
|
}
|
|
|
|
// Invite the new user
|
|
var newUser = await userManagementService.InviteUserAsync(inviter, email, username);
|
|
if (newUser == null)
|
|
{
|
|
throw new InvalidOperationException(
|
|
"Failed to invite user. Either you have no available invites, or the email/username is already in use.");
|
|
}
|
|
|
|
return new UserDto
|
|
{
|
|
Id = newUser.Id,
|
|
CreatedTime = newUser.CreatedTime,
|
|
LastUpdatedTime = newUser.LastUpdatedTime,
|
|
Username = newUser.Username,
|
|
Email = newUser.Email,
|
|
Disabled = newUser.Disabled,
|
|
AvailableInvites = newUser.AvailableInvites,
|
|
InviterId = newUser.InviterId
|
|
};
|
|
}
|
|
}
|