109 lines
2.7 KiB
PHP
109 lines
2.7 KiB
PHP
<?php
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
require_once __DIR__ . '/../config/database.php';
|
|
require_once __DIR__ . '/get_table_name.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => 'Method not allowed'
|
|
], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
$rawBody = file_get_contents('php://input');
|
|
$data = json_decode($rawBody, true);
|
|
if (!is_array($data)) {
|
|
http_response_code(400);
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => 'Invalid JSON payload'
|
|
], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
$tableId = isset($data['tableId']) ? trim((string)$data['tableId']) : '';
|
|
$messageType = isset($data['messageType']) ? trim((string)$data['messageType']) : '';
|
|
$messageText = isset($data['messageText']) ? trim((string)$data['messageText']) : '';
|
|
$qrHash = isset($data['qrHash']) ? trim((string)$data['qrHash']) : '';
|
|
|
|
$allowedTypes = ['waiter_call', 'bill_request'];
|
|
if ($messageType === '' || !in_array($messageType, $allowedTypes, true)) {
|
|
http_response_code(422);
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => 'Invalid messageType'
|
|
], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
if ($tableId === '' && $qrHash !== '' && isset($conn)) {
|
|
$resolved = getTableNameByHash($conn, $qrHash);
|
|
if ($resolved !== '') {
|
|
$tableId = $resolved;
|
|
}
|
|
}
|
|
|
|
if ($tableId === '') {
|
|
http_response_code(422);
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => 'tableId is required'
|
|
], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
if ($messageText === '') {
|
|
http_response_code(422);
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => 'messageText is required'
|
|
], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
if (strlen($tableId) > 32) {
|
|
$tableId = substr($tableId, 0, 32);
|
|
}
|
|
|
|
try {
|
|
$pdo = getAnalyticsPdo();
|
|
$stmt = $pdo->prepare("
|
|
INSERT INTO guest_action_queue (
|
|
table_id,
|
|
message_type,
|
|
message_text,
|
|
api_sent,
|
|
status_kds,
|
|
created_at
|
|
) VALUES (
|
|
:table_id,
|
|
:message_type,
|
|
:message_text,
|
|
0,
|
|
0,
|
|
NOW(3)
|
|
)
|
|
");
|
|
|
|
$stmt->execute([
|
|
':table_id' => $tableId,
|
|
':message_type' => $messageType,
|
|
':message_text' => $messageText,
|
|
]);
|
|
|
|
echo json_encode([
|
|
'status' => 'success',
|
|
'id' => (int)$pdo->lastInsertId()
|
|
], JSON_UNESCAPED_UNICODE);
|
|
} catch (Throwable $e) {
|
|
http_response_code(500);
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => 'Queue insert failed'
|
|
], JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|