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,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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user