300 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			300 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Asp.Versioning;
 | |
| using Core.Thalos.Application.UseCases.Users.Input;
 | |
| using Core.Thalos.BuildingBlocks;
 | |
| using Core.Thalos.External.Clients.Thalos.Requests.Users;
 | |
| using Lib.Architecture.BuildingBlocks;
 | |
| using LSA.Dashboard.External.Clients.Dashboard;
 | |
| using Microsoft.AspNetCore.Authorization;
 | |
| using Microsoft.AspNetCore.Mvc;
 | |
| using System.Text.Json;
 | |
| 
 | |
| namespace Core.Thalos.BFF.Api.Controllers
 | |
| {
 | |
|     /// <summary>
 | |
|     /// Handles all requests for user authentication.
 | |
|     /// </summary>
 | |
|     [ApiVersion("1.0")]
 | |
|     [Route("api/v{version:apiVersion}/[controller]")]
 | |
|     [Consumes("application/json")]
 | |
|     [Produces("application/json")]
 | |
|     [ApiController]
 | |
|     public class UserController(IThalosServiceClient thalosServiceClient, ILogger<UserController> logger) : BaseController(logger)
 | |
|     {
 | |
|         /// <summary>
 | |
|         /// Gets all users.
 | |
|         /// </summary>
 | |
|         [HttpGet("GetAll")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
 | |
|         [Permission("UserManagement.Read")]
 | |
|         public async Task<IActionResult> GetAllUsersService(CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(GetAllUsersService)} - Request received - Payload: ");
 | |
| 
 | |
|                 return await Handle(() => thalosServiceClient.GetAllUsersService(new GetAllUsersRequest { }, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(GetAllUsersService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Creates a new user.
 | |
|         /// </summary>
 | |
|         [HttpPost("Create")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
 | |
|         [Permission("UserManagement.Write")]
 | |
|         public async Task<IActionResult> CreateUserService(CreateUserRequest newUser, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(CreateUserService)} - Request received - Payload: {JsonSerializer.Serialize(newUser)}");
 | |
| 
 | |
|                 if (newUser == null) return BadRequest("Invalid user object");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(newUser.Email)) return BadRequest("Invalid user email");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(newUser.Name)) return BadRequest("Invalid user name");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(newUser.LastName)) return BadRequest("Invalid user lastname");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(newUser.RoleId)) return BadRequest("Invalid role id");
 | |
| 
 | |
|                 return await Handle(() => thalosServiceClient.CreateUserService(newUser, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(CreateUserService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newUser)}");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Gets the user by identifier.
 | |
|         /// </summary>
 | |
|         [HttpPost("GetById")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
 | |
|         [Permission("UserManagement.Read")]
 | |
|         public async Task<IActionResult> GetUserByIdService(GetUserRequest request, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(GetUserByIdService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(request._Id)) return BadRequest("Invalid user identifier");
 | |
| 
 | |
|                 return await Handle(() => thalosServiceClient.GetUserByIdService(request, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(GetUserByIdService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Gets the user by email.
 | |
|         /// </summary>
 | |
|         [HttpPost("GetByEmail")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
 | |
|         [Permission("UserManagement.Read")]
 | |
|         public async Task<IActionResult> GetUserByEmailService(GetUserByEmailRequest request, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(GetUserByEmailService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(request.Email)) return BadRequest("Invalid user email");
 | |
| 
 | |
|                 return await Handle(() => thalosServiceClient.GetUserByEmailService(request, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(GetUserByIdService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Updates a full user by identifier.
 | |
|         /// </summary>
 | |
|         [HttpPut("Update")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
 | |
|         [Permission("UserManagement.Write")]
 | |
|         public async Task<IActionResult> UpdateUserService(UpdateUserRequest request, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(UpdateUserService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
 | |
| 
 | |
|                 if (request == null) return BadRequest("Invalid user object");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(request.Email)) return BadRequest("Invalid user email");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(request.Name)) return BadRequest("Invalid user name");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(request.LastName)) return BadRequest("Invalid user lastname");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(request.RoleId)) return BadRequest("Invalid role id");
 | |
| 
 | |
|                 if (!request.Companies.Any()) return BadRequest("The user must contain at least one company");
 | |
| 
 | |
|                 return await Handle(() => thalosServiceClient.UpdateUserService(request, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(UpdateUserService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Logs in the user.
 | |
|         /// </summary>
 | |
|         [HttpPatch("LoginUser")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         [Authorize(AuthenticationSchemes = $"{Schemes.GoogleScheme}, {Schemes.DefaultScheme}")]
 | |
|         public async Task<IActionResult> LoginUserService([FromBody] LoginUserRequest request, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(LoginUserService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(request.Email)) return BadRequest("Invalid user email");
 | |
| 
 | |
|                 return await Handle(() => thalosServiceClient.LoginUserService(request, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(LoginUserService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Logs out the user.
 | |
|         /// </summary>
 | |
|         [HttpPatch("LogoutUser")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         [Authorize(AuthenticationSchemes = $"{Schemes.GoogleScheme}, {Schemes.DefaultScheme}")]
 | |
|         public async Task<IActionResult> LogoutUserService([FromBody] LogoutUserRequest request, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(LogoutUserService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(request.Email)) return BadRequest("Invalid user email");
 | |
| 
 | |
|                 return await Handle(() => thalosServiceClient.LogoutUserService(request, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(LogoutUserService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Changes the status of the user.
 | |
|         /// </summary>
 | |
|         [HttpPatch]
 | |
|         [Route("ChangeStatus")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status204NoContent)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
 | |
|         [Permission("UserManagement.Write")]
 | |
|         public async Task<IActionResult> ChangeUserStatusService([FromBody] ChangeUserStatusRequest request, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(ChangeUserStatusService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(request._Id)) { return BadRequest("Invalid user identifier"); }
 | |
| 
 | |
|                 return await Handle(() => thalosServiceClient.ChangeUserStatusService(request, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(ChangeUserStatusService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Gets the user by email.
 | |
|         /// </summary>
 | |
|         [HttpPost("ValidateExistence")]
 | |
|         [ProducesResponseType(StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
 | |
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         [AllowAnonymous]
 | |
|         public async Task<IActionResult> ValidateUserExistenceService(ValidateUserExistenceRequest request, CancellationToken cancellationToken)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 logger.LogInformation($"{nameof(ValidateUserExistenceService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
 | |
| 
 | |
|                 if (string.IsNullOrEmpty(request.Email)) return BadRequest("Invalid user email");
 | |
| 
 | |
|                 return await Handle(() => thalosServiceClient.ValidateUserExistenceService(request, cancellationToken)).ConfigureAwait(false);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 logger.LogError($"{nameof(GetUserByIdService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
 | |
|                 throw;
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| }
 |