payfrit-api/api/beacons/lookup.php
John Mizerek 1f81d98c52 Initial PHP API migration from CFML
Complete port of all 163 API endpoints from Lucee/CFML to PHP 8.3.
Shared helpers in api/helpers.php (DB, auth, request/response, security).
PDO prepared statements throughout. Same JSON response shapes as CFML.
2026-03-14 14:26:59 -07:00

75 lines
2.8 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
try {
$data = readJsonBody();
if (!isset($data['Beacons']) || !is_array($data['Beacons']) || count($data['Beacons']) === 0) {
jsonResponse(['OK' => true, 'ERROR' => '', 'BEACONS' => []]);
}
$beacons = [];
foreach ($data['Beacons'] as $beaconData) {
$uuid = '';
$major = 0;
$minor = -1;
if (!empty($beaconData['UUID'])) {
$rawUuid = strtoupper(str_replace('-', '', $beaconData['UUID']));
if (strlen($rawUuid) === 32) {
$uuid = strtolower(
substr($rawUuid, 0, 8) . '-' . substr($rawUuid, 8, 4) . '-' .
substr($rawUuid, 12, 4) . '-' . substr($rawUuid, 16, 4) . '-' .
substr($rawUuid, 20, 12)
);
}
}
if (isset($beaconData['Major']) && is_numeric($beaconData['Major'])) {
$major = (int) $beaconData['Major'];
}
if (isset($beaconData['Minor']) && is_numeric($beaconData['Minor'])) {
$minor = (int) $beaconData['Minor'];
}
if ($uuid === '' || $major < 0 || $minor < 0) continue;
$qShard = queryOne("
SELECT
biz.ID AS BusinessID, biz.Name AS BusinessName,
biz.ParentBusinessID,
COALESCE(parent.Name, '') AS ParentBusinessName,
sp.ID AS ServicePointID, sp.Name AS ServicePointName,
(SELECT COUNT(*) FROM Businesses WHERE ParentBusinessID = biz.ID) AS ChildCount
FROM BeaconShards bs
JOIN Businesses biz ON biz.BeaconShardID = bs.ID AND biz.BeaconMajor = ?
LEFT JOIN ServicePoints sp ON sp.BusinessID = biz.ID AND sp.BeaconMinor = ? AND sp.IsActive = 1
LEFT JOIN Businesses parent ON biz.ParentBusinessID = parent.ID
WHERE bs.UUID = ? AND bs.IsActive = 1
AND biz.IsDemo = 0 AND biz.IsPrivate = 0
LIMIT 1
", [$major, $minor, $uuid]);
if ($qShard) {
$beacons[] = [
'UUID' => strtoupper(str_replace('-', '', $uuid)),
'Major' => $major,
'Minor' => $minor,
'BusinessID' => (int) $qShard['BusinessID'],
'BusinessName' => $qShard['BusinessName'],
'ServicePointID' => (int) ($qShard['ServicePointID'] ?? 0),
'ServicePointName' => $qShard['ServicePointName'] ?? '',
'ParentBusinessID' => (int) ($qShard['ParentBusinessID'] ?? 0),
'ParentBusinessName' => $qShard['ParentBusinessName'],
'HasChildren' => (int) $qShard['ChildCount'] > 0,
];
}
}
jsonResponse(['OK' => true, 'ERROR' => '', 'BEACONS' => $beacons]);
} catch (Exception $e) {
jsonResponse(['OK' => false, 'ERROR' => $e->getMessage()]);
}