response = { "OK": false, "steps": [] }; try { lazyDaisyID = 37; // Get all beacons qBeacons = queryExecute("SELECT BeaconID, BeaconUUID FROM Beacons", {}, { datasource: "payfrit" }); response.steps.append("Found " & qBeacons.recordCount & " beacons"); // Create service point for Table 1 if it doesn't exist qSP = queryExecute(" SELECT ServicePointID FROM ServicePoints WHERE ServicePointBusinessID = :bizID AND ServicePointName = 'Table 1' ", { bizID: lazyDaisyID }, { datasource: "payfrit" }); if (qSP.recordCount == 0) { queryExecute(" INSERT INTO ServicePoints (ServicePointBusinessID, ServicePointName, ServicePointTypeID) VALUES (:bizID, 'Table 1', 1) ", { bizID: lazyDaisyID }, { datasource: "payfrit" }); qSP = queryExecute("SELECT LAST_INSERT_ID() as id", {}, { datasource: "payfrit" }); servicePointID = qSP.id; response.steps.append("Created service point 'Table 1' (ID: " & servicePointID & ")"); } else { servicePointID = qSP.ServicePointID; response.steps.append("Found existing service point 'Table 1' (ID: " & servicePointID & ")"); } // Map all beacons to Lazy Daisy with Table 1 for (i = 1; i <= qBeacons.recordCount; i++) { beaconID = qBeacons.BeaconID[i]; // Check if mapping exists qMap = queryExecute(" SELECT * FROM lt_Beacon_Businesses_ServicePoints WHERE BeaconID = :beaconID ", { beaconID: beaconID }, { datasource: "payfrit" }); if (qMap.recordCount == 0) { queryExecute(" INSERT INTO lt_Beacon_Businesses_ServicePoints (BeaconID, BusinessID, ServicePointID) VALUES (:beaconID, :bizID, :spID) ", { beaconID: beaconID, bizID: lazyDaisyID, spID: servicePointID }, { datasource: "payfrit" }); response.steps.append("Created mapping for beacon " & beaconID); } else { queryExecute(" UPDATE lt_Beacon_Businesses_ServicePoints SET BusinessID = :bizID, ServicePointID = :spID WHERE BeaconID = :beaconID ", { beaconID: beaconID, bizID: lazyDaisyID, spID: servicePointID }, { datasource: "payfrit" }); response.steps.append("Updated mapping for beacon " & beaconID); } } // Get final status qFinal = queryExecute(" SELECT lt.BeaconID, b.BeaconUUID, lt.BusinessID, biz.BusinessName, lt.ServicePointID, sp.ServicePointName FROM lt_Beacon_Businesses_ServicePoints lt JOIN Beacons b ON b.BeaconID = lt.BeaconID JOIN Businesses biz ON biz.BusinessID = lt.BusinessID LEFT JOIN ServicePoints sp ON sp.ServicePointID = lt.ServicePointID ", {}, { datasource: "payfrit" }); beacons = []; for (i = 1; i <= qFinal.recordCount; i++) { arrayAppend(beacons, { "BeaconID": qFinal.BeaconID[i], "UUID": qFinal.BeaconUUID[i], "BusinessID": qFinal.BusinessID[i], "BusinessName": qFinal.BusinessName[i], "ServicePointID": qFinal.ServicePointID[i], "ServicePointName": qFinal.ServicePointName[i] }); } response.OK = true; response.beacons = beacons; } catch (any e) { response.error = e.message; if (len(e.detail)) { response.detail = e.detail; } } writeOutput(serializeJSON(response));