43 lines
1.4 KiB
PHP
43 lines
1.4 KiB
PHP
<?php
|
|
/**
|
|
* Retourne le room_slug pour un RDV.
|
|
* - Si existe en BD -> le retourne
|
|
* - Sinon, ne crée QUE si $allowCreate = true (praticien + start=1)
|
|
* Table attendue: visio_sessions (id, rendez_vous_id UNIQUE, room_slug, created_at)
|
|
*/
|
|
|
|
function visio_room_for_rdv(PDO $pdo, int $rdvId, string $secret, bool $allowCreate): ?string
|
|
{
|
|
// 1) Déjà créée ?
|
|
$sel = $pdo->prepare("SELECT room_slug FROM visio_sessions WHERE rendez_vous_id = ? LIMIT 1");
|
|
$sel->execute([$rdvId]);
|
|
$row = $sel->fetch(PDO::FETCH_ASSOC);
|
|
if ($row && !empty($row['room_slug'])) {
|
|
return $row['room_slug'];
|
|
}
|
|
|
|
if (!$allowCreate) {
|
|
return null; // patient avant ouverture par praticien
|
|
}
|
|
|
|
// 2) Créer un slug non prévisible
|
|
// -> stable pour la séance, mais non devinable
|
|
$raw = $rdvId . '|' . $secret . '|' . random_bytes(8);
|
|
$slug = 'uslugi-' . substr(hash('sha256', $raw), 0, 20);
|
|
|
|
// 3) Insérer (idempotent via UNIQUE(rendez_vous_id))
|
|
$ins = $pdo->prepare("
|
|
INSERT INTO visio_sessions (rendez_vous_id, room_slug)
|
|
VALUES (:r, :s)
|
|
ON DUPLICATE KEY UPDATE room_slug = VALUES(room_slug)
|
|
");
|
|
$ins->execute(['r' => $rdvId, 's' => $slug]);
|
|
|
|
// 4) Relire pour retourner la valeur
|
|
$sel->execute([$rdvId]);
|
|
$row = $sel->fetch(PDO::FETCH_ASSOC);
|
|
|
|
return $row['room_slug'] ?? null;
|
|
}
|
|
|