Files
maria_uslugi/messages/api/get_conversation.php
T
2026-03-05 10:25:39 +01:00

61 lines
1.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// /messages/api/get_conversation.php
declare(strict_types=1);
require_once __DIR__ . '/../_bootstrap.php';
header('Content-Type: application/json; charset=utf-8');
$role = $GLOBALS['MESSAGES_CTX']['role']; // 'praticien' | 'client'
$meId = (int)$GLOBALS['MESSAGES_CTX']['me_id'];
$convId = (int)($_GET['conversation_id'] ?? 0);
if ($convId <= 0) {
json_response(['ok'=>false, 'error'=>'bad_params'], 400);
}
// Vérifie l'accès et récupère la conversation de base
$conv = assert_conversation_access($pdo, $convId, $role, $meId);
// Récupère détails + noms/prénoms + états d’archivage
$sql = "
SELECT
c.id, c.status, c.praticien_id, c.client_id,
c.archived_by_praticien, c.archived_by_client,
p.nom AS p_nom, p.prenom AS p_prenom,
cl.nom AS c_nom, cl.prenom AS c_prenom
FROM conversations c
JOIN praticien p ON p.id = c.praticien_id
JOIN client cl ON cl.id = c.client_id
WHERE c.id = ?
LIMIT 1";
$stmt = $pdo->prepare($sql);
$stmt->execute([$convId]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row) {
json_response(['ok'=>false, 'error'=>'conversation_not_found'], 404);
}
// Nom de l’interlocuteur (peer)
if ($role === 'praticien') {
$peerName = trim(sprintf('%s %s', $row['c_prenom'] ?? '', $row['c_nom'] ?? ''));
$archForMe = (int)$row['archived_by_praticien'];
} else { // client
$peerName = trim(sprintf('%s %s', $row['p_prenom'] ?? '', $row['p_nom'] ?? ''));
$archForMe = (int)$row['archived_by_client'];
}
if ($peerName === '') $peerName = 'Conversation #' . $convId;
// Réponse
json_response([
'ok' => true,
'id' => (int)$row['id'],
'status' => (string)$row['status'], // 'open' | 'closed'
'archived_for_me' => $archForMe, // 0|1
'peer_name' => $peerName,
// (optionnel utile côté UI)
'praticien_id' => (int)$row['praticien_id'],
'client_id' => (int)$row['client_id'],
]);