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/tasks/callServer.cfm
John Pinkyfloyd b9755a1e72 Add role-aware cash routing and backend improvements
Staff cash goes to worker payout ledger, admin/manager cash deletes
pending payout and reverses withholding. Add RoleID to myBusinesses
response. Various order and webhook improvements.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-04 20:04:46 -08:00

153 lines
5.2 KiB
Text

<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/json; charset=utf-8" reset="true">
<cfscript>
// Customer calls server to their table
// Input: BusinessID, ServicePointID, OrderID (optional), Message (optional)
// Output: { OK: true, TASK_ID: ... }
function apiAbort(required struct payload) {
writeOutput(serializeJSON(payload));
abort;
}
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 {};
}
try {
data = readJsonBody();
businessID = val(structKeyExists(data, "BusinessID") ? data.BusinessID : 0);
servicePointID = val(structKeyExists(data, "ServicePointID") ? data.ServicePointID : 0);
orderID = val(structKeyExists(data, "OrderID") ? data.OrderID : 0);
message = trim(structKeyExists(data, "Message") ? data.Message : "");
userID = val(structKeyExists(data, "UserID") ? data.UserID : 0);
taskTypeID = val(structKeyExists(data, "TaskTypeID") ? data.TaskTypeID : 0);
if (businessID == 0) {
apiAbort({ "OK": false, "ERROR": "missing_params", "MESSAGE": "BusinessID is required" });
}
// If servicePointID not provided but orderID is, look it up from the order
if (servicePointID == 0 && orderID > 0) {
qOrderSP = queryExecute("
SELECT ServicePointID FROM Orders WHERE ID = :orderID
", { orderID: { value: orderID, cfsqltype: "cf_sql_integer" } }, { datasource: "payfrit" });
if (qOrderSP.recordCount && val(qOrderSP.ServicePointID) > 0) {
servicePointID = val(qOrderSP.ServicePointID);
}
}
if (servicePointID == 0) {
apiAbort({ "OK": false, "ERROR": "missing_params", "MESSAGE": "ServicePointID is required" });
}
// Get service point info (table name)
spQuery = queryExecute("
SELECT Name FROM ServicePoints WHERE ID = :spID
", { spID: { value: servicePointID, cfsqltype: "cf_sql_integer" } }, { datasource: "payfrit" });
tableName = spQuery.recordCount ? spQuery.Name : "Table ##" & servicePointID;
// Get user name if available
userName = "";
if (userID > 0) {
userQuery = queryExecute("
SELECT FirstName FROM Users WHERE ID = :userID
", { userID: { value: userID, cfsqltype: "cf_sql_integer" } }, { datasource: "payfrit" });
if (userQuery.recordCount && len(trim(userQuery.FirstName))) {
userName = userQuery.FirstName;
}
}
// Get task type name if TaskTypeID provided
taskTypeName = "";
if (taskTypeID > 0) {
typeQuery = queryExecute("
SELECT Name FROM tt_TaskTypes WHERE ID = :typeID
", { typeID: { value: taskTypeID, cfsqltype: "cf_sql_integer" } }, { datasource: "payfrit" });
if (typeQuery.recordCount && len(trim(typeQuery.Name))) {
taskTypeName = typeQuery.Name;
}
}
// Create task title and details - use task type name if available
if (len(taskTypeName)) {
taskTitle = taskTypeName & " - " & tableName;
} else {
taskTitle = "Service Request - " & tableName;
}
taskDetails = "";
if (len(taskTypeName)) {
taskDetails &= "Task: " & taskTypeName & chr(10);
}
if (len(userName)) {
taskDetails &= "Customer: " & userName & chr(10);
}
taskDetails &= "Location: " & tableName & chr(10);
if (len(message)) {
taskDetails &= "Request: " & message;
} else {
taskDetails &= "Customer is requesting assistance";
}
// Insert task (no CategoryID - using TaskTypeID only)
queryExecute("
INSERT INTO Tasks (
BusinessID,
ServicePointID,
UserID,
OrderID,
TaskTypeID,
Title,
Details,
ClaimedByUserID,
CreatedOn
) VALUES (
:businessID,
:servicePointID,
:userID,
:orderID,
:taskTypeID,
:title,
:details,
0,
NOW()
)
", {
businessID: { value: businessID, cfsqltype: "cf_sql_integer" },
servicePointID: { value: servicePointID, cfsqltype: "cf_sql_integer" },
userID: { value: userID > 0 ? userID : javaCast("null", ""), cfsqltype: "cf_sql_integer", null: userID == 0 },
orderID: { value: orderID > 0 ? orderID : javaCast("null", ""), cfsqltype: "cf_sql_integer", null: orderID == 0 },
taskTypeID: { value: taskTypeID, cfsqltype: "cf_sql_integer" },
title: { value: taskTitle, cfsqltype: "cf_sql_varchar" },
details: { value: taskDetails, cfsqltype: "cf_sql_varchar" }
}, { datasource: "payfrit" });
// Get the new task ID
result = queryExecute("SELECT LAST_INSERT_ID() as newID", [], { datasource: "payfrit" });
taskID = result.newID;
apiAbort({
"OK": true,
"TASK_ID": taskID,
"MESSAGE": "Staff has been notified"
});
} catch (any e) {
apiAbort({
"OK": false,
"ERROR": "server_error",
"MESSAGE": e.message
});
}
</cfscript>