function apiAbort(obj) { writeOutput(serializeJSON(obj)); abort; } function readJsonBody() { raw = toString(getHttpRequestData().content); if (isNull(raw) || len(trim(raw)) EQ 0) return {}; try { parsed = deserializeJSON(raw); } catch(any e) { apiAbort({ OK=false, ERROR="bad_json", MESSAGE="Invalid JSON body" }); } if (!isStruct(parsed)) return {}; return parsed; } function normStr(v) { if (isNull(v)) return ""; return trim(toString(v)); } data = readJsonBody(); if (!structKeyExists(request, "BusinessID") || !isNumeric(request.BusinessID) || request.BusinessID LTE 0) { apiAbort({ OK=false, ERROR="no_business_selected" }); } // Verify the business exists qBiz = queryExecute( "SELECT BusinessID FROM Businesses WHERE BusinessID = ? LIMIT 1", [ { value=request.BusinessID, cfsqltype="cf_sql_integer" } ], { datasource="payfrit" } ); if (qBiz.recordCount EQ 0) { apiAbort({ OK=false, ERROR="invalid_business", MESSAGE="Business ID #request.BusinessID# does not exist. Please log out and log back in." }); } if (!structKeyExists(data, "BeaconName") || len(normStr(data.BeaconName)) EQ 0) { apiAbort({ OK=false, ERROR="missing_beacon_name", MESSAGE="BeaconName is required" }); } beaconId = 0; if (structKeyExists(data, "BeaconID") && isNumeric(data.BeaconID) && int(data.BeaconID) GT 0) { beaconId = int(data.BeaconID); } beaconName = normStr(data.BeaconName); uuid = structKeyExists(data, "UUID") ? normStr(data.UUID) : ""; isActive = 1; if (structKeyExists(data, "IsActive")) { if (isBoolean(data.IsActive)) isActive = (data.IsActive ? 1 : 0); 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 = SELECT ServicePointID FROM lt_Beacon_Businesses_ServicePoints WHERE BeaconID = LIMIT 1 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, BeaconName, BeaconUUID, BeaconIsActive ) VALUES ( , , , ) 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 ( , , ) SELECT BeaconID, BeaconBusinessID, BeaconName, BeaconUUID, BeaconIsActive FROM Beacons WHERE BeaconID = AND BeaconBusinessID = LIMIT 1 #serializeJSON({ OK=true, ERROR="", BEACON=beacon })# #serializeJSON({ OK=false, ERROR="server_error", MESSAGE=cfcatch.message, DETAIL=cfcatch.detail })#