// 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, TaskTypeID ) VALUES ( :businessId, :orderId, :title, NOW(), 0, 1 ) ", { 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));