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>
This commit is contained in:
parent
f7df6b614c
commit
f919ef1cfe
12 changed files with 566 additions and 71 deletions
|
|
@ -112,12 +112,14 @@ if (len(request._api_path)) {
|
||||||
if (findNoCase("/api/tasks/getDetails.cfm", request._api_path)) request._api_isPublic = true;
|
if (findNoCase("/api/tasks/getDetails.cfm", request._api_path)) request._api_isPublic = true;
|
||||||
if (findNoCase("/api/tasks/callserver", request._api_path)) request._api_isPublic = true;
|
if (findNoCase("/api/tasks/callserver", request._api_path)) request._api_isPublic = true;
|
||||||
if (findNoCase("/api/tasks/createChat.cfm", request._api_path)) request._api_isPublic = true;
|
if (findNoCase("/api/tasks/createChat.cfm", request._api_path)) request._api_isPublic = true;
|
||||||
|
if (findNoCase("/api/tasks/expirestalechats.cfm", request._api_path)) request._api_isPublic = true;
|
||||||
|
|
||||||
// Chat endpoints
|
// Chat endpoints
|
||||||
if (findNoCase("/api/chat/getMessages.cfm", request._api_path)) request._api_isPublic = true;
|
if (findNoCase("/api/chat/getMessages.cfm", request._api_path)) request._api_isPublic = true;
|
||||||
if (findNoCase("/api/chat/sendMessage.cfm", request._api_path)) request._api_isPublic = true;
|
if (findNoCase("/api/chat/sendMessage.cfm", request._api_path)) request._api_isPublic = true;
|
||||||
if (findNoCase("/api/chat/markRead.cfm", request._api_path)) request._api_isPublic = true;
|
if (findNoCase("/api/chat/markRead.cfm", request._api_path)) request._api_isPublic = true;
|
||||||
if (findNoCase("/api/chat/getActiveChat.cfm", request._api_path)) request._api_isPublic = true;
|
if (findNoCase("/api/chat/getActiveChat.cfm", request._api_path)) request._api_isPublic = true;
|
||||||
|
if (findNoCase("/api/chat/closechat.cfm", request._api_path)) request._api_isPublic = true;
|
||||||
|
|
||||||
// Token validation (for WebSocket server)
|
// Token validation (for WebSocket server)
|
||||||
if (findNoCase("/api/auth/validateToken.cfm", request._api_path)) request._api_isPublic = true;
|
if (findNoCase("/api/auth/validateToken.cfm", request._api_path)) request._api_isPublic = true;
|
||||||
|
|
|
||||||
26
api/admin/closeAllChats.cfm
Normal file
26
api/admin/closeAllChats.cfm
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
<cfsetting showdebugoutput="false">
|
||||||
|
<cfcontent type="application/json; charset=utf-8">
|
||||||
|
<cfscript>
|
||||||
|
// Close all open chat tasks
|
||||||
|
try {
|
||||||
|
result = queryExecute("
|
||||||
|
UPDATE Tasks
|
||||||
|
SET TaskCompletedOn = NOW()
|
||||||
|
WHERE TaskTypeID = 2
|
||||||
|
AND TaskCompletedOn IS NULL
|
||||||
|
", {}, { datasource: "payfrit" });
|
||||||
|
|
||||||
|
affected = result.recordCount ?: 0;
|
||||||
|
|
||||||
|
writeOutput(serializeJSON({
|
||||||
|
"OK": true,
|
||||||
|
"MESSAGE": "Closed all open chats",
|
||||||
|
"AFFECTED": affected
|
||||||
|
}));
|
||||||
|
} catch (any e) {
|
||||||
|
writeOutput(serializeJSON({
|
||||||
|
"OK": false,
|
||||||
|
"ERROR": e.message
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
</cfscript>
|
||||||
|
|
@ -2,6 +2,25 @@
|
||||||
<cfsetting enablecfoutputonly="true">
|
<cfsetting enablecfoutputonly="true">
|
||||||
<cfcontent type="application/json; charset=utf-8">
|
<cfcontent type="application/json; charset=utf-8">
|
||||||
|
|
||||||
|
<cfscript>
|
||||||
|
// Check for action parameter
|
||||||
|
data = {};
|
||||||
|
try {
|
||||||
|
rawContent = getHttpRequestData().content;
|
||||||
|
if (len(trim(rawContent))) data = deserializeJSON(rawContent);
|
||||||
|
} catch (any e) {}
|
||||||
|
|
||||||
|
// Close all open chats action
|
||||||
|
if (structKeyExists(data, "action") && data.action == "closeAllChats") {
|
||||||
|
queryExecute("
|
||||||
|
UPDATE Tasks SET TaskCompletedOn = NOW()
|
||||||
|
WHERE TaskTypeID = 2 AND TaskCompletedOn IS NULL
|
||||||
|
", {}, { datasource: "payfrit" });
|
||||||
|
writeOutput(serializeJSON({ "OK": true, "MESSAGE": "All open chats closed" }));
|
||||||
|
abort;
|
||||||
|
}
|
||||||
|
</cfscript>
|
||||||
|
|
||||||
<cftry>
|
<cftry>
|
||||||
<cfset qTasks = queryExecute("
|
<cfset qTasks = queryExecute("
|
||||||
SELECT
|
SELECT
|
||||||
|
|
|
||||||
78
api/orders/abandonOrder.cfm
Normal file
78
api/orders/abandonOrder.cfm
Normal file
|
|
@ -0,0 +1,78 @@
|
||||||
|
<cfsetting showdebugoutput="false">
|
||||||
|
<cfsetting enablecfoutputonly="true">
|
||||||
|
|
||||||
|
<cffunction name="readJsonBody" access="public" returntype="struct" output="false">
|
||||||
|
<cfset var raw = getHttpRequestData().content>
|
||||||
|
<cfif isNull(raw) OR len(trim(raw)) EQ 0>
|
||||||
|
<cfreturn {}>
|
||||||
|
</cfif>
|
||||||
|
<cftry>
|
||||||
|
<cfset var data = deserializeJSON(raw)>
|
||||||
|
<cfif isStruct(data)>
|
||||||
|
<cfreturn data>
|
||||||
|
<cfelse>
|
||||||
|
<cfreturn {}>
|
||||||
|
</cfif>
|
||||||
|
<cfcatch>
|
||||||
|
<cfreturn {}>
|
||||||
|
</cfcatch>
|
||||||
|
</cftry>
|
||||||
|
</cffunction>
|
||||||
|
|
||||||
|
<cffunction name="apiAbort" access="public" returntype="void" output="true">
|
||||||
|
<cfargument name="payload" type="struct" required="true">
|
||||||
|
<cfcontent type="application/json; charset=utf-8">
|
||||||
|
<cfoutput>#serializeJSON(arguments.payload)#</cfoutput>
|
||||||
|
<cfabort>
|
||||||
|
</cffunction>
|
||||||
|
|
||||||
|
<cfset data = readJsonBody()>
|
||||||
|
|
||||||
|
<cfset OrderID = val(structKeyExists(data, "OrderID") ? data.OrderID : 0)>
|
||||||
|
|
||||||
|
<cfif OrderID LTE 0>
|
||||||
|
<cfset apiAbort({ "OK": false, "ERROR": "missing_params", "MESSAGE": "OrderID is required.", "DETAIL": "" })>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<cftry>
|
||||||
|
<!--- Verify order exists and is in cart status (0) --->
|
||||||
|
<cfset qOrder = queryExecute(
|
||||||
|
"SELECT OrderID, OrderStatusID FROM Orders WHERE OrderID = ? LIMIT 1",
|
||||||
|
[{ value = OrderID, cfsqltype = "cf_sql_integer" }],
|
||||||
|
{ datasource = "payfrit" }
|
||||||
|
)>
|
||||||
|
|
||||||
|
<cfif qOrder.recordCount EQ 0>
|
||||||
|
<cfset apiAbort({ "OK": false, "ERROR": "not_found", "MESSAGE": "Order not found.", "DETAIL": "" })>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<cfif qOrder.OrderStatusID NEQ 0>
|
||||||
|
<cfset apiAbort({ "OK": false, "ERROR": "invalid_status", "MESSAGE": "Only cart orders can be abandoned.", "DETAIL": "" })>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<!--- Delete the order completely (cascades to line items via FK or we delete them first) --->
|
||||||
|
<!--- First delete all line items --->
|
||||||
|
<cfset queryExecute(
|
||||||
|
"DELETE FROM OrderLineItems WHERE OrderLineItemOrderID = ?",
|
||||||
|
[{ value = OrderID, cfsqltype = "cf_sql_integer" }],
|
||||||
|
{ datasource = "payfrit" }
|
||||||
|
)>
|
||||||
|
|
||||||
|
<!--- Then delete the order itself --->
|
||||||
|
<cfset queryExecute(
|
||||||
|
"DELETE FROM Orders WHERE OrderID = ?",
|
||||||
|
[{ value = OrderID, cfsqltype = "cf_sql_integer" }],
|
||||||
|
{ datasource = "payfrit" }
|
||||||
|
)>
|
||||||
|
|
||||||
|
<cfset apiAbort({ "OK": true, "MESSAGE": "Order abandoned successfully." })>
|
||||||
|
|
||||||
|
<cfcatch>
|
||||||
|
<cfset apiAbort({
|
||||||
|
"OK": false,
|
||||||
|
"ERROR": "server_error",
|
||||||
|
"MESSAGE": "Failed to abandon order: " & cfcatch.message,
|
||||||
|
"DETAIL": cfcatch.message
|
||||||
|
})>
|
||||||
|
</cfcatch>
|
||||||
|
</cftry>
|
||||||
98
api/orders/getActiveCart.cfm
Normal file
98
api/orders/getActiveCart.cfm
Normal file
|
|
@ -0,0 +1,98 @@
|
||||||
|
<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>
|
||||||
|
|
@ -64,13 +64,15 @@
|
||||||
<cfset apiAbort({ "OK": false, "ERROR": "not_found", "MESSAGE": "Order not found.", "DETAIL": "" })>
|
<cfset apiAbort({ "OK": false, "ERROR": "not_found", "MESSAGE": "Order not found.", "DETAIL": "" })>
|
||||||
</cfif>
|
</cfif>
|
||||||
|
|
||||||
<!--- Get business delivery fee for display in cart --->
|
<!--- Get business info for display in cart --->
|
||||||
<cfset qBusiness = queryExecute(
|
<cfset qBusiness = queryExecute(
|
||||||
"SELECT BusinessDeliveryFlatFee FROM Businesses WHERE BusinessID = ? LIMIT 1",
|
"SELECT BusinessDeliveryFlatFee, BusinessOrderTypes FROM Businesses WHERE BusinessID = ? LIMIT 1",
|
||||||
[ { value = qOrder.OrderBusinessID, cfsqltype = "cf_sql_integer" } ],
|
[ { value = qOrder.OrderBusinessID, cfsqltype = "cf_sql_integer" } ],
|
||||||
{ datasource = "payfrit" }
|
{ datasource = "payfrit" }
|
||||||
)>
|
)>
|
||||||
<cfset businessDeliveryFee = qBusiness.recordCount GT 0 ? qBusiness.BusinessDeliveryFlatFee : 0>
|
<cfset businessDeliveryFee = qBusiness.recordCount GT 0 ? qBusiness.BusinessDeliveryFlatFee : 0>
|
||||||
|
<cfset businessOrderTypes = qBusiness.recordCount GT 0 AND len(trim(qBusiness.BusinessOrderTypes)) ? qBusiness.BusinessOrderTypes : "1,2,3">
|
||||||
|
<cfset businessOrderTypesArray = listToArray(businessOrderTypes, ",")>
|
||||||
|
|
||||||
<cfset qLI = queryExecute(
|
<cfset qLI = queryExecute(
|
||||||
"
|
"
|
||||||
|
|
@ -131,6 +133,7 @@
|
||||||
"OrderTypeID": qOrder.OrderTypeID,
|
"OrderTypeID": qOrder.OrderTypeID,
|
||||||
"OrderDeliveryFee": qOrder.OrderDeliveryFee,
|
"OrderDeliveryFee": qOrder.OrderDeliveryFee,
|
||||||
"BusinessDeliveryFee": businessDeliveryFee,
|
"BusinessDeliveryFee": businessDeliveryFee,
|
||||||
|
"BusinessOrderTypes": businessOrderTypesArray,
|
||||||
"OrderStatusID": qOrder.OrderStatusID,
|
"OrderStatusID": qOrder.OrderStatusID,
|
||||||
"OrderAddressID": qOrder.OrderAddressID,
|
"OrderAddressID": qOrder.OrderAddressID,
|
||||||
"OrderPaymentID": qOrder.OrderPaymentID,
|
"OrderPaymentID": qOrder.OrderPaymentID,
|
||||||
|
|
|
||||||
|
|
@ -90,19 +90,25 @@
|
||||||
<cfset var qLI = queryExecute(
|
<cfset var qLI = queryExecute(
|
||||||
"
|
"
|
||||||
SELECT
|
SELECT
|
||||||
OrderLineItemID,
|
oli.OrderLineItemID,
|
||||||
OrderLineItemParentOrderLineItemID,
|
oli.OrderLineItemParentOrderLineItemID,
|
||||||
OrderLineItemOrderID,
|
oli.OrderLineItemOrderID,
|
||||||
OrderLineItemItemID,
|
oli.OrderLineItemItemID,
|
||||||
OrderLineItemStatusID,
|
oli.OrderLineItemStatusID,
|
||||||
OrderLineItemPrice,
|
oli.OrderLineItemPrice,
|
||||||
OrderLineItemQuantity,
|
oli.OrderLineItemQuantity,
|
||||||
OrderLineItemRemark,
|
oli.OrderLineItemRemark,
|
||||||
OrderLineItemIsDeleted,
|
oli.OrderLineItemIsDeleted,
|
||||||
OrderLineItemAddedOn
|
oli.OrderLineItemAddedOn,
|
||||||
FROM OrderLineItems
|
i.ItemName,
|
||||||
WHERE OrderLineItemOrderID = ?
|
i.ItemParentItemID,
|
||||||
ORDER BY OrderLineItemID
|
i.ItemIsCheckedByDefault,
|
||||||
|
parent.ItemName AS ItemParentName
|
||||||
|
FROM OrderLineItems oli
|
||||||
|
INNER JOIN Items i ON i.ItemID = oli.OrderLineItemItemID
|
||||||
|
LEFT JOIN Items parent ON parent.ItemID = i.ItemParentItemID
|
||||||
|
WHERE oli.OrderLineItemOrderID = ?
|
||||||
|
ORDER BY oli.OrderLineItemID
|
||||||
",
|
",
|
||||||
[ { value = arguments.OrderID, cfsqltype = "cf_sql_integer" } ],
|
[ { value = arguments.OrderID, cfsqltype = "cf_sql_integer" } ],
|
||||||
{ datasource = "payfrit" }
|
{ datasource = "payfrit" }
|
||||||
|
|
@ -120,7 +126,11 @@
|
||||||
"OrderLineItemQuantity": qLI.OrderLineItemQuantity,
|
"OrderLineItemQuantity": qLI.OrderLineItemQuantity,
|
||||||
"OrderLineItemRemark": qLI.OrderLineItemRemark,
|
"OrderLineItemRemark": qLI.OrderLineItemRemark,
|
||||||
"OrderLineItemIsDeleted": qLI.OrderLineItemIsDeleted,
|
"OrderLineItemIsDeleted": qLI.OrderLineItemIsDeleted,
|
||||||
"OrderLineItemAddedOn": qLI.OrderLineItemAddedOn
|
"OrderLineItemAddedOn": qLI.OrderLineItemAddedOn,
|
||||||
|
"ItemName": qLI.ItemName,
|
||||||
|
"ItemParentItemID": qLI.ItemParentItemID,
|
||||||
|
"ItemParentName": qLI.ItemParentName,
|
||||||
|
"ItemIsCheckedByDefault": qLI.ItemIsCheckedByDefault
|
||||||
})>
|
})>
|
||||||
</cfloop>
|
</cfloop>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -281,13 +281,34 @@
|
||||||
|
|
||||||
<cfset OrderID = val( structKeyExists(data,"OrderID") ? data.OrderID : 0 )>
|
<cfset OrderID = val( structKeyExists(data,"OrderID") ? data.OrderID : 0 )>
|
||||||
<cfset ParentLineItemID = val( structKeyExists(data,"ParentOrderLineItemID") ? data.ParentOrderLineItemID : 0 )>
|
<cfset ParentLineItemID = val( structKeyExists(data,"ParentOrderLineItemID") ? data.ParentOrderLineItemID : 0 )>
|
||||||
<cfset ItemID = val( structKeyExists(data,"ItemID") ? data.ItemID : 0 )>
|
<cfset OriginalItemID = structKeyExists(data,"ItemID") ? data.ItemID : 0>
|
||||||
|
<cfset ItemID = val(OriginalItemID)>
|
||||||
|
|
||||||
|
<!--- Decode virtual IDs from menu API (format: menuItemID * 100000 + realItemID) --->
|
||||||
|
<!--- If ItemID > 100000, extract the real ItemID --->
|
||||||
|
<cfset WasDecoded = false>
|
||||||
|
<cfif ItemID GT 100000>
|
||||||
|
<cfset WasDecoded = true>
|
||||||
|
<cfset ItemID = ItemID MOD 100000>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<!--- Store debug info for response --->
|
||||||
|
<cfset request.itemDebug = {
|
||||||
|
"OriginalItemID": OriginalItemID,
|
||||||
|
"ParsedItemID": val(OriginalItemID),
|
||||||
|
"WasDecoded": WasDecoded,
|
||||||
|
"FinalItemID": ItemID
|
||||||
|
}>
|
||||||
<cfset IsSelected = false>
|
<cfset IsSelected = false>
|
||||||
<cfif structKeyExists(data, "IsSelected")>
|
<cfif structKeyExists(data, "IsSelected")>
|
||||||
<cfset IsSelected = (data.IsSelected EQ true OR data.IsSelected EQ 1 OR (isSimpleValue(data.IsSelected) AND lcase(toString(data.IsSelected)) EQ "true"))>
|
<cfset IsSelected = (data.IsSelected EQ true OR data.IsSelected EQ 1 OR (isSimpleValue(data.IsSelected) AND lcase(toString(data.IsSelected)) EQ "true"))>
|
||||||
</cfif>
|
</cfif>
|
||||||
<cfset Quantity = structKeyExists(data,"Quantity") ? val(data.Quantity) : 0>
|
<cfset Quantity = structKeyExists(data,"Quantity") ? val(data.Quantity) : 0>
|
||||||
<cfset Remark = structKeyExists(data,"Remark") ? toString(data.Remark) : "">
|
<cfset Remark = structKeyExists(data,"Remark") ? toString(data.Remark) : "">
|
||||||
|
<cfset ForceNew = false>
|
||||||
|
<cfif structKeyExists(data, "ForceNew")>
|
||||||
|
<cfset ForceNew = (data.ForceNew EQ true OR data.ForceNew EQ 1 OR (isSimpleValue(data.ForceNew) AND lcase(toString(data.ForceNew)) EQ "true"))>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
<cfif OrderID LTE 0 OR ItemID LTE 0>
|
<cfif OrderID LTE 0 OR ItemID LTE 0>
|
||||||
<cfset apiAbort({ "OK": false, "ERROR": "missing_params", "MESSAGE": "OrderID and ItemID are required.", "DETAIL": "" })>
|
<cfset apiAbort({ "OK": false, "ERROR": "missing_params", "MESSAGE": "OrderID and ItemID are required.", "DETAIL": "" })>
|
||||||
|
|
@ -307,7 +328,13 @@
|
||||||
)>
|
)>
|
||||||
|
|
||||||
<cfif qItem.recordCount EQ 0 OR qItem.ItemIsActive NEQ 1>
|
<cfif qItem.recordCount EQ 0 OR qItem.ItemIsActive NEQ 1>
|
||||||
<cfset apiAbort({ "OK": false, "ERROR": "bad_item", "MESSAGE": "Item not found or inactive.", "DETAIL": "" })>
|
<cfset apiAbort({
|
||||||
|
"OK": false,
|
||||||
|
"ERROR": "bad_item",
|
||||||
|
"MESSAGE": "Item not found or inactive. Original=#OriginalItemID# Decoded=#ItemID# WasDecoded=#WasDecoded#",
|
||||||
|
"DETAIL": "",
|
||||||
|
"DEBUG_ITEM": request.itemDebug
|
||||||
|
})>
|
||||||
</cfif>
|
</cfif>
|
||||||
|
|
||||||
<!--- Root vs modifier rules --->
|
<!--- Root vs modifier rules --->
|
||||||
|
|
@ -365,23 +392,28 @@
|
||||||
</cfif>
|
</cfif>
|
||||||
</cfif>
|
</cfif>
|
||||||
|
|
||||||
<!--- Find existing line item (by order, parent LI, item) --->
|
<!--- Find existing line item (by order, parent LI, item) - unless ForceNew is set --->
|
||||||
<cfset qExisting = queryExecute(
|
<cfif ForceNew>
|
||||||
"
|
<!--- ForceNew: Skip existing lookup, will always create new line item --->
|
||||||
SELECT OrderLineItemID
|
<cfset qExisting = queryNew("OrderLineItemID", "integer")>
|
||||||
FROM OrderLineItems
|
<cfelse>
|
||||||
WHERE OrderLineItemOrderID = ?
|
<cfset qExisting = queryExecute(
|
||||||
AND OrderLineItemParentOrderLineItemID = ?
|
"
|
||||||
AND OrderLineItemItemID = ?
|
SELECT OrderLineItemID
|
||||||
LIMIT 1
|
FROM OrderLineItems
|
||||||
",
|
WHERE OrderLineItemOrderID = ?
|
||||||
[
|
AND OrderLineItemParentOrderLineItemID = ?
|
||||||
{ value = OrderID, cfsqltype = "cf_sql_integer" },
|
AND OrderLineItemItemID = ?
|
||||||
{ value = ParentLineItemID, cfsqltype = "cf_sql_integer" },
|
LIMIT 1
|
||||||
{ value = ItemID, cfsqltype = "cf_sql_integer" }
|
",
|
||||||
],
|
[
|
||||||
{ datasource = "payfrit" }
|
{ value = OrderID, cfsqltype = "cf_sql_integer" },
|
||||||
)>
|
{ value = ParentLineItemID, cfsqltype = "cf_sql_integer" },
|
||||||
|
{ value = ItemID, cfsqltype = "cf_sql_integer" }
|
||||||
|
],
|
||||||
|
{ datasource = "payfrit" }
|
||||||
|
)>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
<!--- Initialize debug array at start of processing --->
|
<!--- Initialize debug array at start of processing --->
|
||||||
<cfset request.attachDebug = ["Flow start: qExisting.recordCount=#qExisting.recordCount#, IsSelected=#IsSelected#"]>
|
<cfset request.attachDebug = ["Flow start: qExisting.recordCount=#qExisting.recordCount#, IsSelected=#IsSelected#"]>
|
||||||
|
|
|
||||||
|
|
@ -90,19 +90,25 @@
|
||||||
<cfset var qLI = queryExecute(
|
<cfset var qLI = queryExecute(
|
||||||
"
|
"
|
||||||
SELECT
|
SELECT
|
||||||
OrderLineItemID,
|
oli.OrderLineItemID,
|
||||||
OrderLineItemParentOrderLineItemID,
|
oli.OrderLineItemParentOrderLineItemID,
|
||||||
OrderLineItemOrderID,
|
oli.OrderLineItemOrderID,
|
||||||
OrderLineItemItemID,
|
oli.OrderLineItemItemID,
|
||||||
OrderLineItemStatusID,
|
oli.OrderLineItemStatusID,
|
||||||
OrderLineItemPrice,
|
oli.OrderLineItemPrice,
|
||||||
OrderLineItemQuantity,
|
oli.OrderLineItemQuantity,
|
||||||
OrderLineItemRemark,
|
oli.OrderLineItemRemark,
|
||||||
OrderLineItemIsDeleted,
|
oli.OrderLineItemIsDeleted,
|
||||||
OrderLineItemAddedOn
|
oli.OrderLineItemAddedOn,
|
||||||
FROM OrderLineItems
|
i.ItemName,
|
||||||
WHERE OrderLineItemOrderID = ?
|
i.ItemParentItemID,
|
||||||
ORDER BY OrderLineItemID
|
i.ItemIsCheckedByDefault,
|
||||||
|
parent.ItemName AS ItemParentName
|
||||||
|
FROM OrderLineItems oli
|
||||||
|
INNER JOIN Items i ON i.ItemID = oli.OrderLineItemItemID
|
||||||
|
LEFT JOIN Items parent ON parent.ItemID = i.ItemParentItemID
|
||||||
|
WHERE oli.OrderLineItemOrderID = ?
|
||||||
|
ORDER BY oli.OrderLineItemID
|
||||||
",
|
",
|
||||||
[ { value = arguments.OrderID, cfsqltype = "cf_sql_integer" } ],
|
[ { value = arguments.OrderID, cfsqltype = "cf_sql_integer" } ],
|
||||||
{ datasource = "payfrit" }
|
{ datasource = "payfrit" }
|
||||||
|
|
@ -120,7 +126,11 @@
|
||||||
"OrderLineItemQuantity": qLI.OrderLineItemQuantity,
|
"OrderLineItemQuantity": qLI.OrderLineItemQuantity,
|
||||||
"OrderLineItemRemark": qLI.OrderLineItemRemark,
|
"OrderLineItemRemark": qLI.OrderLineItemRemark,
|
||||||
"OrderLineItemIsDeleted": qLI.OrderLineItemIsDeleted,
|
"OrderLineItemIsDeleted": qLI.OrderLineItemIsDeleted,
|
||||||
"OrderLineItemAddedOn": qLI.OrderLineItemAddedOn
|
"OrderLineItemAddedOn": qLI.OrderLineItemAddedOn,
|
||||||
|
"ItemName": qLI.ItemName,
|
||||||
|
"ItemParentItemID": qLI.ItemParentItemID,
|
||||||
|
"ItemParentName": qLI.ItemParentName,
|
||||||
|
"ItemIsCheckedByDefault": qLI.ItemIsCheckedByDefault
|
||||||
})>
|
})>
|
||||||
</cfloop>
|
</cfloop>
|
||||||
|
|
||||||
|
|
@ -146,12 +156,12 @@
|
||||||
})>
|
})>
|
||||||
</cfif>
|
</cfif>
|
||||||
|
|
||||||
<!--- OrderTypeID: 1=dine-in, 2=takeaway, 3=delivery --->
|
<!--- OrderTypeID: 0=undecided, 1=dine-in, 2=takeaway, 3=delivery --->
|
||||||
<cfif OrderTypeID LT 1 OR OrderTypeID GT 3>
|
<cfif OrderTypeID LT 0 OR OrderTypeID GT 3>
|
||||||
<cfset apiAbort({
|
<cfset apiAbort({
|
||||||
"OK": false,
|
"OK": false,
|
||||||
"ERROR": "invalid_order_type",
|
"ERROR": "invalid_order_type",
|
||||||
"MESSAGE": "OrderTypeID must be 1 (dine-in), 2 (takeaway), or 3 (delivery).",
|
"MESSAGE": "OrderTypeID must be 0 (undecided), 1 (dine-in), 2 (takeaway), or 3 (delivery).",
|
||||||
"DETAIL": ""
|
"DETAIL": ""
|
||||||
})>
|
})>
|
||||||
</cfif>
|
</cfif>
|
||||||
|
|
@ -202,29 +212,50 @@
|
||||||
|
|
||||||
<!--- Update order type and address --->
|
<!--- Update order type and address --->
|
||||||
<cftry>
|
<cftry>
|
||||||
<cfset queryExecute(
|
<cfif OrderTypeID EQ 3>
|
||||||
"
|
<!--- Delivery: set address and fee --->
|
||||||
UPDATE Orders
|
<cfset queryExecute(
|
||||||
SET OrderTypeID = ?,
|
"
|
||||||
OrderAddressID = ?,
|
UPDATE Orders
|
||||||
OrderDeliveryFee = ?,
|
SET OrderTypeID = ?,
|
||||||
OrderLastEditedOn = ?
|
OrderAddressID = ?,
|
||||||
WHERE OrderID = ?
|
OrderDeliveryFee = ?,
|
||||||
",
|
OrderLastEditedOn = ?
|
||||||
[
|
WHERE OrderID = ?
|
||||||
{ value = OrderTypeID, cfsqltype = "cf_sql_integer" },
|
",
|
||||||
{ value = (OrderTypeID EQ 3 ? AddressID : javaCast("null", "")), cfsqltype = "cf_sql_integer", null = (OrderTypeID NEQ 3) },
|
[
|
||||||
{ value = deliveryFee, cfsqltype = "cf_sql_decimal" },
|
{ value = OrderTypeID, cfsqltype = "cf_sql_integer" },
|
||||||
{ value = now(), cfsqltype = "cf_sql_timestamp" },
|
{ value = AddressID, cfsqltype = "cf_sql_integer" },
|
||||||
{ value = OrderID, cfsqltype = "cf_sql_integer" }
|
{ value = deliveryFee, cfsqltype = "cf_sql_decimal" },
|
||||||
],
|
{ value = now(), cfsqltype = "cf_sql_timestamp" },
|
||||||
{ datasource = "payfrit" }
|
{ value = OrderID, cfsqltype = "cf_sql_integer" }
|
||||||
)>
|
],
|
||||||
|
{ datasource = "payfrit" }
|
||||||
|
)>
|
||||||
|
<cfelse>
|
||||||
|
<!--- Takeaway/Dine-in: clear address and fee --->
|
||||||
|
<cfset queryExecute(
|
||||||
|
"
|
||||||
|
UPDATE Orders
|
||||||
|
SET OrderTypeID = ?,
|
||||||
|
OrderAddressID = NULL,
|
||||||
|
OrderDeliveryFee = 0,
|
||||||
|
OrderLastEditedOn = ?
|
||||||
|
WHERE OrderID = ?
|
||||||
|
",
|
||||||
|
[
|
||||||
|
{ value = OrderTypeID, cfsqltype = "cf_sql_integer" },
|
||||||
|
{ value = now(), cfsqltype = "cf_sql_timestamp" },
|
||||||
|
{ value = OrderID, cfsqltype = "cf_sql_integer" }
|
||||||
|
],
|
||||||
|
{ datasource = "payfrit" }
|
||||||
|
)>
|
||||||
|
</cfif>
|
||||||
<cfcatch>
|
<cfcatch>
|
||||||
<cfset apiAbort({
|
<cfset apiAbort({
|
||||||
"OK": false,
|
"OK": false,
|
||||||
"ERROR": "update_error",
|
"ERROR": "update_error",
|
||||||
"MESSAGE": "Failed to update order type",
|
"MESSAGE": "Failed to update order type: " & cfcatch.message & " | SQL: " & (structKeyExists(cfcatch, "sql") ? cfcatch.sql : "none"),
|
||||||
"DETAIL": cfcatch.message
|
"DETAIL": cfcatch.message
|
||||||
})>
|
})>
|
||||||
</cfcatch>
|
</cfcatch>
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,54 @@ try {
|
||||||
apiAbort({ "OK": false, "ERROR": "missing_params", "MESSAGE": "ServicePointID is required" });
|
apiAbort({ "OK": false, "ERROR": "missing_params", "MESSAGE": "ServicePointID is required" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for existing open chat for this user at this business
|
||||||
|
// An open chat is one where TaskTypeID=2 (Chat) and TaskCompletedOn IS NULL
|
||||||
|
// Also check it's tied to the current order if we have one
|
||||||
|
forceNew = structKeyExists(data, "ForceNew") && data.ForceNew == true;
|
||||||
|
|
||||||
|
if (userID > 0 && !forceNew) {
|
||||||
|
existingChat = queryExecute("
|
||||||
|
SELECT t.TaskID, t.TaskAddedOn
|
||||||
|
FROM Tasks t
|
||||||
|
LEFT JOIN Orders o ON o.OrderID = t.TaskOrderID
|
||||||
|
WHERE t.TaskBusinessID = :businessID
|
||||||
|
AND t.TaskTypeID = 2
|
||||||
|
AND t.TaskCompletedOn IS NULL
|
||||||
|
AND (
|
||||||
|
(t.TaskOrderID = :orderID AND :orderID > 0)
|
||||||
|
OR (t.TaskOrderID IS NULL AND o.OrderUserID = :userID)
|
||||||
|
OR (t.TaskOrderID IS NULL AND t.TaskSourceID = :servicePointID)
|
||||||
|
)
|
||||||
|
ORDER BY t.TaskAddedOn DESC
|
||||||
|
LIMIT 1
|
||||||
|
", {
|
||||||
|
businessID: { value: businessID, cfsqltype: "cf_sql_integer" },
|
||||||
|
userID: { value: userID, cfsqltype: "cf_sql_integer" },
|
||||||
|
servicePointID: { value: servicePointID, cfsqltype: "cf_sql_integer" },
|
||||||
|
orderID: { value: orderID, cfsqltype: "cf_sql_integer" }
|
||||||
|
}, { datasource: "payfrit" });
|
||||||
|
|
||||||
|
if (existingChat.recordCount > 0) {
|
||||||
|
// Check if chat is stale (more than 20 minutes old with no activity)
|
||||||
|
chatAge = dateDiff("n", existingChat.TaskAddedOn, now());
|
||||||
|
if (chatAge > 20) {
|
||||||
|
// Auto-close stale chat
|
||||||
|
queryExecute("
|
||||||
|
UPDATE Tasks SET TaskCompletedOn = NOW()
|
||||||
|
WHERE TaskID = :taskID
|
||||||
|
", { taskID: { value: existingChat.TaskID, cfsqltype: "cf_sql_integer" } }, { datasource: "payfrit" });
|
||||||
|
} else {
|
||||||
|
// Return existing chat
|
||||||
|
apiAbort({
|
||||||
|
"OK": true,
|
||||||
|
"TaskID": existingChat.TaskID,
|
||||||
|
"MESSAGE": "Rejoined existing chat",
|
||||||
|
"EXISTING": true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Get service point info (table name)
|
// Get service point info (table name)
|
||||||
spQuery = queryExecute("
|
spQuery = queryExecute("
|
||||||
SELECT ServicePointName FROM ServicePoints WHERE ServicePointID = :spID
|
SELECT ServicePointName FROM ServicePoints WHERE ServicePointID = :spID
|
||||||
|
|
|
||||||
74
api/tasks/expireStaleChats.cfm
Normal file
74
api/tasks/expireStaleChats.cfm
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
<cfsetting showdebugoutput="false" requesttimeout="30">
|
||||||
|
<cfset request._api_isPublic = true>
|
||||||
|
<cfsetting enablecfoutputonly="true">
|
||||||
|
<cfcontent type="application/json; charset=utf-8" reset="true">
|
||||||
|
|
||||||
|
<cfscript>
|
||||||
|
// Scheduled task to expire stale chats (older than 20 minutes with no recent activity)
|
||||||
|
// Should be called every minute by a cron job or scheduled task
|
||||||
|
|
||||||
|
function apiAbort(required struct payload) {
|
||||||
|
writeOutput(serializeJSON(payload));
|
||||||
|
abort;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Find open chat tasks that are stale
|
||||||
|
// A chat is stale if:
|
||||||
|
// 1. TaskTypeID = 2 (Chat)
|
||||||
|
// 2. TaskCompletedOn IS NULL (not closed)
|
||||||
|
// 3. No messages in the last 20 minutes AND task is older than 20 minutes
|
||||||
|
|
||||||
|
staleChats = queryExecute("
|
||||||
|
SELECT t.TaskID, t.TaskAddedOn,
|
||||||
|
(SELECT MAX(cm.CreatedOn) FROM ChatMessages cm WHERE cm.TaskID = t.TaskID) as LastMessageOn
|
||||||
|
FROM Tasks t
|
||||||
|
WHERE t.TaskTypeID = 2
|
||||||
|
AND t.TaskCompletedOn IS NULL
|
||||||
|
AND t.TaskAddedOn < DATE_SUB(NOW(), INTERVAL 20 MINUTE)
|
||||||
|
", {}, { datasource: "payfrit" });
|
||||||
|
|
||||||
|
expiredCount = 0;
|
||||||
|
expiredIds = [];
|
||||||
|
|
||||||
|
for (chat in staleChats) {
|
||||||
|
// Check last message time - if no messages or last message > 20 min ago, expire
|
||||||
|
shouldExpire = false;
|
||||||
|
|
||||||
|
if (isNull(chat.LastMessageOn) || !isDate(chat.LastMessageOn)) {
|
||||||
|
// No messages at all - expire if task is old enough
|
||||||
|
shouldExpire = true;
|
||||||
|
} else {
|
||||||
|
// Has messages - check if last one is older than 20 minutes
|
||||||
|
lastMsgAge = dateDiff("n", chat.LastMessageOn, now());
|
||||||
|
if (lastMsgAge > 20) {
|
||||||
|
shouldExpire = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shouldExpire) {
|
||||||
|
queryExecute("
|
||||||
|
UPDATE Tasks SET TaskCompletedOn = NOW()
|
||||||
|
WHERE TaskID = :taskID
|
||||||
|
", { taskID: { value: chat.TaskID, cfsqltype: "cf_sql_integer" } }, { datasource: "payfrit" });
|
||||||
|
|
||||||
|
expiredCount++;
|
||||||
|
arrayAppend(expiredIds, chat.TaskID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
apiAbort({
|
||||||
|
"OK": true,
|
||||||
|
"MESSAGE": "Expired #expiredCount# stale chat(s)",
|
||||||
|
"EXPIRED_TASK_IDS": expiredIds,
|
||||||
|
"CHECKED_COUNT": staleChats.recordCount
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (any e) {
|
||||||
|
apiAbort({
|
||||||
|
"OK": false,
|
||||||
|
"ERROR": "server_error",
|
||||||
|
"MESSAGE": e.message
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</cfscript>
|
||||||
74
cron/expireStaleChats.cfm
Normal file
74
cron/expireStaleChats.cfm
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
<cfsetting showdebugoutput="false" requesttimeout="30">
|
||||||
|
<cfset request._api_isPublic = true>
|
||||||
|
<cfsetting enablecfoutputonly="true">
|
||||||
|
<cfcontent type="application/json; charset=utf-8" reset="true">
|
||||||
|
|
||||||
|
<cfscript>
|
||||||
|
// Scheduled task to expire stale chats (older than 20 minutes with no recent activity)
|
||||||
|
// Should be called every minute by a cron job or scheduled task
|
||||||
|
|
||||||
|
function apiAbort(required struct payload) {
|
||||||
|
writeOutput(serializeJSON(payload));
|
||||||
|
abort;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Find open chat tasks that are stale
|
||||||
|
// A chat is stale if:
|
||||||
|
// 1. TaskTypeID = 2 (Chat)
|
||||||
|
// 2. TaskCompletedOn IS NULL (not closed)
|
||||||
|
// 3. No messages in the last 20 minutes AND task is older than 20 minutes
|
||||||
|
|
||||||
|
staleChats = queryExecute("
|
||||||
|
SELECT t.TaskID, t.TaskAddedOn,
|
||||||
|
(SELECT MAX(cm.CreatedOn) FROM ChatMessages cm WHERE cm.TaskID = t.TaskID) as LastMessageOn
|
||||||
|
FROM Tasks t
|
||||||
|
WHERE t.TaskTypeID = 2
|
||||||
|
AND t.TaskCompletedOn IS NULL
|
||||||
|
AND t.TaskAddedOn < DATE_SUB(NOW(), INTERVAL 20 MINUTE)
|
||||||
|
", {}, { datasource: "payfrit" });
|
||||||
|
|
||||||
|
expiredCount = 0;
|
||||||
|
expiredIds = [];
|
||||||
|
|
||||||
|
for (chat in staleChats) {
|
||||||
|
// Check last message time - if no messages or last message > 20 min ago, expire
|
||||||
|
shouldExpire = false;
|
||||||
|
|
||||||
|
if (isNull(chat.LastMessageOn) || !isDate(chat.LastMessageOn)) {
|
||||||
|
// No messages at all - expire if task is old enough
|
||||||
|
shouldExpire = true;
|
||||||
|
} else {
|
||||||
|
// Has messages - check if last one is older than 20 minutes
|
||||||
|
lastMsgAge = dateDiff("n", chat.LastMessageOn, now());
|
||||||
|
if (lastMsgAge > 20) {
|
||||||
|
shouldExpire = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shouldExpire) {
|
||||||
|
queryExecute("
|
||||||
|
UPDATE Tasks SET TaskCompletedOn = NOW()
|
||||||
|
WHERE TaskID = :taskID
|
||||||
|
", { taskID: { value: chat.TaskID, cfsqltype: "cf_sql_integer" } }, { datasource: "payfrit" });
|
||||||
|
|
||||||
|
expiredCount++;
|
||||||
|
arrayAppend(expiredIds, chat.TaskID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
apiAbort({
|
||||||
|
"OK": true,
|
||||||
|
"MESSAGE": "Expired #expiredCount# stale chat(s)",
|
||||||
|
"EXPIRED_TASK_IDS": expiredIds,
|
||||||
|
"CHECKED_COUNT": staleChats.recordCount
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (any e) {
|
||||||
|
apiAbort({
|
||||||
|
"OK": false,
|
||||||
|
"ERROR": "server_error",
|
||||||
|
"MESSAGE": e.message
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</cfscript>
|
||||||
Loading…
Add table
Reference in a new issue