namespace Netina.Core.EntityServices.DiscountHandlers; public class CheckUserDiscountFirstUseCommandHandler : IRequestHandler { private readonly IRepositoryWrapper _repositoryWrapper; private readonly ICurrentUserService _currentUserService; public CheckUserDiscountFirstUseCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService) { _repositoryWrapper = repositoryWrapper; _currentUserService = currentUserService; } public async Task Handle(CheckUserDiscountFirstUseCommand request, CancellationToken cancellationToken) { if (_currentUserService.UserId != null && Guid.TryParse(_currentUserService.UserId, out Guid userId)) { var customer = await _repositoryWrapper.SetRepository() .TableNoTracking .FirstOrDefaultAsync(c => c.UserId == userId, cancellationToken); if (customer == null) throw new BaseApiException(ApiResultStatusCode.NotFound, "Customer not found"); var discountedUserOrder = await _repositoryWrapper.SetRepository() .TableNoTracking .FirstOrDefaultAsync(f => f.CustomerId == customer.Id && f.DiscountCode == request.DiscountCode, cancellationToken); if (discountedUserOrder != null) return false; return true; } else throw new BaseApiException(ApiResultStatusCode.BadRequest,"UserId is wrong"); } }