Add customer communication system
This commit is contained in:
@@ -10,16 +10,19 @@ public class OrderService : IOrderService
|
||||
{
|
||||
private readonly LittleShopContext _context;
|
||||
private readonly ILogger<OrderService> _logger;
|
||||
private readonly ICustomerService _customerService;
|
||||
|
||||
public OrderService(LittleShopContext context, ILogger<OrderService> logger)
|
||||
public OrderService(LittleShopContext context, ILogger<OrderService> logger, ICustomerService customerService)
|
||||
{
|
||||
_context = context;
|
||||
_logger = logger;
|
||||
_customerService = customerService;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<OrderDto>> GetAllOrdersAsync()
|
||||
{
|
||||
var orders = await _context.Orders
|
||||
.Include(o => o.Customer)
|
||||
.Include(o => o.Items)
|
||||
.ThenInclude(oi => oi.Product)
|
||||
.Include(o => o.Payments)
|
||||
@@ -32,6 +35,7 @@ public class OrderService : IOrderService
|
||||
public async Task<IEnumerable<OrderDto>> GetOrdersByIdentityAsync(string identityReference)
|
||||
{
|
||||
var orders = await _context.Orders
|
||||
.Include(o => o.Customer)
|
||||
.Include(o => o.Items)
|
||||
.ThenInclude(oi => oi.Product)
|
||||
.Include(o => o.Payments)
|
||||
@@ -45,6 +49,7 @@ public class OrderService : IOrderService
|
||||
public async Task<OrderDto?> GetOrderByIdAsync(Guid id)
|
||||
{
|
||||
var order = await _context.Orders
|
||||
.Include(o => o.Customer)
|
||||
.Include(o => o.Items)
|
||||
.ThenInclude(oi => oi.Product)
|
||||
.Include(o => o.Payments)
|
||||
@@ -59,10 +64,38 @@ public class OrderService : IOrderService
|
||||
|
||||
try
|
||||
{
|
||||
// Handle customer creation/linking during checkout
|
||||
Guid? customerId = null;
|
||||
string? identityReference = null;
|
||||
|
||||
if (createOrderDto.CustomerInfo != null)
|
||||
{
|
||||
// Create customer during checkout process
|
||||
var customer = await _customerService.GetOrCreateCustomerAsync(
|
||||
createOrderDto.CustomerInfo.TelegramUserId,
|
||||
createOrderDto.CustomerInfo.TelegramDisplayName,
|
||||
createOrderDto.CustomerInfo.TelegramUsername,
|
||||
createOrderDto.CustomerInfo.TelegramFirstName,
|
||||
createOrderDto.CustomerInfo.TelegramLastName);
|
||||
|
||||
customerId = customer?.Id;
|
||||
}
|
||||
else if (createOrderDto.CustomerId.HasValue)
|
||||
{
|
||||
// Order for existing customer
|
||||
customerId = createOrderDto.CustomerId;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Anonymous order (legacy support)
|
||||
identityReference = createOrderDto.IdentityReference;
|
||||
}
|
||||
|
||||
var order = new Order
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
IdentityReference = createOrderDto.IdentityReference,
|
||||
CustomerId = customerId,
|
||||
IdentityReference = identityReference,
|
||||
Status = OrderStatus.PendingPayment,
|
||||
TotalAmount = 0,
|
||||
Currency = "GBP",
|
||||
@@ -105,8 +138,16 @@ public class OrderService : IOrderService
|
||||
await _context.SaveChangesAsync();
|
||||
await transaction.CommitAsync();
|
||||
|
||||
_logger.LogInformation("Created order {OrderId} for identity {Identity} with total {Total}",
|
||||
order.Id, createOrderDto.IdentityReference, totalAmount);
|
||||
if (customerId.HasValue)
|
||||
{
|
||||
_logger.LogInformation("Created order {OrderId} for customer {CustomerId} with total {Total}",
|
||||
order.Id, customerId.Value, totalAmount);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("Created order {OrderId} for identity {Identity} with total {Total}",
|
||||
order.Id, identityReference, totalAmount);
|
||||
}
|
||||
|
||||
// Reload order with includes
|
||||
var createdOrder = await GetOrderByIdAsync(order.Id);
|
||||
@@ -175,8 +216,26 @@ public class OrderService : IOrderService
|
||||
return new OrderDto
|
||||
{
|
||||
Id = order.Id,
|
||||
CustomerId = order.CustomerId,
|
||||
IdentityReference = order.IdentityReference,
|
||||
Status = order.Status,
|
||||
Customer = order.Customer != null ? new CustomerSummaryDto
|
||||
{
|
||||
Id = order.Customer.Id,
|
||||
DisplayName = !string.IsNullOrEmpty(order.Customer.TelegramDisplayName) ? order.Customer.TelegramDisplayName :
|
||||
!string.IsNullOrEmpty(order.Customer.TelegramUsername) ? $"@{order.Customer.TelegramUsername}" :
|
||||
$"{order.Customer.TelegramFirstName} {order.Customer.TelegramLastName}".Trim(),
|
||||
TelegramUsername = order.Customer.TelegramUsername,
|
||||
TotalOrders = order.Customer.TotalOrders,
|
||||
TotalSpent = order.Customer.TotalSpent,
|
||||
CustomerType = order.Customer.TotalOrders == 0 ? "New" :
|
||||
order.Customer.TotalOrders == 1 ? "First-time" :
|
||||
order.Customer.TotalOrders < 5 ? "Regular" :
|
||||
order.Customer.TotalOrders < 20 ? "Loyal" : "VIP",
|
||||
RiskScore = order.Customer.RiskScore,
|
||||
LastActiveAt = order.Customer.LastActiveAt,
|
||||
IsBlocked = order.Customer.IsBlocked
|
||||
} : null,
|
||||
TotalAmount = order.TotalAmount,
|
||||
Currency = order.Currency,
|
||||
ShippingName = order.ShippingName,
|
||||
|
||||
Reference in New Issue
Block a user