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/beacons/lookup.cfm
John Mizerek be29352b21 Migrate beacon APIs to shard-only system
- save.cfm: Auto-allocates shard+Major to business, creates ServicePoint with Minor
- list.cfm: Lists ServicePoints with BeaconMinor (removed legacy Beacons table)
- list_all.cfm: Returns shard UUIDs instead of legacy beacon UUIDs
- lookup.cfm: Removed legacy UUID lookup, shard-only resolution

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-14 19:33:49 -08:00

98 lines
3.3 KiB
Text

<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/json; charset=utf-8" reset="true">
<cftry>
<cfset requestData = deserializeJSON(toString(getHttpRequestData().content))>
<!--- Beacon lookup via sharding: { "Beacons": [{ "UUID": "...", "Major": 49, "Minor": 15 }] }
Resolves via BeaconShards -> Businesses (BeaconMajor) -> ServicePoints (BeaconMinor) --->
<cfif NOT structKeyExists(requestData, "Beacons") OR NOT isArray(requestData.Beacons) OR arrayLen(requestData.Beacons) EQ 0>
<cfoutput>#serializeJSON({
"OK" = true,
"ERROR" = "",
"BEACONS" = []
})#</cfoutput>
<cfabort>
</cfif>
<cfset beacons = []>
<cfloop array="#requestData.Beacons#" index="beaconData">
<cfset uuid = "">
<cfset major = 0>
<cfset minor = -1>
<!--- Extract UUID (normalize: remove dashes, add back in standard format) --->
<cfif structKeyExists(beaconData, "UUID")>
<cfset rawUuid = uCase(reReplace(beaconData.UUID, "-", "", "all"))>
<cfif len(rawUuid) EQ 32>
<!--- Convert to standard UUID format with dashes for DB lookup --->
<cfset uuid = lCase(mid(rawUuid,1,8) & "-" & mid(rawUuid,9,4) & "-" & mid(rawUuid,13,4) & "-" & mid(rawUuid,17,4) & "-" & mid(rawUuid,21,12))>
</cfif>
</cfif>
<cfif structKeyExists(beaconData, "Major") AND isNumeric(beaconData.Major)>
<cfset major = int(beaconData.Major)>
</cfif>
<cfif structKeyExists(beaconData, "Minor") AND isNumeric(beaconData.Minor)>
<cfset minor = int(beaconData.Minor)>
</cfif>
<cfif len(uuid) EQ 0 OR major LT 0 OR minor LT 0>
<cfcontinue>
</cfif>
<!--- Resolve via sharding: UUID -> Shard -> Business (Major) -> ServicePoint (Minor) --->
<cfquery name="qShard" datasource="payfrit">
SELECT
biz.ID AS BusinessID,
biz.Name AS BusinessName,
biz.ParentBusinessID,
COALESCE(parent.Name, '') AS ParentBusinessName,
sp.ID AS ServicePointID,
sp.Name AS ServicePointName,
(SELECT COUNT(*) FROM Businesses WHERE ParentBusinessID = biz.ID) AS ChildCount
FROM BeaconShards bs
JOIN Businesses biz ON biz.BeaconShardID = bs.ID AND biz.BeaconMajor = <cfqueryparam cfsqltype="cf_sql_smallint" value="#major#">
LEFT JOIN ServicePoints sp ON sp.BusinessID = biz.ID AND sp.BeaconMinor = <cfqueryparam cfsqltype="cf_sql_smallint" value="#minor#"> AND sp.IsActive = 1
LEFT JOIN Businesses parent ON biz.ParentBusinessID = parent.ID
WHERE bs.UUID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#uuid#">
AND bs.IsActive = 1
AND biz.IsDemo = 0
AND biz.IsPrivate = 0
LIMIT 1
</cfquery>
<cfif qShard.recordCount GT 0>
<cfset arrayAppend(beacons, {
"UUID" = uCase(reReplace(uuid, "-", "", "all")),
"Major" = major,
"Minor" = minor,
"BusinessID" = qShard.BusinessID,
"BusinessName" = qShard.BusinessName,
"ServicePointID" = val(qShard.ServicePointID),
"ServicePointName" = qShard.ServicePointName,
"ParentBusinessID" = val(qShard.ParentBusinessID),
"ParentBusinessName" = qShard.ParentBusinessName,
"HasChildren" = qShard.ChildCount GT 0
})>
</cfif>
</cfloop>
<cfoutput>#serializeJSON({
"OK" = true,
"ERROR" = "",
"BEACONS" = beacons
})#</cfoutput>
<cfcatch type="any">
<cfoutput>#serializeJSON({
"OK" = false,
"ERROR" = cfcatch.message
})#</cfoutput>
</cfcatch>
</cftry>