feat: complete jobcard management workflows and UI
This commit is contained in:
@@ -23,8 +23,12 @@ final class NotificationQueue
|
||||
$lookup = $pdo->prepare('SELECT id FROM users WHERE email = :email AND is_active = 1 LIMIT 1');
|
||||
$insert = $pdo->prepare('INSERT INTO notifications (user_id, type, title, body, deduplication_key, read_at) VALUES (:user, :type, :title, :body, :dedup, :read_at) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id)');
|
||||
$ids = [];
|
||||
$startedTransaction = false;
|
||||
try {
|
||||
$pdo->beginTransaction();
|
||||
if (!$pdo->inTransaction()) {
|
||||
$pdo->beginTransaction();
|
||||
$startedTransaction = true;
|
||||
}
|
||||
foreach ($validation['recipients'] as $email) {
|
||||
$dto = ($this->records ?? new NotificationRecord())->toQueueDto($record, $email);
|
||||
$lookup->execute(['email' => $email]);
|
||||
@@ -34,9 +38,9 @@ final class NotificationQueue
|
||||
$id = (int)$pdo->lastInsertId();
|
||||
if ($id > 0 && !in_array($id, $ids, true)) $ids[] = $id;
|
||||
}
|
||||
$pdo->commit();
|
||||
if ($startedTransaction) $pdo->commit();
|
||||
} catch (\Throwable $exception) {
|
||||
if ($pdo->inTransaction()) $pdo->rollBack();
|
||||
if ($startedTransaction && $pdo->inTransaction()) $pdo->rollBack();
|
||||
throw $exception;
|
||||
}
|
||||
return $ids;
|
||||
@@ -58,4 +62,15 @@ final class NotificationQueue
|
||||
$stmt->execute(['id' => $validation['notification_id'], 'user' => $user]);
|
||||
return $stmt->rowCount() > 0;
|
||||
}
|
||||
|
||||
/** Mark a notification unread only for the authenticated user's row. */
|
||||
public function markUnread(PDO $pdo, int|string $userId, array $command): bool
|
||||
{
|
||||
$user = filter_var($userId, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
|
||||
$validation = ($this->records ?? new NotificationRecord())->validateMarkUnread($command);
|
||||
if ($user === false || !$validation['valid']) throw new InvalidArgumentException('Invalid mark-unread command.');
|
||||
$stmt = $pdo->prepare('UPDATE notifications SET read_at = NULL WHERE id = :id AND user_id = :user');
|
||||
$stmt->execute(['id' => $validation['notification_id'], 'user' => $user]);
|
||||
return $stmt->rowCount() > 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user