37 lines
1.3 KiB
C#
37 lines
1.3 KiB
C#
using MediatR;
|
|
using Modules.User.Application.Gateways;
|
|
using Modules.User.Application.Models;
|
|
|
|
namespace Modules.User.Application.Commands;
|
|
|
|
public class DeleteAllSessionsCommand : IRequest<bool>
|
|
{
|
|
public Guid AccountId { get; set; } = default!;
|
|
}
|
|
|
|
public class DeleteAllSessionsCommandHandler(IUserGateway userGateway, IAccountGateway accountGateway) : IRequestHandler<DeleteAllSessionsCommand, bool>
|
|
{
|
|
public async Task<bool> Handle(DeleteAllSessionsCommand request, CancellationToken cancellationToken)
|
|
{
|
|
//var user = await userGateway.Get(request.AccountId) ?? throw new UserNotFoundException();
|
|
var user = await userGateway.Get(request.AccountId) ?? throw new Exception("User was not found");
|
|
user.Account.ClearAllSessions();
|
|
return await accountGateway.UpdateSessions(user.Account.Id, user.Account.Sessions.Select(q => new Session
|
|
{
|
|
Id = q.Id,
|
|
RefreshToken = q.RefreshToken,
|
|
ClientInfo = new()
|
|
{
|
|
UserAgent = q.ClientInfo.UserAgent,
|
|
Location = new()
|
|
{
|
|
Country = q.ClientInfo.Country,
|
|
Region = q.ClientInfo.Region,
|
|
}
|
|
},
|
|
AccountId = q.AccountId,
|
|
ExpiredDate = q.ExpiredDate,
|
|
}));
|
|
}
|
|
}
|