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

63 lines
2.1 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
global $businessId;
$data = readJsonBody();
$bizId = $businessId;
if ($bizId <= 0) $bizId = (int) ($data['BusinessID'] ?? 0);
if ($bizId <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'missing_business_id', 'MESSAGE' => 'BusinessID is required']);
}
$spId = (int) ($data['ServicePointID'] ?? ($_GET['ServicePointID'] ?? 0));
if ($spId <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'missing_servicepoint_id', 'MESSAGE' => 'ServicePointID is required']);
}
try {
$qSP = queryOne("SELECT ID, BusinessID, Name, BeaconMinor FROM ServicePoints WHERE ID = ? LIMIT 1", [$spId]);
if (!$qSP) {
apiAbort(['OK' => false, 'ERROR' => 'invalid_servicepoint', 'MESSAGE' => 'Service point not found']);
}
if ((int) $qSP['BusinessID'] !== $bizId) {
apiAbort(['OK' => false, 'ERROR' => 'access_denied', 'MESSAGE' => 'Service point does not belong to this business']);
}
// Already allocated
if ($qSP['BeaconMinor'] !== null) {
jsonResponse([
'OK' => true,
'ServicePointID' => $spId,
'BusinessID' => $bizId,
'BeaconMinor' => (int) $qSP['BeaconMinor'],
'ServicePointName' => $qSP['Name'],
'AlreadyAllocated' => true,
]);
}
$qMaxMinor = queryOne("
SELECT COALESCE(MAX(BeaconMinor), -1) AS MaxMinor FROM ServicePoints WHERE BusinessID = ?
", [$bizId]);
$nextMinor = (int) $qMaxMinor['MaxMinor'] + 1;
if ($nextMinor > 65535) {
apiAbort(['OK' => false, 'ERROR' => 'business_full', 'MESSAGE' => 'Business has reached maximum service points (65535)']);
}
queryTimed("UPDATE ServicePoints SET BeaconMinor = ? WHERE ID = ? AND BeaconMinor IS NULL",
[$nextMinor, $spId]);
jsonResponse([
'OK' => true,
'ServicePointID' => $spId,
'BusinessID' => $bizId,
'BeaconMinor' => $nextMinor,
'ServicePointName' => $qSP['Name'],
'AlreadyAllocated' => false,
]);
} catch (Exception $e) {
jsonResponse(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => $e->getMessage()]);
}