|
|
@@ -3,13 +3,16 @@
|
|
|
namespace App\Services;
|
|
|
|
|
|
use App\Enums\AppointmentStatusEnum;
|
|
|
+use App\Enums\NotificationRecipientEnum;
|
|
|
use App\Models\Appointment;
|
|
|
+use Carbon\Carbon;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
class AppointmentService
|
|
|
{
|
|
|
+ public function __construct(protected NotificationService $notificationService) {}
|
|
|
public function getAll(): Collection
|
|
|
{
|
|
|
return Appointment::with(['user', 'partnerAgreement', 'partnerAgreementService'])
|
|
|
@@ -46,6 +49,22 @@ class AppointmentService
|
|
|
return Appointment::create($data);
|
|
|
}
|
|
|
|
|
|
+ public function notifyCreation(Appointment $model): void
|
|
|
+ {
|
|
|
+ $dateStr = $model->date ? Carbon::parse($model->date)->format('d/m/Y') : null;
|
|
|
+ $message = $dateStr
|
|
|
+ ? "Um agendamento #{$model->order_number} foi criado para você" . ($model->time ? " para {$dateStr} às {$model->time}" : " em {$dateStr}") . "."
|
|
|
+ : "Um agendamento #{$model->order_number} foi criado para você.";
|
|
|
+
|
|
|
+ $this->notificationService->createAutoForUser([
|
|
|
+ 'title' => 'Novo agendamento',
|
|
|
+ 'message' => $message,
|
|
|
+ 'recipient' => NotificationRecipientEnum::ASSOCIADO,
|
|
|
+ 'source' => 'appointment',
|
|
|
+ 'source_id' => $model->id,
|
|
|
+ ], $model->user_id);
|
|
|
+ }
|
|
|
+
|
|
|
public function update(int $id, array $data): ?Appointment
|
|
|
{
|
|
|
$model = Appointment::find($id);
|
|
|
@@ -103,11 +122,22 @@ class AppointmentService
|
|
|
return $query->paginate($perPage);
|
|
|
}
|
|
|
|
|
|
- public function approve(int $id): ?Appointment
|
|
|
+ public function approve(int $id, string $date, string $time): ?Appointment
|
|
|
{
|
|
|
$model = Appointment::find($id);
|
|
|
if (!$model) return null;
|
|
|
- $model->update(['status' => AppointmentStatusEnum::CONFIRMADO]);
|
|
|
+ $model->update([
|
|
|
+ 'status' => AppointmentStatusEnum::CONFIRMADO,
|
|
|
+ 'date' => $date,
|
|
|
+ 'time' => $time,
|
|
|
+ ]);
|
|
|
+ $this->notificationService->createAutoForUser([
|
|
|
+ 'title' => 'Agendamento confirmado',
|
|
|
+ 'message' => "Seu agendamento #{$model->order_number} foi confirmado para " . Carbon::parse($date)->format('d/m/Y') . " às {$time}.",
|
|
|
+ 'recipient' => NotificationRecipientEnum::ASSOCIADO,
|
|
|
+ 'source' => 'appointment',
|
|
|
+ 'source_id' => $model->id,
|
|
|
+ ], $model->user_id);
|
|
|
return $model->fresh(['user', 'partnerAgreement', 'partnerAgreementService']);
|
|
|
}
|
|
|
|
|
|
@@ -116,14 +146,32 @@ class AppointmentService
|
|
|
$model = Appointment::find($id);
|
|
|
if (!$model) return null;
|
|
|
$model->update(['status' => AppointmentStatusEnum::RECUSADO]);
|
|
|
+ $this->notificationService->createAutoForUser([
|
|
|
+ 'title' => 'Agendamento recusado',
|
|
|
+ 'message' => "Seu agendamento #{$model->order_number} foi recusado.",
|
|
|
+ 'recipient' => NotificationRecipientEnum::ASSOCIADO,
|
|
|
+ 'source' => 'appointment',
|
|
|
+ 'source_id' => $model->id,
|
|
|
+ ], $model->user_id);
|
|
|
return $model->fresh(['user', 'partnerAgreement', 'partnerAgreementService']);
|
|
|
}
|
|
|
|
|
|
- public function approveByPartner(int $id, int $userId): ?Appointment
|
|
|
+ public function approveByPartner(int $id, int $userId, string $date, string $time): ?Appointment
|
|
|
{
|
|
|
$model = Appointment::whereHas('partnerAgreement', fn($q) => $q->where('user_id', $userId))->find($id);
|
|
|
if (!$model) return null;
|
|
|
- $model->update(['status' => AppointmentStatusEnum::CONFIRMADO]);
|
|
|
+ $model->update([
|
|
|
+ 'status' => AppointmentStatusEnum::CONFIRMADO,
|
|
|
+ 'date' => $date,
|
|
|
+ 'time' => $time,
|
|
|
+ ]);
|
|
|
+ $this->notificationService->createAutoForUser([
|
|
|
+ 'title' => 'Agendamento confirmado',
|
|
|
+ 'message' => "Seu agendamento #{$model->order_number} foi confirmado para " . Carbon::parse($date)->format('d/m/Y') . " às {$time}.",
|
|
|
+ 'recipient' => NotificationRecipientEnum::ASSOCIADO,
|
|
|
+ 'source' => 'appointment',
|
|
|
+ 'source_id' => $model->id,
|
|
|
+ ], $model->user_id);
|
|
|
return $model->fresh(['user', 'partnerAgreement', 'partnerAgreementService']);
|
|
|
}
|
|
|
|
|
|
@@ -132,6 +180,13 @@ class AppointmentService
|
|
|
$model = Appointment::whereHas('partnerAgreement', fn($q) => $q->where('user_id', $userId))->find($id);
|
|
|
if (!$model) return null;
|
|
|
$model->update(['status' => AppointmentStatusEnum::RECUSADO]);
|
|
|
+ $this->notificationService->createAutoForUser([
|
|
|
+ 'title' => 'Agendamento recusado',
|
|
|
+ 'message' => "Seu agendamento #{$model->order_number} foi recusado.",
|
|
|
+ 'recipient' => NotificationRecipientEnum::ASSOCIADO,
|
|
|
+ 'source' => 'appointment',
|
|
|
+ 'source_id' => $model->id,
|
|
|
+ ], $model->user_id);
|
|
|
return $model->fresh(['user', 'partnerAgreement', 'partnerAgreementService']);
|
|
|
}
|
|
|
|