payfrit-works/api/orders/getActiveCart.cfm
John Mizerek f919ef1cfe Add chat expiration and order management improvements
- Auto-expire stale chats older than 20 minutes in createChat.cfm
- Add expireStaleChats.cfm for scheduled cleanup
- Add abandonOrder.cfm for Start Fresh functionality
- Add closeAllChats action to debugTasks.cfm
- Fix setOrderType NULL value for non-delivery orders
- Add ForceNew parameter to setLineItem for customized items
- Add public endpoint allowlist entries for new endpoints

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-13 19:46:39 -08:00

98 lines
3.3 KiB
Text

<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/json; charset=utf-8" reset="true">
<cfheader name="Cache-Control" value="no-store">
<cfscript>
/**
* Get the user's active cart (status=0) if one exists
* Used at app startup to check if user has an existing order in progress
*
* Query params:
* UserID - the user's ID
*
* Returns the cart info including businessId, orderTypeId, and item count
*/
response = { "OK": false };
try {
UserID = val(url.UserID ?: 0);
if (UserID LTE 0) {
response["ERROR"] = "missing_user";
response["MESSAGE"] = "UserID is required";
writeOutput(serializeJSON(response));
abort;
}
// Get active cart (status = 0) for this user
qCart = queryExecute("
SELECT
o.OrderID,
o.OrderUUID,
o.OrderBusinessID,
o.OrderTypeID,
o.OrderStatusID,
o.OrderServicePointID,
o.OrderAddedOn,
b.BusinessName,
b.BusinessOrderTypes,
sp.ServicePointName,
(SELECT COUNT(*)
FROM OrderLineItems oli
WHERE oli.OrderLineItemOrderID = o.OrderID
AND oli.OrderLineItemIsDeleted = 0
AND oli.OrderLineItemParentOrderLineItemID = 0) as ItemCount
FROM Orders o
LEFT JOIN Businesses b ON b.BusinessID = o.OrderBusinessID
LEFT JOIN ServicePoints sp ON sp.ServicePointID = o.OrderServicePointID
WHERE o.OrderUserID = :userId
AND o.OrderStatusID = 0
ORDER BY o.OrderID DESC
LIMIT 1
", {
userId: { value: UserID, cfsqltype: "cf_sql_integer" }
}, { datasource: "payfrit" });
if (qCart.recordCount GT 0) {
orderTypeName = "";
switch (qCart.OrderTypeID) {
case 0: orderTypeName = "Undecided"; break;
case 1: orderTypeName = "Dine-In"; break;
case 2: orderTypeName = "Takeaway"; break;
case 3: orderTypeName = "Delivery"; break;
}
// Parse business order types (e.g., "1,2,3" -> array of ints)
businessOrderTypes = len(trim(qCart.BusinessOrderTypes)) ? qCart.BusinessOrderTypes : "1,2,3";
orderTypesArray = listToArray(businessOrderTypes, ",");
response["OK"] = true;
response["HAS_CART"] = true;
response["CART"] = {
"OrderID": qCart.OrderID,
"OrderUUID": qCart.OrderUUID,
"BusinessID": qCart.OrderBusinessID,
"BusinessName": len(trim(qCart.BusinessName)) ? qCart.BusinessName : "",
"BusinessOrderTypes": orderTypesArray,
"OrderTypeID": qCart.OrderTypeID,
"OrderTypeName": orderTypeName,
"ServicePointID": qCart.OrderServicePointID,
"ServicePointName": len(trim(qCart.ServicePointName)) ? qCart.ServicePointName : "",
"ItemCount": qCart.ItemCount,
"AddedOn": dateTimeFormat(qCart.OrderAddedOn, "yyyy-mm-dd HH:nn:ss")
};
} else {
response["OK"] = true;
response["HAS_CART"] = false;
response["CART"] = javacast("null", "");
}
} catch (any e) {
response["ERROR"] = "server_error";
response["MESSAGE"] = e.message;
}
writeOutput(serializeJSON(response));
</cfscript>