Allow explicit multi-branch practitioner assignment for telemedicine.
Deploy Ladill Care / deploy (push) Successful in 1m29s

Replace optional “All branches” with required branch checkboxes; doctors may switch only among assigned sites, never org-wide by default.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
isaacclad
2026-07-18 08:50:03 +00:00
co-authored by Cursor
parent d643687335
commit 899b08179e
17 changed files with 478 additions and 103 deletions
+38
View File
@@ -5,6 +5,7 @@ namespace App\Models;
use App\Models\Concerns\BelongsToOwner;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
@@ -34,6 +35,15 @@ class Practitioner extends Model
return $this->belongsTo(Branch::class, 'branch_id');
}
/**
* Explicitly assigned branches (telemedicine may include several). Empty = unassigned.
*/
public function branches(): BelongsToMany
{
return $this->belongsToMany(Branch::class, 'care_practitioner_branch', 'practitioner_id', 'branch_id')
->withTimestamps();
}
public function department(): BelongsTo
{
return $this->belongsTo(Department::class, 'department_id');
@@ -53,4 +63,32 @@ class Practitioner extends Model
{
return $this->hasMany(Appointment::class, 'practitioner_id');
}
/**
* @return list<int>
*/
public function assignedBranchIds(): array
{
$ids = $this->relationLoaded('branches')
? $this->branches->pluck('id')->all()
: $this->branches()->pluck('care_branches.id')->all();
if ($ids === [] && $this->branch_id) {
return [(int) $this->branch_id];
}
return array_values(array_unique(array_map('intval', $ids)));
}
/**
* @param list<int|string> $branchIds
*/
public function syncAssignedBranches(array $branchIds): void
{
$ids = array_values(array_unique(array_filter(array_map('intval', $branchIds))));
$this->branches()->sync($ids);
$this->forceFill([
'branch_id' => $ids[0] ?? null,
])->save();
}
}