From 984384749804b2f8a7effd47748bb5967b68a55f Mon Sep 17 00:00:00 2001 From: Mohammad Shabeer Date: Sun, 6 Oct 2024 06:23:43 +0530 Subject: [PATCH] fix:actStatus --- Repositories/Impl/UserRepo.cs | 14 ++++++++++---- Services/Impl/AdminService.cs | 12 ++++++++++-- Services/Impl/AuthService.cs | 10 ++++++---- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Repositories/Impl/UserRepo.cs b/Repositories/Impl/UserRepo.cs index fa35afb..408bd05 100644 --- a/Repositories/Impl/UserRepo.cs +++ b/Repositories/Impl/UserRepo.cs @@ -57,27 +57,33 @@ public async Task UpdateProfile(User user) //method to add rating for vendor public async Task AddVendorRating(User vendor) { - await _users.ReplaceOneAsync(u=>u.Id == vendor.Id,vendor); + await _users.ReplaceOneAsync(u => u.Id == vendor.Id, vendor); } // Method to approve customer accounts public async Task ApproveUserAccount(string userId) { - var update = Builders.Update.Set(u => u.Status, Models.Enums.Status.ACTIVE); + var update = Builders.Update + .Set(u => u.Status, Models.Enums.Status.ACTIVE) + .Set(u => u.IsApproved, true); await _users.UpdateOneAsync(u => u.Id == userId, update); } // Method to deactivate a user account public async Task DeactivateAccount(string userId) { - var update = Builders.Update.Set(u => u.Status, Models.Enums.Status.DEACTIVATED); + var update = Builders.Update + .Set(u => u.Status, Models.Enums.Status.DEACTIVATED) + .Set(u => u.IsApproved, false); await _users.UpdateOneAsync(u => u.Id == userId, update); } // Method to reactivate a user account public async Task ReactivateAccount(string userId) { - var update = Builders.Update.Set(u => u.Status, Models.Enums.Status.ACTIVE); + var update = Builders.Update + .Set(u => u.Status, Models.Enums.Status.ACTIVE) + .Set(u => u.IsApproved, true); await _users.UpdateOneAsync(u => u.Id == userId, update); } //mehtod to delete user diff --git a/Services/Impl/AdminService.cs b/Services/Impl/AdminService.cs index f7ec7d3..5f49b30 100644 --- a/Services/Impl/AdminService.cs +++ b/Services/Impl/AdminService.cs @@ -26,6 +26,9 @@ public async Task CreateUser(CreateUserDto createUserDto) if (existingUser != null) return new ApiRes(409, false, "User already exists.", new { }); var newUser = _mapper.Map(createUserDto); + // auto activate account when admin create users + newUser.IsApproved = true; + await _adminRepository.CreateNewUser(newUser); return new ApiRes(201, true, "User created successfully", new { }); } @@ -81,8 +84,13 @@ public async Task GetAllUsers() var deactivatedCount = users.Count(u => u.Status == Models.Enums.Status.DEACTIVATED); var totalUsers = users.Count; - return new ApiRes(200, true, "Users fethed", new{ - users =userResDto, activeUsers=activeCount , pendingUsers = pendingCount , deactiveUsers =deactivatedCount , totalUsers + return new ApiRes(200, true, "Users fethed", new + { + users = userResDto, + activeUsers = activeCount, + pendingUsers = pendingCount, + deactiveUsers = deactivatedCount, + totalUsers }); } catch (Exception ex) diff --git a/Services/Impl/AuthService.cs b/Services/Impl/AuthService.cs index 452df5d..88eab59 100644 --- a/Services/Impl/AuthService.cs +++ b/Services/Impl/AuthService.cs @@ -22,12 +22,14 @@ public AuthService(IMapper mapper, IUserRepo userRepository, JwtHelper jwtHelper } public async Task Login(LoginDto loginDto) { - var user = await _userRepository.GetUserByEmailAndRole(loginDto.Email,loginDto.Role); + var user = await _userRepository.GetUserByEmailAndRole(loginDto.Email, loginDto.Role); if (user == null) { return new ApiRes(404, false, "user not found", new { }); } - if (!HashPassword.VerifyPasswordHash(user.PasswordHash, loginDto.Password)) + if (user.IsApproved == false) + return new ApiRes(403, false, "account not activated", new { }); + if (!HashPassword.VerifyPasswordHash(user.PasswordHash, loginDto.Password)) { return new ApiRes(403, false, "Password incorrect", new { }); } @@ -35,7 +37,7 @@ public async Task Login(LoginDto loginDto) var loginResDto = _mapper.Map(user); var token = _jwtHelper.GenerateJwt(user); - return new ApiRes(200, true, "login succcess", new { access_token=token ,user=loginResDto,role=loginResDto.Role}); + return new ApiRes(200, true, "login succcess", new { access_token = token, user = loginResDto, role = loginResDto.Role }); } public async Task Register(RegisterDto registerDto) @@ -63,7 +65,7 @@ public async Task Register(RegisterDto registerDto) 201, true, "User created successfully!", - new { access_token=token,user=userResponse } + new { access_token = token, user = userResponse } ); } catch (Exception ex)