Skip to content

Commit f4e9726

Browse files
committed
households
1 parent c03ba4e commit f4e9726

3 files changed

Lines changed: 35 additions & 6 deletions

File tree

app/Filament/Forms/Components/Household.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ protected function setUp(): void
7373
'name',
7474
fn (Builder $query, Get $get) => $query
7575
->where('household_id', $get('household_id'))
76-
->limit(100)
7776
)
77+
->optionsLimit(100)
7878
->preload()
7979
->createOptionModalHeading(__('family.action.create'))
8080
->createOptionForm(function (Get $get) {
@@ -103,14 +103,17 @@ protected function setUp(): void
103103

104104
return $family->getKey();
105105
}),
106-
107106
]),
108107
]);
109108
}
110109

111110
protected function getHouseholds(): Collection
112111
{
113112
return Cache::driver('array')
114-
->remember('households', MINUTE_IN_SECONDS, fn () => HouseholdModel::pluck('name', 'id'));
113+
->remember(
114+
'households-' . auth()->id(),
115+
MINUTE_IN_SECONDS,
116+
fn (): Collection => HouseholdModel::pluck('name', 'id')
117+
);
115118
}
116119
}

app/Filament/Resources/Households/Schemas/HouseholdForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public static function configure(Schema $schema): Schema
2222
->maxLength(200)
2323
->required(),
2424

25+
// TODO: refactor
2526
Hidden::make('nurse_id')
26-
->default(fn () => auth()->id())
27-
->required(),
27+
->default(fn () => auth()->user()?->isNurse() ? auth()->id() : null),
2828

2929
Repeater::make('families')
3030
->label(__('family.label.plural'))

app/Models/Household.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Models;
66

77
use App\Concerns\BelongsToNurse;
8+
use Illuminate\Database\Eloquent\Builder;
89
use Illuminate\Database\Eloquent\Factories\HasFactory;
910
use Illuminate\Database\Eloquent\Model;
1011
use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -22,6 +23,22 @@ class Household extends Model
2223
'name',
2324
];
2425

26+
protected static function booted(): void
27+
{
28+
static::addGlobalScope('forCurrentUser', function (Builder $builder): void {
29+
if (! auth()->check()) {
30+
return;
31+
}
32+
if (auth()->user()->isNurse()) {
33+
$builder->forNurse(auth()->user());
34+
}
35+
36+
if (auth()->user()->isMediator()) {
37+
$builder->forMediator(auth()->user());
38+
}
39+
});
40+
}
41+
2542
public function families(): HasMany
2643
{
2744
return $this->hasMany(Family::class);
@@ -40,9 +57,18 @@ public function getActivitylogOptions(): LogOptions
4057
->logOnlyDirty();
4158
}
4259

60+
public function scopeForMediator(Builder $query, User $user): Builder
61+
{
62+
return $query->whereRelation('beneficiaries', 'mediator_id', $user->id);
63+
}
64+
4365
public static function createForCurrentNurse(array $data): self
4466
{
45-
$data['nurse_id'] = auth()->id();
67+
// A mediator's id must never be stored in nurse_id; mediator-owned
68+
// households are reached through their member beneficiaries instead.
69+
if (auth()->user()?->isNurse()) {
70+
$data['nurse_id'] = auth()->id();
71+
}
4672

4773
return self::create($data);
4874
}

0 commit comments

Comments
 (0)