Skip to content

Commit

Permalink
Merge pull request #7 from AcademiaY4/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
moshdev2213 authored Oct 6, 2024
2 parents ed11f5e + 70f96cb commit 768d0d5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
14 changes: 10 additions & 4 deletions Repositories/Impl/UserRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<User>.Update.Set(u => u.Status, Models.Enums.Status.ACTIVE);
var update = Builders<User>.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<User>.Update.Set(u => u.Status, Models.Enums.Status.DEACTIVATED);
var update = Builders<User>.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<User>.Update.Set(u => u.Status, Models.Enums.Status.ACTIVE);
var update = Builders<User>.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
Expand Down
12 changes: 10 additions & 2 deletions Services/Impl/AdminService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public async Task<ApiRes> CreateUser(CreateUserDto createUserDto)
if (existingUser != null) return new ApiRes(409, false, "User already exists.", new { });

var newUser = _mapper.Map<User>(createUserDto);
// auto activate account when admin create users
newUser.IsApproved = true;

await _adminRepository.CreateNewUser(newUser);
return new ApiRes(201, true, "User created successfully", new { });
}
Expand Down Expand Up @@ -81,8 +84,13 @@ public async Task<ApiRes> 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)
Expand Down
10 changes: 6 additions & 4 deletions Services/Impl/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,22 @@ public AuthService(IMapper mapper, IUserRepo userRepository, JwtHelper jwtHelper
}
public async Task<ApiRes> 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 { });
}

var loginResDto = _mapper.Map<LoginResDto>(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<ApiRes> Register(RegisterDto registerDto)
Expand Down Expand Up @@ -63,7 +65,7 @@ public async Task<ApiRes> Register(RegisterDto registerDto)
201,
true,
"User created successfully!",
new { access_token=token,user=userResponse }
new { access_token = token, user = userResponse }
);
}
catch (Exception ex)
Expand Down

0 comments on commit 768d0d5

Please sign in to comment.