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 <noreply@anthropic.com>
This commit is contained in:
John Mizerek 2026-01-18 14:42:38 -08:00
parent 31e71068d1
commit 0dc64b7868

View file

@ -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;
}
}
</cfscript>
<cfif beaconId GT 0>
<!--- Update, scoped to this business --->
<!--- Update - app is authoritative, reassign to current business --->
<cfquery datasource="payfrit">
UPDATE Beacons
SET
BeaconBusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">,
BeaconName = <cfqueryparam cfsqltype="cf_sql_varchar" value="#beaconName#">,
BeaconUUID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#uuid#" null="#(len(uuid) EQ 0)#">,
BeaconIsActive = <cfqueryparam cfsqltype="cf_sql_tinyint" value="#isActive#">
WHERE BeaconID = <cfqueryparam cfsqltype="cf_sql_integer" value="#beaconId#">
AND BeaconBusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
</cfquery>
<!--- confirm it exists/belongs to business --->
<cfquery name="qCheck" datasource="payfrit">
SELECT BeaconID
FROM Beacons
<!--- Update associated service point if it exists, also reassign to current business --->
<cfquery name="qLink" datasource="payfrit">
SELECT ServicePointID FROM lt_Beacon_Businesses_ServicePoints
WHERE BeaconID = <cfqueryparam cfsqltype="cf_sql_integer" value="#beaconId#">
AND BeaconBusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
LIMIT 1
</cfquery>
<cfif qCheck.recordCount EQ 0>
<cfoutput>#serializeJSON({ OK=false, ERROR="not_found", MESSAGE="Beacon not found or doesn't belong to this business" })#</cfoutput>
<cfabort>
<cfif qLink.recordCount GT 0>
<cfquery datasource="payfrit">
UPDATE ServicePoints
SET ServicePointName = <cfqueryparam cfsqltype="cf_sql_varchar" value="#beaconName#">,
ServicePointBusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
WHERE ServicePointID = <cfqueryparam cfsqltype="cf_sql_integer" value="#qLink.ServicePointID#">
</cfquery>
<!--- Update lt_ link business as well --->
<cfquery datasource="payfrit">
UPDATE lt_Beacon_Businesses_ServicePoints
SET BusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
WHERE BeaconID = <cfqueryparam cfsqltype="cf_sql_integer" value="#beaconId#">
</cfquery>
<cfelse>
<!--- No service point exists yet, create one --->
<cfquery datasource="payfrit">
INSERT INTO ServicePoints (
ServicePointBusinessID,
ServicePointName,
ServicePointTypeID,
ServicePointIsActive
) VALUES (
<cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#beaconName#">,
1,
1
)
</cfquery>
<cfquery name="qSpId" datasource="payfrit">
SELECT LAST_INSERT_ID() AS ServicePointID
</cfquery>
<cfquery datasource="payfrit">
INSERT INTO lt_Beacon_Businesses_ServicePoints (
BusinessID,
BeaconID,
ServicePointID
) VALUES (
<cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">,
<cfqueryparam cfsqltype="cf_sql_integer" value="#beaconId#">,
<cfqueryparam cfsqltype="cf_sql_integer" value="#qSpId.ServicePointID#">
)
</cfquery>
</cfif>
<cfelse>
<!--- Insert --->
<!--- Insert beacon --->
<cfquery datasource="payfrit">
INSERT INTO Beacons (
BeaconBusinessID,
@ -110,6 +158,39 @@ if (structKeyExists(data, "IsActive")) {
SELECT LAST_INSERT_ID() AS BeaconID
</cfquery>
<cfset beaconId = qId.BeaconID>
<!--- Auto-create service point with same name --->
<cfquery datasource="payfrit">
INSERT INTO ServicePoints (
ServicePointBusinessID,
ServicePointName,
ServicePointTypeID,
ServicePointIsActive
) VALUES (
<cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#beaconName#">,
1,
1
)
</cfquery>
<cfquery name="qSpId" datasource="payfrit">
SELECT LAST_INSERT_ID() AS ServicePointID
</cfquery>
<cfset servicePointId = qSpId.ServicePointID>
<!--- Create the lt_ link --->
<cfquery datasource="payfrit">
INSERT INTO lt_Beacon_Businesses_ServicePoints (
BusinessID,
BeaconID,
ServicePointID
) VALUES (
<cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">,
<cfqueryparam cfsqltype="cf_sql_integer" value="#beaconId#">,
<cfqueryparam cfsqltype="cf_sql_integer" value="#servicePointId#">
)
</cfquery>
</cfif>
<!--- Return saved row --->