function apiAbort(obj){ writeOutput(serializeJSON(obj)); abort; } function readJsonBody(){ raw = toString(getHttpRequestData().content); if (isNull(raw) || len(trim(raw)) EQ 0){ apiAbort({OK=false,ERROR="missing_body"}); } try { parsed = deserializeJSON(raw); } catch(any e){ apiAbort({OK=false,ERROR="bad_json",MESSAGE="Invalid JSON body"}); } if (!isStruct(parsed)){ apiAbort({OK=false,ERROR="bad_json",MESSAGE="JSON must be an object"}); } return parsed; } /* ---------- AUTH CONTEXT ---------- */ if (!structKeyExists(request,"BusinessID") || !isNumeric(request.BusinessID) || request.BusinessID LTE 0){ apiAbort({OK=false,ERROR="no_business_selected"}); } /* ---------- INPUT ---------- */ data = readJsonBody(); if (!structKeyExists(data,"BeaconID") || !isNumeric(data.BeaconID) || int(data.BeaconID) LTE 0){ apiAbort({OK=false,ERROR="missing_BeaconID"}); } if (!structKeyExists(data,"ServicePointID") || !isNumeric(data.ServicePointID) || int(data.ServicePointID) LTE 0){ apiAbort({OK=false,ERROR="missing_ServicePointID"}); } BeaconID = int(data.BeaconID); ServicePointID = int(data.ServicePointID); SELECT BusinessID, BusinessParentBusinessID AS ParentBusinessID FROM Businesses WHERE BusinessID = LIMIT 1 SELECT b.ID FROM Beacons b WHERE b.ID = AND ( b.BusinessID = OR b.BusinessID = OR EXISTS ( SELECT 1 FROM lt_BeaconsID_BusinessesID lt WHERE lt.BeaconID = b.ID AND lt.BusinessID = ) ) LIMIT 1 #serializeJSON({OK=false,ERROR="beacon_not_allowed"})# SELECT ServicePointID FROM ServicePoints WHERE ServicePointID = AND ServicePointBusinessID = LIMIT 1 #serializeJSON({OK=false,ERROR="servicepoint_not_found_for_business"})# SELECT ServicePointID FROM ServicePoints WHERE ServicePointID = AND BeaconID = LIMIT 1 #serializeJSON({OK=false,ERROR="assignment_already_exists"})# UPDATE ServicePoints SET BeaconID = , AssignedByUserID = WHERE ServicePointID = AND ServicePointBusinessID = #serializeJSON({ "OK"=true, "ACTION"="assigned", "ServicePointID"=ServicePointID, "BeaconID"=BeaconID, "BusinessID"=(request.BusinessID & "") })#