Add notifications: header bell, task start run and assignment notices
A new Notifications module shows a bell beside the profile menu with the number of unseen notifications and a panel of the last 7 days; opening it marks them seen, and each notification links to its job or task. - A daily run at 14.00 (notifications:task-starts) tells the assignee and members of tasks that start by the next working day, skipping holidays and days off, done tasks and archived jobs. - Jobs publishes PeopleAddedToTask and PeopleAddedToJob when someone else makes a user a task's assignee or member or gives them a role on a job; those users are notified at once. UpdateTask and ScheduleTask now take the acting user. - A notification link to #task-<id> opens that task card on the job page. Also: simplifying job fields per service unit (K31) moves to the end of phase 4, and the workflow template browser test waits for Livewire's progress bar before its last accessibility check. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Notifications\Application\Contracts;
|
||||
|
||||
use App\Modules\Notifications\Application\DTOs\NotificationSummary;
|
||||
use DateTimeImmutable;
|
||||
|
||||
interface NotificationQueries
|
||||
{
|
||||
/**
|
||||
* The user's notifications made since the moment, newest first.
|
||||
*
|
||||
* @return list<NotificationSummary>
|
||||
*/
|
||||
public function recentFor(int $userId, DateTimeImmutable $since): array;
|
||||
|
||||
public function unseenCount(int $userId, DateTimeImmutable $since): int;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Notifications\Application\DTOs;
|
||||
|
||||
use App\Modules\Notifications\Domain\Enums\NotificationType;
|
||||
|
||||
/**
|
||||
* A notification for the bell. The time is in the business time zone as
|
||||
* "Y-m-d H:i".
|
||||
*/
|
||||
final readonly class NotificationSummary
|
||||
{
|
||||
/**
|
||||
* @param array<string, int|string> $params
|
||||
*/
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public NotificationType $type,
|
||||
public array $params,
|
||||
public string $createdAt,
|
||||
public bool $seen,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Notifications\Application\Services;
|
||||
|
||||
use App\Modules\Shared\Application\UseCases\HolidayCalendar;
|
||||
use DateTimeImmutable;
|
||||
|
||||
/**
|
||||
* Working days: Monday to Friday except holidays in use and the company's
|
||||
* days off.
|
||||
*/
|
||||
final readonly class WorkingDays
|
||||
{
|
||||
public function __construct(private HolidayCalendar $holidays) {}
|
||||
|
||||
public function isWorkingDay(DateTimeImmutable $day): bool
|
||||
{
|
||||
$date = $day->format('Y-m-d');
|
||||
|
||||
return (int) $day->format('N') <= 5 && $this->holidays->activeDatesBetween($date, $date) === [];
|
||||
}
|
||||
|
||||
/**
|
||||
* The first working day after the given day.
|
||||
*/
|
||||
public function nextAfter(DateTimeImmutable $day): DateTimeImmutable
|
||||
{
|
||||
$next = $day->modify('+1 day');
|
||||
|
||||
// A month of days off in a row would be a mistake in the settings.
|
||||
for ($i = 0; $i < 31 && ! $this->isWorkingDay($next); $i++) {
|
||||
$next = $next->modify('+1 day');
|
||||
}
|
||||
|
||||
return $next;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Notifications\Application\UseCases;
|
||||
|
||||
use App\Modules\Notifications\Domain\Contracts\NotificationRepository;
|
||||
use App\Modules\Users\Application\Contracts\Clock;
|
||||
|
||||
/**
|
||||
* Opening the notifications marks them seen.
|
||||
*/
|
||||
final class MarkNotificationsSeen
|
||||
{
|
||||
public function __construct(
|
||||
private NotificationRepository $notifications,
|
||||
private Clock $clock,
|
||||
) {}
|
||||
|
||||
public function execute(int $userId): void
|
||||
{
|
||||
$this->notifications->markSeen($userId, $this->clock->now());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Notifications\Application\UseCases;
|
||||
|
||||
use App\Modules\Jobs\Domain\Events\PeopleAddedToJob;
|
||||
use App\Modules\Jobs\Domain\Events\PeopleAddedToTask;
|
||||
use App\Modules\Notifications\Domain\Contracts\NotificationRepository;
|
||||
use App\Modules\Notifications\Domain\Enums\NotificationType;
|
||||
use App\Modules\Notifications\Domain\ValueObjects\NewNotification;
|
||||
use App\Modules\Users\Application\Contracts\Clock;
|
||||
|
||||
/**
|
||||
* Tells people right away that someone else made them a task's assignee or
|
||||
* member, or gave them a role on a customer job. Nobody is told about what
|
||||
* they did themselves.
|
||||
*/
|
||||
final class NotifyAddedPeople
|
||||
{
|
||||
public function __construct(
|
||||
private NotificationRepository $notifications,
|
||||
private Clock $clock,
|
||||
) {}
|
||||
|
||||
public function forTask(PeopleAddedToTask $event): void
|
||||
{
|
||||
$now = $this->clock->now();
|
||||
$params = [
|
||||
'task_id' => $event->taskId,
|
||||
'task_title' => $event->taskTitle,
|
||||
'job_id' => $event->jobId,
|
||||
'job_number' => $event->jobNumber,
|
||||
'job_name' => $event->jobName,
|
||||
];
|
||||
$roles = $event->newAssigneeId === null ? [] : [$event->newAssigneeId => 'assignee'];
|
||||
|
||||
foreach ($event->newMemberIds as $memberId) {
|
||||
$roles[$memberId] ??= 'member';
|
||||
}
|
||||
|
||||
foreach ($roles as $userId => $role) {
|
||||
if ($userId !== $event->actorUserId) {
|
||||
$this->notifications->addIfNew(NewNotification::assigned(NotificationType::TaskAssigned, $userId, $event->taskId, [...$params, 'role' => $role], $now));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function forJob(PeopleAddedToJob $event): void
|
||||
{
|
||||
$now = $this->clock->now();
|
||||
|
||||
foreach ($event->newRoles as $userId => $roles) {
|
||||
if ($userId === $event->actorUserId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->notifications->addIfNew(NewNotification::assigned(NotificationType::JobAssigned, $userId, $event->jobId, [
|
||||
'job_id' => $event->jobId,
|
||||
'job_number' => $event->jobNumber,
|
||||
'job_name' => $event->jobName,
|
||||
'roles' => implode(',', $roles),
|
||||
], $now));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Notifications\Application\UseCases;
|
||||
|
||||
use App\Modules\Jobs\Application\Contracts\UpcomingTasks;
|
||||
use App\Modules\Notifications\Application\Services\WorkingDays;
|
||||
use App\Modules\Notifications\Domain\Contracts\NotificationRepository;
|
||||
use App\Modules\Notifications\Domain\ValueObjects\NewNotification;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
use App\Modules\Users\Application\Contracts\Clock;
|
||||
|
||||
/**
|
||||
* The daily run: on a working day, tells the assignee and members of every
|
||||
* task starting by the next working day (after today) that it starts, e.g.
|
||||
* on Friday about tasks starting on Saturday to Monday. Running again does
|
||||
* not notify twice. Notifications older than a week are removed.
|
||||
*/
|
||||
final class NotifyTasksStartingSoon
|
||||
{
|
||||
/**
|
||||
* How long notifications are kept and shown.
|
||||
*/
|
||||
public const int KEEP_DAYS = 7;
|
||||
|
||||
public function __construct(
|
||||
private UpcomingTasks $tasks,
|
||||
private NotificationRepository $notifications,
|
||||
private WorkingDays $workingDays,
|
||||
private TransactionManager $transactions,
|
||||
private Clock $clock,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return int How many notifications were added.
|
||||
*/
|
||||
public function execute(): int
|
||||
{
|
||||
$today = $this->clock->today();
|
||||
$now = $this->clock->now();
|
||||
|
||||
return $this->transactions->run(function () use ($today, $now): int {
|
||||
$this->notifications->deleteOlderThan($now->modify('-'.self::KEEP_DAYS.' days'));
|
||||
|
||||
if (! $this->workingDays->isWorkingDay($today)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$until = $this->workingDays->nextAfter($today)->format('Y-m-d');
|
||||
$added = 0;
|
||||
|
||||
foreach ($this->tasks->startingBetween($today->format('Y-m-d'), $until) as $task) {
|
||||
$params = [
|
||||
'task_id' => $task->taskId,
|
||||
'task_title' => $task->title,
|
||||
'job_id' => $task->jobId,
|
||||
'job_number' => $task->jobNumber,
|
||||
'job_name' => $task->jobName,
|
||||
'starts_on' => $task->plannedFrom,
|
||||
];
|
||||
|
||||
foreach ($task->recipientIds as $userId) {
|
||||
if ($this->notifications->addIfNew(NewNotification::taskStartsSoon($userId, $task->taskId, $task->plannedFrom, $params, $now))) {
|
||||
$added++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $added;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Notifications\Domain\Contracts;
|
||||
|
||||
use App\Modules\Notifications\Domain\ValueObjects\NewNotification;
|
||||
use DateTimeImmutable;
|
||||
|
||||
interface NotificationRepository
|
||||
{
|
||||
/**
|
||||
* Adds the notification unless one with the same dedupe key exists.
|
||||
*
|
||||
* @return bool Whether it was added.
|
||||
*/
|
||||
public function addIfNew(NewNotification $notification): bool;
|
||||
|
||||
/**
|
||||
* Marks the user's unseen notifications seen.
|
||||
*/
|
||||
public function markSeen(int $userId, DateTimeImmutable $at): void;
|
||||
|
||||
public function deleteOlderThan(DateTimeImmutable $moment): void;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Notifications\Domain\Enums;
|
||||
|
||||
/**
|
||||
* What a notification tells about; its text is
|
||||
* `notifications.types.<value>` with the notification's parameters.
|
||||
*/
|
||||
enum NotificationType: string
|
||||
{
|
||||
/**
|
||||
* A task the user does or is a member of starts on the next working day.
|
||||
*/
|
||||
case TaskStartsSoon = 'task_starts_soon';
|
||||
|
||||
/**
|
||||
* Someone else made the user a task's assignee or member.
|
||||
*/
|
||||
case TaskAssigned = 'task_assigned';
|
||||
|
||||
/**
|
||||
* Someone else gave the user a role on a customer job.
|
||||
*/
|
||||
case JobAssigned = 'job_assigned';
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Notifications\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Notifications\Domain\Enums\NotificationType;
|
||||
use DateTimeImmutable;
|
||||
|
||||
/**
|
||||
* A notification to add for a user, once per dedupe key.
|
||||
*/
|
||||
final readonly class NewNotification
|
||||
{
|
||||
/**
|
||||
* @param array<string, int|string> $params
|
||||
*/
|
||||
public function __construct(
|
||||
public int $userId,
|
||||
public NotificationType $type,
|
||||
public array $params,
|
||||
public string $dedupeKey,
|
||||
public DateTimeImmutable $createdAt,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param array<string, int|string> $params
|
||||
*/
|
||||
public static function taskStartsSoon(int $userId, int $taskId, string $startsOn, array $params, DateTimeImmutable $at): self
|
||||
{
|
||||
return new self($userId, NotificationType::TaskStartsSoon, $params, "task_starts:{$taskId}:{$startsOn}:{$userId}", $at);
|
||||
}
|
||||
|
||||
/**
|
||||
* Each assignment is its own notification: one removed and added again
|
||||
* is told again.
|
||||
*
|
||||
* @param array<string, int|string> $params
|
||||
*/
|
||||
public static function assigned(NotificationType $type, int $userId, int $subjectId, array $params, DateTimeImmutable $at): self
|
||||
{
|
||||
return new self($userId, $type, $params, "{$type->value}:{$subjectId}:{$userId}:".$at->format('U.u').':'.bin2hex(random_bytes(4)), $at);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Notifications\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use App\Modules\Notifications\Domain\Enums\NotificationType;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Persistence model of a user's notification, used only inside the
|
||||
* Notifications Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $user_id
|
||||
* @property NotificationType $type
|
||||
* @property array<string, int|string> $params
|
||||
* @property string $dedupe_key
|
||||
* @property CarbonImmutable|null $seen_at
|
||||
* @property CarbonImmutable $created_at
|
||||
*/
|
||||
#[Table('user_notifications')]
|
||||
#[Fillable(['user_id', 'type', 'params', 'dedupe_key', 'seen_at', 'created_at'])]
|
||||
class NotificationModel extends Model
|
||||
{
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'user_id' => 'integer',
|
||||
'type' => NotificationType::class,
|
||||
'params' => 'array',
|
||||
'seen_at' => 'immutable_datetime',
|
||||
'created_at' => 'immutable_datetime',
|
||||
'updated_at' => 'immutable_datetime',
|
||||
];
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Notifications\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Notifications\Application\Contracts\NotificationQueries;
|
||||
use App\Modules\Notifications\Application\DTOs\NotificationSummary;
|
||||
use App\Modules\Notifications\Infrastructure\Persistence\Eloquent\NotificationModel;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class EloquentNotificationQueries implements NotificationQueries
|
||||
{
|
||||
public function recentFor(int $userId, DateTimeImmutable $since): array
|
||||
{
|
||||
return NotificationModel::query()
|
||||
->where('user_id', $userId)
|
||||
->where('created_at', '>=', $since)
|
||||
->orderByDesc('created_at')
|
||||
->orderByDesc('id')
|
||||
->get()
|
||||
->map(fn (NotificationModel $model): NotificationSummary => new NotificationSummary(
|
||||
$model->id,
|
||||
$model->type,
|
||||
$model->params,
|
||||
$model->created_at->setTimezone((string) config('app.business_timezone'))->format('Y-m-d H:i'),
|
||||
$model->seen_at !== null,
|
||||
))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
public function unseenCount(int $userId, DateTimeImmutable $since): int
|
||||
{
|
||||
return NotificationModel::query()
|
||||
->where('user_id', $userId)
|
||||
->where('created_at', '>=', $since)
|
||||
->whereNull('seen_at')
|
||||
->count();
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Notifications\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Notifications\Domain\Contracts\NotificationRepository;
|
||||
use App\Modules\Notifications\Domain\ValueObjects\NewNotification;
|
||||
use App\Modules\Notifications\Infrastructure\Persistence\Eloquent\NotificationModel;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class EloquentNotificationRepository implements NotificationRepository
|
||||
{
|
||||
public function addIfNew(NewNotification $notification): bool
|
||||
{
|
||||
$model = NotificationModel::query()->firstOrCreate(
|
||||
['dedupe_key' => $notification->dedupeKey],
|
||||
[
|
||||
'user_id' => $notification->userId,
|
||||
'type' => $notification->type,
|
||||
'params' => $notification->params,
|
||||
'created_at' => $notification->createdAt,
|
||||
],
|
||||
);
|
||||
|
||||
return $model->wasRecentlyCreated;
|
||||
}
|
||||
|
||||
public function markSeen(int $userId, DateTimeImmutable $at): void
|
||||
{
|
||||
NotificationModel::query()
|
||||
->where('user_id', $userId)
|
||||
->whereNull('seen_at')
|
||||
->update(['seen_at' => $at]);
|
||||
}
|
||||
|
||||
public function deleteOlderThan(DateTimeImmutable $moment): void
|
||||
{
|
||||
NotificationModel::query()->where('created_at', '<', $moment)->delete();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Notifications\Infrastructure\Providers;
|
||||
|
||||
use App\Modules\Jobs\Domain\Events\PeopleAddedToJob;
|
||||
use App\Modules\Jobs\Domain\Events\PeopleAddedToTask;
|
||||
use App\Modules\Notifications\Application\Contracts\NotificationQueries;
|
||||
use App\Modules\Notifications\Application\UseCases\NotifyAddedPeople;
|
||||
use App\Modules\Notifications\Domain\Contracts\NotificationRepository;
|
||||
use App\Modules\Notifications\Infrastructure\Persistence\Repositories\EloquentNotificationQueries;
|
||||
use App\Modules\Notifications\Infrastructure\Persistence\Repositories\EloquentNotificationRepository;
|
||||
use App\Modules\Notifications\Presentation\Console\NotifyTasksStartingSoonCommand;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Livewire\Livewire;
|
||||
|
||||
/**
|
||||
* Wires the Notifications module into Laravel: bindings, the
|
||||
* `notifications::` Livewire namespace (the header's bell), the listeners
|
||||
* of the Jobs module's assignment events and the daily command, scheduled
|
||||
* in routes/console.php.
|
||||
*/
|
||||
class NotificationsServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* @var array<class-string, class-string>
|
||||
*/
|
||||
public array $bindings = [
|
||||
NotificationRepository::class => EloquentNotificationRepository::class,
|
||||
NotificationQueries::class => EloquentNotificationQueries::class,
|
||||
];
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
Livewire::addNamespace('notifications', viewPath: dirname(__DIR__, 2).'/Presentation/Livewire');
|
||||
|
||||
// Published by Jobs after the change is committed.
|
||||
Event::listen(PeopleAddedToTask::class, fn (PeopleAddedToTask $event) => app(NotifyAddedPeople::class)->forTask($event));
|
||||
Event::listen(PeopleAddedToJob::class, fn (PeopleAddedToJob $event) => app(NotifyAddedPeople::class)->forJob($event));
|
||||
|
||||
if ($this->app->runningInConsole()) {
|
||||
$this->commands([NotifyTasksStartingSoonCommand::class]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Notifications\Presentation\Console;
|
||||
|
||||
use App\Modules\Notifications\Application\UseCases\NotifyTasksStartingSoon;
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
#[Signature('notifications:task-starts')]
|
||||
#[Description('Notify assignees and members of tasks that start on the next working day (run daily at 14.00)')]
|
||||
class NotifyTasksStartingSoonCommand extends Command
|
||||
{
|
||||
public function handle(NotifyTasksStartingSoon $notify): int
|
||||
{
|
||||
$added = $notify->execute();
|
||||
|
||||
$this->components->info("{$added} notifications added.");
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Notifications\Application\Contracts\NotificationQueries;
|
||||
use App\Modules\Notifications\Application\DTOs\NotificationSummary;
|
||||
use App\Modules\Notifications\Application\UseCases\MarkNotificationsSeen;
|
||||
use App\Modules\Notifications\Application\UseCases\NotifyTasksStartingSoon;
|
||||
use App\Modules\Notifications\Domain\Enums\NotificationType;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
/**
|
||||
* The signed-in user's notifications in the header: a bell with the number
|
||||
* of unseen ones and a panel with those of the last 7 days. Opening the
|
||||
* panel marks them seen; those that were new are marked "Uusi" until the
|
||||
* page is left.
|
||||
*/
|
||||
new class extends Component
|
||||
{
|
||||
/**
|
||||
* Notifications that were unseen when the panel was opened.
|
||||
*
|
||||
* @var list<int>
|
||||
*/
|
||||
#[Locked]
|
||||
public array $newIds = [];
|
||||
|
||||
/**
|
||||
* @return list<NotificationSummary>
|
||||
*/
|
||||
#[Computed]
|
||||
public function notifications(): array
|
||||
{
|
||||
return app(NotificationQueries::class)->recentFor((int) auth()->id(), $this->since());
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function unseenCount(): int
|
||||
{
|
||||
return app(NotificationQueries::class)->unseenCount((int) auth()->id(), $this->since());
|
||||
}
|
||||
|
||||
public function open(MarkNotificationsSeen $markSeen): void
|
||||
{
|
||||
$unseen = array_values(array_map(
|
||||
fn (NotificationSummary $notification): int => $notification->id,
|
||||
array_filter($this->notifications, fn (NotificationSummary $notification): bool => ! $notification->seen),
|
||||
));
|
||||
|
||||
if ($unseen === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->newIds = array_values(array_unique([...$this->newIds, ...$unseen]));
|
||||
$markSeen->execute((int) auth()->id());
|
||||
unset($this->notifications, $this->unseenCount);
|
||||
}
|
||||
|
||||
public function text(NotificationSummary $notification): string
|
||||
{
|
||||
return match ($notification->type) {
|
||||
NotificationType::TaskStartsSoon => __('notifications.types.task_starts_soon', [
|
||||
'task' => (string) $notification->params['task_title'],
|
||||
'number' => (string) $notification->params['job_number'],
|
||||
'job' => (string) $notification->params['job_name'],
|
||||
'date' => $this->formatDay((string) $notification->params['starts_on']),
|
||||
]),
|
||||
NotificationType::TaskAssigned => __('notifications.types.task_assigned.'.$notification->params['role'], [
|
||||
'task' => (string) $notification->params['task_title'],
|
||||
'number' => (string) $notification->params['job_number'],
|
||||
'job' => (string) $notification->params['job_name'],
|
||||
]),
|
||||
NotificationType::JobAssigned => __('notifications.types.job_assigned', [
|
||||
'number' => (string) $notification->params['job_number'],
|
||||
'job' => (string) $notification->params['job_name'],
|
||||
'roles' => $this->roleList((string) $notification->params['roles']),
|
||||
]),
|
||||
};
|
||||
}
|
||||
|
||||
public function url(NotificationSummary $notification): string
|
||||
{
|
||||
return match ($notification->type) {
|
||||
NotificationType::TaskStartsSoon, NotificationType::TaskAssigned => route('jobs.show', (int) $notification->params['job_id']).'#task-'.$notification->params['task_id'],
|
||||
NotificationType::JobAssigned => route('jobs.show', (int) $notification->params['job_id']),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* "vastuuhenkilöksi ja jäseneksi" from "responsible,member".
|
||||
*/
|
||||
private function roleList(string $roles): string
|
||||
{
|
||||
$names = array_map(fn (string $role): string => __('notifications.job_roles.'.$role), array_filter(explode(',', $roles)));
|
||||
$last = array_pop($names);
|
||||
|
||||
return $names === [] ? (string) $last : implode(', ', $names).' '.__('notifications.and').' '.$last;
|
||||
}
|
||||
|
||||
/**
|
||||
* "25.11.2026 klo 14.05" from "2026-11-25 14:05".
|
||||
*/
|
||||
public function formatTime(string $moment): string
|
||||
{
|
||||
$time = CarbonImmutable::createFromFormat('Y-m-d H:i', $moment);
|
||||
|
||||
return __('notifications.at', ['date' => $time->format('j.n.Y'), 'time' => $time->format('H.i')]);
|
||||
}
|
||||
|
||||
/**
|
||||
* "ma 30.11.2026" from "2026-11-30".
|
||||
*/
|
||||
private function formatDay(string $date): string
|
||||
{
|
||||
$day = CarbonImmutable::createFromFormat('Y-m-d', $date);
|
||||
|
||||
return __('holidays.weekdays.'.$day->dayOfWeekIso).' '.$day->format('j.n.Y');
|
||||
}
|
||||
|
||||
private function since(): DateTimeImmutable
|
||||
{
|
||||
return CarbonImmutable::now()->subDays(NotifyTasksStartingSoon::KEEP_DAYS)->toDateTimeImmutable();
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
{{--
|
||||
A disclosure like the profile menu (not role="menu"): the bell's name stays "Ilmoitukset", and the number of
|
||||
unseen notifications is visible text that the button refers to with aria-describedby. Opening the panel marks
|
||||
them seen. Escape, a click outside and moving focus out close it.
|
||||
--}}
|
||||
<div
|
||||
class="relative"
|
||||
x-data="{ open: false }"
|
||||
x-on:keydown.escape="if (open) { open = false; $refs.trigger.focus() }"
|
||||
x-on:click.outside="open = false"
|
||||
x-on:focusout="if (open && $event.relatedTarget && ! $el.contains($event.relatedTarget)) open = false"
|
||||
wire:poll.300s.visible
|
||||
>
|
||||
@php($count = $this->unseenCount)
|
||||
|
||||
<button
|
||||
type="button"
|
||||
x-ref="trigger"
|
||||
x-on:click="open = ! open; if (open) $wire.open()"
|
||||
x-bind:aria-expanded="open.toString()"
|
||||
aria-expanded="false"
|
||||
aria-controls="notifications-panel"
|
||||
aria-describedby="notifications-count"
|
||||
data-notifications-bell
|
||||
class="relative flex min-h-9 items-center gap-1 rounded-md px-3 py-2 text-sm font-medium text-zinc-200 hover:bg-zinc-700 hover:text-white focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-400"
|
||||
>
|
||||
<x-icon name="bell" class="size-5" aria-hidden="true" />
|
||||
<span class="sr-only">{{ __('notifications.title') }}</span>
|
||||
@if ($count > 0)
|
||||
<span aria-hidden="true" class="min-w-5 rounded-full bg-red-700 px-1.5 text-center text-xs font-bold leading-5 text-white">{{ $count > 99 ? '99+' : $count }}</span>
|
||||
@endif
|
||||
</button>
|
||||
<span id="notifications-count" class="sr-only">{{ trans_choice('notifications.unseen', $count, ['count' => $count]) }}</span>
|
||||
|
||||
<div
|
||||
id="notifications-panel"
|
||||
x-show="open"
|
||||
x-cloak
|
||||
class="z-40 mt-1 w-96 max-w-[calc(100vw-2rem)] rounded-md border border-zinc-600 bg-zinc-800 p-4 text-sm text-zinc-100 shadow-lg sm:absolute sm:end-0"
|
||||
>
|
||||
{{-- Not a heading: the header comes before the page's h1. --}}
|
||||
<p id="notifications-panel-title" class="mb-2 font-semibold">{{ __('notifications.recent') }}</p>
|
||||
|
||||
@if ($this->notifications === [])
|
||||
<p class="text-zinc-300">{{ __('notifications.empty') }}</p>
|
||||
@else
|
||||
<ul aria-labelledby="notifications-panel-title" class="max-h-[60vh] space-y-1 overflow-y-auto">
|
||||
@foreach ($this->notifications as $notification)
|
||||
<li wire:key="notification-{{ $notification->id }}">
|
||||
<a
|
||||
href="{{ $this->url($notification) }}"
|
||||
wire:navigate
|
||||
class="block rounded-md px-2 py-2 hover:bg-zinc-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-400"
|
||||
>
|
||||
@if (in_array($notification->id, $newIds, true))
|
||||
<span class="me-1 rounded bg-zinc-100 px-1.5 py-0.5 text-xs font-semibold text-zinc-900">{{ __('notifications.new') }}</span>
|
||||
@endif
|
||||
<span class="break-words">{{ $this->text($notification) }}</span>
|
||||
<span class="mt-0.5 block text-xs text-zinc-300">{{ $this->formatTime($notification->createdAt) }}</span>
|
||||
</a>
|
||||
</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user