Skip to content

Commit 93305df

Browse files
authored
refactor: compile vulnerabilities list after catagraphy save (#431)
1 parent 6bfc481 commit 93305df

8 files changed

Lines changed: 39 additions & 15 deletions

File tree

app/DataTransferObjects/VulnerabilityListItem.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public function __construct(
1010
public string $label,
1111
public string | int | null $value,
1212
public string | null $type = null,
13+
public string | null $category = null,
1314
public bool $valid = false,
1415
) {
1516
//

app/Filament/Resources/Beneficiaries/Resources/Catagraphies/Concerns/GetRecordFromParentRecord.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
namespace App\Filament\Resources\Beneficiaries\Resources\Catagraphies\Concerns;
66

77
use App\Models\Catagraphy;
8+
use Override;
89

910
trait GetRecordFromParentRecord
1011
{
12+
#[Override]
1113
public function mount(int | string | null $record = null): void
1214
{
1315
$this->record = $this->getRecord();
@@ -19,8 +21,15 @@ public function mount(int | string | null $record = null): void
1921
}
2022
}
2123

24+
#[Override]
2225
public function getRecord(): Catagraphy
2326
{
2427
return $this->getParentRecord()->catagraphy;
2528
}
29+
30+
#[Override]
31+
public function hasRecord(): bool
32+
{
33+
return filled($this->getRecord());
34+
}
2635
}

app/Filament/Resources/Beneficiaries/Resources/Catagraphies/Pages/EditCatagraphy.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected function handleRecordUpdate(Model $record, array $data): Model
4747
{
4848
$data['nurse_id'] = auth()->id();
4949
$data['beneficiary_id'] = $this->getParentRecord()->id;
50-
// dd($data);
50+
5151
// Handle pregnancy data
5252
if (! Vulnerability::isPregnancy($data['cat_rep'])) {
5353
$data['cat_preg'] = null;
@@ -73,6 +73,31 @@ protected function handleRecordUpdate(Model $record, array $data): Model
7373
$record->diseases->each->delete();
7474
}
7575

76+
// Fix issues with inconsistent calling of $this->record
77+
// and $this->getRecord() in the parent class.
78+
$this->record = $record;
79+
7680
return $record;
7781
}
82+
83+
protected function afterSave()
84+
{
85+
$catagraphy = $this->getRecord();
86+
87+
$allVulnerabilities = $catagraphy->all_vulnerabilities_items;
88+
89+
$properties = collect($allVulnerabilities->pluck('value'))
90+
->concat($allVulnerabilities->pluck('category'))
91+
->filter()
92+
->unique()
93+
->sort()
94+
->values();
95+
96+
activity('vulnerabilities')
97+
->causedBy(auth()->user())
98+
->performedOn($catagraphy->beneficiary)
99+
->withProperties($properties)
100+
->event('updated')
101+
->log('updated');
102+
}
78103
}

app/Models/Catagraphy.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,6 @@ public function tapActivity(Activity $activity, string $eventName)
9191
$beneficiary = $activity->subject->beneficiary;
9292

9393
$activity->beneficiary()->associate($beneficiary);
94-
95-
once(
96-
fn () => activity('vulnerabilities')
97-
->causedBy($activity->causer)
98-
->performedOn($beneficiary)
99-
->withProperties($this->all_vulnerabilities_items->pluck('value'))
100-
->event($eventName)
101-
->tap(function (Activity $activity) use ($beneficiary) {
102-
$activity->beneficiary()->associate($beneficiary);
103-
})
104-
->log($eventName)
105-
);
10694
}
10795

10896
public function nurse(): BelongsTo

app/Models/Disability.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public function vulnerabilityListItem(): VulnerabilityListItem
9696
return new VulnerabilityListItem(
9797
label: "{$type->name}: {$vulnerability->name}",
9898
value: $vulnerability->id,
99+
category: $type?->id,
99100
type: $this->getMorphClass(),
100101
valid: true
101102
);

app/Models/Disease.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public function vulnerabilityListItem(): VulnerabilityListItem
110110
return new VulnerabilityListItem(
111111
label: "{$vulnerability->name}: {$category?->name}",
112112
value: $vulnerability->id,
113+
category: $category?->id,
113114
type: $this->getMorphClass(),
114115
valid: true
115116
);

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"spatie/laravel-activitylog": "^4.11",
2727
"spatie/laravel-csp": "^3.22",
2828
"spatie/laravel-onboard": "^2.6",
29-
"spatie/once": "^3.1",
3029
"staudenmeir/belongs-to-through": "^2.16",
3130
"staudenmeir/laravel-merged-relations": "^1.9",
3231
"stevegrunwell/time-constants": "^1.2",

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)