feat: add bulk client notifications and email enhancements with review fixes (Stories 3.4 & 3.5)

Story 3-4: Bulk client notification scheduling — BulkNotificationController,
BulkActionBar component, checkbox selection on declarations index.

Story 3-5: Email notification enhancement — observer-driven email on
en_attente_client, cache invalidation on ferme, workspace branding on
all email templates, 11 feature tests.

Code review fixes:
- Move bulk-notify route above resource wildcard to prevent shadowing
- Add static $suppressEmail flag to prevent observer double-sending
  when DeclarationMessageController already sends the email
- Fix canBulkNotify logic (was granting workers access)
- Add WorkspaceUserRole check to BulkNotifyRequest::authorize()
- Replace firstOrCreate with explicit invitation lookup that syncs
  client email and handles used/expired invitations correctly
- Watch declarations.data instead of current_page to clear selection
  on filter/sort changes

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-26 14:31:36 +01:00
parent 32e11db2b5
commit 1d4f3bcd0f
17 changed files with 1384 additions and 7 deletions

View File

@@ -0,0 +1,106 @@
<?php
namespace App\Http\Controllers;
use App\Concerns\HasWorkspaceScope;
use App\Enums\DeclarationStatus;
use App\Http\Requests\BulkNotifyRequest;
use App\Mail\DeclarationFileRequestMail;
use App\Models\Declaration;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
class BulkNotificationController extends Controller
{
use HasWorkspaceScope;
public function store(BulkNotifyRequest $request): RedirectResponse
{
$workspace = $this->currentWorkspace();
$user = $request->user();
$workspaceUser = $user->currentWorkspaceUser();
$declarations = Declaration::where('workspace_id', $workspace->id)
->forUser($user, $workspaceUser)
->where('status', DeclarationStatus::EnAttenteClient)
->whereIn('id', $request->validated('declaration_ids'))
->with('client')
->get()
->filter(fn (Declaration $d) => $d->client !== null);
if ($declarations->isEmpty()) {
return back()->with('flash', [
'type' => 'warning',
'message' => 'Aucune déclaration éligible trouvée.',
]);
}
// DB transaction for invitation creation/update, collect mail data for queuing after commit
$mailJobs = DB::transaction(function () use ($declarations) {
$jobs = [];
foreach ($declarations as $declaration) {
$clientEmail = $declaration->client->contact_email;
$invitation = $declaration->invitations()
->whereNull('used_at')
->latest()
->first();
if ($invitation && $invitation->isValid()) {
if ($invitation->email !== $clientEmail) {
$invitation->update(['email' => $clientEmail]);
$invitation->refresh();
}
} elseif ($invitation && ! $invitation->isValid()) {
$invitation->update([
'email' => $clientEmail,
'expires_at' => now()->addDays(30),
]);
$invitation->refresh();
} else {
$invitation = $declaration->invitations()->create([
'email' => $clientEmail,
'expires_at' => now()->addDays(30),
]);
}
$body = 'Nous vous invitons à déposer les documents complémentaires pour votre déclaration "'
. $declaration->title . '".';
$jobs[] = [
'email' => $declaration->client->contact_email,
'mailable' => new DeclarationFileRequestMail($declaration, $invitation, $body),
];
}
return $jobs;
});
// Queue emails outside transaction (Redis is not transactional with MySQL)
foreach ($mailJobs as $job) {
Mail::to($job['email'])->queue($job['mailable']);
}
activity()
->performedOn($workspace)
->causedBy($user)
->withProperties([
'count' => $declarations->count(),
'declaration_ids' => $declarations->pluck('id')->all(),
])
->log('bulk_client_notification');
// Invalidate notification caches for workspace users
$workspace->users->each(function ($wsUser) use ($workspace) {
Cache::forget("user:{$wsUser->id}:workspace:{$workspace->id}:unread_notifications");
});
return back()->with('flash', [
'type' => 'success',
'message' => $declarations->count() . ' notifications envoyées',
]);
}
}