complete 3.8 with qa test

This commit is contained in:
Naser Mansour
2025-12-26 20:17:40 +02:00
parent 6254d54fe9
commit e593beacfc
14 changed files with 864 additions and 28 deletions
@@ -0,0 +1,73 @@
<?php
namespace App\Console\Commands;
use App\Enums\ConsultationStatus;
use App\Models\Consultation;
use App\Notifications\ConsultationReminder24h;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class Send24HourReminders extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'reminders:send-24h';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send consultation reminders 24 hours before appointment';
/**
* Execute the console command.
*/
public function handle(): int
{
$targetTime = now()->addHours(24);
$windowStart = $targetTime->copy()->subMinutes(30);
$windowEnd = $targetTime->copy()->addMinutes(30);
$consultations = Consultation::query()
->where('status', ConsultationStatus::Approved)
->whereNull('reminder_24h_sent_at')
->whereDate('booking_date', $targetTime->toDateString())
->get()
->filter(function ($consultation) use ($windowStart, $windowEnd) {
$consultationDateTime = Carbon::parse(
$consultation->booking_date->format('Y-m-d').' '.
$consultation->booking_time
);
return $consultationDateTime->between($windowStart, $windowEnd);
});
$count = 0;
foreach ($consultations as $consultation) {
try {
$consultation->user->notify(new ConsultationReminder24h($consultation));
$consultation->update(['reminder_24h_sent_at' => now()]);
$count++;
$this->info("Sent 24h reminder for consultation #{$consultation->id}");
} catch (\Exception $e) {
$this->error("Failed to send reminder for consultation #{$consultation->id}: {$e->getMessage()}");
Log::error('24h reminder failed', [
'consultation_id' => $consultation->id,
'error' => $e->getMessage(),
]);
}
}
$this->info("Sent {$count} 24-hour reminders");
return Command::SUCCESS;
}
}
@@ -0,0 +1,73 @@
<?php
namespace App\Console\Commands;
use App\Enums\ConsultationStatus;
use App\Models\Consultation;
use App\Notifications\ConsultationReminder2h;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class Send2HourReminders extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'reminders:send-2h';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send consultation reminders 2 hours before appointment';
/**
* Execute the console command.
*/
public function handle(): int
{
$targetTime = now()->addHours(2);
$windowStart = $targetTime->copy()->subMinutes(15);
$windowEnd = $targetTime->copy()->addMinutes(15);
$consultations = Consultation::query()
->where('status', ConsultationStatus::Approved)
->whereNull('reminder_2h_sent_at')
->whereDate('booking_date', $targetTime->toDateString())
->get()
->filter(function ($consultation) use ($windowStart, $windowEnd) {
$consultationDateTime = Carbon::parse(
$consultation->booking_date->format('Y-m-d').' '.
$consultation->booking_time
);
return $consultationDateTime->between($windowStart, $windowEnd);
});
$count = 0;
foreach ($consultations as $consultation) {
try {
$consultation->user->notify(new ConsultationReminder2h($consultation));
$consultation->update(['reminder_2h_sent_at' => now()]);
$count++;
$this->info("Sent 2h reminder for consultation #{$consultation->id}");
} catch (\Exception $e) {
$this->error("Failed to send reminder for consultation #{$consultation->id}: {$e->getMessage()}");
Log::error('2h reminder failed', [
'consultation_id' => $consultation->id,
'error' => $e->getMessage(),
]);
}
}
$this->info("Sent {$count} 2-hour reminders");
return Command::SUCCESS;
}
}
+4
View File
@@ -25,6 +25,8 @@ class Consultation extends Model
'payment_received_at',
'status',
'admin_notes',
'reminder_24h_sent_at',
'reminder_2h_sent_at',
];
protected function casts(): array
@@ -37,6 +39,8 @@ class Consultation extends Model
'payment_amount' => 'decimal:2',
'payment_received_at' => 'datetime',
'admin_notes' => 'array',
'reminder_24h_sent_at' => 'datetime',
'reminder_2h_sent_at' => 'datetime',
];
}
@@ -0,0 +1,80 @@
<?php
namespace App\Notifications;
use App\Models\Consultation;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class ConsultationReminder24h extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*/
public function __construct(
public Consultation $consultation
) {}
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
$locale = $notifiable->preferred_language ?? 'ar';
return (new MailMessage)
->subject($this->getSubject($locale))
->view('emails.reminder-24h', [
'consultation' => $this->consultation,
'locale' => $locale,
'user' => $notifiable,
'showPaymentReminder' => $this->shouldShowPaymentReminder(),
]);
}
/**
* Get the subject based on locale.
*/
private function getSubject(string $locale): string
{
return $locale === 'ar'
? 'تذكير: موعدك غداً مع مكتب ليبرا للمحاماة'
: 'Reminder: Your consultation is tomorrow';
}
/**
* Check if payment reminder should be shown.
*/
private function shouldShowPaymentReminder(): bool
{
return $this->consultation->consultation_type->value === 'paid' &&
$this->consultation->payment_status->value === 'pending';
}
/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
'type' => 'consultation_reminder_24h',
'consultation_id' => $this->consultation->id,
];
}
}
@@ -0,0 +1,80 @@
<?php
namespace App\Notifications;
use App\Models\Consultation;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class ConsultationReminder2h extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*/
public function __construct(
public Consultation $consultation
) {}
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
$locale = $notifiable->preferred_language ?? 'ar';
return (new MailMessage)
->subject($this->getSubject($locale))
->view('emails.reminder-2h', [
'consultation' => $this->consultation,
'locale' => $locale,
'user' => $notifiable,
'showPaymentReminder' => $this->shouldShowPaymentReminder(),
]);
}
/**
* Get the subject based on locale.
*/
private function getSubject(string $locale): string
{
return $locale === 'ar'
? 'تذكير: موعدك خلال ساعتين'
: 'Reminder: Your consultation is in 2 hours';
}
/**
* Check if payment reminder should be shown.
*/
private function shouldShowPaymentReminder(): bool
{
return $this->consultation->consultation_type->value === 'paid' &&
$this->consultation->payment_status->value === 'pending';
}
/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
'type' => 'consultation_reminder_2h',
'consultation_id' => $this->consultation->id,
];
}
}