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/saveType.cfm
John Mizerek 1210249f54 Normalize database column and table names across entire codebase
Update all SQL queries, query result references, and ColdFusion code to match
the renamed database schema. Tables use plural CamelCase, PKs are all `ID`,
column prefixes stripped (e.g. BusinessName→Name, UserFirstName→FirstName).

Key changes:
- Strip table-name prefixes from all column references (Businesses, Users,
  Addresses, Hours, Menus, Categories, Items, Stations, Orders,
  OrderLineItems, Tasks, TaskCategories, TaskRatings, QuickTaskTemplates,
  ScheduledTaskDefinitions, ChatMessages, Beacons, ServicePoints, Employees,
  VisitorTrackings, ApiPerfLogs, tt_States, tt_Days, tt_AddressTypes,
  tt_OrderTypes, tt_TaskTypes)
- Rename PK references from {TableName}ID to ID in all queries
- Rewrite 7 admin beacon files to use ServicePoints.BeaconID instead of
  dropped lt_Beacon_Businesses_ServicePoints link table
- Rewrite beacon assignment files (list, save, delete) for new schema
- Fix FK references incorrectly changed to ID (OrderLineItems.OrderID,
  Categories.MenuID, Tasks.CategoryID, ServicePoints.BeaconID)
- Update Addresses: AddressLat→Latitude, AddressLng→Longitude
- Update Users: UserPassword→Password, UserIsEmailVerified→IsEmailVerified,
  UserIsActive→IsActive, UserBalance→Balance, etc.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-30 15:39:12 -08:00

163 lines
6.5 KiB
Text

<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/json; charset=utf-8" reset="true">
<cfscript>
// Create or update a task type for a business
// Input: BusinessID (required), TaskTypeName (required), TaskTypeDescription (optional), TaskTypeIcon (optional), TaskTypeID (optional - for update)
// Output: { OK: true, TASK_TYPE_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();
// Get BusinessID
businessID = 0;
if (structKeyExists(data, "BusinessID") && isNumeric(data.BusinessID)) {
businessID = int(data.BusinessID);
}
if (businessID == 0) {
apiAbort({ "OK": false, "ERROR": "missing_params", "MESSAGE": "BusinessID is required" });
}
// Get TaskTypeName
taskTypeName = "";
if (structKeyExists(data, "TaskTypeName")) {
taskTypeName = trim(toString(data.TaskTypeName));
}
if (!len(taskTypeName)) {
apiAbort({ "OK": false, "ERROR": "missing_params", "MESSAGE": "TaskTypeName is required" });
}
if (len(taskTypeName) > 45) {
apiAbort({ "OK": false, "ERROR": "invalid_params", "MESSAGE": "TaskTypeName must be 45 characters or less" });
}
// Get TaskTypeDescription (optional)
taskTypeDescription = "";
if (structKeyExists(data, "TaskTypeDescription")) {
taskTypeDescription = trim(toString(data.TaskTypeDescription));
}
if (len(taskTypeDescription) > 100) {
apiAbort({ "OK": false, "ERROR": "invalid_params", "MESSAGE": "TaskTypeDescription must be 100 characters or less" });
}
// Get TaskTypeIcon (optional, default to 'notifications')
taskTypeIcon = "notifications";
if (structKeyExists(data, "TaskTypeIcon") && len(trim(toString(data.TaskTypeIcon)))) {
taskTypeIcon = trim(toString(data.TaskTypeIcon));
}
if (len(taskTypeIcon) > 30) {
apiAbort({ "OK": false, "ERROR": "invalid_params", "MESSAGE": "TaskTypeIcon must be 30 characters or less" });
}
// Get TaskTypeColor (optional, default to purple)
taskTypeColor = "##9C27B0";
if (structKeyExists(data, "TaskTypeColor") && len(trim(toString(data.TaskTypeColor)))) {
taskTypeColor = trim(toString(data.TaskTypeColor));
// Ensure it starts with # (incoming JSON has single #, we store single #)
if (left(taskTypeColor, 1) != chr(35)) {
taskTypeColor = chr(35) & taskTypeColor;
}
}
if (len(taskTypeColor) > 7) {
apiAbort({ "OK": false, "ERROR": "invalid_params", "MESSAGE": "TaskTypeColor must be a valid hex color" });
}
// Get TaskTypeCategoryID (optional - links to TaskCategories for task creation)
taskTypeCategoryID = javaCast("null", "");
if (structKeyExists(data, "TaskTypeCategoryID") && isNumeric(data.TaskTypeCategoryID) && data.TaskTypeCategoryID > 0) {
taskTypeCategoryID = int(data.TaskTypeCategoryID);
} else if (structKeyExists(data, "CategoryID") && isNumeric(data.CategoryID) && data.CategoryID > 0) {
taskTypeCategoryID = int(data.CategoryID);
}
// Get TaskTypeID (optional - for update)
taskTypeID = 0;
if (structKeyExists(data, "TaskTypeID") && isNumeric(data.TaskTypeID)) {
taskTypeID = int(data.TaskTypeID);
}
if (taskTypeID > 0) {
// UPDATE - verify it belongs to this business
qCheck = queryExecute("
SELECT tt_TaskTypeID FROM tt_TaskTypes
WHERE tt_TaskTypeID = :taskTypeID
AND BusinessID = :businessID
", {
taskTypeID: { value: taskTypeID, cfsqltype: "cf_sql_integer" },
businessID: { value: businessID, cfsqltype: "cf_sql_integer" }
}, { datasource: "payfrit" });
if (qCheck.recordCount == 0) {
apiAbort({ "OK": false, "ERROR": "not_found", "MESSAGE": "Task type not found or does not belong to this business" });
}
queryExecute("
UPDATE tt_TaskTypes
SET Name = :taskTypeName,
Description = :taskTypeDescription,
Icon = :taskTypeIcon,
Color = :taskTypeColor,
TaskCategoryID = :categoryID
WHERE tt_TaskTypeID = :taskTypeID
", {
taskTypeName: { value: taskTypeName, cfsqltype: "cf_sql_varchar" },
taskTypeDescription: { value: taskTypeDescription, cfsqltype: "cf_sql_varchar", null: !len(taskTypeDescription) },
taskTypeIcon: { value: taskTypeIcon, cfsqltype: "cf_sql_varchar" },
taskTypeColor: { value: taskTypeColor, cfsqltype: "cf_sql_varchar" },
categoryID: { value: taskTypeCategoryID, cfsqltype: "cf_sql_integer", null: isNull(taskTypeCategoryID) },
taskTypeID: { value: taskTypeID, cfsqltype: "cf_sql_integer" }
}, { datasource: "payfrit" });
apiAbort({
"OK": true,
"TASK_TYPE_ID": taskTypeID,
"MESSAGE": "Task type updated"
});
} else {
// INSERT new task type
queryExecute("
INSERT INTO tt_TaskTypes (Name, Description, Icon, Color, BusinessID, TaskCategoryID)
VALUES (:taskTypeName, :taskTypeDescription, :taskTypeIcon, :taskTypeColor, :businessID, :categoryID)
", {
taskTypeName: { value: taskTypeName, cfsqltype: "cf_sql_varchar" },
taskTypeDescription: { value: taskTypeDescription, cfsqltype: "cf_sql_varchar", null: !len(taskTypeDescription) },
taskTypeIcon: { value: taskTypeIcon, cfsqltype: "cf_sql_varchar" },
taskTypeColor: { value: taskTypeColor, cfsqltype: "cf_sql_varchar" },
businessID: { value: businessID, cfsqltype: "cf_sql_integer" },
categoryID: { value: taskTypeCategoryID, cfsqltype: "cf_sql_integer", null: isNull(taskTypeCategoryID) }
}, { datasource: "payfrit" });
qNew = queryExecute("SELECT LAST_INSERT_ID() as newID", [], { datasource: "payfrit" });
newID = qNew.newID;
apiAbort({
"OK": true,
"TASK_TYPE_ID": newID,
"MESSAGE": "Task type created"
});
}
} catch (any e) {
apiAbort({
"OK": false,
"ERROR": "server_error",
"MESSAGE": e.message
});
}
</cfscript>