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; public record WorkScheduleResponse( bool IsAlwaysReady, Dictionary>? SchedulePerDay ); public record TimePeriod( [property: JsonPropertyName("start")] string Start, [property: JsonPropertyName("end")] string End ); public record ProfileResponse( Guid Id, string Phone, List Roles, string FirstName, string LastName, string? Patronymic, string? CompanyName, string? Inn, string? Description, string? Location, string? CurrentLocation, string FullName, string? AvatarUrl, List Competencies, WorkScheduleResponse? WorkSchedule); public class GetProfileQueryHandler : IRequestHandler { private readonly IAccountRepository _accountRepository; private readonly ICurrentUserService _currentUserService; public GetProfileQueryHandler(IAccountRepository accountRepository, ICurrentUserService currentUserService) { _accountRepository = accountRepository; _currentUserService = currentUserService; } public async Task 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>? schedulePerDay = null; if (account.Profile.WorkSchedule.WorkingDays != null) { var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; schedulePerDay = JsonSerializer.Deserialize>>(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); } }