complete story 3.7 with qa test

This commit is contained in:
Naser Mansour
2025-12-26 20:05:03 +02:00
parent 7af029e1af
commit 6254d54fe9
32 changed files with 2882 additions and 45 deletions
+8
View File
@@ -6,4 +6,12 @@ enum ConsultationType: string
{
case Free = 'free';
case Paid = 'paid';
public function label(): string
{
return match ($this) {
self::Free => __('enums.consultation_type.free'),
self::Paid => __('enums.consultation_type.paid'),
};
}
}
+9
View File
@@ -7,4 +7,13 @@ enum PaymentStatus: string
case Pending = 'pending';
case Received = 'received';
case NotApplicable = 'na';
public function label(): string
{
return match ($this) {
self::Pending => __('enums.payment_status.pending'),
self::Received => __('enums.payment_status.received'),
self::NotApplicable => __('enums.payment_status.na'),
};
}
}
+142
View File
@@ -5,6 +5,7 @@ namespace App\Models;
use App\Enums\ConsultationStatus;
use App\Enums\ConsultationType;
use App\Enums\PaymentStatus;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -21,6 +22,7 @@ class Consultation extends Model
'consultation_type',
'payment_amount',
'payment_status',
'payment_received_at',
'status',
'admin_notes',
];
@@ -33,6 +35,8 @@ class Consultation extends Model
'payment_status' => PaymentStatus::class,
'status' => ConsultationStatus::class,
'payment_amount' => 'decimal:2',
'payment_received_at' => 'datetime',
'admin_notes' => 'array',
];
}
@@ -43,4 +47,142 @@ class Consultation extends Model
{
return $this->belongsTo(User::class);
}
/**
* Mark consultation as completed.
*
* @throws \InvalidArgumentException
*/
public function markAsCompleted(): void
{
if ($this->status !== ConsultationStatus::Approved) {
throw new \InvalidArgumentException(
__('messages.invalid_status_transition', ['from' => $this->status->value, 'to' => 'completed'])
);
}
$this->update(['status' => ConsultationStatus::Completed]);
}
/**
* Mark consultation as no-show.
*
* @throws \InvalidArgumentException
*/
public function markAsNoShow(): void
{
if ($this->status !== ConsultationStatus::Approved) {
throw new \InvalidArgumentException(
__('messages.invalid_status_transition', ['from' => $this->status->value, 'to' => 'no_show'])
);
}
$this->update(['status' => ConsultationStatus::NoShow]);
}
/**
* Cancel the consultation.
*
* @throws \InvalidArgumentException
*/
public function cancel(): void
{
if (! in_array($this->status, [ConsultationStatus::Pending, ConsultationStatus::Approved])) {
throw new \InvalidArgumentException(
__('messages.cannot_cancel_consultation')
);
}
$this->update(['status' => ConsultationStatus::Cancelled]);
}
/**
* Mark payment as received.
*
* @throws \InvalidArgumentException
*/
public function markPaymentReceived(): void
{
if ($this->consultation_type !== ConsultationType::Paid) {
throw new \InvalidArgumentException(__('messages.not_paid_consultation'));
}
if ($this->payment_status === PaymentStatus::Received) {
throw new \InvalidArgumentException(__('messages.payment_already_received'));
}
$this->update([
'payment_status' => PaymentStatus::Received,
'payment_received_at' => now(),
]);
}
/**
* Reschedule the consultation to a new date and time.
*/
public function reschedule(string $newDate, string $newTime): void
{
$this->update([
'booking_date' => $newDate,
'booking_time' => $newTime,
]);
}
/**
* Add an admin note to the consultation.
*/
public function addNote(string $note, int $adminId): void
{
$notes = $this->admin_notes ?? [];
$notes[] = [
'text' => $note,
'admin_id' => $adminId,
'created_at' => now()->toISOString(),
];
$this->update(['admin_notes' => $notes]);
}
/**
* Update an admin note at the given index.
*/
public function updateNote(int $index, string $newText): void
{
$notes = $this->admin_notes ?? [];
if (isset($notes[$index])) {
$notes[$index]['text'] = $newText;
$notes[$index]['updated_at'] = now()->toISOString();
$this->update(['admin_notes' => $notes]);
}
}
/**
* Delete an admin note at the given index.
*/
public function deleteNote(int $index): void
{
$notes = $this->admin_notes ?? [];
if (isset($notes[$index])) {
array_splice($notes, $index, 1);
$this->update(['admin_notes' => array_values($notes)]);
}
}
/**
* Scope for upcoming approved consultations.
*/
public function scopeUpcoming(Builder $query): Builder
{
return $query->where('booking_date', '>=', today())
->where('status', ConsultationStatus::Approved);
}
/**
* Scope for past consultations.
*/
public function scopePast(Builder $query): Builder
{
return $query->where(function ($q) {
$q->where('booking_date', '<', today())
->orWhereIn('status', [
ConsultationStatus::Completed,
ConsultationStatus::Cancelled,
ConsultationStatus::NoShow,
]);
});
}
}
@@ -0,0 +1,70 @@
<?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 ConsultationCancelled 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.consultation-cancelled', [
'consultation' => $this->consultation,
'locale' => $locale,
'user' => $notifiable,
]);
}
/**
* Get the subject based on locale.
*/
private function getSubject(string $locale): string
{
return $locale === 'ar'
? 'تم إلغاء موعد استشارتك'
: 'Your Consultation Has Been Cancelled';
}
/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
'type' => 'consultation_cancelled',
'consultation_id' => $this->consultation->id,
];
}
}
@@ -0,0 +1,89 @@
<?php
namespace App\Notifications;
use App\Models\Consultation;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class ConsultationRescheduled extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*/
public function __construct(
public Consultation $consultation,
public Carbon $oldDate,
public string $oldTime,
public string $icsContent
) {}
/**
* 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';
$message = (new MailMessage)
->subject($this->getSubject($locale))
->view('emails.consultation-rescheduled', [
'consultation' => $this->consultation,
'oldDate' => $this->oldDate,
'oldTime' => $this->oldTime,
'locale' => $locale,
'user' => $notifiable,
]);
// Attach new .ics file
if (! empty($this->icsContent)) {
$message->attachData(
$this->icsContent,
'consultation.ics',
['mime' => 'text/calendar']
);
}
return $message;
}
/**
* Get the subject based on locale.
*/
private function getSubject(string $locale): string
{
return $locale === 'ar'
? 'تم إعادة جدولة موعد استشارتك'
: 'Your Consultation Has Been Rescheduled';
}
/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
'type' => 'consultation_rescheduled',
'consultation_id' => $this->consultation->id,
'old_date' => $this->oldDate->toDateString(),
'old_time' => $this->oldTime,
];
}
}