91 lines
2.9 KiB
PHP
91 lines
2.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Client;
|
||
|
|
|
||
|
|
use App\Enums\ActorType;
|
||
|
|
use App\Enums\FolderStatus;
|
||
|
|
use App\Enums\MessageType;
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Mail\FolderTextMessageMail;
|
||
|
|
use Illuminate\Http\RedirectResponse;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Support\Facades\Mail;
|
||
|
|
use Inertia\Inertia;
|
||
|
|
use Inertia\Response;
|
||
|
|
|
||
|
|
class UploadController extends Controller
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Show the client upload page.
|
||
|
|
*/
|
||
|
|
public function show(Request $request, string $token): Response
|
||
|
|
{
|
||
|
|
$invitation = $request->attributes->get('folder_invitation');
|
||
|
|
$folder = $invitation->folder;
|
||
|
|
|
||
|
|
$folder->load(['client']);
|
||
|
|
$documents = $folder->getMedia('documents')->map(fn ($m) => [
|
||
|
|
'id' => $m->id,
|
||
|
|
'name' => $m->name,
|
||
|
|
'file_name' => $m->file_name,
|
||
|
|
'size' => $m->human_readable_size,
|
||
|
|
'created_at' => $m->created_at->format('d/m/Y H:i'),
|
||
|
|
])->values()->all();
|
||
|
|
|
||
|
|
return Inertia::render('client/Upload', [
|
||
|
|
'folder' => [
|
||
|
|
'id' => $folder->id,
|
||
|
|
'title' => $folder->title,
|
||
|
|
'client_name' => $folder->client->company_name,
|
||
|
|
],
|
||
|
|
'token' => $token,
|
||
|
|
'documents' => $documents,
|
||
|
|
'uploadUrl' => route('client.upload.store', ['token' => $token]),
|
||
|
|
'csrfToken' => csrf_token(),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Store uploaded files.
|
||
|
|
*/
|
||
|
|
public function store(Request $request, string $token): RedirectResponse
|
||
|
|
{
|
||
|
|
$invitation = $request->attributes->get('folder_invitation');
|
||
|
|
$folder = $invitation->folder;
|
||
|
|
|
||
|
|
$request->validate([
|
||
|
|
'files' => ['required', 'array', 'min:1'],
|
||
|
|
'files.*' => ['file', 'max:10240'],
|
||
|
|
]);
|
||
|
|
|
||
|
|
$message = $folder->messages()->create([
|
||
|
|
'type' => MessageType::Text,
|
||
|
|
'body' => 'Documents déposés par le client.',
|
||
|
|
'sent_by_type' => ActorType::Client,
|
||
|
|
'sent_by_id' => $folder->client_id,
|
||
|
|
'metadata' => ['invitation_id' => $invitation->id],
|
||
|
|
]);
|
||
|
|
|
||
|
|
foreach ($request->file('files') as $file) {
|
||
|
|
$folder->addMedia($file)
|
||
|
|
->withCustomProperties([
|
||
|
|
'message_id' => $message->id,
|
||
|
|
'uploaded_by_type' => ActorType::Client,
|
||
|
|
'uploaded_by_id' => $folder->client_id,
|
||
|
|
])
|
||
|
|
->toMediaCollection('documents');
|
||
|
|
}
|
||
|
|
|
||
|
|
$folder->update(['status' => FolderStatus::DocumentsReceived]);
|
||
|
|
|
||
|
|
$recipient = $folder->assignee ?? $folder->creator;
|
||
|
|
if ($recipient?->email) {
|
||
|
|
Mail::to($recipient->email)->send(
|
||
|
|
new FolderTextMessageMail($folder, 'Le client a déposé des documents.', null)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
return back()->with('flash', ['type' => 'success', 'message' => 'Documents envoyés avec succès.']);
|
||
|
|
}
|
||
|
|
}
|