2024-06-12 11:05:15 +03:30
|
|
|
|
namespace Netina.Core.EntityServices.DiscountHandlers;
|
|
|
|
|
|
|
2024-08-09 21:55:16 +03:30
|
|
|
|
public class CheckUserDiscountFirstUseCommandHandler(
|
|
|
|
|
|
IRepositoryWrapper repositoryWrapper,
|
|
|
|
|
|
ICurrentUserService currentUserService)
|
|
|
|
|
|
: IRequestHandler<CheckUserDiscountFirstUseCommand, bool>
|
2024-06-12 11:05:15 +03:30
|
|
|
|
{
|
|
|
|
|
|
public async Task<bool> Handle(CheckUserDiscountFirstUseCommand request, CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
2024-08-09 21:55:16 +03:30
|
|
|
|
if (currentUserService.UserId != null && Guid.TryParse(currentUserService.UserId, out Guid userId))
|
2024-06-12 11:05:15 +03:30
|
|
|
|
{
|
2024-08-09 21:55:16 +03:30
|
|
|
|
var customer = await repositoryWrapper.SetRepository<Customer>()
|
2024-06-12 11:05:15 +03:30
|
|
|
|
.TableNoTracking
|
|
|
|
|
|
.FirstOrDefaultAsync(c => c.UserId == userId, cancellationToken);
|
|
|
|
|
|
if (customer == null)
|
|
|
|
|
|
throw new BaseApiException(ApiResultStatusCode.NotFound, "Customer not found");
|
|
|
|
|
|
|
2024-08-09 21:55:16 +03:30
|
|
|
|
var discountedUserOrder = await repositoryWrapper.SetRepository<Order>()
|
2024-06-12 11:05:15 +03:30
|
|
|
|
.TableNoTracking
|
|
|
|
|
|
.FirstOrDefaultAsync(f => f.CustomerId == customer.Id && f.DiscountCode == request.DiscountCode,
|
|
|
|
|
|
cancellationToken);
|
|
|
|
|
|
if (discountedUserOrder != null)
|
2024-06-24 22:32:44 +03:30
|
|
|
|
return false;
|
|
|
|
|
|
return true;
|
2024-06-12 11:05:15 +03:30
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
throw new BaseApiException(ApiResultStatusCode.BadRequest,"UserId is wrong");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|