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

119 lines
4.7 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
function resolveSingle(string $uuid, int $major, int $minor): array {
$qBiz = queryOne("
SELECT b.ID AS BusinessID, b.Name AS BusinessName
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' => 'business_not_found'];
}
$qSP = queryOne("
SELECT ID, Name, Code, TypeID, Description
FROM ServicePoints
WHERE BusinessID = ? AND BeaconMinor = ? AND IsActive = 1
LIMIT 1
", [(int) $qBiz['BusinessID'], $minor]);
if (!$qSP) {
return [
'Found' => false, 'Error' => 'servicepoint_not_found',
'BusinessID' => (int) $qBiz['BusinessID'],
'BusinessName' => $qBiz['BusinessName'],
];
}
return [
'Found' => true,
'ServicePointID' => (int) $qSP['ID'],
'ServicePointName' => $qSP['Name'],
'ServicePointCode' => $qSP['Code'] ?? '',
'ServicePointTypeID' => (int) $qSP['TypeID'],
'ServicePointDescription' => $qSP['Description'] ?? '',
'BusinessID' => (int) $qBiz['BusinessID'],
'BusinessName' => $qBiz['BusinessName'],
];
}
try {
$data = readJsonBody();
// Batch request
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;
$minor = isset($beacon['Minor']) && is_numeric($beacon['Minor']) ? (int) $beacon['Minor'] : -1;
if ($uuid === '' || $major <= 0 || $minor < 0) {
$results[] = ['UUID' => $uuid, 'Major' => $major, 'Minor' => $minor, 'ServicePointID' => null, 'Error' => 'invalid_params'];
continue;
}
$resolved = resolveSingle($uuid, $major, $minor);
if ($resolved['Found']) {
$results[] = [
'UUID' => $uuid, 'Major' => $major, 'Minor' => $minor,
'ServicePointID' => $resolved['ServicePointID'],
'ServicePointName' => $resolved['ServicePointName'],
'ServicePointCode' => $resolved['ServicePointCode'],
'BusinessID' => $resolved['BusinessID'],
'BusinessName' => $resolved['BusinessName'],
];
} else {
$errResult = ['UUID' => $uuid, 'Major' => $major, 'Minor' => $minor, 'ServicePointID' => null, 'Error' => $resolved['Error']];
if (isset($resolved['BusinessID'])) {
$errResult['BusinessID'] = $resolved['BusinessID'];
$errResult['BusinessName'] = $resolved['BusinessName'];
}
$results[] = $errResult;
}
}
jsonResponse(['OK' => true, 'COUNT' => count($results), 'Results' => $results]);
}
// Single request
$uuid = trim($data['UUID'] ?? ($_GET['UUID'] ?? ''));
$major = (int) ($data['Major'] ?? ($_GET['Major'] ?? 0));
$minor = isset($data['Minor']) ? (int) $data['Minor'] : (isset($_GET['Minor']) ? (int) $_GET['Minor'] : -1);
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']);
if ($minor < 0) apiAbort(['OK' => false, 'ERROR' => 'missing_minor', 'MESSAGE' => 'Minor is required']);
$resolved = resolveSingle($uuid, $major, $minor);
if (!$resolved['Found']) {
$err = ['OK' => false, 'ERROR' => $resolved['Error']];
if (isset($resolved['BusinessID'])) {
$err['BusinessID'] = $resolved['BusinessID'];
$err['BusinessName'] = $resolved['BusinessName'];
$err['MESSAGE'] = 'Service point not found for this business';
} else {
$err['MESSAGE'] = 'No business found for this beacon';
}
apiAbort($err);
}
jsonResponse([
'OK' => true,
'ServicePointID' => $resolved['ServicePointID'],
'ServicePointName' => $resolved['ServicePointName'],
'ServicePointCode' => $resolved['ServicePointCode'],
'ServicePointTypeID' => $resolved['ServicePointTypeID'],
'ServicePointDescription' => $resolved['ServicePointDescription'],
'BusinessID' => $resolved['BusinessID'],
'BusinessName' => $resolved['BusinessName'],
]);
} catch (Exception $e) {
jsonResponse(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => $e->getMessage()]);
}