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

85 lines
2.4 KiB
PHP
Raw Permalink 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/list_conversations.php
declare(strict_types=1);
require_once __DIR__ . '/../_bootstrap.php';
header('Content-Type: application/json; charset=utf-8');
$role = $GLOBALS['MESSAGES_CTX']['role'] ?? '';
$meId = (int)($GLOBALS['MESSAGES_CTX']['me_id'] ?? 0);
// Sécurité: on ne gère ici que praticien/client
if (!in_array($role, ['praticien','client'], true)) {
json_response(['items' => []]);
}
if ($meId <= 0) {
json_response(['items' => []]);
}
// colonnes de visibilité/suppression selon le rôle
$hideCol = ($role === 'praticien') ? 'deleted_for_praticien' : 'deleted_for_client';
$archCol = ($role === 'praticien') ? 'archived_by_praticien' : 'archived_by_client'; // (pas utilisé ici, mais OK)
// Condition WHERE selon le rôle (évite :role répété => HY093)
$where = ($role === 'praticien')
? 'c.praticien_id = :me'
: 'c.client_id = :me';
// Récupère les conversations qui me concernent (non supprimées pour moi)
$sql = "
SELECT
c.id,
c.praticien_id,
c.client_id,
c.status,
cc.p_unread,
cc.c_unread,
-- dernier message (pour l’aperçu)
(SELECT m.body FROM messages m
WHERE m.conversation_id = c.id
AND m.$hideCol = 0
ORDER BY m.id DESC LIMIT 1) AS last_body,
-- jointures pour noms
p.nom AS p_nom, p.prenom AS p_prenom,
cl.nom AS c_nom, cl.prenom AS c_prenom
FROM conversations c
JOIN conversation_counters cc ON cc.conversation_id = c.id
JOIN praticien p ON p.id = c.praticien_id
JOIN client cl ON cl.id = c.client_id
WHERE
$where
AND c.$hideCol = 0
ORDER BY c.id DESC
";
$stmt = $pdo->prepare($sql);
$stmt->execute([':me' => $meId]);
$items = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$id = (int)$row['id'];
// nom affiché = l’autre participant
if ($role === 'praticien') {
$peer = trim(($row['c_prenom'] ?? '').' '.($row['c_nom'] ?? ''));
} else {
$peer = trim(($row['p_prenom'] ?? '').' '.($row['p_nom'] ?? ''));
}
if ($peer === '') $peer = "Conversation #$id"; // fallback
// compteur non lus côté moi
$unread = ($role === 'praticien') ? (int)$row['p_unread'] : (int)$row['c_unread'];
$items[] = [
'id' => $id,
'peer_name' => $peer,
'last_body' => (string)($row['last_body'] ?? ''),
'unread' => $unread,
'status' => (string)$row['status'],
];
}
json_response(['items' => $items]);