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; } data = readJsonBody(); httpHeaders = getHttpRequestData().headers; // Get BusinessID bizId = 0; if (structKeyExists(request, "BusinessID") && isNumeric(request.BusinessID) && request.BusinessID GT 0) { bizId = int(request.BusinessID); } if (bizId LTE 0 && structKeyExists(data, "BusinessID") && isNumeric(data.BusinessID) && data.BusinessID GT 0) { bizId = int(data.BusinessID); } if (bizId LTE 0 && structKeyExists(httpHeaders, "X-Business-ID") && isNumeric(httpHeaders["X-Business-ID"]) && httpHeaders["X-Business-ID"] GT 0) { bizId = int(httpHeaders["X-Business-ID"]); } if (bizId LTE 0) { apiAbort({ OK=false, ERROR="missing_business_id", MESSAGE="BusinessID is required" }); } // Get ServicePointID spId = 0; if (structKeyExists(data, "ServicePointID") && isNumeric(data.ServicePointID) && data.ServicePointID GT 0) { spId = int(data.ServicePointID); } if (spId LTE 0) { apiAbort({ OK=false, ERROR="missing_servicepoint_id", MESSAGE="ServicePointID is required" }); } SELECT b.ID, b.Name, b.BeaconShardID, b.BeaconMajor FROM Businesses b WHERE b.ID = LIMIT 1 SELECT ID, UUID, BusinessCount FROM BeaconShards WHERE IsActive = 1 AND BusinessCount < MaxBusinesses ORDER BY BusinessCount ASC LIMIT 1 FOR UPDATE SELECT COALESCE(MAX(BeaconMajor), -1) AS MaxMajor FROM Businesses WHERE BeaconShardID = UPDATE Businesses SET BeaconShardID = , BeaconMajor = WHERE ID = AND (BeaconShardID IS NULL OR BeaconMajor IS NULL) UPDATE BeaconShards SET BusinessCount = BusinessCount + 1 WHERE ID = SELECT UUID FROM BeaconShards WHERE ID = SELECT ID, BusinessID, Name, BeaconMinor FROM ServicePoints WHERE ID = LIMIT 1 SELECT COALESCE(MAX(BeaconMinor), -1) AS MaxMinor FROM ServicePoints WHERE BusinessID = UPDATE ServicePoints SET BeaconMinor = WHERE ID = AND BeaconMinor IS NULL #serializeJSON({ OK = true, UUID = qShardUUID.UUID, Major = beaconMajor, Minor = beaconMinor, MeasuredPower = -100, AdvInterval = 2, TxPower = 1, ServicePointName = qSP.Name, BusinessName = qBiz.Name })# #serializeJSON({ OK=false, ERROR="server_error", MESSAGE=cfcatch.message, DETAIL=cfcatch.detail })#