Przebudowa - code review aplikaacji, podział na moduły.
This commit is contained in:
@@ -47,6 +47,29 @@ function hasPendingGuestAction(PDO $pdo, string $tableId, string $messageType):
|
||||
return (bool) $stmt->fetchColumn();
|
||||
}
|
||||
|
||||
/**
|
||||
* Limit: max 1 nowe zgłoszenie danego typu na stolik w oknie czasowym
|
||||
* (nawet po obsłużeniu poprzedniego w KDS).
|
||||
*/
|
||||
function isGuestActionRateLimited(PDO $pdo, string $tableId, string $messageType, int $windowSeconds = 60): bool
|
||||
{
|
||||
$windowSeconds = max(1, min(600, $windowSeconds));
|
||||
$stmt = $pdo->prepare("
|
||||
SELECT 1
|
||||
FROM guest_action_queue
|
||||
WHERE table_id = :table_id
|
||||
AND message_type = :message_type
|
||||
AND created_at >= (NOW(3) - INTERVAL {$windowSeconds} SECOND)
|
||||
LIMIT 1
|
||||
");
|
||||
$stmt->execute([
|
||||
':table_id' => $tableId,
|
||||
':message_type' => $messageType,
|
||||
]);
|
||||
|
||||
return (bool) $stmt->fetchColumn();
|
||||
}
|
||||
|
||||
function fetchPendingGuestActions(PDO $pdo, string $tableId): array
|
||||
{
|
||||
$stmt = $pdo->prepare("
|
||||
@@ -316,6 +339,18 @@ try {
|
||||
exit;
|
||||
}
|
||||
|
||||
if (isGuestActionRateLimited($pdo, $tableId, $messageType, 60)) {
|
||||
http_response_code(429);
|
||||
echo json_encode([
|
||||
'status' => 'error',
|
||||
'code' => 'rate_limited',
|
||||
'message' => $messageType === 'waiter_call'
|
||||
? 'Wezwanie zostało już niedawno wysłane. Spróbuj ponownie za chwilę.'
|
||||
: 'Prośba o rachunek została już niedawno wysłana. Spróbuj ponownie za chwilę.',
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare("
|
||||
INSERT INTO guest_action_queue (
|
||||
table_id,
|
||||
|
||||
Reference in New Issue
Block a user