From 0dc64b786806d8ebe3953c2edd2e751226eb8f75 Mon Sep 17 00:00:00 2001 From: John Mizerek Date: Sun, 18 Jan 2026 14:42:38 -0800 Subject: [PATCH] Beacon save: auto-create service point and lt_ link, app is authoritative - When saving a beacon, automatically create service point with same name - Create lt_Beacon_Businesses_ServicePoints link record - If UUID already exists, update instead of creating duplicate - App is authoritative: reassigns beacon/servicepoint/lt_ to current business Co-Authored-By: Claude Opus 4.5 --- api/beacons/save.cfm | 105 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 93 insertions(+), 12 deletions(-) diff --git a/api/beacons/save.cfm b/api/beacons/save.cfm index a3c4b74..615f2e5 100644 --- a/api/beacons/save.cfm +++ b/api/beacons/save.cfm @@ -62,36 +62,84 @@ if (structKeyExists(data, "IsActive")) { else if (isNumeric(data.IsActive)) isActive = int(data.IsActive); else if (isSimpleValue(data.IsActive)) isActive = (lcase(trim(toString(data.IsActive))) EQ "true" ? 1 : 0); } + +// App is authoritative: if UUID exists, treat as update (overwrite) +if (beaconId EQ 0 && len(uuid) GT 0) { + qExisting = queryExecute( + "SELECT BeaconID FROM Beacons WHERE BeaconUUID = ? LIMIT 1", + [ { value=uuid, cfsqltype="cf_sql_varchar" } ], + { datasource="payfrit" } + ); + if (qExisting.recordCount GT 0) { + beaconId = qExisting.BeaconID; + } +} - + UPDATE Beacons SET + BeaconBusinessID = , BeaconName = , BeaconUUID = , BeaconIsActive = WHERE BeaconID = - AND BeaconBusinessID = - - - SELECT BeaconID - FROM Beacons + + + SELECT ServicePointID FROM lt_Beacon_Businesses_ServicePoints WHERE BeaconID = - AND BeaconBusinessID = LIMIT 1 - - - #serializeJSON({ OK=false, ERROR="not_found", MESSAGE="Beacon not found or doesn't belong to this business" })# - + + + UPDATE ServicePoints + SET ServicePointName = , + ServicePointBusinessID = + WHERE ServicePointID = + + + + UPDATE lt_Beacon_Businesses_ServicePoints + SET BusinessID = + WHERE BeaconID = + + + + + INSERT INTO ServicePoints ( + ServicePointBusinessID, + ServicePointName, + ServicePointTypeID, + ServicePointIsActive + ) VALUES ( + , + , + 1, + 1 + ) + + + SELECT LAST_INSERT_ID() AS ServicePointID + + + INSERT INTO lt_Beacon_Businesses_ServicePoints ( + BusinessID, + BeaconID, + ServicePointID + ) VALUES ( + , + , + + ) + - + INSERT INTO Beacons ( BeaconBusinessID, @@ -110,6 +158,39 @@ if (structKeyExists(data, "IsActive")) { SELECT LAST_INSERT_ID() AS BeaconID + + + + INSERT INTO ServicePoints ( + ServicePointBusinessID, + ServicePointName, + ServicePointTypeID, + ServicePointIsActive + ) VALUES ( + , + , + 1, + 1 + ) + + + + SELECT LAST_INSERT_ID() AS ServicePointID + + + + + + INSERT INTO lt_Beacon_Businesses_ServicePoints ( + BusinessID, + BeaconID, + ServicePointID + ) VALUES ( + , + , + + ) +