Files
nashel-backend/src/Modules/Identity/Application/Queries/GetProfile/GetProfileQuery.cs
2026-02-27 23:32:09 +03:00

107 lines
3.4 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
using MediatR;
using Nashel.BuildingBlocks.Application.Abstractions;
using Nashel.Modules.Identity.Domain.Repositories;
namespace Nashel.Modules.Identity.Application.Queries.GetProfile;
public record GetProfileQuery : IRequest<ProfileResponse>;
public record WorkScheduleResponse(
bool IsAlwaysReady,
Dictionary<string, List<TimePeriod>>? SchedulePerDay
);
public record TimePeriod(
[property: JsonPropertyName("start")] string Start,
[property: JsonPropertyName("end")] string End
);
public record ProfileResponse(
Guid Id,
string Phone,
List<string> Roles,
string FirstName,
string LastName,
string? Patronymic,
string? CompanyName,
string? Inn,
string? Description,
string? Location,
string? CurrentLocation,
string FullName,
string? AvatarUrl,
List<string> Competencies,
WorkScheduleResponse? WorkSchedule);
public class GetProfileQueryHandler : IRequestHandler<GetProfileQuery, ProfileResponse>
{
private readonly IAccountRepository _accountRepository;
private readonly ICurrentUserService _currentUserService;
public GetProfileQueryHandler(IAccountRepository accountRepository, ICurrentUserService currentUserService)
{
_accountRepository = accountRepository;
_currentUserService = currentUserService;
}
public async Task<ProfileResponse> Handle(GetProfileQuery request, CancellationToken cancellationToken)
{
var userId = _currentUserService.UserId;
if (userId == null)
{
throw new UnauthorizedAccessException();
}
var account = await _accountRepository.GetByIdAsync(userId.Value, cancellationToken);
if (account == null)
{
throw new Exception("Account not found");
}
var fullName = $"{account.Profile.FirstName} {account.Profile.LastName}";
if (!string.IsNullOrEmpty(account.Profile.Patronymic))
{
fullName += $" {account.Profile.Patronymic}";
}
// Парсинг WorkSchedule
WorkScheduleResponse? workScheduleResponse = null;
if (account.Profile.WorkSchedule != null)
{
Dictionary<string, List<TimePeriod>>? schedulePerDay = null;
if (account.Profile.WorkSchedule.WorkingDays != null)
{
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
schedulePerDay = JsonSerializer.Deserialize<Dictionary<string, List<TimePeriod>>>(account.Profile.WorkSchedule.WorkingDays, options);
}
workScheduleResponse = new WorkScheduleResponse(
account.Profile.WorkSchedule.IsAlwaysReady,
schedulePerDay
);
}
return new ProfileResponse(
account.Id,
account.Phone,
account.Roles.Select(r => r.ToString()).ToList(),
account.Profile.FirstName,
account.Profile.LastName,
account.Profile.Patronymic,
account.Profile.CompanyName,
account.Profile.Inn,
account.Profile.Description,
account.Profile.Location,
account.Profile.CurrentLocation,
fullName,
account.Profile.AvatarUrl,
account.Profile.Competencies.Select(c => c.Name).ToList(),
workScheduleResponse);
}
}