69 lines
2.2 KiB
PHP
69 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Department;
|
|
use App\Models\Feedback;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
class ClosingService
|
|
{
|
|
public function close(Feedback $feedback, User $actor): void
|
|
{
|
|
if ($feedback->status === 'closed') {
|
|
throw ValidationException::withMessages([
|
|
'status' => __('feedback.already_closed'),
|
|
]);
|
|
}
|
|
|
|
if (! $actor->hasPermissionTo('close-ticket')) {
|
|
throw new HttpException(403, __('feedback.close_permission_denied'));
|
|
}
|
|
|
|
if ($actor->hasRole('manager') && ! $actor->hasRole('admin')) {
|
|
$managedDeptIds = Department::where('manager_id', $actor->id)->pluck('id');
|
|
if (! $managedDeptIds->contains($feedback->current_department_id)) {
|
|
throw new HttpException(403, __('feedback.close_department_only'));
|
|
}
|
|
}
|
|
|
|
$fileService = App::make(FileService::class);
|
|
|
|
if (! $fileService->hasCollection($feedback, 'proof_of_resolution')) {
|
|
throw ValidationException::withMessages([
|
|
'status' => __('feedback.proof_required'),
|
|
]);
|
|
}
|
|
|
|
$feedback->update(['status' => 'closed']);
|
|
|
|
$this->notifyStakeholders($feedback, $actor);
|
|
}
|
|
|
|
public function resolve(Feedback $feedback, User $actor): void
|
|
{
|
|
if (in_array($feedback->status, ['closed', 'resolved'])) {
|
|
throw ValidationException::withMessages([
|
|
'status' => __('feedback.already_resolved'),
|
|
]);
|
|
}
|
|
|
|
$feedback->update(['status' => 'resolved']);
|
|
}
|
|
|
|
protected function notifyStakeholders(Feedback $feedback, User $actor): void
|
|
{
|
|
if ($feedback->assignedTo) {
|
|
$feedback->assignedTo->notify(new \App\Notifications\TicketClosed($feedback, $actor));
|
|
}
|
|
|
|
$department = $feedback->currentDepartment;
|
|
if ($department && $department->manager && $department->manager->id !== ($feedback->assignedTo?->id ?? null)) {
|
|
$department->manager->notify(new \App\Notifications\TicketClosed($feedback, $actor));
|
|
}
|
|
}
|
|
}
|