final
This commit is contained in:
@@ -11,11 +11,13 @@ namespace LittleShop.Areas.Admin.Controllers;
|
||||
public class MessagesController : Controller
|
||||
{
|
||||
private readonly ICustomerMessageService _messageService;
|
||||
private readonly ICustomerService _customerService;
|
||||
private readonly ILogger<MessagesController> _logger;
|
||||
|
||||
public MessagesController(ICustomerMessageService messageService, ILogger<MessagesController> logger)
|
||||
public MessagesController(ICustomerMessageService messageService, ICustomerService customerService, ILogger<MessagesController> logger)
|
||||
{
|
||||
_messageService = messageService;
|
||||
_customerService = customerService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@@ -28,9 +30,42 @@ public class MessagesController : Controller
|
||||
public async Task<IActionResult> Customer(Guid id)
|
||||
{
|
||||
var conversation = await _messageService.GetMessageThreadAsync(id);
|
||||
|
||||
// If no conversation exists yet, create an empty one for this customer
|
||||
if (conversation == null)
|
||||
{
|
||||
return NotFound();
|
||||
// Check if customer exists
|
||||
var customerExists = await _messageService.ValidateCustomerExistsAsync(id);
|
||||
if (!customerExists)
|
||||
{
|
||||
TempData["Error"] = "Customer not found.";
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
// Get customer information
|
||||
var customer = await _customerService.GetCustomerByIdAsync(id);
|
||||
if (customer == null)
|
||||
{
|
||||
TempData["Error"] = "Customer information not available.";
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
// Create empty conversation structure for new conversation
|
||||
conversation = new MessageThreadDto
|
||||
{
|
||||
ThreadId = id,
|
||||
Subject = customer.DisplayName,
|
||||
CustomerId = id,
|
||||
CustomerName = customer.DisplayName,
|
||||
OrderId = null,
|
||||
OrderReference = null,
|
||||
StartedAt = DateTime.UtcNow,
|
||||
LastMessageAt = DateTime.UtcNow,
|
||||
MessageCount = 0,
|
||||
HasUnreadMessages = false,
|
||||
RequiresResponse = false,
|
||||
Messages = new List<CustomerMessageDto>()
|
||||
};
|
||||
}
|
||||
|
||||
return View(conversation);
|
||||
|
||||
@@ -4,6 +4,15 @@
|
||||
ViewData["Title"] = $"Conversation with {Model.CustomerName}";
|
||||
}
|
||||
|
||||
@{
|
||||
// Get customer info if name is not loaded
|
||||
if (Model.CustomerName == "Loading...")
|
||||
{
|
||||
// This would need to be loaded via a service call
|
||||
Model.CustomerName = "Customer"; // Fallback
|
||||
}
|
||||
}
|
||||
|
||||
<div class="container-fluid">
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
@@ -41,8 +50,19 @@
|
||||
</small>
|
||||
</div>
|
||||
<div class="card-body" style="max-height: 500px; overflow-y: auto;">
|
||||
@foreach (var message in Model.Messages.OrderBy(m => m.CreatedAt))
|
||||
@if (!Model.Messages.Any())
|
||||
{
|
||||
<div class="text-center text-muted py-4">
|
||||
<i class="fas fa-comments fa-3x mb-3"></i>
|
||||
<h5>No messages yet</h5>
|
||||
<p>This is the start of your conversation with @Model.CustomerName.</p>
|
||||
<p class="small">Send a message below to begin the conversation.</p>
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
@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%;">
|
||||
@@ -84,6 +104,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
@@ -118,7 +139,7 @@
|
||||
<!-- Reply Form -->
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h6><i class="fas fa-reply"></i> Send Reply</h6>
|
||||
<h6><i class="fas fa-@(Model.MessageCount == 0 ? "comment" : "reply")"></i> @(Model.MessageCount == 0 ? "Start Conversation" : "Send Reply")</h6>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form method="post" action="@Url.Action("Reply")">
|
||||
@@ -126,7 +147,7 @@
|
||||
|
||||
<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>
|
||||
<textarea class="form-control" id="content" name="content" rows="4" required placeholder="@(Model.MessageCount == 0 ? "Start the conversation with this customer..." : "Type your reply here...")"></textarea>
|
||||
</div>
|
||||
|
||||
<div class="mb-3 form-check">
|
||||
@@ -137,7 +158,7 @@
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary">
|
||||
<i class="fas fa-paper-plane"></i> Send Reply
|
||||
<i class="fas fa-paper-plane"></i> @(Model.MessageCount == 0 ? "Send Message" : "Send Reply")
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user