Skip to content

Commit

Permalink
feature: verificar email de administrador e funcionário quando atuali…
Browse files Browse the repository at this point in the history
…za-lo
  • Loading branch information
EduardoKroetz committed Jun 30, 2024
1 parent 4e72a3c commit 58b37a7
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 41 deletions.
4 changes: 2 additions & 2 deletions Hotel.Domain/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ public async Task<IActionResult> UpdateNameAsync([FromBody] Name name)
//Editar email do administrador.
//Administradores autenticados podem acessar.
[HttpPatch("email")]
public async Task<IActionResult> UpdateEmailAsync([FromBody] Email email)
public async Task<IActionResult> UpdateEmailAsync([FromBody] Email email, [FromQuery] string? code)
{
var adminId = _userService.GetUserIdentifier(User);
return Ok(await _handler.HandleUpdateEmailAsync(adminId, email));
return Ok(await _handler.HandleUpdateEmailAsync(adminId, email, code));
}

//Editar telefone do administrador.
Expand Down
4 changes: 2 additions & 2 deletions Hotel.Domain/Controllers/EmployeeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ public async Task<IActionResult> UpdateNameAsync([FromBody] Name name)
}

[HttpPatch("email")]
public async Task<IActionResult> UpdateEmailAsync([FromBody] Email email)
public async Task<IActionResult> UpdateEmailAsync([FromBody] Email email, [FromQuery] string? code)
{
var userId = _userService.GetUserIdentifier(User);
return Ok(await _handler.HandleUpdateEmailAsync(userId, email));
return Ok(await _handler.HandleUpdateEmailAsync(userId, email, code));
}

[HttpPatch("phone")]
Expand Down
2 changes: 1 addition & 1 deletion Hotel.Domain/Handlers/AdminHandlers/HandleCreateAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public partial class AdminHandler : GenericUserHandler<IAdminRepository, Admin>,
private readonly IPermissionRepository _permissionRepository;
private readonly IEmailService _emailService;

public AdminHandler(IAdminRepository repository, IPermissionRepository permissionRepository, IEmailService emailService) : base(repository)
public AdminHandler(IAdminRepository repository, IPermissionRepository permissionRepository, IEmailService emailService) : base(repository, emailService)
{
_repository = repository;
_permissionRepository = permissionRepository;
Expand Down
14 changes: 11 additions & 3 deletions Hotel.Domain/Handlers/Base/GenericUserHandler/UpdateUserEmail.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Hotel.Domain.DTOs;
using Hotel.Domain.Entities.Base;
using Hotel.Domain.Repositories.Base.Interfaces;
using Hotel.Domain.Services.EmailServices.Interfaces;
using Hotel.Domain.ValueObjects;

namespace Hotel.Domain.Handlers.Base.GenericUserHandler;
Expand All @@ -10,12 +11,19 @@ public partial class GenericUserHandler<TRepository, TUser>
where TUser : User
{
private readonly TRepository _repository;
private readonly IEmailService _emailService;

public GenericUserHandler(TRepository repository)
=> _repository = repository;
public GenericUserHandler(TRepository repository, IEmailService emailService)
{
_repository = repository;
_emailService = emailService;
}

public async Task<Response> HandleUpdateEmailAsync(Guid userId, Email email)
public async Task<Response> HandleUpdateEmailAsync(Guid userId, Email email, string? code)
{
//Validação do código
await _emailService.VerifyEmailCodeAsync(email, code);

var user = await _repository.GetEntityByIdAsync(userId);
if (user == null)
throw new ArgumentException("Não foi possível encontrar o usuário.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial class CustomerHandler : GenericUserHandler<ICustomerRepository, H
private readonly IEmailService _emailService;
private readonly IStripeService _stripeService;

public CustomerHandler(ICustomerRepository repository, IEmailService emailService, IStripeService stripeService) : base(repository)
public CustomerHandler(ICustomerRepository repository, IEmailService emailService, IStripeService stripeService) : base(repository, emailService)
{
_repository = repository;
_emailService = emailService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public partial class EmployeeHandler : GenericUserHandler<IEmployeeRepository, E
private readonly IPermissionRepository _permissionRepository;
private readonly IEmailService _emailService;

public EmployeeHandler(IEmployeeRepository repository, IResponsibilityRepository responsibilityRepository, IPermissionRepository permissionRepository, IEmailService emailService) : base(repository)
public EmployeeHandler(IEmployeeRepository repository, IResponsibilityRepository responsibilityRepository, IPermissionRepository permissionRepository, IEmailService emailService) : base(repository, emailService)
{
_repository = repository;
_responsibilityRepository = responsibilityRepository;
Expand Down
36 changes: 6 additions & 30 deletions Hotel.Tests/IntegrationTests/Controllers/AdminControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hotel.Domain.DTOs.Base.User;
using Hotel.Domain.Entities.AdminEntity;
using Hotel.Domain.Entities.PermissionEntity;
using Hotel.Domain.Entities.VerificationCodeEntity;
using Hotel.Domain.Enums;
using Hotel.Domain.Services.Permissions;
using Hotel.Domain.Services.TokenServices;
Expand Down Expand Up @@ -240,35 +241,6 @@ public async Task RemoveAdminPermission_ShouldReturn_OK()
Assert.AreEqual(adminWithPermissions.Permissions.Count, 0);
}

//[TestMethod]
//public async Task UpdateToRootAdmin_ShouldReturn_OK()
//{
// //Arrange
// var admin = new Admin
// (
// new Name("Beatriz", "Santos"),
// new Email("[email protected]"),
// new Phone("+55 (31) 99876-5432"),
// "password3",
// EGender.Feminine,
// DateTime.Now.AddYears(-27),
// new Address("Brazil", "Belo Horizonte", "MG-303", 303)
// );

// await _dbContext.Admins.AddAsync(admin);
// await _dbContext.SaveChangesAsync();

// //Act
// var response = await _client.PostAsync($"{_baseUrl}/to-root-admin/{admin.Id}", null);

// //Assert
// var updatedAdmin = await _dbContext.Admins.FirstOrDefaultAsync(x => x.Id == admin.Id);

// Assert.IsNotNull(response);
// response.EnsureSuccessStatusCode();
// Assert.IsTrue(updatedAdmin!.IsRootAdmin);
//}

[TestMethod]
public async Task UpdateAdmin_ShouldReturn_OK()
{
Expand Down Expand Up @@ -408,8 +380,12 @@ public async Task UpdateEmailAdmin_ShouldReturn_OK()

var body = new Email("[email protected]");

var verificationNewEmailCode = new VerificationCode(body);
await _dbContext.VerificationCodes.AddAsync(verificationNewEmailCode);
await _dbContext.SaveChangesAsync();

//Act
var response = await _client.PatchAsJsonAsync($"{_baseUrl}/email", body);
var response = await _client.PatchAsJsonAsync($"{_baseUrl}/email?code={verificationNewEmailCode.Code}", body);

//Assert
var updatedAdmin = await _dbContext.Admins.FirstOrDefaultAsync(x => x.Id == admin.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Net.Http.Headers;
using System.Net.Http.Json;
using Hotel.Domain.DTOs.EmployeeDTOs;
using Hotel.Domain.Entities.VerificationCodeEntity;

namespace Hotel.Tests.IntegrationTests.Controllers;

Expand Down Expand Up @@ -386,8 +387,12 @@ public async Task UpdateEmployeeEmail_ShouldReturn_OK()

var body = new Email("[email protected]");

var verificationNewEmailCode = new VerificationCode(body);
await _dbContext.VerificationCodes.AddAsync(verificationNewEmailCode);
await _dbContext.SaveChangesAsync();

//Act
var response = await _client.PatchAsJsonAsync($"{_baseUrl}/email", body);
var response = await _client.PatchAsJsonAsync($"{_baseUrl}/email?code={verificationNewEmailCode.Code}", body);

//Assert
var updatedEmployee = await _dbContext.Employees.FirstOrDefaultAsync(x => x.Id == employee.Id);
Expand Down

0 comments on commit 58b37a7

Please sign in to comment.