diff --git a/Api.SeaHavenIndustries/Controllers/DispatchPublicController.cs b/Api.SeaHavenIndustries/Controllers/DispatchPublicController.cs new file mode 100644 index 0000000..928c643 --- /dev/null +++ b/Api.SeaHavenIndustries/Controllers/DispatchPublicController.cs @@ -0,0 +1,89 @@ +using Data.SeaHavenIndustries; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace Api.SeaHavenIndustries.Controllers +{ + [ApiController] + [Route("api/dispatch")] + public class DispatchPublicController : Controller + { + private readonly ApplicationDbContext _db; + + public DispatchPublicController(ApplicationDbContext db) + { + _db = db; + } + + [HttpGet("accept/{token}")] + public async Task Accept(string token) + { + var dispatch = await _db.Dispatches + .Include(d => d.Vendor) + .FirstOrDefaultAsync(d => d.AcceptToken == token); + + if (dispatch == null) + { + return Content(BuildHtml("Dispatch Not Found", "This dispatch link is invalid or has expired.", "#dc3545"), "text/html"); + } + + if (dispatch.Status != "Sent") + { + var alreadyMsg = dispatch.AcknowledgedAt.HasValue + ? $"This dispatch was already accepted on {dispatch.AcknowledgedAt.Value:MMMM dd, yyyy 'at' h:mm tt} UTC." + : "This dispatch has already been processed."; + return Content(BuildHtml("Already Accepted", alreadyMsg, "#ffc107"), "text/html"); + } + + dispatch.Status = "Acknowledged"; + dispatch.AcknowledgedAt = DateTime.UtcNow; + + _db.WorkOrderAuditLogs.Add(new WorkOrderAuditLog + { + WorkOrderId = dispatch.WorkOrderId ?? 0, + FieldName = $"Dispatch {dispatch.DispatchNumber} Status", + OldValue = "Sent", + NewValue = "Acknowledged", + Action = "vendor_accept", + CreatedAt = DateTime.UtcNow + }); + + await _db.SaveChangesAsync(); + + var vendorName = dispatch.Vendor?.CompanyName ?? "Vendor"; + return Content(BuildHtml( + "Dispatch Accepted", + $"{vendorName} has accepted dispatch {dispatch.DispatchNumber}.

Thank you for confirming. The dispatcher has been notified.", + "#28a745" + ), "text/html"); + } + + private static string BuildHtml(string title, string message, string color) + { + return $@" + + + + + {title} - Sea Haven Industries + + + +
+
✓
+

{title}

+

{message}

+ +
+ +"; + } + } +} diff --git a/Api.SeaHavenIndustries/Controllers/WorkOrderController.cs b/Api.SeaHavenIndustries/Controllers/WorkOrderController.cs index a16297c..cf85523 100644 --- a/Api.SeaHavenIndustries/Controllers/WorkOrderController.cs +++ b/Api.SeaHavenIndustries/Controllers/WorkOrderController.cs @@ -1136,6 +1136,10 @@ namespace Api.SeaHavenIndustries.Controllers

Description

{model.Description ?? primaryWO.Description}

+
+
+ Accept Dispatch +

{senderName}
Sea Haven Industries


Reply to this email to communicate about this dispatch.

";