public async Task <IActionResult> Delete([FromRoute] string userNameOrId) { if (string.IsNullOrWhiteSpace(userNameOrId)) { return(ErrorResult .BadRequest($"The identifier '{userNameOrId}' provided in the url path is invalid. Must be a valid email address or GUID.", ResultErrorCode.ValidationError) .ActionResult()); } var teamCloudInstance = await teamCloudRepository .GetAsync() .ConfigureAwait(false); if (teamCloudInstance is null) { return(ErrorResult .NotFound($"No TeamCloud Instance was found.") .ActionResult()); } if (!Guid.TryParse(userNameOrId, out var userId)) { var idLookup = await userService .GetUserIdAsync(userNameOrId) .ConfigureAwait(false); if (!idLookup.HasValue || idLookup.Value == Guid.Empty) { return(ErrorResult .NotFound($"A User with the email '{userNameOrId}' could not be found.") .ActionResult()); } userId = idLookup.Value; } var user = teamCloudInstance.Users?.FirstOrDefault(u => u.Id == userId); if (user is null) { return(ErrorResult .NotFound($"The specified User could not be found in this TeamCloud Instance.") .ActionResult()); } var command = new OrchestratorTeamCloudUserDeleteCommand(CurrentUser, user); var commandResult = await orchestrator .InvokeAsync(command) .ConfigureAwait(false); if (commandResult.Links.TryGetValue("status", out var statusUrl)) { return(StatusResult .Accepted(commandResult.CommandId.ToString(), statusUrl, commandResult.RuntimeStatus.ToString(), commandResult.CustomStatus) .ActionResult()); } throw new Exception("This shoudn't happen, but we need to decide to do when it does."); }
public async Task <IActionResult> Delete([FromRoute] string userNameOrId) { if (string.IsNullOrWhiteSpace(userNameOrId)) { return(ErrorResult .BadRequest($"The identifier '{userNameOrId}' provided in the url path is invalid. Must be a valid email address or GUID.", ResultErrorCode.ValidationError) .ActionResult()); } var userId = await userService .GetUserIdAsync(userNameOrId) .ConfigureAwait(false); if (string.IsNullOrEmpty(userId)) { return(ErrorResult .NotFound($"The user '{userNameOrId}' could not be found.") .ActionResult()); } var user = await usersRepository .GetAsync(userId) .ConfigureAwait(false); if (user is null) { return(ErrorResult .NotFound($"The specified User could not be found in this TeamCloud Instance.") .ActionResult()); } if (user.IsAdmin()) { var otherAdmins = await usersRepository .ListAdminsAsync() .AnyAsync(a => a.Id != user.Id) .ConfigureAwait(false); if (!otherAdmins) { return(ErrorResult .BadRequest($"The TeamCloud instance must have at least one Admin user. To delete this user you must first add another Admin user.", ResultErrorCode.ValidationError) .ActionResult()); } } var currentUserForCommand = await userService .CurrentUserAsync() .ConfigureAwait(false); var command = new OrchestratorTeamCloudUserDeleteCommand(currentUserForCommand, user); return(await orchestrator .InvokeAndReturnAccepted(command) .ConfigureAwait(false)); }