|
|
@@ -10,6 +10,7 @@ use App\Enums\ServicePackageStatusEnum;
|
|
|
use App\Enums\UserTypeEnum;
|
|
|
use App\Enums\NotificationTypeEnum;
|
|
|
use App\Jobs\StartScheduleJob;
|
|
|
+use App\Jobs\ScheduleStartingSoonJob;
|
|
|
use App\Models\Provider;
|
|
|
use App\Models\Schedule;
|
|
|
use App\Models\ServicePackage;
|
|
|
@@ -20,6 +21,8 @@ use App\Notifications\Push\Cliente\Agendamento\PrestadorAceitouPush;
|
|
|
use App\Notifications\Push\Cliente\Agendamento\PrestadorRecusouPush;
|
|
|
use App\Notifications\Push\Prestador\Agendamento\ClienteAceitouPush;
|
|
|
use App\Notifications\Push\Prestador\Pagamento\ClienteEfetuouPagamentoPush;
|
|
|
+use App\Notifications\Push\Cliente\Agendamento\AgendamentoProximoPrestadorPush;
|
|
|
+use App\Notifications\Push\Prestador\Agendamento\AgendamentoProximoClientePush;
|
|
|
use App\Notifications\Push\Cliente\Agendamento\PrestadorCancelouPush;
|
|
|
use App\Notifications\Push\Prestador\Agendamento\ClienteCancelouPush;
|
|
|
use App\Notifications\Push\Prestador\Agendamento\NewPushRequest;
|
|
|
@@ -265,7 +268,7 @@ class ScheduleService
|
|
|
'type' => NotificationTypeEnum::SCHEDULE_PROVIDER_CLIENT_CANCELLED->value,
|
|
|
'user_id' => $schedule->provider->user_id,
|
|
|
]);
|
|
|
- $this->sendClientCancelledPush($schedule);
|
|
|
+ $this->sendClientCancelledPush($schedule);
|
|
|
|
|
|
break;
|
|
|
|
|
|
@@ -278,8 +281,8 @@ class ScheduleService
|
|
|
'type' => NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_CANCELLED->value,
|
|
|
'user_id' => $schedule->client->user_id,
|
|
|
]);
|
|
|
-
|
|
|
- $this->sendProviderCancelledPush($schedule);
|
|
|
+
|
|
|
+ $this->sendProviderCancelledPush($schedule);
|
|
|
|
|
|
break;
|
|
|
|
|
|
@@ -335,7 +338,7 @@ class ScheduleService
|
|
|
case 'paid':
|
|
|
$notificationService = app(NotificationService::class);
|
|
|
if ($schedule->provider_id) {
|
|
|
-
|
|
|
+
|
|
|
$notificationService->create([
|
|
|
'title' => __('notifications.payment_confirmed_title'),
|
|
|
'description' => __('notifications.payment_confirmed_description'),
|
|
|
@@ -350,12 +353,34 @@ class ScheduleService
|
|
|
$date_cleaned = Carbon::parse($schedule->date)
|
|
|
->format('Y-m-d');
|
|
|
|
|
|
- $date_time_dispatch = Carbon::parse(
|
|
|
+ $start_date_time = Carbon::parse(
|
|
|
$date_cleaned . ' ' . $schedule->start_time
|
|
|
- )->subHour();
|
|
|
+ );
|
|
|
+
|
|
|
+ // =====================================================
|
|
|
+ // ScheduleStartingSoonJob
|
|
|
+ // =====================================================
|
|
|
+
|
|
|
+ // TESTE LOCAL: dispara 15 segundos depois do pagamento
|
|
|
+ ScheduleStartingSoonJob::dispatch($schedule->id)
|
|
|
+ ->delay(now()->addSeconds(15));
|
|
|
+
|
|
|
+ /*
|
|
|
+ PRODUÇÃO: dispara 1 hora antes do início
|
|
|
+
|
|
|
+ $notification_date_time = $start_date_time->copy()->subHour();
|
|
|
|
|
|
+ ScheduleStartingSoonJob::dispatch($schedule->id)
|
|
|
+ ->delay($notification_date_time);
|
|
|
+ */
|
|
|
+
|
|
|
+ // =====================================================
|
|
|
+ // StartScheduleJob
|
|
|
+ // =====================================================
|
|
|
+
|
|
|
+ // Aqui continua sendo o horário REAL de início
|
|
|
StartScheduleJob::dispatch($schedule->id)
|
|
|
- ->delay($date_time_dispatch);
|
|
|
+ ->delay($start_date_time);
|
|
|
|
|
|
break;
|
|
|
|
|
|
@@ -430,42 +455,42 @@ class ScheduleService
|
|
|
|
|
|
public function getClientProviderBlocks(int $clientId, int $providerId): array
|
|
|
{
|
|
|
- $weekStart = Carbon::today()->startOfWeek(Carbon::SUNDAY)->format('Y-m-d');
|
|
|
-
|
|
|
- $schedules = Schedule::where('client_id', $clientId)
|
|
|
- ->where('provider_id', $providerId)
|
|
|
- ->whereNotIn('status', self::EXCLUDED_STATUSES)
|
|
|
- ->whereDate('date', '>=', $weekStart)
|
|
|
- ->orderBy('date')
|
|
|
- ->orderBy('start_time')
|
|
|
- ->get(['id', 'date', 'start_time', 'end_time', 'status']);
|
|
|
-
|
|
|
- $existingSchedules = $schedules->map(function ($schedule) {
|
|
|
- return [
|
|
|
- 'id' => $schedule->id,
|
|
|
- 'date' => Carbon::parse($schedule->date)->format('Y-m-d'),
|
|
|
- 'start_time' => $schedule->start_time,
|
|
|
- 'end_time' => $schedule->end_time,
|
|
|
- 'status' => $schedule->status,
|
|
|
- ];
|
|
|
- })->values();
|
|
|
-
|
|
|
- $fullyBlockedWeeks = $schedules
|
|
|
- ->groupBy(function ($schedule) {
|
|
|
- return Carbon::parse($schedule->date)
|
|
|
- ->startOfWeek(Carbon::SUNDAY)
|
|
|
- ->format('Y-m-d');
|
|
|
- })
|
|
|
- ->filter(function ($weekSchedules) {
|
|
|
- return $weekSchedules->count() >= 2;
|
|
|
- })
|
|
|
- ->keys()
|
|
|
- ->values();
|
|
|
-
|
|
|
- return [
|
|
|
- 'existing_schedules' => $existingSchedules,
|
|
|
- 'fully_blocked_weeks' => $fullyBlockedWeeks,
|
|
|
- ];
|
|
|
+ $weekStart = Carbon::today()->startOfWeek(Carbon::SUNDAY)->format('Y-m-d');
|
|
|
+
|
|
|
+ $schedules = Schedule::where('client_id', $clientId)
|
|
|
+ ->where('provider_id', $providerId)
|
|
|
+ ->whereNotIn('status', self::EXCLUDED_STATUSES)
|
|
|
+ ->whereDate('date', '>=', $weekStart)
|
|
|
+ ->orderBy('date')
|
|
|
+ ->orderBy('start_time')
|
|
|
+ ->get(['id', 'date', 'start_time', 'end_time', 'status']);
|
|
|
+
|
|
|
+ $existingSchedules = $schedules->map(function ($schedule) {
|
|
|
+ return [
|
|
|
+ 'id' => $schedule->id,
|
|
|
+ 'date' => Carbon::parse($schedule->date)->format('Y-m-d'),
|
|
|
+ 'start_time' => $schedule->start_time,
|
|
|
+ 'end_time' => $schedule->end_time,
|
|
|
+ 'status' => $schedule->status,
|
|
|
+ ];
|
|
|
+ })->values();
|
|
|
+
|
|
|
+ $fullyBlockedWeeks = $schedules
|
|
|
+ ->groupBy(function ($schedule) {
|
|
|
+ return Carbon::parse($schedule->date)
|
|
|
+ ->startOfWeek(Carbon::SUNDAY)
|
|
|
+ ->format('Y-m-d');
|
|
|
+ })
|
|
|
+ ->filter(function ($weekSchedules) {
|
|
|
+ return $weekSchedules->count() >= 2;
|
|
|
+ })
|
|
|
+ ->keys()
|
|
|
+ ->values();
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'existing_schedules' => $existingSchedules,
|
|
|
+ 'fully_blocked_weeks' => $fullyBlockedWeeks,
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
public function getFinished()
|
|
|
@@ -811,6 +836,52 @@ class ScheduleService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public function sendScheduleStartingSoonPushes(Schedule $schedule): void
|
|
|
+ {
|
|
|
+ $pushNotificationService = app(PushNotificationService::class);
|
|
|
+
|
|
|
+ $clientUser = $schedule->client?->user;
|
|
|
+ $providerUser = $schedule->provider?->user;
|
|
|
+
|
|
|
+ if ($clientUser) {
|
|
|
+ try {
|
|
|
+ $pushNotificationService->sendToUser(
|
|
|
+ $clientUser,
|
|
|
+ new AgendamentoProximoPrestadorPush(
|
|
|
+ $providerUser?->name ?? 'Prestador'
|
|
|
+ )
|
|
|
+ );
|
|
|
+ } catch (\Throwable $exception) {
|
|
|
+ Log::error('Falha ao enviar push de agendamento próximo para o cliente', [
|
|
|
+ 'schedule_id' => $schedule->id,
|
|
|
+ 'user_id' => $clientUser->id,
|
|
|
+ 'error' => $exception->getMessage(),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // PUSH PARA O PRESTADOR
|
|
|
+ if ($providerUser) {
|
|
|
+ try {
|
|
|
+ $pushNotificationService->sendToUser(
|
|
|
+ $providerUser,
|
|
|
+ new AgendamentoProximoClientePush(
|
|
|
+ $clientUser?->name ?? 'Cliente'
|
|
|
+ )
|
|
|
+ );
|
|
|
+ } catch (\Throwable $exception) {
|
|
|
+ Log::error('Falha ao enviar push de agendamento próximo para o prestador', [
|
|
|
+ 'schedule_id' => $schedule->id,
|
|
|
+ 'user_id' => $providerUser->id,
|
|
|
+ 'error' => $exception->getMessage(),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //dq pra cima e as notificações
|
|
|
+
|
|
|
private function calculateAmount(Provider $provider, string $periodType): float
|
|
|
{
|
|
|
$hourlyRates = [
|