Refactor user command handlers and update JWT token logic

This commit is contained in:
masoodafar-web
2025-11-14 04:37:35 +03:30
parent c48a04d5f9
commit 2944e3b648
8 changed files with 75 additions and 12 deletions

View File

@@ -1,16 +1,24 @@
using CMSMicroservice.Protobuf.Protos.User;
namespace FrontOffice.BFF.Application.UserCQ.Commands.SetPasswordForUser;
public class SetPasswordForUserCommandHandler : IRequestHandler<SetPasswordForUserCommand, Unit>
{
private readonly IApplicationContractContext _context;
private readonly ICurrentUserService _currentUserService;
public SetPasswordForUserCommandHandler(IApplicationContractContext context)
public SetPasswordForUserCommandHandler(IApplicationContractContext context, ICurrentUserService currentUserService)
{
_context = context;
_currentUserService = currentUserService;
}
public async Task<Unit> Handle(SetPasswordForUserCommand request, CancellationToken cancellationToken)
{
//TODO: Implement your business logic
var setPasswordRequest = request.Adapt<SetPasswordForUserRequest>();
setPasswordRequest.UserId = long.Parse(_currentUserService.UserId ?? throw new InvalidOperationException());
await _context.User.SetPasswordForUserAsync(setPasswordRequest, cancellationToken: cancellationToken);
return new Unit();
}
}

View File

@@ -1,6 +1,7 @@
using CMSMicroservice.Protobuf.Protos.User;
namespace FrontOffice.BFF.Application.UserCQ.Commands.UpdateUser;
public class UpdateUserCommandHandler : IRequestHandler<UpdateUserCommand, Unit>
{
private readonly IApplicationContractContext _context;
@@ -12,7 +13,56 @@ public class UpdateUserCommandHandler : IRequestHandler<UpdateUserCommand, Unit>
public async Task<Unit> Handle(UpdateUserCommand request, CancellationToken cancellationToken)
{
await _context.User.UpdateUserAsync(request: request.Adapt<UpdateUserRequest>(), cancellationToken: cancellationToken);
var updatingUserRequest = new UpdateUserRequest();
var existUser = await _context.User.GetUserAsync(new GetUserRequest()
{
Id = request.Id
}, cancellationToken: cancellationToken);
if (existUser == null)
throw new NotFoundException("User not found");
if (!string.IsNullOrEmpty(request.FirstName))
{
updatingUserRequest.FirstName = request.FirstName;
}
if (!string.IsNullOrEmpty(request.LastName))
{
updatingUserRequest.LastName = request.LastName;
}
if (!string.IsNullOrEmpty(request.NationalCode))
{
updatingUserRequest.NationalCode = request.NationalCode;
}
if (request.BirthDate.HasValue)
{
updatingUserRequest.BirthDate = Timestamp.FromDateTime(DateTime.SpecifyKind(request.BirthDate.Value, DateTimeKind.Utc));;
}
if (!string.IsNullOrEmpty(request.AvatarPath))
{
updatingUserRequest.AvatarPath = request.AvatarPath;
}
// if (request.AvatarFile!= null)
// {
// }
if (request.PushNotifications!=existUser.PushNotifications)
{
updatingUserRequest.PushNotifications = request.PushNotifications;
}
if (request.EmailNotifications!=existUser.EmailNotifications)
{
updatingUserRequest.EmailNotifications = request.EmailNotifications;
}
if (request.SmsNotifications!=existUser.SmsNotifications)
{
updatingUserRequest.SmsNotifications = request.SmsNotifications;
}
await _context.User.UpdateUserAsync(request: request.Adapt<UpdateUserRequest>(),
cancellationToken: cancellationToken);
return Unit.Value;
}
}
}

View File

@@ -1,3 +1,5 @@
using CMSMicroservice.Protobuf.Protos.User;
namespace FrontOffice.BFF.Application.UserCQ.Queries.AdminGetJwtToken;
public class AdminGetJwtTokenQueryHandler : IRequestHandler<AdminGetJwtTokenQuery, AdminGetJwtTokenResponseDto>
{
@@ -10,7 +12,8 @@ public class AdminGetJwtTokenQueryHandler : IRequestHandler<AdminGetJwtTokenQuer
public async Task<AdminGetJwtTokenResponseDto> Handle(AdminGetJwtTokenQuery request, CancellationToken cancellationToken)
{
//TODO: Implement your business logic
return new AdminGetJwtTokenResponseDto();
var response = await _context.User.AdminGetJwtTokenAsync(request.Adapt<AdminGetJwtTokenRequest>(),
cancellationToken: cancellationToken);
return response.Adapt<AdminGetJwtTokenResponseDto>();
}
}

View File

@@ -7,7 +7,7 @@
<ItemGroup>
<PackageReference Include="Afrino.PYMSMicroservice.Protobuf" Version="0.0.11" />
<PackageReference Include="Foursat.CMSMicroservice.Protobuf" Version="0.0.117" />
<PackageReference Include="Foursat.CMSMicroservice.Protobuf" Version="0.0.118" />
<PackageReference Include="Google.Protobuf" Version="3.33.0" />
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.54.0" />
<PackageReference Include="Grpc.Tools" Version="2.76.0">

View File

@@ -31,8 +31,8 @@ public static class ConfigureServices
jwtBearerOptions.RequireHttpsMetadata = false;
jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateIssuer = false,//todo change to true in production
ValidateAudience = false,//todo change to true in production
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = configuration["JwtIssuer"],

View File

@@ -1,4 +1,3 @@
using FrontOffice.BFF.Protobuf.Protos.User;
using FrontOffice.BFF.WebApi.Common.Services;
using FrontOffice.BFF.Application.UserCQ.Commands.UpdateUser;
using FrontOffice.BFF.Application.UserCQ.Commands.DeleteUser;
@@ -8,6 +7,8 @@ using FrontOffice.BFF.Application.UserCQ.Commands.CreateNewOtpToken;
using FrontOffice.BFF.Application.UserCQ.Commands.VerifyOtpToken;
using FrontOffice.BFF.Application.UserCQ.Queries.AdminGetJwtToken;
using FrontOffice.BFF.Application.UserCQ.Commands.SetPasswordForUser;
using FrontOffice.BFF.User.Protobuf.Protos.User;
namespace FrontOffice.BFF.WebApi.Services;
public class UserService : UserContract.UserContractBase
{
@@ -17,6 +18,7 @@ public class UserService : UserContract.UserContractBase
{
_dispatchRequestToCQRS = dispatchRequestToCQRS;
}
[Authorize(Roles = "user")]
public override async Task<Empty> UpdateUser(UpdateUserRequest request, ServerCallContext context)
{
return await _dispatchRequestToCQRS.Handle<UpdateUserRequest, UpdateUserCommand>(request, context);

View File

@@ -1,5 +1,5 @@
using FluentValidation;
using FrontOfficeMicroservice.Protobuf.Protos.User;
using FrontOffice.BFF.User.Protobuf.Protos.User;
namespace FrontOfficeMicroservice.Protobuf.Validator.User;
public class AdminGetJwtTokenRequestValidator : AbstractValidator<AdminGetJwtTokenRequest>

View File

@@ -1,5 +1,5 @@
using FluentValidation;
using FrontOfficeMicroservice.Protobuf.Protos.User;
using FrontOffice.BFF.User.Protobuf.Protos.User;
namespace FrontOfficeMicroservice.Protobuf.Validator.User;
public class SetPasswordForUserRequestValidator : AbstractValidator<SetPasswordForUserRequest>