Files
bot_palmas/services/PendingApproval.php
2026-06-12 12:18:21 -05:00

114 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
class PendingApproval
{
public static function create(int $companyId, string $phone, ?string $name, string $incomingMsg, ?array $botResponse, string $botType, array $context): array
{
$stmt = db()->prepare("
INSERT INTO pending_approval (company_id, phone_number, contact_name, incoming_msg, bot_response, bot_type, context_json)
VALUES (?, ?, ?, ?, ?, ?, ?)
");
$stmt->execute([
$companyId,
$phone,
$name,
$incomingMsg,
$botResponse !== null ? json_encode($botResponse) : null,
$botType,
json_encode($context),
]);
return [
'id' => (int)db()->lastInsertId(),
'status' => 'pending',
];
}
public static function approve(int $id, ?string $reviewedBy = null, ?string $note = null): ?array
{
$stmt = db()->prepare("SELECT * FROM pending_approval WHERE id = ? AND status = 'pending' LIMIT 1");
$stmt->execute([$id]);
$item = $stmt->fetch();
if (!$item) {
return null;
}
$stmt = db()->prepare("UPDATE pending_approval SET status = 'approved', reviewed_by = ?, review_note = ?, reviewed_at = NOW() WHERE id = ?");
$stmt->execute([$reviewedBy, $note, $id]);
$botResponse = $item['bot_response'] ? json_decode($item['bot_response'], true) : null;
if ($botResponse !== null) {
$company = CompanyRepository::findById((int)$item['company_id']);
if ($company !== null) {
$stmt = db()->prepare("INSERT INTO outbound_queue (company_id, to_number, message_type, payload) VALUES (?, ?, ?, ?)");
$stmt->execute([
$company['id'],
$item['phone_number'],
$botResponse['type'] ?? 'text',
$botResponse['payload'] ?? json_encode(['text' => '']),
]);
}
}
return $item;
}
public static function reject(int $id, ?string $reviewedBy = null, ?string $note = null): ?array
{
$stmt = db()->prepare("SELECT * FROM pending_approval WHERE id = ? AND status = 'pending' LIMIT 1");
$stmt->execute([$id]);
$item = $stmt->fetch();
if (!$item) {
return null;
}
$stmt = db()->prepare("UPDATE pending_approval SET status = 'rejected', reviewed_by = ?, review_note = ?, reviewed_at = NOW() WHERE id = ?");
$stmt->execute([$reviewedBy, $note, $id]);
return $item;
}
public static function findByCompany(int $companyId, string $status = 'pending', int $limit = 50): array
{
$stmt = db()->prepare("
SELECT pa.*, c.name AS company_name, c.display_name AS company_display
FROM pending_approval pa
JOIN companies c ON c.id = pa.company_id
WHERE pa.company_id = ? AND pa.status = ?
ORDER BY pa.created_at DESC
LIMIT ?
");
$stmt->execute([$companyId, $status, $limit]);
return $stmt->fetchAll();
}
public static function findAll(string $status = 'pending', int $limit = 50): array
{
$stmt = db()->prepare("
SELECT pa.*, c.name AS company_name, c.display_name AS company_display
FROM pending_approval pa
JOIN companies c ON c.id = pa.company_id
WHERE pa.status = ?
ORDER BY pa.created_at DESC
LIMIT ?
");
$stmt->execute([$status, $limit]);
return $stmt->fetchAll();
}
public static function countPending(?int $companyId = null): int
{
if ($companyId !== null) {
$stmt = db()->prepare("SELECT COUNT(*) FROM pending_approval WHERE company_id = ? AND status = 'pending'");
$stmt->execute([$companyId]);
} else {
$stmt = db()->query("SELECT COUNT(*) FROM pending_approval WHERE status = 'pending'");
}
return (int)$stmt->fetchColumn();
}
}