using Netina.Domain.Entities.Orders; using Netina.Repository.Abstracts; namespace Netina.Repository.Handlers.Orders; public class GetOrderLDtoQueryHandler(IRepositoryWrapper repositoryWrapper,ICurrentUserService currentUserService) : IRequestHandler { public async Task Handle(GetOrderLDtoQuery request, CancellationToken cancellationToken) { if (currentUserService.Permissions == null) throw new BaseApiException(ApiResultStatusCode.UnAuthorized); if (request.Id == default) throw new AppException("Order id is null"); var order = await repositoryWrapper.SetRepository() .TableNoTracking .Where(o => o.Id == request.Id) .Select(OrderMapper.ProjectToLDto) .FirstOrDefaultAsync(cancellationToken); if (currentUserService.Permissions.Contains(ApplicationPermission.ViewMineOrders) && !currentUserService.Permissions.Contains(ApplicationPermission.ViewAllOrders)) { if (currentUserService.UserId.IsNullOrEmpty() || !Guid.TryParse(currentUserService.UserId, out Guid userId)) throw new BaseApiException(ApiResultStatusCode.UnAuthorized); var customer = await repositoryWrapper.SetRepository() .TableNoTracking .FirstOrDefaultAsync(c => c.UserId == userId, cancellationToken); if (customer == null || order.CustomerId != customer.Id) throw new BaseApiException(ApiResultStatusCode.UnAuthorized); } if (order == null) throw new AppException("Order not found", ApiResultStatusCode.NotFound); return order; } }