feat: Implement file management and authorization features
Build and Deploy to Kubernetes / build-and-deploy (push) Failing after 3m9s
Build and Deploy to Kubernetes / build-and-deploy (push) Failing after 3m9s
- Add RequiresPermissionAttribute for gRPC method access control. - Create IFileManagementService interface for file upload and management. - Implement AddProductImageCommand and handler for adding product images. - Implement CreateNewProductsCommand and handler for creating new products with image uploads. - Implement DeleteProductsCommand and handler for deleting products and their associations. - Implement RemoveProductImageCommand and handler for removing product images from galleries. - Implement UpdateProductsCommand and handler for updating product details and images. - Create GetProductGalleryQuery and handler for retrieving product galleries. - Implement PermissionService for role-based access control using JWT claims. - Implement FileManagementService for handling file uploads and image optimization. - Define gRPC service and messages for file management in fms.proto. - Add FluentValidation for request validation in various commands. - Create PermissionInterceptor for enforcing permissions on gRPC methods.
This commit is contained in:
@@ -9,20 +9,35 @@ using CMSMicroservice.Application.TransactionsCQ.Commands.VerifyTransaction;
|
||||
using CMSMicroservice.Application.TransactionsCQ.Commands.RefundTransaction;
|
||||
using CMSMicroservice.Application.TransactionsCQ.Queries.GetCustomerTransaction;
|
||||
using CMSMicroservice.Application.TransactionsCQ.Queries.GetCustomerTransactionsByFilter;
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using AppModels = CMSMicroservice.Application.Common.Models;
|
||||
using Grpc.Core;
|
||||
using MediatR;
|
||||
using Mapster;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq;
|
||||
|
||||
namespace CMSMicroservice.WebApi.Services;
|
||||
public class TransactionsService : TransactionsContract.TransactionsContractBase
|
||||
{
|
||||
private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS;
|
||||
private readonly ISender _sender;
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IPaymentGatewayService _paymentGateway;
|
||||
|
||||
public TransactionsService(IDispatchRequestToCQRS dispatchRequestToCQRS, ISender sender)
|
||||
public TransactionsService(
|
||||
IDispatchRequestToCQRS dispatchRequestToCQRS,
|
||||
ISender sender,
|
||||
IApplicationDbContext context,
|
||||
ICurrentUserService currentUserService,
|
||||
IPaymentGatewayService paymentGateway)
|
||||
{
|
||||
_dispatchRequestToCQRS = dispatchRequestToCQRS;
|
||||
_sender = sender;
|
||||
_context = context;
|
||||
_currentUserService = currentUserService;
|
||||
_paymentGateway = paymentGateway;
|
||||
}
|
||||
public override async Task<CreateNewTransactionsResponse> CreateNewTransactions(CreateNewTransactionsRequest request, ServerCallContext context)
|
||||
{
|
||||
@@ -121,26 +136,114 @@ public class TransactionsService : TransactionsContract.TransactionsContractBase
|
||||
|
||||
public override async Task<CustomerPaymentRequestResponse> CustomerPaymentRequest(CustomerPaymentRequestRequest request, ServerCallContext context)
|
||||
{
|
||||
// Mock payment gateway response
|
||||
var userId = GetCurrentUserId();
|
||||
|
||||
// Get user mobile for payment gateway
|
||||
var user = await _context.Users
|
||||
.AsNoTracking()
|
||||
.Where(u => u.Id == userId)
|
||||
.Select(u => new { u.Mobile, u.Email })
|
||||
.FirstOrDefaultAsync(context.CancellationToken);
|
||||
|
||||
// Create transaction record in DB
|
||||
var transaction = new CMSMicroservice.Domain.Entities.Transaction
|
||||
{
|
||||
Amount = request.Amount,
|
||||
Description = request.Description ?? "پرداخت آنلاین",
|
||||
PaymentStatus = Domain.Enums.PaymentStatus.Pending,
|
||||
Type = Domain.Enums.TransactionType.DepositIpg
|
||||
};
|
||||
|
||||
_context.Transactions.Add(transaction);
|
||||
await _context.SaveChangesAsync(context.CancellationToken);
|
||||
|
||||
// Initiate payment with gateway
|
||||
var paymentResult = await _paymentGateway.InitiatePaymentAsync(new PaymentRequest
|
||||
{
|
||||
Amount = request.Amount,
|
||||
UserId = userId,
|
||||
Mobile = request.Mobile ?? user?.Mobile ?? string.Empty,
|
||||
Description = request.Description ?? "پرداخت آنلاین",
|
||||
CallbackUrl = request.CallbackUrl
|
||||
}, context.CancellationToken);
|
||||
|
||||
if (!paymentResult.IsSuccess)
|
||||
{
|
||||
// Update transaction status to failed
|
||||
transaction.PaymentStatus = Domain.Enums.PaymentStatus.Reject;
|
||||
await _context.SaveChangesAsync(context.CancellationToken);
|
||||
|
||||
throw new RpcException(new Status(StatusCode.Internal,
|
||||
paymentResult.ErrorMessage ?? "خطا در ارتباط با درگاه پرداخت"));
|
||||
}
|
||||
|
||||
// Save RefId from gateway
|
||||
transaction.RefId = paymentResult.RefId;
|
||||
await _context.SaveChangesAsync(context.CancellationToken);
|
||||
|
||||
return new CustomerPaymentRequestResponse
|
||||
{
|
||||
PaymentGWUrl = $"https://payment.gateway.com/payment?amount={request.Amount}&callback={request.CallbackUrl}&description={request.Description}"
|
||||
PaymentGWUrl = paymentResult.GatewayUrl ?? string.Empty
|
||||
};
|
||||
}
|
||||
|
||||
public override async Task<CustomerPaymentVerificationResponse> CustomerPaymentVerification(CustomerPaymentVerificationRequest request, ServerCallContext context)
|
||||
{
|
||||
// Mock payment verification response
|
||||
bool isSuccessful = request.Status == "OK";
|
||||
// Find the transaction by authority/refId
|
||||
var transaction = await _context.Transactions
|
||||
.Where(t => t.RefId == request.Authority && !t.IsDeleted)
|
||||
.FirstOrDefaultAsync(context.CancellationToken);
|
||||
|
||||
if (transaction == null)
|
||||
throw new RpcException(new Status(StatusCode.NotFound, "تراکنش یافت نشد"));
|
||||
|
||||
// If status from gateway callback is not OK, mark as rejected
|
||||
if (request.Status != "OK")
|
||||
{
|
||||
transaction.PaymentStatus = Domain.Enums.PaymentStatus.Reject;
|
||||
await _context.SaveChangesAsync(context.CancellationToken);
|
||||
|
||||
return new CustomerPaymentVerificationResponse
|
||||
{
|
||||
Id = transaction.Id,
|
||||
PaymentStatus = false,
|
||||
Message = "پرداخت توسط کاربر لغو شد",
|
||||
VerificationStatusCode = -1
|
||||
};
|
||||
}
|
||||
|
||||
// Verify with gateway
|
||||
var verifyResult = await _paymentGateway.VerifyPaymentAsync(
|
||||
request.Authority, request.Status, context.CancellationToken);
|
||||
|
||||
if (verifyResult.IsSuccess)
|
||||
{
|
||||
transaction.PaymentStatus = Domain.Enums.PaymentStatus.Success;
|
||||
transaction.PaymentDate = DateTime.UtcNow;
|
||||
transaction.RefId = verifyResult.RefId;
|
||||
}
|
||||
else
|
||||
{
|
||||
transaction.PaymentStatus = Domain.Enums.PaymentStatus.Reject;
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync(context.CancellationToken);
|
||||
|
||||
return new CustomerPaymentVerificationResponse
|
||||
{
|
||||
Id = 12345,
|
||||
PaymentStatus = isSuccessful,
|
||||
Message = isSuccessful ? "پرداخت با موفقیت انجام شد" : "پرداخت ناموفق",
|
||||
RefId = isSuccessful ? "REF123456789" : null,
|
||||
OrderId = "ORDER001",
|
||||
VerificationStatusCode = isSuccessful ? 101 : 102
|
||||
Id = transaction.Id,
|
||||
PaymentStatus = verifyResult.IsSuccess,
|
||||
Message = verifyResult.IsSuccess ? "پرداخت با موفقیت انجام شد" : (verifyResult.Message ?? "پرداخت ناموفق"),
|
||||
RefId = verifyResult.RefId ?? string.Empty,
|
||||
OrderId = string.Empty,
|
||||
VerificationStatusCode = verifyResult.IsSuccess ? 100 : -1
|
||||
};
|
||||
}
|
||||
|
||||
private long GetCurrentUserId()
|
||||
{
|
||||
if (long.TryParse(_currentUserService.UserId, out var userId) && userId > 0)
|
||||
return userId;
|
||||
throw new RpcException(new Status(StatusCode.Unauthenticated, "لطفاً وارد حساب کاربری خود شوید"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user