chore: update document approval workflow and bug fixes
This commit is contained in:
@@ -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']}.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user