Implement bidirectional customer conversations with customer-based grouping and order tagging
This commit is contained in:
72
LittleShop/Areas/Admin/Controllers/MessagesController.cs
Normal file
72
LittleShop/Areas/Admin/Controllers/MessagesController.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using LittleShop.DTOs;
|
||||
using LittleShop.Services;
|
||||
using LittleShop.Models;
|
||||
|
||||
namespace LittleShop.Areas.Admin.Controllers;
|
||||
|
||||
[Area("Admin")]
|
||||
[Authorize(Policy = "AdminOnly")]
|
||||
public class MessagesController : Controller
|
||||
{
|
||||
private readonly ICustomerMessageService _messageService;
|
||||
private readonly ILogger<MessagesController> _logger;
|
||||
|
||||
public MessagesController(ICustomerMessageService messageService, ILogger<MessagesController> logger)
|
||||
{
|
||||
_messageService = messageService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
var threads = await _messageService.GetActiveThreadsAsync();
|
||||
return View(threads);
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Customer(Guid id)
|
||||
{
|
||||
var conversation = await _messageService.GetMessageThreadAsync(id);
|
||||
if (conversation == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return View(conversation);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Reply(Guid customerId, string content, bool isUrgent = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
var createMessageDto = new CreateCustomerMessageDto
|
||||
{
|
||||
CustomerId = customerId,
|
||||
Type = MessageType.CustomerService,
|
||||
Subject = "Support Reply", // Simple subject for admin replies
|
||||
Content = content,
|
||||
IsUrgent = isUrgent,
|
||||
Priority = isUrgent ? 1 : 5
|
||||
};
|
||||
|
||||
var message = await _messageService.CreateMessageAsync(createMessageDto);
|
||||
if (message != null)
|
||||
{
|
||||
TempData["Success"] = "Reply sent successfully!";
|
||||
}
|
||||
else
|
||||
{
|
||||
TempData["Error"] = "Failed to send reply.";
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error sending reply");
|
||||
TempData["Error"] = "An error occurred sending the reply.";
|
||||
}
|
||||
|
||||
return RedirectToAction("Customer", new { id = customerId });
|
||||
}
|
||||
}
|
||||
149
LittleShop/Areas/Admin/Views/Messages/Customer.cshtml
Normal file
149
LittleShop/Areas/Admin/Views/Messages/Customer.cshtml
Normal file
@@ -0,0 +1,149 @@
|
||||
@model LittleShop.DTOs.MessageThreadDto
|
||||
|
||||
@{
|
||||
ViewData["Title"] = $"Conversation with {Model.CustomerName}";
|
||||
}
|
||||
|
||||
<div class="container-fluid">
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||
<h2><i class="fas fa-comments"></i> Conversation with @Model.CustomerName</h2>
|
||||
<a href="@Url.Action("Index")" class="btn btn-secondary">
|
||||
<i class="fas fa-arrow-left"></i> Back to Messages
|
||||
</a>
|
||||
</div>
|
||||
|
||||
@if (TempData["Success"] != null)
|
||||
{
|
||||
<div class="alert alert-success alert-dismissible fade show" role="alert">
|
||||
@TempData["Success"]
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||||
</div>
|
||||
}
|
||||
|
||||
@if (TempData["Error"] != null)
|
||||
{
|
||||
<div class="alert alert-danger alert-dismissible fade show" role="alert">
|
||||
@TempData["Error"]
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||||
</div>
|
||||
}
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-8">
|
||||
<!-- Conversation Thread -->
|
||||
<div class="card mb-4">
|
||||
<div class="card-header">
|
||||
<h5>Conversation with @Model.CustomerName</h5>
|
||||
<small class="text-muted">
|
||||
@Model.MessageCount messages since @Model.StartedAt.ToString("MMM dd, yyyy")
|
||||
</small>
|
||||
</div>
|
||||
<div class="card-body" style="max-height: 500px; overflow-y: auto;">
|
||||
@foreach (var message in Model.Messages.OrderBy(m => m.CreatedAt))
|
||||
{
|
||||
<div class="mb-3 @(message.Direction == 0 ? "ms-4" : "me-4")">
|
||||
<div class="d-flex @(message.Direction == 0 ? "justify-content-end" : "justify-content-start")">
|
||||
<div class="card @(message.Direction == 0 ? "bg-primary text-white" : "bg-light") @(message.Direction == 0 ? "ms-auto" : "me-auto")" style="max-width: 70%;">
|
||||
<div class="card-body p-3">
|
||||
<div class="d-flex justify-content-between align-items-start mb-2">
|
||||
<strong class="@(message.Direction == 0 ? "text-white" : "text-primary")">
|
||||
@if (message.Direction == 0)
|
||||
{
|
||||
<i class="fas fa-user-tie"></i>
|
||||
@:Admin
|
||||
}
|
||||
else
|
||||
{
|
||||
<i class="fas fa-user"></i>
|
||||
@Model.CustomerName
|
||||
}
|
||||
</strong>
|
||||
<small class="@(message.Direction == 0 ? "text-white-50" : "text-muted")">
|
||||
@message.CreatedAt.ToString("MMM dd, HH:mm")
|
||||
</small>
|
||||
</div>
|
||||
<div class="@(message.Direction == 0 ? "text-white" : "text-dark")">
|
||||
@Html.Raw(message.Content.Replace("\n", "<br>"))
|
||||
</div>
|
||||
@if (!string.IsNullOrEmpty(message.OrderReference))
|
||||
{
|
||||
<div class="mt-2">
|
||||
<a href="@Url.Action("Details", "Orders", new { id = message.OrderId })"
|
||||
class="badge @(message.Direction == 0 ? "bg-light text-primary" : "bg-primary text-white") text-decoration-none">
|
||||
<i class="fas fa-box"></i> Order @message.OrderReference
|
||||
</a>
|
||||
</div>
|
||||
}
|
||||
@if (message.IsUrgent)
|
||||
{
|
||||
<span class="badge bg-danger mt-2">Urgent</span>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-4">
|
||||
<!-- Customer Info -->
|
||||
<div class="card mb-4">
|
||||
<div class="card-header">
|
||||
<h6><i class="fas fa-user"></i> Customer Information</h6>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<p><strong>Name:</strong> @Model.CustomerName</p>
|
||||
<p><strong>Messages:</strong> @Model.MessageCount</p>
|
||||
<p><strong>First Contact:</strong> @Model.StartedAt.ToString("MMM dd, yyyy HH:mm")</p>
|
||||
<p><strong>Last Message:</strong> @Model.LastMessageAt.ToString("MMM dd, yyyy HH:mm")</p>
|
||||
@if (Model.HasUnreadMessages)
|
||||
{
|
||||
<div class="alert alert-warning p-2">
|
||||
<i class="fas fa-exclamation-triangle"></i> Has unread messages
|
||||
</div>
|
||||
}
|
||||
@if (Model.RequiresResponse)
|
||||
{
|
||||
<div class="alert alert-danger p-2">
|
||||
<i class="fas fa-reply"></i> Requires response
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Reply Form -->
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h6><i class="fas fa-reply"></i> Send Reply</h6>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form method="post" action="@Url.Action("Reply")">
|
||||
<input type="hidden" name="customerId" value="@Model.CustomerId" />
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="content" class="form-label">Message</label>
|
||||
<textarea class="form-control" id="content" name="content" rows="4" required placeholder="Type your reply here..."></textarea>
|
||||
</div>
|
||||
|
||||
<div class="mb-3 form-check">
|
||||
<input type="checkbox" class="form-check-input" id="isUrgent" name="isUrgent" value="true">
|
||||
<label class="form-check-label" for="isUrgent">
|
||||
Mark as urgent
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary">
|
||||
<i class="fas fa-paper-plane"></i> Send Reply
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
86
LittleShop/Areas/Admin/Views/Messages/Index.cshtml
Normal file
86
LittleShop/Areas/Admin/Views/Messages/Index.cshtml
Normal file
@@ -0,0 +1,86 @@
|
||||
@model IEnumerable<LittleShop.DTOs.MessageThreadDto>
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "Customer Messages";
|
||||
}
|
||||
|
||||
<div class="container-fluid">
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||
<h2><i class="fas fa-comments"></i> Customer Messages</h2>
|
||||
</div>
|
||||
|
||||
@if (!Model.Any())
|
||||
{
|
||||
<div class="alert alert-info">
|
||||
<i class="fas fa-info-circle"></i> No customer messages yet.
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h5>Active Conversations</h5>
|
||||
</div>
|
||||
<div class="card-body p-0">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-hover mb-0">
|
||||
<thead class="table-dark">
|
||||
<tr>
|
||||
<th>Customer</th>
|
||||
<th>Subject</th>
|
||||
<th>Last Message</th>
|
||||
<th>Messages</th>
|
||||
<th>Status</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var thread in Model.OrderByDescending(t => t.LastMessageAt))
|
||||
{
|
||||
<tr class="@(thread.HasUnreadMessages ? "table-warning" : "")">
|
||||
<td>
|
||||
<strong>@thread.CustomerName</strong>
|
||||
<br><small class="text-muted">Customer conversation</small>
|
||||
</td>
|
||||
<td>
|
||||
<span class="text-muted">Latest activity</span>
|
||||
</td>
|
||||
<td>
|
||||
@thread.LastMessageAt.ToString("MMM dd, HH:mm")
|
||||
<br><small class="text-muted">Started: @thread.StartedAt.ToString("MMM dd")</small>
|
||||
</td>
|
||||
<td>
|
||||
<span class="badge bg-primary">@thread.MessageCount</span>
|
||||
@if (thread.HasUnreadMessages)
|
||||
{
|
||||
<span class="badge bg-warning ms-1">Unread</span>
|
||||
}
|
||||
</td>
|
||||
<td>
|
||||
@if (thread.RequiresResponse)
|
||||
{
|
||||
<span class="badge bg-danger">Needs Response</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span class="badge bg-success">Up to date</span>
|
||||
}
|
||||
</td>
|
||||
<td>
|
||||
<a href="@Url.Action("Customer", new { id = thread.CustomerId })" class="btn btn-sm btn-primary">
|
||||
<i class="fas fa-comments"></i> View Conversation
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -12,8 +12,11 @@
|
||||
<div class="col-auto">
|
||||
@if (Model.Customer != null)
|
||||
{
|
||||
<button class="btn btn-success" onclick="showMessageModal('@Model.Id', '@Model.Customer.DisplayName')">
|
||||
<i class="fas fa-comment"></i> Message Customer
|
||||
<a href="@Url.Action("Customer", "Messages", new { area = "Admin", id = Model.Customer.Id })" class="btn btn-success">
|
||||
<i class="fas fa-comments"></i> View Conversation
|
||||
</a>
|
||||
<button type="button" class="btn btn-primary" onclick="showMessageModal('@Model.Id', '@Model.Customer.DisplayName')">
|
||||
<i class="fas fa-comment"></i> Send Order Update
|
||||
</button>
|
||||
}
|
||||
<a href="@Url.Action("Edit", new { id = Model.Id })" class="btn btn-primary">
|
||||
|
||||
@@ -40,6 +40,11 @@
|
||||
<i class="fas fa-shopping-cart"></i> Orders
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="@Url.Action("Index", "Messages", new { area = "Admin" })">
|
||||
<i class="fas fa-comments"></i> Messages
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="@Url.Action("Index", "ShippingRates", new { area = "Admin" })">
|
||||
<i class="fas fa-truck"></i> Shipping
|
||||
|
||||
@@ -72,10 +72,60 @@ public class BotMessagesController : ControllerBase
|
||||
|
||||
return Ok(message);
|
||||
}
|
||||
|
||||
[HttpPost("customer-create")]
|
||||
public async Task<ActionResult> CreateCustomerMessage([FromBody] CreateCustomerMessageFromTelegramDto dto)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create CustomerToAdmin message directly
|
||||
var message = new CustomerMessage
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
CustomerId = dto.CustomerId,
|
||||
OrderId = dto.OrderId,
|
||||
Direction = MessageDirection.CustomerToAdmin,
|
||||
Type = dto.Type,
|
||||
Subject = dto.Subject,
|
||||
Content = dto.Content,
|
||||
Status = MessageStatus.Read, // Customer messages are immediately "read" by system
|
||||
Platform = "Telegram",
|
||||
Priority = dto.Priority,
|
||||
IsUrgent = dto.IsUrgent,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ThreadId = Guid.NewGuid() // Will be updated if part of existing thread
|
||||
};
|
||||
|
||||
// Save directly to database (bypass the regular CreateMessageAsync which is for AdminToCustomer)
|
||||
var result = await _messageService.CreateCustomerToAdminMessageAsync(message);
|
||||
if (!result)
|
||||
{
|
||||
return BadRequest("Failed to create customer message");
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest($"Error creating customer message: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TEMPORARY DTO FOR TESTING
|
||||
public class CreateTestMessageDto
|
||||
{
|
||||
public Guid CustomerId { get; set; }
|
||||
public Guid? OrderId { get; set; }
|
||||
public MessageType Type { get; set; }
|
||||
public string Subject { get; set; } = string.Empty;
|
||||
public string Content { get; set; } = string.Empty;
|
||||
public int Priority { get; set; } = 5;
|
||||
public bool IsUrgent { get; set; } = false;
|
||||
}
|
||||
|
||||
// DTO for customer messages from Telegram
|
||||
public class CreateCustomerMessageFromTelegramDto
|
||||
{
|
||||
public Guid CustomerId { get; set; }
|
||||
public Guid? OrderId { get; set; }
|
||||
|
||||
@@ -30,18 +30,8 @@ public class CustomerMessageService : ICustomerMessageService
|
||||
message.Status = MessageStatus.Pending;
|
||||
message.Platform = "Telegram";
|
||||
|
||||
// Generate thread ID if this is a new conversation
|
||||
if (message.ParentMessageId == null)
|
||||
{
|
||||
message.ThreadId = Guid.NewGuid();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Get parent message's thread ID
|
||||
var parentMessage = await _context.CustomerMessages
|
||||
.FirstOrDefaultAsync(m => m.Id == message.ParentMessageId);
|
||||
message.ThreadId = parentMessage?.ThreadId ?? Guid.NewGuid();
|
||||
}
|
||||
// Use customer-based threading - all messages for a customer are in one conversation
|
||||
message.ThreadId = message.CustomerId;
|
||||
|
||||
_context.CustomerMessages.Add(message);
|
||||
await _context.SaveChangesAsync();
|
||||
@@ -161,28 +151,29 @@ public class CustomerMessageService : ICustomerMessageService
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<MessageThreadDto?> GetMessageThreadAsync(Guid threadId)
|
||||
public async Task<MessageThreadDto?> GetMessageThreadAsync(Guid customerId)
|
||||
{
|
||||
// Get all messages for this customer (customer-based conversation)
|
||||
var messages = await _context.CustomerMessages
|
||||
.Include(m => m.Customer)
|
||||
.Include(m => m.Order)
|
||||
.Include(m => m.AdminUser)
|
||||
.Where(m => m.ThreadId == threadId)
|
||||
.Where(m => m.CustomerId == customerId && !m.IsArchived)
|
||||
.OrderBy(m => m.CreatedAt)
|
||||
.ToListAsync();
|
||||
|
||||
if (!messages.Any()) return null;
|
||||
|
||||
var firstMessage = messages.First();
|
||||
var customer = messages.First().Customer;
|
||||
var thread = new MessageThreadDto
|
||||
{
|
||||
ThreadId = threadId,
|
||||
Subject = firstMessage.Subject,
|
||||
CustomerId = firstMessage.CustomerId,
|
||||
CustomerName = firstMessage.Customer?.DisplayName ?? "Unknown",
|
||||
OrderId = firstMessage.OrderId,
|
||||
OrderReference = firstMessage.Order?.Id.ToString().Substring(0, 8),
|
||||
StartedAt = firstMessage.CreatedAt,
|
||||
ThreadId = customerId, // Use CustomerId as thread identifier
|
||||
Subject = customer?.DisplayName ?? "Unknown Customer", // Customer name as subject
|
||||
CustomerId = customerId,
|
||||
CustomerName = customer?.DisplayName ?? "Unknown",
|
||||
OrderId = null, // No single order - conversation spans multiple orders
|
||||
OrderReference = null, // No single order reference
|
||||
StartedAt = messages.Min(m => m.CreatedAt),
|
||||
LastMessageAt = messages.Max(m => m.CreatedAt),
|
||||
MessageCount = messages.Count,
|
||||
HasUnreadMessages = messages.Any(m => m.Direction == MessageDirection.CustomerToAdmin && m.Status != MessageStatus.Read),
|
||||
@@ -195,19 +186,20 @@ public class CustomerMessageService : ICustomerMessageService
|
||||
|
||||
public async Task<IEnumerable<MessageThreadDto>> GetActiveThreadsAsync()
|
||||
{
|
||||
// Group by Customer instead of ThreadId for customer-based conversations
|
||||
var threads = await _context.CustomerMessages
|
||||
.Include(m => m.Customer)
|
||||
.Include(m => m.Order)
|
||||
.Where(m => !m.IsArchived)
|
||||
.GroupBy(m => m.ThreadId)
|
||||
.GroupBy(m => m.CustomerId)
|
||||
.Select(g => new MessageThreadDto
|
||||
{
|
||||
ThreadId = g.Key ?? Guid.Empty,
|
||||
Subject = g.First().Subject,
|
||||
ThreadId = g.First().CustomerId, // Use CustomerId as conversation identifier
|
||||
Subject = g.First().Customer != null ? g.First().Customer.DisplayName : "Unknown Customer",
|
||||
CustomerId = g.First().CustomerId,
|
||||
CustomerName = g.First().Customer != null ? g.First().Customer.DisplayName : "Unknown",
|
||||
OrderId = g.First().OrderId,
|
||||
OrderReference = g.First().Order != null ? g.First().Order.Id.ToString().Substring(0, 8) : null,
|
||||
OrderId = null, // No single order - will show multiple orders in thread
|
||||
OrderReference = null, // No single order reference
|
||||
StartedAt = g.Min(m => m.CreatedAt),
|
||||
LastMessageAt = g.Max(m => m.CreatedAt),
|
||||
MessageCount = g.Count(),
|
||||
@@ -230,4 +222,26 @@ public class CustomerMessageService : ICustomerMessageService
|
||||
{
|
||||
return await _context.Orders.AnyAsync(o => o.Id == orderId && o.CustomerId == customerId);
|
||||
}
|
||||
|
||||
public async Task<bool> CreateCustomerToAdminMessageAsync(CustomerMessage message)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Use customer-based threading for all messages
|
||||
message.ThreadId = message.CustomerId;
|
||||
|
||||
_context.CustomerMessages.Add(message);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
_logger.LogInformation("Created customer message {MessageId} from customer {CustomerId}",
|
||||
message.Id, message.CustomerId);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error creating customer message from customer {CustomerId}", message.CustomerId);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,4 +17,5 @@ public interface ICustomerMessageService
|
||||
Task<IEnumerable<MessageThreadDto>> GetActiveThreadsAsync();
|
||||
Task<bool> ValidateCustomerExistsAsync(Guid customerId);
|
||||
Task<bool> ValidateOrderBelongsToCustomerAsync(Guid orderId, Guid customerId);
|
||||
Task<bool> CreateCustomerToAdminMessageAsync(CustomerMessage message);
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user