From ce334cf95c59f4e32d7248b48ec0e805ff5465cb Mon Sep 17 00:00:00 2001 From: John Pinkyfloyd Date: Sun, 15 Feb 2026 15:45:22 -0800 Subject: [PATCH] Add fixOrder admin script for webhook recovery --- api/admin/fixOrder.cfm | 108 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 api/admin/fixOrder.cfm diff --git a/api/admin/fixOrder.cfm b/api/admin/fixOrder.cfm new file mode 100644 index 0000000..8980e27 --- /dev/null +++ b/api/admin/fixOrder.cfm @@ -0,0 +1,108 @@ + + + + +// Fix order that missed webhook - updates status and creates kitchen task +// Usage: ?businessId=55 (defaults to most recent order) + +function readJsonBody() { + var raw = getHttpRequestData().content; + if (isNull(raw)) raw = ""; + if (!len(trim(raw))) return {}; + try { + var data = deserializeJSON(raw); + if (isStruct(data)) return data; + } catch (any e) {} + return {}; +} + +data = readJsonBody(); +businessId = val(structKeyExists(url, "businessId") ? url.businessId : (structKeyExists(data, "businessId") ? data.businessId : 55)); +orderId = val(structKeyExists(url, "orderId") ? url.orderId : (structKeyExists(data, "orderId") ? data.orderId : 0)); + +// Find order +if (orderId > 0) { + qOrder = queryExecute(" + SELECT o.ID, o.UUID, o.UserID, o.BusinessID, o.StatusID, o.ServicePointID, o.PaymentStatus, + u.FirstName, sp.Name AS ServicePointName + FROM Orders o + LEFT JOIN Users u ON u.ID = o.UserID + LEFT JOIN ServicePoints sp ON sp.ID = o.ServicePointID + WHERE o.ID = :orderId + LIMIT 1 + ", { orderId: { value: orderId, cfsqltype: "cf_sql_integer" } }, { datasource: "payfrit" }); +} else { + qOrder = queryExecute(" + SELECT o.ID, o.UUID, o.UserID, o.BusinessID, o.StatusID, o.ServicePointID, o.PaymentStatus, + u.FirstName, sp.Name AS ServicePointName + FROM Orders o + LEFT JOIN Users u ON u.ID = o.UserID + LEFT JOIN ServicePoints sp ON sp.ID = o.ServicePointID + WHERE o.BusinessID = :businessId + ORDER BY o.ID DESC + LIMIT 1 + ", { businessId: { value: businessId, cfsqltype: "cf_sql_integer" } }, { datasource: "payfrit" }); +} + +if (qOrder.recordCount == 0) { + writeOutput(serializeJSON({ "OK": false, "MESSAGE": "No orders found" })); + abort; +} + +orderId = qOrder.ID; +businessId = qOrder.BusinessID; + +result = { + "OrderID": orderId, + "UserName": qOrder.FirstName, + "CurrentStatus": qOrder.StatusID, + "PaymentStatus": qOrder.PaymentStatus, + "ServicePointID": qOrder.ServicePointID, + "ServicePointName": qOrder.ServicePointName +}; + +// Update order status to Submitted (1) if it's still Cart (0) +if (qOrder.StatusID == 0) { + queryExecute(" + UPDATE Orders + SET StatusID = 1, + PaymentStatus = 'paid', + SubmittedOn = NOW() + WHERE ID = :orderId + ", { orderId: { value: orderId, cfsqltype: "cf_sql_integer" } }, { datasource: "payfrit" }); + result["StatusUpdated"] = true; +} else { + result["StatusUpdated"] = false; +} + +// Check if kitchen task exists +qTask = queryExecute(" + SELECT ID FROM Tasks WHERE OrderID = :orderId +", { orderId: { value: orderId, cfsqltype: "cf_sql_integer" } }, { datasource: "payfrit" }); + +if (qTask.recordCount == 0) { + // Create kitchen task + tableName = len(trim(qOrder.ServicePointName)) ? qOrder.ServicePointName : "Table"; + taskTitle = "Prepare Order ##" & orderId & " for " & tableName; + + queryExecute(" + INSERT INTO Tasks ( + BusinessID, OrderID, Title, CreatedOn, ClaimedByUserID + ) VALUES ( + :businessId, :orderId, :title, NOW(), 0 + ) + ", { + businessId: { value: businessId, cfsqltype: "cf_sql_integer" }, + orderId: { value: orderId, cfsqltype: "cf_sql_integer" }, + title: { value: taskTitle, cfsqltype: "cf_sql_varchar" } + }, { datasource: "payfrit" }); + result["TaskCreated"] = true; +} else { + result["TaskCreated"] = false; + result["ExistingTaskID"] = qTask.ID; +} + +result["OK"] = true; +result["MESSAGE"] = "Order fixed"; +writeOutput(serializeJSON(result)); +