65 lines
2.3 KiB
PHP
65 lines
2.3 KiB
PHP
<?php
|
|
// /push/push_endpoint.php
|
|
declare(strict_types=1);
|
|
|
|
function push_queue_notification(
|
|
PDO $pdo,
|
|
string $targetRole,
|
|
?int $targetReferenceId,
|
|
?int $targetCompteId,
|
|
string $type,
|
|
string $title,
|
|
string $body,
|
|
?string $url = null,
|
|
?array $data = null
|
|
): ?int {
|
|
// Sécurité / hygiène
|
|
$title = trim($title);
|
|
$body = trim($body);
|
|
if ($title === '' || $body === '') return null;
|
|
|
|
$dataJson = $data ? json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : null;
|
|
|
|
$stmt = $pdo->prepare("
|
|
INSERT INTO push_notifications
|
|
(target_role, target_reference_id, target_compte_id, type, title, body, url, data_json, status, attempts, created_at)
|
|
VALUES
|
|
(:role, :ref_id, :compte_id, :type, :title, :body, :url, :data_json, 'pending', 0, NOW())
|
|
");
|
|
$stmt->execute([
|
|
':role' => $targetRole,
|
|
':ref_id' => $targetReferenceId,
|
|
':compte_id' => $targetCompteId,
|
|
':type' => $type,
|
|
':title' => mb_substr($title, 0, 120),
|
|
':body' => mb_substr($body, 0, 255),
|
|
':url' => $url ? mb_substr($url, 0, 255) : null,
|
|
':data_json' => $dataJson,
|
|
]);
|
|
|
|
return (int)$pdo->lastInsertId();
|
|
}
|
|
|
|
/**
|
|
* Helpers “RDV” : cible métier par reference_id (client_id / praticien_id)
|
|
*/
|
|
function resolve_compte_id(PDO $pdo, string $role, int $refId): int {
|
|
$st = $pdo->prepare("SELECT id FROM compte WHERE role = ? AND reference_id = ? LIMIT 1");
|
|
$st->execute([$role, $refId]);
|
|
return (int)($st->fetchColumn() ?: 0);
|
|
}
|
|
|
|
function push_rdv_to_client(PDO $pdo, int $clientId, string $title, string $body, string $url, array $data = []): ?int {
|
|
$cid = resolve_compte_id($pdo, 'client', $clientId);
|
|
return push_queue_notification($pdo, 'client', $clientId, ($cid>0?$cid:null), 'rdv', $title, $body, $url, $data);
|
|
}
|
|
|
|
function push_rdv_to_praticien(PDO $pdo, int $praticienId, string $title, string $body, string $url, array $data = []): ?int {
|
|
$cid = resolve_compte_id($pdo, 'praticien', $praticienId);
|
|
return push_queue_notification($pdo, 'praticien', $praticienId, ($cid>0?$cid:null), 'rdv', $title, $body, $url, $data);
|
|
}
|
|
|
|
function push_rdv_to_secretaire(PDO $pdo, int $secretaireCompteId, string $title, string $body, string $url, array $data = []): ?int {
|
|
return push_queue_notification($pdo, 'secretaire', null, $secretaireCompteId, 'rdv', $title, $body, $url, $data);
|
|
}
|