Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #7

Merged
merged 2 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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