payfrit-api/api/beacon-sharding/resolve_business.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

85 lines
3 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
function resolveSingleBiz(string $uuid, int $major): array {
$qBiz = queryOne("
SELECT b.ID, b.Name, b.BrandColor, b.HeaderImageExtension
FROM Businesses b
JOIN BeaconShards bs ON b.BeaconShardID = bs.ID
WHERE bs.UUID = ? AND b.BeaconMajor = ?
LIMIT 1
", [$uuid, $major]);
if (!$qBiz) {
return ['Found' => false, 'Error' => 'not_found'];
}
$headerImageURL = '';
if (!empty($qBiz['HeaderImageExtension'])) {
$headerImageURL = '/uploads/headers/' . $qBiz['ID'] . '.' . $qBiz['HeaderImageExtension'];
}
return [
'Found' => true,
'BusinessID' => (int) $qBiz['ID'],
'BusinessName' => $qBiz['Name'],
'BrandColor' => $qBiz['BrandColor'] ?? '',
'HeaderImageURL' => $headerImageURL,
];
}
try {
$data = readJsonBody();
// Batch
if (isset($data['Beacons']) && is_array($data['Beacons'])) {
$results = [];
foreach ($data['Beacons'] as $beacon) {
$uuid = trim($beacon['UUID'] ?? '');
$major = isset($beacon['Major']) && is_numeric($beacon['Major']) ? (int) $beacon['Major'] : 0;
if ($uuid === '' || $major <= 0) {
$results[] = ['UUID' => $uuid, 'Major' => $major, 'BusinessID' => null, 'Error' => 'invalid_params'];
continue;
}
$resolved = resolveSingleBiz($uuid, $major);
if ($resolved['Found']) {
$results[] = [
'UUID' => $uuid, 'Major' => $major,
'BusinessID' => $resolved['BusinessID'],
'BusinessName' => $resolved['BusinessName'],
'BrandColor' => $resolved['BrandColor'],
'HeaderImageURL' => $resolved['HeaderImageURL'],
];
} else {
$results[] = ['UUID' => $uuid, 'Major' => $major, 'BusinessID' => null, 'Error' => 'not_found'];
}
}
jsonResponse(['OK' => true, 'COUNT' => count($results), 'Results' => $results]);
}
// Single
$uuid = trim($data['UUID'] ?? ($_GET['UUID'] ?? ''));
$major = (int) ($data['Major'] ?? ($_GET['Major'] ?? 0));
if ($uuid === '') apiAbort(['OK' => false, 'ERROR' => 'missing_uuid', 'MESSAGE' => 'UUID is required']);
if ($major <= 0) apiAbort(['OK' => false, 'ERROR' => 'missing_major', 'MESSAGE' => 'Major is required']);
$resolved = resolveSingleBiz($uuid, $major);
if (!$resolved['Found']) {
apiAbort(['OK' => false, 'ERROR' => 'not_found', 'MESSAGE' => 'No business found for this beacon']);
}
jsonResponse([
'OK' => true,
'BusinessID' => $resolved['BusinessID'],
'BusinessName' => $resolved['BusinessName'],
'BrandColor' => $resolved['BrandColor'],
'HeaderImageURL' => $resolved['HeaderImageURL'],
]);
} catch (Exception $e) {
jsonResponse(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => $e->getMessage()]);
}