Files
minicrm/app/Services/TransferService.php
phuongtc 8c6b71cb8a
Some checks failed
Tests / PHP 8.3 (push) Has been cancelled
Tests / PHP 8.4 (push) Has been cancelled
Tests / PHP 8.5 (push) Has been cancelled
feat: add i18n support for Vietnamese and English
2026-05-04 10:40:39 +00:00

55 lines
1.5 KiB
PHP

<?php
namespace App\Services;
use App\Models\Department;
use App\Models\Feedback;
use App\Models\TicketTransferLog;
use App\Models\User;
use Illuminate\Validation\ValidationException;
class TransferService
{
public function transfer(
Feedback $feedback,
Department $toDepartment,
string $reason,
User $sender,
?int $newHandlerId = null,
): TicketTransferLog {
if (empty(trim($reason))) {
throw ValidationException::withMessages([
'reason' => __('feedback.transfer_reason_required'),
]);
}
$fromDepartmentId = $feedback->current_department_id;
$log = TicketTransferLog::create([
'feedback_id' => $feedback->id,
'from_department_id' => $fromDepartmentId,
'to_department_id' => $toDepartment->id,
'sender_id' => $sender->id,
'reason' => $reason,
]);
$feedback->update([
'current_department_id' => $toDepartment->id,
'assigned_to' => $newHandlerId ?? $toDepartment->manager_id ?? null,
]);
$this->notifyDepartmentManager($toDepartment, $feedback, $log, $sender);
return $log;
}
protected function notifyDepartmentManager(Department $department, Feedback $feedback, TicketTransferLog $log, User $sender): void
{
$manager = $department->manager;
if ($manager) {
$manager->notify(new \App\Notifications\TicketTransferred($feedback, $log, $sender));
}
}
}