/** * Setup Lazy Daisy Beacons * Creates a beacon for each service point and links them */ response = { "OK": false, "steps": [] }; try { lazyDaisyID = 37; // Get all service points for Lazy Daisy qServicePoints = queryExecute(" SELECT ServicePointID, ServicePointName FROM ServicePoints WHERE ServicePointBusinessID = :bizID AND ServicePointIsActive = 1 ORDER BY ServicePointID ", { bizID: lazyDaisyID }, { datasource: "payfrit" }); response.steps.append("Found " & qServicePoints.recordCount & " service points for Lazy Daisy"); // Create a beacon for each service point beaconsCreated = 0; for (sp in qServicePoints) { beaconName = "Beacon - " & sp.ServicePointName; // Check if beacon already exists for this business with this name qExisting = queryExecute(" SELECT BeaconID FROM Beacons WHERE BeaconBusinessID = :bizId AND BeaconName = :name ", { bizId: lazyDaisyID, name: beaconName }, { datasource: "payfrit" }); if (qExisting.recordCount == 0) { // Generate a unique UUID for this beacon (32 hex chars, no dashes) beaconUUID = "PAYFRIT00037" & numberFormat(sp.ServicePointID, "0000000000000000000"); queryExecute(" INSERT INTO Beacons (BeaconBusinessID, BeaconName, BeaconUUID, BeaconIsActive) VALUES (:bizId, :name, :uuid, 1) ", { bizId: lazyDaisyID, name: beaconName, uuid: beaconUUID }, { datasource: "payfrit" }); qNewBeacon = queryExecute("SELECT LAST_INSERT_ID() as id", {}, { datasource: "payfrit" }); newBeaconId = qNewBeacon.id; // Create assignment to service point queryExecute(" INSERT INTO lt_Beacon_Businesses_ServicePoints (BeaconID, BusinessID, ServicePointID, lt_Beacon_Businesses_ServicePointAssignedByUserID) VALUES (:beaconId, :bizId, :spId, 1) ", { beaconId: newBeaconId, bizId: lazyDaisyID, spId: sp.ServicePointID }, { datasource: "payfrit" }); response.steps.append("Created beacon '" & beaconName & "' (ID: " & newBeaconId & ") -> " & sp.ServicePointName); beaconsCreated++; } else { response.steps.append("Beacon '" & beaconName & "' already exists, skipping"); } } // Get final status qFinal = queryExecute(" SELECT lt.BeaconID, b.BeaconUUID, b.BeaconName, 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 WHERE lt.BusinessID = :bizId ORDER BY sp.ServicePointName ", { bizId: lazyDaisyID }, { datasource: "payfrit" }); beacons = []; for (i = 1; i <= qFinal.recordCount; i++) { arrayAppend(beacons, { "BeaconID": qFinal.BeaconID[i], "BeaconName": qFinal.BeaconName[i], "UUID": qFinal.BeaconUUID[i], "BusinessName": qFinal.BusinessName[i], "ServicePointName": qFinal.ServicePointName[i] }); } response.OK = true; response.beaconsCreated = beaconsCreated; response.beacons = beacons; } catch (any e) { response.error = e.message; if (len(e.detail)) { response.detail = e.detail; } } writeOutput(serializeJSON(response));