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(); // Required fields hardwareId = normStr(structKeyExists(data, "HardwareId") ? data.HardwareId : ""); if (len(hardwareId) EQ 0) { apiAbort({ OK=false, ERROR="missing_hardware_id", MESSAGE="HardwareId is required" }); } bizId = 0; if (structKeyExists(data, "BusinessID") && isNumeric(data.BusinessID)) { bizId = int(data.BusinessID); } if (bizId LTE 0) { apiAbort({ OK=false, ERROR="missing_business_id", MESSAGE="BusinessID is required" }); } spId = 0; if (structKeyExists(data, "ServicePointID") && isNumeric(data.ServicePointID)) { spId = int(data.ServicePointID); } if (spId LTE 0) { apiAbort({ OK=false, ERROR="missing_servicepoint_id", MESSAGE="ServicePointID is required" }); } beaconUUID = normStr(structKeyExists(data, "UUID") ? data.UUID : ""); if (len(beaconUUID) EQ 0) { apiAbort({ OK=false, ERROR="missing_uuid", MESSAGE="UUID is required" }); } major = 0; if (structKeyExists(data, "Major") && isNumeric(data.Major)) { major = int(data.Major); } minor = 0; if (structKeyExists(data, "Minor") && isNumeric(data.Minor)) { minor = int(data.Minor); } // Optional fields txPower = ""; if (structKeyExists(data, "TxPower") && isNumeric(data.TxPower)) { txPower = int(data.TxPower); } advInterval = ""; if (structKeyExists(data, "AdvertisingInterval") && isNumeric(data.AdvertisingInterval)) { advInterval = int(data.AdvertisingInterval); } firmwareVersion = normStr(structKeyExists(data, "FirmwareVersion") ? data.FirmwareVersion : ""); SELECT b.ID, b.BeaconShardID, b.BeaconMajor, bs.UUID AS ShardUUID FROM Businesses b LEFT JOIN BeaconShards bs ON b.BeaconShardID = bs.ID WHERE b.ID = LIMIT 1 SELECT ID, BusinessID, BeaconMinor, Name FROM ServicePoints WHERE ID = LIMIT 1 UPDATE ServicePoints SET BeaconMinor = WHERE ID = SELECT ID, Status FROM BeaconHardware WHERE HardwareId = LIMIT 1 UPDATE BeaconHardware SET BusinessID = , ServicePointID = , ShardUUID = , Major = , Minor = , Status = 'assigned', TxPower = , AdvertisingInterval = , FirmwareVersion = , UpdatedAt = NOW() WHERE HardwareId = INSERT INTO BeaconHardware ( HardwareId, BusinessID, ServicePointID, ShardUUID, Major, Minor, Status ,TxPower ,AdvertisingInterval ,FirmwareVersion ) VALUES ( , , , , , , 'assigned' , , , ) SELECT LAST_INSERT_ID() AS ID #serializeJSON({ OK = true, BeaconHardwareID = hwId, HardwareId = hardwareId, BusinessID = bizId, ServicePointID = spId, ServicePointName = qSP.Name, UUID = beaconUUID, Major = major, Minor = minor, Status = "assigned" })# #serializeJSON({ OK=false, ERROR="server_error", MESSAGE=cfcatch.message, DETAIL=cfcatch.detail })#