|
@@ -6,6 +6,8 @@ use App\Jobs\StartScheduleJob;
|
|
|
use App\Models\Provider;
|
|
use App\Models\Provider;
|
|
|
use App\Models\Schedule;
|
|
use App\Models\Schedule;
|
|
|
use App\Rules\ScheduleBusinessRules;
|
|
use App\Rules\ScheduleBusinessRules;
|
|
|
|
|
+use App\Services\NotificationService;
|
|
|
|
|
+use App\Enums\NotificationTypeEnum;
|
|
|
use Carbon\Carbon;
|
|
use Carbon\Carbon;
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\DB;
|
|
@@ -41,22 +43,54 @@ class ScheduleService
|
|
|
public function createSingleOrMultiple(array $baseData, array $schedules)
|
|
public function createSingleOrMultiple(array $baseData, array $schedules)
|
|
|
{
|
|
{
|
|
|
try {
|
|
try {
|
|
|
|
|
+
|
|
|
DB::beginTransaction();
|
|
DB::beginTransaction();
|
|
|
|
|
+
|
|
|
$createdSchedules = [];
|
|
$createdSchedules = [];
|
|
|
|
|
|
|
|
|
|
+
|
|
|
foreach ($schedules as $schedule) {
|
|
foreach ($schedules as $schedule) {
|
|
|
|
|
+
|
|
|
$datasMerged = array_merge($baseData, $schedule);
|
|
$datasMerged = array_merge($baseData, $schedule);
|
|
|
|
|
|
|
|
|
|
+
|
|
|
$this->validateProviderAvailability($datasMerged, null);
|
|
$this->validateProviderAvailability($datasMerged, null);
|
|
|
|
|
|
|
|
|
|
+
|
|
|
$scheduleData = array_merge($datasMerged, [
|
|
$scheduleData = array_merge($datasMerged, [
|
|
|
'code' => str_pad(random_int(0, 9999), 4, '0', STR_PAD_LEFT),
|
|
'code' => str_pad(random_int(0, 9999), 4, '0', STR_PAD_LEFT),
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
- $createdSchedules[] = Schedule::create($scheduleData);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ $newSchedule = Schedule::create($scheduleData);
|
|
|
|
|
+
|
|
|
|
|
+ /*NOTIFICAÇÃO PRESTADOR*/
|
|
|
|
|
+ if ($newSchedule->provider_id) {
|
|
|
|
|
+
|
|
|
|
|
+ $notificationService = app(NotificationService::class);
|
|
|
|
|
+
|
|
|
|
|
+ $notificationService->create([
|
|
|
|
|
+ 'title' => 'Nova solicitação de diária!',
|
|
|
|
|
+
|
|
|
|
|
+ 'description' =>
|
|
|
|
|
+ 'Você recebeu uma nova solicitação de diária.',
|
|
|
|
|
+
|
|
|
|
|
+ 'origin' => 'schedule',
|
|
|
|
|
+
|
|
|
|
|
+ 'origin_id' => $newSchedule->id,
|
|
|
|
|
+
|
|
|
|
|
+ 'type' => NotificationTypeEnum::SCHEDULE_PROVIDER_CLIENT_NEW_SOLICITATION->value,
|
|
|
|
|
+
|
|
|
|
|
+ 'user_id' => $newSchedule->provider->user_id,
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $createdSchedules[] = $newSchedule;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
DB::commit();
|
|
DB::commit();
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
|
|
+
|
|
|
DB::rollBack();
|
|
DB::rollBack();
|
|
|
|
|
|
|
|
throw new \Exception(__($e->getMessage()));
|
|
throw new \Exception(__($e->getMessage()));
|
|
@@ -270,26 +304,189 @@ class ScheduleService
|
|
|
$schedule->update(['status' => $status]);
|
|
$schedule->update(['status' => $status]);
|
|
|
|
|
|
|
|
switch ($status) {
|
|
switch ($status) {
|
|
|
|
|
+
|
|
|
case 'pending':
|
|
case 'pending':
|
|
|
|
|
+
|
|
|
break;
|
|
break;
|
|
|
|
|
+
|
|
|
case 'accepted':
|
|
case 'accepted':
|
|
|
|
|
+
|
|
|
|
|
+ $notificationService = app(NotificationService::class);
|
|
|
|
|
+
|
|
|
|
|
+ // CLIENTE
|
|
|
|
|
+ $notificationService->create([
|
|
|
|
|
+ 'title' => 'Agendamento aceito!',
|
|
|
|
|
+
|
|
|
|
|
+ 'description' =>
|
|
|
|
|
+ $schedule->provider->user->name .
|
|
|
|
|
+ ' aceitou sua solicitação de diária.',
|
|
|
|
|
+
|
|
|
|
|
+ 'origin' => 'schedule',
|
|
|
|
|
+
|
|
|
|
|
+ 'origin_id' => $schedule->id,
|
|
|
|
|
+
|
|
|
|
|
+ 'type' => NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_ACCEPTED->value,
|
|
|
|
|
+
|
|
|
|
|
+ 'user_id' => $schedule->client->user_id,
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
break;
|
|
break;
|
|
|
|
|
+
|
|
|
case 'rejected':
|
|
case 'rejected':
|
|
|
|
|
+
|
|
|
|
|
+ $notificationService = app(NotificationService::class);
|
|
|
|
|
+
|
|
|
|
|
+ // CLIENTE
|
|
|
|
|
+ $notificationService->create([
|
|
|
|
|
+ 'title' => 'Agendamento recusado!',
|
|
|
|
|
+
|
|
|
|
|
+ 'description' =>
|
|
|
|
|
+ 'O diarista não poderá atender. Veja outros profissionais disponíveis.',
|
|
|
|
|
+
|
|
|
|
|
+ 'origin' => 'schedule',
|
|
|
|
|
+
|
|
|
|
|
+ 'origin_id' => $schedule->id,
|
|
|
|
|
+
|
|
|
|
|
+ 'type' => NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_REFUSED->value,
|
|
|
|
|
+
|
|
|
|
|
+ 'user_id' => $schedule->client->user_id,
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
break;
|
|
break;
|
|
|
|
|
+
|
|
|
case 'paid':
|
|
case 'paid':
|
|
|
- $date_cleaned = Carbon::parse($schedule->date)->format('Y-m-d');
|
|
|
|
|
- $date_time_dispatch = Carbon::parse($date_cleaned.' '.$schedule->start_time);
|
|
|
|
|
|
|
|
|
|
- // StartScheduleJob::dispatch($schedule->id)->delay($date_time_dispatch);
|
|
|
|
|
|
|
+ $notificationService = app(NotificationService::class);
|
|
|
|
|
+
|
|
|
|
|
+ // PRESTADOR
|
|
|
|
|
+ if ($schedule->provider_id) {
|
|
|
|
|
+
|
|
|
|
|
+ $notificationService->create([
|
|
|
|
|
+ 'title' => 'Pagamento confirmado!',
|
|
|
|
|
+
|
|
|
|
|
+ 'description' =>
|
|
|
|
|
+ 'O cliente confirmou o pagamento da diária.',
|
|
|
|
|
+
|
|
|
|
|
+ 'origin' => 'schedule',
|
|
|
|
|
+
|
|
|
|
|
+ 'origin_id' => $schedule->id,
|
|
|
|
|
+
|
|
|
|
|
+ 'type' => NotificationTypeEnum::SCHEDULE_PROVIDER_START->value,
|
|
|
|
|
+
|
|
|
|
|
+ 'user_id' => $schedule->provider->user_id,
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $date_cleaned = Carbon::parse($schedule->date)
|
|
|
|
|
+ ->format('Y-m-d');
|
|
|
|
|
+
|
|
|
|
|
+ $date_time_dispatch = Carbon::parse(
|
|
|
|
|
+ $date_cleaned . ' ' . $schedule->start_time
|
|
|
|
|
+ )->subHour();
|
|
|
|
|
+
|
|
|
|
|
+ StartScheduleJob::dispatch($schedule->id)
|
|
|
|
|
+ ->delay($date_time_dispatch);
|
|
|
|
|
|
|
|
- // dispatch de teste em local
|
|
|
|
|
- StartScheduleJob::dispatch($schedule->id)->delay(now()->addSeconds(15));
|
|
|
|
|
break;
|
|
break;
|
|
|
|
|
+
|
|
|
case 'cancelled':
|
|
case 'cancelled':
|
|
|
|
|
+
|
|
|
|
|
+ $notificationService = app(NotificationService::class);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ switch (Auth::user()->type) {
|
|
|
|
|
+ case 'client':
|
|
|
|
|
+
|
|
|
|
|
+ // PRESTADOR
|
|
|
|
|
+ if ($schedule->provider_id) {
|
|
|
|
|
+
|
|
|
|
|
+ $notificationService->create(['title' => 'Agendamento cancelado!',
|
|
|
|
|
+
|
|
|
|
|
+ 'description' =>'O cliente cancelou a diária.',
|
|
|
|
|
+
|
|
|
|
|
+ 'origin' => 'schedule',
|
|
|
|
|
+
|
|
|
|
|
+ 'origin_id' => $schedule->id,
|
|
|
|
|
+
|
|
|
|
|
+ 'type' => NotificationTypeEnum::SCHEDULE_PROVIDER_CLIENT_CANCELLED->value,
|
|
|
|
|
+
|
|
|
|
|
+ 'user_id' => $schedule->provider->user_id,
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 'provider':
|
|
|
|
|
+
|
|
|
|
|
+ // CLIENTE
|
|
|
|
|
+ $notificationService->create([
|
|
|
|
|
+ 'title' => 'Agendamento cancelado!',
|
|
|
|
|
+
|
|
|
|
|
+ 'description' =>
|
|
|
|
|
+ $schedule->provider->user->name .
|
|
|
|
|
+ ' cancelou a diária.',
|
|
|
|
|
+
|
|
|
|
|
+ 'origin' => 'schedule',
|
|
|
|
|
+
|
|
|
|
|
+ 'origin_id' => $schedule->id,
|
|
|
|
|
+
|
|
|
|
|
+ 'type' => NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_CANCELLED->value,
|
|
|
|
|
+
|
|
|
|
|
+ 'user_id' => $schedule->client->user_id,
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ default:
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
break;
|
|
break;
|
|
|
|
|
+
|
|
|
case 'started':
|
|
case 'started':
|
|
|
|
|
+
|
|
|
|
|
+ $notificationService = app(NotificationService::class);
|
|
|
|
|
+
|
|
|
|
|
+ // CLIENTE
|
|
|
|
|
+ $notificationService->create([
|
|
|
|
|
+ 'title' => 'Diarista a caminho!',
|
|
|
|
|
+
|
|
|
|
|
+ 'description' =>
|
|
|
|
|
+ 'Informe o código ' .
|
|
|
|
|
+ $schedule->code .
|
|
|
|
|
+ ' para confirmar a chegada da diarista e liberar o início do serviço.',
|
|
|
|
|
+
|
|
|
|
|
+ 'origin' => 'schedule',
|
|
|
|
|
+
|
|
|
|
|
+ 'origin_id' => $schedule->id,
|
|
|
|
|
+
|
|
|
|
|
+ 'type' => NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_COMING->value,
|
|
|
|
|
+
|
|
|
|
|
+ 'user_id' => $schedule->client->user_id,
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
break;
|
|
break;
|
|
|
|
|
+
|
|
|
case 'finished':
|
|
case 'finished':
|
|
|
|
|
+
|
|
|
|
|
+ $notificationService = app(NotificationService::class);
|
|
|
|
|
+
|
|
|
|
|
+ // CLIENTE
|
|
|
|
|
+ $notificationService->create([
|
|
|
|
|
+ 'title' => 'Diária finalizada!',
|
|
|
|
|
+
|
|
|
|
|
+ 'description' =>
|
|
|
|
|
+ 'Avalie o serviço feito pelo diarista e conte-nos como foi sua experiência.',
|
|
|
|
|
+
|
|
|
|
|
+ 'origin' => 'schedule',
|
|
|
|
|
+
|
|
|
|
|
+ 'origin_id' => $schedule->id,
|
|
|
|
|
+
|
|
|
|
|
+ 'type' => NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_FINISHED->value,
|
|
|
|
|
+
|
|
|
|
|
+ 'user_id' => $schedule->client->user_id,
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -298,7 +495,7 @@ class ScheduleService
|
|
|
return $schedule->fresh(['client.user', 'provider.user', 'address']);
|
|
return $schedule->fresh(['client.user', 'provider.user', 'address']);
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
DB::rollBack();
|
|
DB::rollBack();
|
|
|
- Log::error('Erro ao atualizar status do agendamento: '.$e->getMessage());
|
|
|
|
|
|
|
+ Log::error('Erro ao atualizar status do agendamento: ' . $e->getMessage());
|
|
|
throw new \Exception('Não foi possível atualizar o status do agendamento.');
|
|
throw new \Exception('Não foi possível atualizar o status do agendamento.');
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -330,7 +527,7 @@ class ScheduleService
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
DB::rollBack();
|
|
DB::rollBack();
|
|
|
|
|
|
|
|
- Log::error('Erro ao cancelar agendamento: '.$e->getMessage());
|
|
|
|
|
|
|
+ Log::error('Erro ao cancelar agendamento: ' . $e->getMessage());
|
|
|
|
|
|
|
|
throw new \Exception('Não foi possível cancelar o agendamento.');
|
|
throw new \Exception('Não foi possível cancelar o agendamento.');
|
|
|
}
|
|
}
|