From 4ff76018438c0b22737f3f5c6bc6c0eec3450455 Mon Sep 17 00:00:00 2001 From: "Amir.H Khademi" Date: Wed, 12 Jun 2024 11:05:15 +0330 Subject: [PATCH] fix(CheckUserFirstDiscountUser) -Add new command for CheckUserFirstDiscountUser -Fix CheckUserFirstDiscountUser --- .version | 2 +- Netina.Api/Netina.Api.csproj | 4 +-- .../CalculateOrderDiscountCommandHandler.cs | 23 ++++-------- ...CheckUserDiscountFirstUseCommandHandler.cs | 36 +++++++++++++++++++ .../SubmitDiscountActionCommandHandler.cs | 7 +++- .../Commands/DiscountCommands.cs | 3 +- 6 files changed, 54 insertions(+), 21 deletions(-) create mode 100644 Netina.Core/EntityServices/DiscountHandlers/CheckUserDiscountFirstUseCommandHandler.cs diff --git a/.version b/.version index bd614ad..9bfdcc8 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -1.0.5.6 \ No newline at end of file +1.0.6.7 \ No newline at end of file diff --git a/Netina.Api/Netina.Api.csproj b/Netina.Api/Netina.Api.csproj index 69aacfa..d991910 100644 --- a/Netina.Api/Netina.Api.csproj +++ b/Netina.Api/Netina.Api.csproj @@ -6,8 +6,8 @@ enable true Linux - 1.0.5.6 - 1.0.5.6 + 1.0.6.7 + 1.0.6.7 diff --git a/Netina.Core/EntityServices/DiscountHandlers/CalculateOrderDiscountCommandHandler.cs b/Netina.Core/EntityServices/DiscountHandlers/CalculateOrderDiscountCommandHandler.cs index ba1c7ac..0890210 100644 --- a/Netina.Core/EntityServices/DiscountHandlers/CalculateOrderDiscountCommandHandler.cs +++ b/Netina.Core/EntityServices/DiscountHandlers/CalculateOrderDiscountCommandHandler.cs @@ -21,29 +21,20 @@ public class CalculateOrderDiscountCommandHandler : IRequestHandler() - .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 == discount.Code, cancellationToken); - if (discountedUserOrder != null) - throw new AppException("شما یک بار از این کد تخفیف استفاده کرده اید", ApiResultStatusCode.BadRequest); - } + if (discount.IsForFirstPurchase) { if (_currentUserService.UserId != null && Guid.TryParse(_currentUserService.UserId, out Guid firstPurchaseUserId)) { + var customer = await _repositoryWrapper.SetRepository() + .TableNoTracking + .FirstOrDefaultAsync(c => c.UserId == firstPurchaseUserId, cancellationToken); + if (customer == null) + throw new BaseApiException(ApiResultStatusCode.NotFound, "Customer not found"); var userOrderCount = await _repositoryWrapper.SetRepository() .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) throw new AppException("شما قبلا خریدی داشته اید و نمیتوانید از تخفیف اولین خرید استفاده کنید", ApiResultStatusCode.BadRequest); } diff --git a/Netina.Core/EntityServices/DiscountHandlers/CheckUserDiscountFirstUseCommandHandler.cs b/Netina.Core/EntityServices/DiscountHandlers/CheckUserDiscountFirstUseCommandHandler.cs new file mode 100644 index 0000000..6ba28bf --- /dev/null +++ b/Netina.Core/EntityServices/DiscountHandlers/CheckUserDiscountFirstUseCommandHandler.cs @@ -0,0 +1,36 @@ +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 true; + return false; + } + else + throw new BaseApiException(ApiResultStatusCode.BadRequest,"UserId is wrong"); + } +} \ No newline at end of file diff --git a/Netina.Core/EntityServices/OrderBagHandlers/SubmitDiscountActionCommandHandler.cs b/Netina.Core/EntityServices/OrderBagHandlers/SubmitDiscountActionCommandHandler.cs index 5960ae0..ae6ec3c 100644 --- a/Netina.Core/EntityServices/OrderBagHandlers/SubmitDiscountActionCommandHandler.cs +++ b/Netina.Core/EntityServices/OrderBagHandlers/SubmitDiscountActionCommandHandler.cs @@ -20,12 +20,17 @@ public class SubmitDiscountActionCommandHandler : IRequestHandler() .TableNoTracking .FirstOrDefaultAsync(d => d.Code == request.DiscountCode, cancellationToken); if (discount == null || discount.IsExpired()) 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); } else diff --git a/Netina.Domain/CommandQueries/Commands/DiscountCommands.cs b/Netina.Domain/CommandQueries/Commands/DiscountCommands.cs index 0d772e8..acf51b6 100644 --- a/Netina.Domain/CommandQueries/Commands/DiscountCommands.cs +++ b/Netina.Domain/CommandQueries/Commands/DiscountCommands.cs @@ -51,4 +51,5 @@ public sealed record UpdateDiscountCommand( public sealed record DeleteDiscountCommand(Guid Id) : IRequest; -public sealed record CalculateOrderDiscountCommand(string DiscountCode, Order Order) : IRequest; \ No newline at end of file +public sealed record CalculateOrderDiscountCommand(string DiscountCode, Order Order) : IRequest; +public sealed record CheckUserDiscountFirstUseCommand(string DiscountCode) : IRequest; \ No newline at end of file