nyavokevin 56b0c50111 feat(auth): add employee user linking and password setup flow
Add user management endpoints and link employees to existing users
through `user_id`, including API resources, validation, repository
support, and database migrations.

Introduce a two-step login flow that checks email first and lets users
without a password create one before signing in.

Update the employee detail UI with a dedicated user tab and refresh the
employee and intervention side navigation to support the new account
management flow.
2026-04-08 13:31:57 +03:00

93 lines
2.1 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Employee extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'first_name',
'last_name',
'user_id',
'email',
'phone',
'job_title',
'hire_date',
'active',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'active' => 'boolean',
'hire_date' => 'date',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
/**
* Get the thanatopractitioner associated with the employee.
*/
public function thanatopractitioner(): HasOne
{
return $this->hasOne(Thanatopractitioner::class);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* Get the full name of the employee.
*/
public function getFullNameAttribute(): string
{
return $this->first_name . ' ' . $this->last_name;
}
/**
* Scope a query to only include active employees.
*/
public function scopeActive($query)
{
return $query->where('active', true);
}
/**
* Scope a query to only include inactive employees.
*/
public function scopeInactive($query)
{
return $query->where('active', false);
}
/**
* Scope a query to search employees.
*/
public function scopeSearch($query, string $term)
{
return $query->where(function ($q) use ($term) {
$q->where('first_name', 'like', '%' . $term . '%')
->orWhere('last_name', 'like', '%' . $term . '%')
->orWhere('email', 'like', '%' . $term . '%')
->orWhere('job_title', 'like', '%' . $term . '%');
});
}
}