// 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); if (businessID == 0) { apiAbort({ "OK": false, "ERROR": "missing_params", "MESSAGE": "BusinessID is required" }); } if (servicePointID == 0) { apiAbort({ "OK": false, "ERROR": "missing_params", "MESSAGE": "ServicePointID is required" }); } // Get service point info (table name) spQuery = queryExecute(" SELECT ServicePointName FROM ServicePoints WHERE ServicePointID = :spID ", { spID: { value: servicePointID, cfsqltype: "cf_sql_integer" } }, { datasource: "payfrit" }); tableName = spQuery.recordCount ? spQuery.ServicePointName : "Table ##" & servicePointID; // Get user name if available userName = ""; if (userID > 0) { userQuery = queryExecute(" SELECT UserFirstName FROM Users WHERE UserID = :userID ", { userID: { value: userID, cfsqltype: "cf_sql_integer" } }, { datasource: "payfrit" }); if (userQuery.recordCount && len(trim(userQuery.UserFirstName))) { userName = userQuery.UserFirstName; } } // Create task title and details taskTitle = "Service Request - " & tableName; taskDetails = ""; if (len(userName)) { taskDetails &= "Customer: " & userName & chr(10); } if (len(message)) { taskDetails &= "Request: " & message; } else { taskDetails &= "Customer is requesting assistance"; } // Look up or create a "Service" category for this business catQuery = queryExecute(" SELECT TaskCategoryID FROM TaskCategories WHERE TaskCategoryBusinessID = :businessID AND TaskCategoryName = 'Service' LIMIT 1 ", { businessID: { value: businessID, cfsqltype: "cf_sql_integer" } }, { datasource: "payfrit" }); if (catQuery.recordCount == 0) { // Create the category queryExecute(" INSERT INTO TaskCategories (TaskCategoryBusinessID, TaskCategoryName, TaskCategoryColor) VALUES (:businessID, 'Service', '##FF9800') ", { businessID: { value: businessID, cfsqltype: "cf_sql_integer" } }, { datasource: "payfrit" }); catResult = queryExecute("SELECT LAST_INSERT_ID() as newID", [], { datasource: "payfrit" }); categoryID = catResult.newID; } else { categoryID = catQuery.TaskCategoryID; } // Insert task queryExecute(" INSERT INTO Tasks ( TaskBusinessID, TaskCategoryID, TaskOrderID, TaskTypeID, TaskTitle, TaskDetails, TaskClaimedByUserID, TaskAddedOn ) VALUES ( :businessID, :categoryID, :orderID, 1, :title, :details, 0, NOW() ) ", { businessID: { value: businessID, cfsqltype: "cf_sql_integer" }, categoryID: { value: categoryID, cfsqltype: "cf_sql_integer" }, orderID: { value: orderID > 0 ? orderID : javaCast("null", ""), cfsqltype: "cf_sql_integer", null: orderID == 0 }, 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": "Server has been notified" }); } catch (any e) { apiAbort({ "OK": false, "ERROR": "server_error", "MESSAGE": e.message }); }