feat: Implement customer profile and referral queries
- Add GetCustomerProfileResponseDto for retrieving customer profile information. - Create GetCustomerReferralsQuery and GetCustomerReferralsQueryHandler to fetch customer referrals with pagination and filtering options. - Introduce GetCustomerReferralsResponseDto to structure the response for customer referrals. - Implement GetCustomerSettingsQuery and GetCustomerSettingsQueryHandler to retrieve user settings. - Add GetCustomerOrder and GetCustomerOrderQueryHandler for fetching specific customer orders. - Create GetCustomerOrderHistoryQuery and GetCustomerOrderHistoryQueryHandler to retrieve order history with filtering options. - Implement GetCustomerOrdersQuery and GetCustomerOrdersQueryHandler for fetching multiple customer orders with filters. - Add GetCustomerWalletChangeLogQuery and GetCustomerWalletChangeLogQueryHandler for retrieving wallet change logs. - Implement GetCustomerWithdrawalSettingsQuery and GetCustomerWithdrawalSettingsQueryHandler for fetching withdrawal settings. - Create GetCustomerWithdrawalsQuery and GetCustomerWithdrawalsQueryHandler to retrieve customer withdrawal requests.
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
||||
using MediatR;
|
||||
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.CreateCustomerAddress;
|
||||
|
||||
/// <summary>
|
||||
/// Command برای ایجاد آدرس جدید برای کاربر فعلی
|
||||
/// </summary>
|
||||
public class CreateCustomerAddressCommand : IRequest<CreateCustomerAddressCommandResponse>
|
||||
{
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string Address { get; set; } = string.Empty;
|
||||
public string PostalCode { get; set; } = string.Empty;
|
||||
public bool IsDefault { get; set; }
|
||||
public long CityId { get; set; }
|
||||
// UserId from ICurrentUserService
|
||||
}
|
||||
|
||||
public class CreateCustomerAddressCommandResponse
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public string Message { get; set; } = string.Empty;
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using CMSMicroservice.Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.CreateCustomerAddress;
|
||||
|
||||
public class CreateCustomerAddressCommandHandler : IRequestHandler<CreateCustomerAddressCommand, CreateCustomerAddressCommandResponse>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
|
||||
public CreateCustomerAddressCommandHandler(IApplicationDbContext context, ICurrentUserService currentUser)
|
||||
{
|
||||
_context = context;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
public async Task<CreateCustomerAddressCommandResponse> Handle(CreateCustomerAddressCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// Extract UserId from JWT token
|
||||
var userId = long.TryParse(_currentUser.UserId, out var id) ? id : 0;
|
||||
if (userId == 0)
|
||||
{
|
||||
throw new UnauthorizedAccessException("User not authenticated");
|
||||
}
|
||||
|
||||
// If this address is set as default, unset other defaults
|
||||
if (request.IsDefault)
|
||||
{
|
||||
var existingDefaults = await _context.UserAddresses
|
||||
.Where(ua => ua.UserId == userId && ua.IsDefault && !ua.IsDeleted)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
foreach (var addr in existingDefaults)
|
||||
{
|
||||
addr.IsDefault = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Create new address
|
||||
var address = new UserAddress
|
||||
{
|
||||
UserId = userId,
|
||||
Title = request.Title,
|
||||
Address = request.Address,
|
||||
PostalCode = request.PostalCode,
|
||||
IsDefault = request.IsDefault,
|
||||
CityId = request.CityId,
|
||||
Created = DateTime.UtcNow
|
||||
};
|
||||
|
||||
_context.UserAddresses.Add(address);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateCustomerAddressCommandResponse
|
||||
{
|
||||
Id = address.Id,
|
||||
Message = "آدرس با موفقیت ایجاد شد"
|
||||
};
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
using MediatR;
|
||||
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.DeleteCustomerAddress;
|
||||
|
||||
/// <summary>
|
||||
/// Command برای حذف آدرس کاربر فعلی
|
||||
/// </summary>
|
||||
public class DeleteCustomerAddressCommand : IRequest<Unit>
|
||||
{
|
||||
public long Id { get; set; }
|
||||
// UserId from ICurrentUserService
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.DeleteCustomerAddress;
|
||||
|
||||
public class DeleteCustomerAddressCommandHandler : IRequestHandler<DeleteCustomerAddressCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
|
||||
public DeleteCustomerAddressCommandHandler(IApplicationDbContext context, ICurrentUserService currentUser)
|
||||
{
|
||||
_context = context;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(DeleteCustomerAddressCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// Extract UserId from JWT token
|
||||
var userId = long.TryParse(_currentUser.UserId, out var id) ? id : 0;
|
||||
if (userId == 0)
|
||||
{
|
||||
throw new UnauthorizedAccessException("User not authenticated");
|
||||
}
|
||||
|
||||
// Find address and verify ownership
|
||||
var address = await _context.UserAddresses
|
||||
.FirstOrDefaultAsync(ua => ua.Id == request.Id && ua.UserId == userId && !ua.IsDeleted, cancellationToken);
|
||||
|
||||
if (address == null)
|
||||
{
|
||||
throw new Exception("آدرس یافت نشد یا به شما تعلق ندارد");
|
||||
}
|
||||
|
||||
// Soft delete
|
||||
address.IsDeleted = true;
|
||||
address.LastModified = DateTime.UtcNow;
|
||||
|
||||
_context.UserAddresses.Update(address);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
using MediatR;
|
||||
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.SetCustomerDefaultAddress;
|
||||
|
||||
/// <summary>
|
||||
/// Command برای تنظیم آدرس پیشفرض کاربر فعلی
|
||||
/// </summary>
|
||||
public class SetCustomerDefaultAddressCommand : IRequest<Unit>
|
||||
{
|
||||
public long Id { get; set; }
|
||||
// UserId from ICurrentUserService
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.SetCustomerDefaultAddress;
|
||||
|
||||
public class SetCustomerDefaultAddressCommandHandler : IRequestHandler<SetCustomerDefaultAddressCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
|
||||
public SetCustomerDefaultAddressCommandHandler(IApplicationDbContext context, ICurrentUserService currentUser)
|
||||
{
|
||||
_context = context;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(SetCustomerDefaultAddressCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// Extract UserId from JWT token
|
||||
var userId = long.TryParse(_currentUser.UserId, out var id) ? id : 0;
|
||||
if (userId == 0)
|
||||
{
|
||||
throw new UnauthorizedAccessException("User not authenticated");
|
||||
}
|
||||
|
||||
// Find address and verify ownership
|
||||
var address = await _context.UserAddresses
|
||||
.FirstOrDefaultAsync(ua => ua.Id == request.Id && ua.UserId == userId && !ua.IsDeleted, cancellationToken);
|
||||
|
||||
if (address == null)
|
||||
{
|
||||
throw new Exception("آدرس یافت نشد یا به شما تعلق ندارد");
|
||||
}
|
||||
|
||||
// Unset all other defaults for this user
|
||||
var existingDefaults = await _context.UserAddresses
|
||||
.Where(ua => ua.UserId == userId && ua.IsDefault && ua.Id != request.Id && !ua.IsDeleted)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
foreach (var addr in existingDefaults)
|
||||
{
|
||||
addr.IsDefault = false;
|
||||
}
|
||||
|
||||
// Set this address as default
|
||||
address.IsDefault = true;
|
||||
address.LastModified = DateTime.UtcNow;
|
||||
|
||||
_context.UserAddresses.Update(address);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
using MediatR;
|
||||
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.UpdateCustomerAddress;
|
||||
|
||||
/// <summary>
|
||||
/// Command برای بهروزرسانی آدرس کاربر فعلی
|
||||
/// </summary>
|
||||
public class UpdateCustomerAddressCommand : IRequest<Unit>
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string Address { get; set; } = string.Empty;
|
||||
public string PostalCode { get; set; } = string.Empty;
|
||||
public bool IsDefault { get; set; }
|
||||
public long CityId { get; set; }
|
||||
// UserId from ICurrentUserService
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.UpdateCustomerAddress;
|
||||
|
||||
public class UpdateCustomerAddressCommandHandler : IRequestHandler<UpdateCustomerAddressCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
|
||||
public UpdateCustomerAddressCommandHandler(IApplicationDbContext context, ICurrentUserService currentUser)
|
||||
{
|
||||
_context = context;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(UpdateCustomerAddressCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// Extract UserId from JWT token
|
||||
var userId = long.TryParse(_currentUser.UserId, out var id) ? id : 0;
|
||||
if (userId == 0)
|
||||
{
|
||||
throw new UnauthorizedAccessException("User not authenticated");
|
||||
}
|
||||
|
||||
// Find address and verify ownership
|
||||
var address = await _context.UserAddresses
|
||||
.FirstOrDefaultAsync(ua => ua.Id == request.Id && ua.UserId == userId && !ua.IsDeleted, cancellationToken);
|
||||
|
||||
if (address == null)
|
||||
{
|
||||
throw new Exception("آدرس یافت نشد یا به شما تعلق ندارد");
|
||||
}
|
||||
|
||||
// If setting as default, unset other defaults
|
||||
if (request.IsDefault && !address.IsDefault)
|
||||
{
|
||||
var existingDefaults = await _context.UserAddresses
|
||||
.Where(ua => ua.UserId == userId && ua.IsDefault && ua.Id != request.Id && !ua.IsDeleted)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
foreach (var addr in existingDefaults)
|
||||
{
|
||||
addr.IsDefault = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Update address
|
||||
address.Title = request.Title;
|
||||
address.Address = request.Address;
|
||||
address.PostalCode = request.PostalCode;
|
||||
address.IsDefault = request.IsDefault;
|
||||
address.CityId = request.CityId;
|
||||
address.LastModified = DateTime.UtcNow;
|
||||
|
||||
_context.UserAddresses.Update(address);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
using MediatR;
|
||||
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Queries.GetCustomerAddresses;
|
||||
|
||||
/// <summary>
|
||||
/// Query برای دریافت لیست آدرسهای کاربر فعلی
|
||||
/// </summary>
|
||||
public class GetCustomerAddressesQuery : IRequest<GetCustomerAddressesQueryResponse>
|
||||
{
|
||||
// UserId from ICurrentUserService
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Queries.GetCustomerAddresses;
|
||||
|
||||
public class GetCustomerAddressesQueryHandler : IRequestHandler<GetCustomerAddressesQuery, GetCustomerAddressesQueryResponse>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
|
||||
public GetCustomerAddressesQueryHandler(IApplicationDbContext context, ICurrentUserService currentUser)
|
||||
{
|
||||
_context = context;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
public async Task<GetCustomerAddressesQueryResponse> Handle(GetCustomerAddressesQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
// Extract UserId from JWT token
|
||||
var userId = long.TryParse(_currentUser.UserId, out var id) ? id : 0;
|
||||
if (userId == 0)
|
||||
{
|
||||
throw new UnauthorizedAccessException("User not authenticated");
|
||||
}
|
||||
|
||||
// Get all addresses for the current user
|
||||
var addresses = await _context.UserAddresses
|
||||
.Where(ua => ua.UserId == userId && !ua.IsDeleted)
|
||||
.OrderByDescending(ua => ua.IsDefault)
|
||||
.ThenByDescending(ua => ua.Created)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
var response = new GetCustomerAddressesQueryResponse();
|
||||
|
||||
foreach (var addr in addresses)
|
||||
{
|
||||
response.Addresses.Add(new CustomerAddressModel
|
||||
{
|
||||
Id = addr.Id,
|
||||
Title = addr.Title,
|
||||
Address = addr.Address,
|
||||
PostalCode = addr.PostalCode,
|
||||
IsDefault = addr.IsDefault,
|
||||
CityId = addr.CityId,
|
||||
CityName = string.Empty, // Will be populated by FrontOffice from City service
|
||||
ProvinceName = string.Empty // Will be populated by FrontOffice from City service
|
||||
});
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Queries.GetCustomerAddresses;
|
||||
|
||||
public class GetCustomerAddressesQueryResponse
|
||||
{
|
||||
public List<CustomerAddressModel> Addresses { get; set; } = new();
|
||||
}
|
||||
|
||||
public class CustomerAddressModel
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string Address { get; set; } = string.Empty;
|
||||
public string PostalCode { get; set; } = string.Empty;
|
||||
public bool IsDefault { get; set; }
|
||||
public long CityId { get; set; }
|
||||
public string CityName { get; set; } = string.Empty;
|
||||
public string ProvinceName { get; set; } = string.Empty;
|
||||
}
|
||||
Reference in New Issue
Block a user