This repository has been archived on 2026-03-21. You can view files and clone it, but cannot push or open issues or pull requests.
payfrit-biz/api/orders/getActiveCart.cfm
John Mizerek 6b66d2cef8 Fix normalized DB column names across all API files
Sweep of 26 API files to use prefixed column names matching the
database schema (e.g. BusinessID not ID, BusinessName not Name,
BusinessDeliveryFlatFee not DeliveryFlatFee, ServicePointName not Name).

Files fixed: auth, beacons, businesses, menu, orders, setup, stripe,
tasks, and workers endpoints.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-31 16:56:41 -08:00

98 lines
3.2 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.ID,
o.UUID,
o.BusinessID,
o.OrderTypeID,
o.StatusID,
o.ServicePointID,
o.AddedOn,
b.BusinessName,
b.BusinessOrderTypes,
sp.ServicePointName,
(SELECT COUNT(*)
FROM OrderLineItems oli
WHERE oli.OrderID = o.ID
AND oli.IsDeleted = 0
AND oli.ParentOrderLineItemID = 0) as ItemCount
FROM Orders o
LEFT JOIN Businesses b ON b.BusinessID = o.BusinessID
LEFT JOIN ServicePoints sp ON sp.ServicePointID = o.ServicePointID
WHERE o.UserID = :userId
AND o.StatusID = 0
ORDER BY o.ID 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": val(qCart.OrderID),
"UUID": qCart.UUID ?: "",
"BusinessID": val(qCart.BusinessID),
"Name": len(trim(qCart.BusinessName)) ? qCart.BusinessName : "",
"OrderTypes": orderTypesArray,
"OrderTypeID": val(qCart.OrderTypeID),
"OrderTypeName": orderTypeName,
"ServicePointID": val(qCart.ServicePointID),
"ServicePointName": len(trim(qCart.ServicePointName)) ? qCart.ServicePointName : "",
"ItemCount": val(qCart.ItemCount),
"AddedOn": dateTimeFormat(qCart.AddedOn, "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>