// Create a task (service bell request or photo task) // Input for service bell: BusinessID, ServicePointID, TaskTypeID, Message // Input for photo task: BusinessID, ItemID, TaskType, Instructions, PYTReward // Output: { OK: true, TASK_ID: ... } response = { "OK": false }; try { requestBody = toString(getHttpRequestData().content); if (!len(requestBody)) { throw("Request body is required"); } jsonData = deserializeJSON(requestBody); businessID = val(jsonData.BusinessID ?: 0); if (businessID == 0) { throw("BusinessID is required"); } // Check if this is a service bell request (has TaskTypeID) taskTypeID = val(jsonData.TaskTypeID ?: 0); if (taskTypeID > 0) { // Service bell task servicePointID = val(jsonData.ServicePointID ?: 0); orderID = val(jsonData.OrderID ?: 0); userID = val(jsonData.UserID ?: 0); message = jsonData.Message ?: ""; // Get task type info for display (including category) taskTypeQuery = queryTimed(" SELECT Name, Color, Icon, TaskCategoryID FROM tt_TaskTypes WHERE ID = :taskTypeID ", { taskTypeID: taskTypeID }, { datasource: "payfrit" }); taskTitle = message; categoryID = 0; if (taskTypeQuery.recordCount) { if (len(trim(taskTypeQuery.Name))) { taskTitle = taskTypeQuery.Name; } if (!isNull(taskTypeQuery.TaskCategoryID) && isNumeric(taskTypeQuery.TaskCategoryID) && taskTypeQuery.TaskCategoryID > 0) { categoryID = taskTypeQuery.TaskCategoryID; } } // If no category from task type, look up or create default "Service" category if (categoryID == 0) { catQuery = queryTimed(" SELECT ID FROM TaskCategories WHERE BusinessID = :businessID AND Name = 'Service' LIMIT 1 ", { businessID: businessID }, { datasource: "payfrit" }); if (catQuery.recordCount > 0) { categoryID = catQuery.ID; } else { // Create the Service category queryTimed(" INSERT INTO TaskCategories (BusinessID, Name, Color) VALUES (:businessID, 'Service', '##FF9800') ", { businessID: businessID }, { datasource: "payfrit" }); catResult = queryTimed("SELECT LAST_INSERT_ID() as newID", {}, { datasource: "payfrit" }); categoryID = catResult.newID; } } // Use message as details taskDetails = message; // Insert service bell task with ServicePointID and UserID queryTimed(" INSERT INTO Tasks ( BusinessID, ServicePointID, TaskTypeID, CategoryID, OrderID, UserID, Title, Details, CreatedOn, ClaimedByUserID ) VALUES ( :businessID, :servicePointID, :taskTypeID, :categoryID, :orderID, :userID, :taskTitle, :taskDetails, NOW(), 0 ) ", { businessID: businessID, servicePointID: servicePointID, taskTypeID: taskTypeID, categoryID: categoryID, orderID: orderID, userID: userID, taskTitle: taskTitle, taskDetails: taskDetails }, { datasource: "payfrit" }); } else { // Legacy photo task itemID = val(jsonData.ItemID ?: 0); taskType = jsonData.TaskType ?: "employee_photo"; instructions = jsonData.Instructions ?: ""; pytReward = val(jsonData.PYTReward ?: 0); // Get item info if itemID provided itemName = ""; if (itemID > 0) { itemQuery = queryTimed(" SELECT Name FROM Items WHERE ID = :itemID ", { itemID: itemID }); if (itemQuery.recordCount) { itemName = itemQuery.Name; } } // Create task description taskDescription = ""; switch(taskType) { case "employee_photo": taskDescription = "Take a photo of: " & itemName; break; case "user_photo": taskDescription = "Submit a photo of " & itemName & " to earn " & pytReward & " PYT"; break; default: taskDescription = instructions; } // Insert legacy task queryTimed(" INSERT INTO Tasks ( BusinessID, TaskItemID, TaskType, TaskDescription, TaskInstructions, TaskPYTReward, TaskStatus, CreatedOn ) VALUES ( :businessID, :itemID, :taskType, :description, :instructions, :pytReward, 'pending', NOW() ) ", { businessID: businessID, itemID: itemID, taskType: taskType, description: taskDescription, instructions: instructions, pytReward: pytReward }); } // Get the new task ID result = queryTimed("SELECT LAST_INSERT_ID() as newID", {}, { datasource: "payfrit" }); taskID = result.newID; response = { "OK": true, "TASK_ID": taskID, "MESSAGE": "Task created successfully" }; } catch (any e) { response = { "OK": false, "ERROR": e.message, "DETAIL": e.detail ?: "" }; } cfheader(name="Content-Type", value="application/json"); writeOutput(serializeJSON(response));