diff --git a/Application/Features/Auth/Interfaces/IUserService.cs b/Application/Features/Auth/Interfaces/IUserService.cs index 50fbd7b..8e99345 100644 --- a/Application/Features/Auth/Interfaces/IUserService.cs +++ b/Application/Features/Auth/Interfaces/IUserService.cs @@ -1,4 +1,5 @@ using Application.Features.Auth.DTOs; +using Domain.Enums; namespace Application.Features.Auth.Interfaces; @@ -6,6 +7,7 @@ public interface IUserService { Task RegisterUserAsync(RegisterUserRequestDto registerUserRequestDto); Task LoginUserAsync(LoginUserRequestDto loginUserRequestDto); + Task ChangeRoleTo(UserRole role); Task LogoutUserAsync(); Task RefreshTokenAsync(string refreshToken); Task ViewProfileAsync(); diff --git a/Application/Features/Auth/Services/UserService.cs b/Application/Features/Auth/Services/UserService.cs index ccdcca1..da1725b 100644 --- a/Application/Features/Auth/Services/UserService.cs +++ b/Application/Features/Auth/Services/UserService.cs @@ -4,6 +4,7 @@ using Application.Features.Auth.DTOs; using Application.Features.Auth.Interfaces; using Domain.Entities; +using Domain.Enums; namespace Application.Features.Auth.Services; @@ -68,6 +69,13 @@ public async Task LoginUserAsync(LoginUserRequestDto login return new LoginUserResponseDto(token, refreshTokenValue); } + public async Task ChangeRoleTo(UserRole role) + { + var userId =_userContext.UserId; + var user =await _userRepository.GetUserByIdAsync(userId); + user?.ChangeRoleTo(role); + } + public async Task LogoutUserAsync() { var userId = _userContext.UserId; diff --git a/Application/Features/Tenant/Services/TenantService.cs b/Application/Features/Tenant/Services/TenantService.cs index 05bffc3..b7aa15c 100644 --- a/Application/Features/Tenant/Services/TenantService.cs +++ b/Application/Features/Tenant/Services/TenantService.cs @@ -1,24 +1,46 @@ +using Application.Common.Interfaces; +using Application.Features.Auth.Interfaces; +using Application.Features.Auth.Services; using Application.Features.Tenant.DTO_s; using Application.Features.Tenant.Interfaces; using Domain.Entities; +using Domain.Enums; +using Domain.Exceptions; +using Microsoft.Extensions.Configuration; + namespace Application.Features.Tenant.Services; public class TenantService:TenantServiceContract { private readonly TenantRepositoryContract _tenantRepository; + private readonly IUSerContext _userContext; + private readonly IUserRepository _userRepository; - public TenantService(TenantRepositoryContract tenantRepository) + public TenantService(TenantRepositoryContract tenantRepository, IUSerContext userContext, IUserRepository userRepository) { _tenantRepository = tenantRepository; + _userContext = userContext; + _userRepository = userRepository; } public async Task Register(RegisterTenantDTO dto) { //Todo: slug maker // Todo : Date manager + // Todo : if for duplicate slug var tenant = new Domain.Entities.Tenant(dto.Name,dto.Slug,dto.SubscriptionExpireDate); + + var userId=_userContext.UserId; + var user = await _userRepository.GetUserByIdAsync(userId); + + if(user==null) + throw new NotFoundException("User not found"); + await _tenantRepository.RegisterTenant(tenant); + user.ChangeRoleTo(UserRole.Admin); + user.AssignTenantToUser(tenant.Id); await _tenantRepository.SaveAsync(); - return $"{tenant.Name} Registered"; + + return $"{tenant.Name} Registered belong to user {user.FullName} created"; } } \ No newline at end of file diff --git a/Domain/Entities/User.cs b/Domain/Entities/User.cs index cedf899..5827fac 100644 --- a/Domain/Entities/User.cs +++ b/Domain/Entities/User.cs @@ -42,6 +42,14 @@ public User(string fullName, string email, string phoneNumber, UserRole role, st Password = password; } + public void AssignTenantToUser(Guid tenantId) + { + TenantId = tenantId; + } + public void ChangeRoleTo(UserRole role) + { + Role = role; + } public void UpdateProfile(string fullName, string phoneNumber) { FullName = fullName; diff --git a/api/Controllers/TenantController.cs b/api/Controllers/TenantController.cs index 94200cb..f6a67f0 100644 --- a/api/Controllers/TenantController.cs +++ b/api/Controllers/TenantController.cs @@ -1,5 +1,6 @@ using Application.Features.Tenant.DTO_s; using Application.Features.Tenant.Services; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace api.Controllers; @@ -15,6 +16,7 @@ public TenantController(TenantServiceContract tenantService) } [HttpPost] + [Authorize(Roles = "User,Admin")] public async Task RegisterTenant([FromBody] RegisterTenantDTO dto) { // Todo: expire date base on plan