complere story 11.1 with qa tests
This commit is contained in:
@@ -16,6 +16,9 @@ class Consultation extends Model
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'guest_name',
|
||||
'guest_email',
|
||||
'guest_phone',
|
||||
'booking_date',
|
||||
'booking_time',
|
||||
'problem_summary',
|
||||
@@ -44,6 +47,22 @@ class Consultation extends Model
|
||||
];
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function (Consultation $consultation) {
|
||||
// Either user_id or guest fields must be present
|
||||
if (is_null($consultation->user_id)) {
|
||||
if (empty($consultation->guest_name) ||
|
||||
empty($consultation->guest_email) ||
|
||||
empty($consultation->guest_phone)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Guest consultations require guest_name, guest_email, and guest_phone'
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user that owns the consultation.
|
||||
*/
|
||||
@@ -52,6 +71,44 @@ class Consultation extends Model
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this is a guest consultation (no linked user).
|
||||
*/
|
||||
public function isGuest(): bool
|
||||
{
|
||||
return is_null($this->user_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the client's display name.
|
||||
*/
|
||||
public function getClientName(): string
|
||||
{
|
||||
return $this->isGuest()
|
||||
? $this->guest_name
|
||||
: $this->user->full_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the client's email address.
|
||||
*/
|
||||
public function getClientEmail(): string
|
||||
{
|
||||
return $this->isGuest()
|
||||
? $this->guest_email
|
||||
: $this->user->email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the client's phone number.
|
||||
*/
|
||||
public function getClientPhone(): ?string
|
||||
{
|
||||
return $this->isGuest()
|
||||
? $this->guest_phone
|
||||
: $this->user->phone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark consultation as completed.
|
||||
*
|
||||
@@ -205,4 +262,20 @@ class Consultation extends Model
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope for guest consultations (no linked user).
|
||||
*/
|
||||
public function scopeGuests(Builder $query): Builder
|
||||
{
|
||||
return $query->whereNull('user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope for client consultations (has linked user).
|
||||
*/
|
||||
public function scopeClients(Builder $query): Builder
|
||||
{
|
||||
return $query->whereNotNull('user_id');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user