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/admin/cleanupCategories.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

173 lines
6.2 KiB
Text

<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/json; charset=utf-8" reset="true">
<!--- Only allow from localhost --->
<cfif NOT (cgi.remote_addr EQ "127.0.0.1" OR cgi.remote_addr EQ "::1" OR findNoCase("localhost", cgi.server_name))>
<cfoutput>#serializeJSON({"OK": false, "ERROR": "admin_only"})#</cfoutput>
<cfabort>
</cfif>
<cfscript>
/**
* Cleanup Categories - Final step after migration verification
*
* This script:
* 1. Verifies all Items have BusinessID set
* 2. Finds orphan items (ParentID=0, no children, not in links)
* 3. Drops CategoryID column
* 4. Drops IsModifierTemplate column (derived from lt_ItemID_TemplateItemID now)
* 5. Drops Categories table
*
* Query param: ?confirm=YES to actually execute (otherwise shows verification only)
*/
response = { "OK": false, "verification": {}, "orphans": [], "steps": [] };
try {
confirm = structKeyExists(url, "confirm") && url.confirm == "YES";
// Verification Step 1: Check for items without BusinessID
qNoBusinessID = queryExecute("
SELECT COUNT(*) as cnt FROM Items
WHERE BusinessID IS NULL OR BusinessID = 0
", {}, { datasource: "payfrit" });
response.verification["itemsWithoutBusinessID"] = qNoBusinessID.cnt;
// Verification Step 2: Check that all categories were converted
qCategories = queryExecute("
SELECT COUNT(*) as cnt FROM Categories
", {}, { datasource: "payfrit" });
response.verification["categoriesRemaining"] = qCategories.cnt;
// Verification Step 3: Check category Items exist (ParentID=0 with children)
qCategoryItems = queryExecute("
SELECT COUNT(DISTINCT p.ItemID) as cnt
FROM Items p
INNER JOIN Items c ON c.ParentItemID = p.ItemID
WHERE p.ParentItemID = 0
AND p.BusinessID > 0
AND NOT EXISTS (
SELECT 1 FROM lt_ItemID_TemplateItemID tl WHERE tl.TemplateItemID = p.ItemID
)
", {}, { datasource: "payfrit" });
response.verification["categoryItemsCreated"] = qCategoryItems.cnt;
// Verification Step 4: Check templates exist (in lt_ItemID_TemplateItemID)
qTemplates = queryExecute("
SELECT COUNT(DISTINCT tl.TemplateItemID) as cnt
FROM lt_ItemID_TemplateItemID tl
INNER JOIN Items t ON t.ItemID = tl.TemplateItemID
", {}, { datasource: "payfrit" });
response.verification["templatesInLinks"] = qTemplates.cnt;
// Verification Step 5: Find orphans at ParentID=0
// Orphan = ParentID=0, no children pointing to it, not in lt_ItemID_TemplateItemID
qOrphans = queryExecute("
SELECT i.ID, i.Name, i.BusinessID
FROM Items i
WHERE i.ParentItemID = 0
AND NOT EXISTS (
SELECT 1 FROM Items child WHERE child.ParentItemID = i.ID
)
AND NOT EXISTS (
SELECT 1 FROM lt_ItemID_TemplateItemID tl WHERE tl.TemplateItemID = i.ID
)
ORDER BY i.BusinessID, i.Name
", {}, { datasource: "payfrit" });
response.verification["orphanCount"] = qOrphans.recordCount;
for (orphan in qOrphans) {
arrayAppend(response.orphans, {
"ItemID": orphan.ItemID,
"Name": orphan.Name,
"BusinessID": orphan.BusinessID
});
}
// Summary
safeToCleanup = (qNoBusinessID.cnt == 0);
response.verification["safeToCleanup"] = safeToCleanup;
if (!safeToCleanup) {
arrayAppend(response.steps, "VERIFICATION FAILED - Cannot cleanup yet");
arrayAppend(response.steps, "- " & qNoBusinessID.cnt & " items still missing BusinessID");
response["OK"] = false;
writeOutput(serializeJSON(response));
abort;
}
if (qOrphans.recordCount > 0) {
arrayAppend(response.steps, "WARNING: " & qOrphans.recordCount & " orphan items found (see orphans array)");
arrayAppend(response.steps, "These will NOT be deleted - review and handle manually if needed");
}
arrayAppend(response.steps, "Verification passed - safe to cleanup");
if (!confirm) {
arrayAppend(response.steps, "Add ?confirm=YES to execute cleanup");
response["OK"] = true;
writeOutput(serializeJSON(response));
abort;
}
// Execute cleanup
arrayAppend(response.steps, "Executing cleanup...");
// Step 1: Drop CategoryID column
try {
queryExecute("
ALTER TABLE Items DROP COLUMN CategoryID
", {}, { datasource: "payfrit" });
arrayAppend(response.steps, "Dropped CategoryID column from Items");
} catch (any e) {
if (findNoCase("check that column", e.message) || findNoCase("Unknown column", e.message)) {
arrayAppend(response.steps, "CategoryID column already dropped");
} else {
arrayAppend(response.steps, "Warning dropping CategoryID: " & e.message);
}
}
// Step 2: Drop IsModifierTemplate column (now derived from lt_ItemID_TemplateItemID)
try {
queryExecute("
ALTER TABLE Items DROP COLUMN IsModifierTemplate
", {}, { datasource: "payfrit" });
arrayAppend(response.steps, "Dropped IsModifierTemplate column from Items");
} catch (any e) {
if (findNoCase("check that column", e.message) || findNoCase("Unknown column", e.message)) {
arrayAppend(response.steps, "IsModifierTemplate column already dropped");
} else {
arrayAppend(response.steps, "Warning dropping IsModifierTemplate: " & e.message);
}
}
// Step 3: Drop Categories table
try {
queryExecute("
DROP TABLE Categories
", {}, { datasource: "payfrit" });
arrayAppend(response.steps, "Dropped Categories table");
} catch (any e) {
if (findNoCase("Unknown table", e.message)) {
arrayAppend(response.steps, "Categories table already dropped");
} else {
arrayAppend(response.steps, "Warning dropping Categories: " & e.message);
}
}
response["OK"] = true;
arrayAppend(response.steps, "CLEANUP COMPLETE - Schema simplified");
} catch (any e) {
response["ERROR"] = e.message;
response["DETAIL"] = e.detail;
}
writeOutput(serializeJSON(response));
</cfscript>