fix(CheckUserFirstDiscountUser)

-Add new command for CheckUserFirstDiscountUser
-Fix CheckUserFirstDiscountUser
subProduct
Amir Hossein Khademi 2024-06-12 11:05:15 +03:30
parent 5edd394252
commit 4ff7601843
6 changed files with 54 additions and 21 deletions

View File

@ -1 +1 @@
1.0.5.6 1.0.6.7

View File

@ -6,8 +6,8 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization> <InvariantGlobalization>true</InvariantGlobalization>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<AssemblyVersion>1.0.5.6</AssemblyVersion> <AssemblyVersion>1.0.6.7</AssemblyVersion>
<FileVersion>1.0.5.6</FileVersion> <FileVersion>1.0.6.7</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -21,29 +21,20 @@ public class CalculateOrderDiscountCommandHandler : IRequestHandler<CalculateOrd
if (discount == null) if (discount == null)
throw new AppException("تخفیف وجود منقضی شده است یا وجود ندارد", ApiResultStatusCode.NotFound); throw new AppException("تخفیف وجود منقضی شده است یا وجود ندارد", ApiResultStatusCode.NotFound);
if (_currentUserService.UserId != null && Guid.TryParse(_currentUserService.UserId, out Guid userId))
{
var customer = await _repositoryWrapper.SetRepository<Customer>()
.TableNoTracking
.FirstOrDefaultAsync(c => c.UserId == userId, cancellationToken);
if (customer == null)
throw new BaseApiException(ApiResultStatusCode.NotFound, "Customer not found");
var discountedUserOrder = await _repositoryWrapper.SetRepository<Order>()
.TableNoTracking
.FirstOrDefaultAsync(f => f.CustomerId == customer.Id && f.DiscountCode == discount.Code, cancellationToken);
if (discountedUserOrder != null)
throw new AppException("شما یک بار از این کد تخفیف استفاده کرده اید", ApiResultStatusCode.BadRequest);
}
if (discount.IsForFirstPurchase) if (discount.IsForFirstPurchase)
{ {
if (_currentUserService.UserId != null && Guid.TryParse(_currentUserService.UserId, out Guid firstPurchaseUserId)) if (_currentUserService.UserId != null && Guid.TryParse(_currentUserService.UserId, out Guid firstPurchaseUserId))
{ {
var customer = await _repositoryWrapper.SetRepository<Customer>()
.TableNoTracking
.FirstOrDefaultAsync(c => c.UserId == firstPurchaseUserId, cancellationToken);
if (customer == null)
throw new BaseApiException(ApiResultStatusCode.NotFound, "Customer not found");
var userOrderCount = await _repositoryWrapper.SetRepository<Order>() var userOrderCount = await _repositoryWrapper.SetRepository<Order>()
.TableNoTracking .TableNoTracking
.CountAsync(f => f.CustomerId == firstPurchaseUserId && f.DiscountCode == discount.Code, cancellationToken); .CountAsync(f => f.CustomerId == customer.Id && f.DiscountCode == discount.Code, cancellationToken);
if (userOrderCount > 0) if (userOrderCount > 0)
throw new AppException("شما قبلا خریدی داشته اید و نمیتوانید از تخفیف اولین خرید استفاده کنید", ApiResultStatusCode.BadRequest); throw new AppException("شما قبلا خریدی داشته اید و نمیتوانید از تخفیف اولین خرید استفاده کنید", ApiResultStatusCode.BadRequest);
} }

View File

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

View File

@ -20,12 +20,17 @@ public class SubmitDiscountActionCommandHandler : IRequestHandler<SubmitDiscount
if (request.DiscountCode != null) if (request.DiscountCode != null)
{ {
var discount = await _repositoryWrapper.SetRepository<Discount>() var discount = await _repositoryWrapper.SetRepository<Discount>()
.TableNoTracking .TableNoTracking
.FirstOrDefaultAsync(d => d.Code == request.DiscountCode, cancellationToken); .FirstOrDefaultAsync(d => d.Code == request.DiscountCode, cancellationToken);
if (discount == null || discount.IsExpired()) if (discount == null || discount.IsExpired())
throw new AppException("تخفیف منقضی شده است یا وجود ندارد", ApiResultStatusCode.NotFound); throw new AppException("تخفیف منقضی شده است یا وجود ندارد", ApiResultStatusCode.NotFound);
var isFirstUserOfDiscount = await _mediator.Send(new CheckUserDiscountFirstUseCommand(request.DiscountCode), cancellationToken);
if (!isFirstUserOfDiscount)
throw new BaseApiException(ApiResultStatusCode.BadRequest, "شما یک بار از این کد تخفیف استفاده نموده اید و قابلیت استفاده مجدد ندارید");
order.SetDiscount(request.DiscountCode); order.SetDiscount(request.DiscountCode);
} }
else else

View File

@ -51,4 +51,5 @@ public sealed record UpdateDiscountCommand(
public sealed record DeleteDiscountCommand(Guid Id) : IRequest<bool>; public sealed record DeleteDiscountCommand(Guid Id) : IRequest<bool>;
public sealed record CalculateOrderDiscountCommand(string DiscountCode, Order Order) : IRequest<double>; public sealed record CalculateOrderDiscountCommand(string DiscountCode, Order Order) : IRequest<double>;
public sealed record CheckUserDiscountFirstUseCommand(string DiscountCode) : IRequest<bool>;