From 7698a5c7ec0db1dbdf049af00602476d46126217 Mon Sep 17 00:00:00 2001 From: Nicolas Demangeat Date: Mon, 31 Jul 2023 12:32:11 +0200 Subject: [PATCH] add dev policy to validate ou reject an estimation --- .../Controllers/Credit/EstimationController.php | 16 +++++++++++++++- .../Requests/Credit/AnswerEstimateRequest.php | 2 +- app/Policies/TicketPolicy.php | 9 ++++++++- .../views/estimations/pendingCard.blade.php | 15 ++++++++++++++- routes/web.php | 3 +++ 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Credit/EstimationController.php b/app/Http/Controllers/Credit/EstimationController.php index 4a66963..7a0fce0 100644 --- a/app/Http/Controllers/Credit/EstimationController.php +++ b/app/Http/Controllers/Credit/EstimationController.php @@ -91,7 +91,6 @@ class EstimationController extends Controller $ticket->id, $estimation->status->label ); - event(new TicketStatusChanged($ticket, $ticket->status, $user)); event(new NotifySubscriptions($ticket, $message)); event(new NotifyAdmin($ticket, $message)); return response()->redirectToRoute('ticket.show', $ticket) @@ -99,4 +98,19 @@ class EstimationController extends Controller } return \back(); } + + public function estimateCancelSubmit(Ticket $ticket) + { + $this->authorize('cancelEstimate', $ticket); + + /** @var \App\Models\Credit\Estimation */ + $estimation = $ticket->estimation; + $ticket->status = TicketStatus::estimateTodo(); + + if ($estimation->deleteOrFail() and $ticket->saveOrFail()) { + return response()->redirectToRoute('ticket.show', $ticket) + ->with('info', trans('L\'estimation a été supprimée.')); + } + return \back(); + } } diff --git a/app/Http/Requests/Credit/AnswerEstimateRequest.php b/app/Http/Requests/Credit/AnswerEstimateRequest.php index 4af0210..81b1ee9 100644 --- a/app/Http/Requests/Credit/AnswerEstimateRequest.php +++ b/app/Http/Requests/Credit/AnswerEstimateRequest.php @@ -18,7 +18,7 @@ class AnswerEstimateRequest extends FormRequest } /** @var \App\Models\User */ $user = auth()->user(); - return !$user->isAdmin() and $user->can('validateEstimate', $this->ticket); + return $user->can('validateEstimate', $this->ticket); } /** diff --git a/app/Policies/TicketPolicy.php b/app/Policies/TicketPolicy.php index ac70886..26566ff 100644 --- a/app/Policies/TicketPolicy.php +++ b/app/Policies/TicketPolicy.php @@ -67,11 +67,18 @@ class TicketPolicy extends RootPolicy TicketStaticRules::make('ticketIsNotArchived', $model) ]), 'validateEstimate' => StaticRules::and([ - URR::make('userIsClient', $user), + StaticRules::or([ + URR::make('userIsClient', $user), + URR::make('userIsDev', $user), + ]), TicketStaticRules::make('ticketsBrandHasAnAccount', $model), TicketStaticRules::make('ticketDoesHaveEstimation', $model), TicketStaticRules::make('ticketIsNotInternal', $model), TicketStaticRules::make('ticketIsNotArchived', $model) + ]), + 'cancelEstimate' => StaticRules::and([ + URR::make('userIsDev', $user), + TicketStaticRules::make('ticketDoesHaveEstimation', $model), ]) ] ]; diff --git a/resources/views/estimations/pendingCard.blade.php b/resources/views/estimations/pendingCard.blade.php index b20586d..5e5cd94 100644 --- a/resources/views/estimations/pendingCard.blade.php +++ b/resources/views/estimations/pendingCard.blade.php @@ -29,13 +29,26 @@
@csrf - +
+ @if (auth()->user()->role->equals(App\Enums\Role::developper())) +
+ @csrf + +
+ @else
@csrf
+ @endif @endcan @endcan diff --git a/routes/web.php b/routes/web.php index e6ed73b..e3926ae 100644 --- a/routes/web.php +++ b/routes/web.php @@ -163,6 +163,9 @@ Route::middleware('auth:web')->group(function () { Route::post('/{ticket}/estimate/answer', [EstimationController::class, 'estimatePendingSubmit']) ->middleware('can:validateEstimate,ticket') ->name('estimatePendingSubmit'); + Route::post('/{ticket}/estimate/cancel', [EstimationController::class, 'estimateCancelSubmit']) + ->middleware('can:cancelEstimate,ticket') + ->name('estimateCancelSubmit'); });