chore: update document approval workflow and bug fixes

This commit is contained in:
2026-05-25 13:05:20 +08:00
parent d573c02893
commit 39a8e1d4cd
910 changed files with 49994 additions and 1010 deletions

View File

@@ -0,0 +1,56 @@
<?php
namespace Modules\DocumentManagement\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Modules\DocumentManagement\Models\Document;
use Illuminate\Support\Facades\Gate;
class DocumentApprovalController extends Controller
{
/**
* Approve or reject a document.
*/
public function __invoke(Request $request, Document $document)
{
// Typically, we would use a Gate or Policy.
// For Contractor Admin, they should have a specific role or permission.
// Assuming there is a policy checking user->hasRole('Contractor Admin') or similar.
Gate::authorize('approve', $document);
$validated = $request->validate([
'status' => 'required|in:Approved,Rejected',
'comments' => 'nullable|string|max:1000',
]);
// Find the global approval chain if it exists
$chain = \Modules\ApprovalWorkflow\Models\ApprovalChain::where('approvable_type', Document::class)
->where('approvable_id', $document->id)
->whereIn('status', ['pending', 'in_review'])
->first();
if ($chain) {
$service = app(\Modules\ApprovalWorkflow\Services\ApprovalService::class);
if ($validated['status'] === 'Approved') {
$service->approve($chain, $request->user(), $validated['comments']);
} else {
$service->reject($chain, $request->user(), $validated['comments']);
}
} else {
// Fallback for older documents that didn't go through the ApprovalWorkflow
$document->approvals()->create([
'approved_by' => $request->user()->id,
'status' => $validated['status'],
'comments' => $validated['comments'] ?? null,
'approved_at' => now(),
]);
$document->update([
'status' => $validated['status'],
]);
}
return back()->with('success', "Document has been {$validated['status']}.");
}
}