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 from: session > body > X-Business-ID header > URL 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 && structKeyExists(url, "BusinessID") && isNumeric(url.BusinessID) && url.BusinessID GT 0) { bizId = int(url.BusinessID); } 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 && structKeyExists(url, "ServicePointID") && isNumeric(url.ServicePointID) && url.ServicePointID GT 0) { spId = int(url.ServicePointID); } if (spId LTE 0) { apiAbort({ OK=false, ERROR="missing_servicepoint_id", MESSAGE="ServicePointID is required" }); } SELECT ID, BusinessID, Name, BeaconMinor FROM ServicePoints WHERE ID = LIMIT 1 #serializeJSON({ OK = true, ServicePointID = spId, BusinessID = bizId, BeaconMinor = qSP.BeaconMinor, ServicePointName = qSP.Name, AlreadyAllocated = true })# SELECT COALESCE(MAX(BeaconMinor), 0) AS MaxMinor FROM ServicePoints WHERE BusinessID = UPDATE ServicePoints SET BeaconMinor = WHERE ID = AND BeaconMinor IS NULL #serializeJSON({ OK = true, ServicePointID = spId, BusinessID = bizId, BeaconMinor = nextMinor, ServicePointName = qSP.Name, AlreadyAllocated = false })# #serializeJSON({ OK=false, ERROR="server_error", MESSAGE=cfcatch.message, DETAIL=cfcatch.detail })#