Skip to content

Commit db9c314

Browse files
authored
hotfix: filter out projects with invalid dates (#406)
* fix: apply HasValidDates scope to Project and BCRProject queries * chore: rename for readability fix
1 parent 3e8f3f5 commit db9c314

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

app/Http/Controllers/HomeController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function index()
2929

3030
'projects' => ProjectCardResource::collection(
3131
Project::whereIsOpen()
32+
->whereHasValidDates('start','end')
3233
->latest()
3334
->limit(12)
3435
->get()
@@ -37,6 +38,7 @@ public function index()
3738
'bcr_projects' => BCRProjectCardResource::collection(
3839
BCRProject::query()
3940
->where('status', ProjectStatus::approved)
41+
->whereHasValidDates()
4042
->latest()
4143
->with('county')
4244
->limit(12)

app/Http/Controllers/ProjectController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ protected function projectList(Request $request, string $view): Response
6262
AllowedSort::custom('donations_count', new ProjectDonationsCountSort),
6363
])
6464
->defaultSort('-id')
65-
->whereIsPublished();
65+
->whereIsPublished()
66+
->whereHasValidDates('start', 'end');
6667

6768
return Inertia::render('Public/Projects/Index', [
6869
'view' => $view,

app/Models/BCRProject.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Concerns\HasSlug;
99
use App\Enums\ProjectStatus;
1010
use App\Traits\HasProjectStatus;
11+
use App\Traits\HasValidDates;
1112
use Embed\Embed;
1213
use Illuminate\Database\Eloquent\Builder;
1314
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -23,6 +24,7 @@ class BCRProject extends Model implements HasMedia
2324
{
2425
use HasFactory;
2526
use HasLocation;
27+
use HasValidDates;
2628
use InteractsWithMedia;
2729
use HasProjectStatus;
2830
use HasSlug;
@@ -90,12 +92,12 @@ public function getEmbeddedVideosAttribute(): array
9092
{
9193
return collect($this->videos)
9294
->pluck('link')
93-
->filter(fn ($video) => ! blank($video))
95+
->filter(fn($video) => ! blank($video))
9496
->map(
95-
fn (string $videoUrl) => Cache::remember(
97+
fn(string $videoUrl) => Cache::remember(
9698
'video-' . hash('sha256', $videoUrl),
9799
MONTH_IN_SECONDS,
98-
fn () => rescue(fn () => (new Embed)->get($videoUrl)->code, '', false)
100+
fn() => rescue(fn() => (new Embed)->get($videoUrl)->code, '', false)
99101
)
100102
)
101103
->filter()

app/Models/Project.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use App\Enums\EuPlatescStatus;
1212
use App\Enums\ProjectStatus;
1313
use App\Traits\HasProjectStatus;
14+
use App\Traits\HasValidDates;
1415
use Embed\Embed;
1516
use Illuminate\Database\Eloquent\Builder;
1617
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -39,6 +40,7 @@ class Project extends Model implements HasMedia
3940
use HasVolunteers;
4041
use InteractsWithMedia;
4142
use HasProjectStatus;
43+
use HasValidDates;
4244
use HasSlug;
4345
use LogsActivity;
4446
use LogsActivityForApproval;
@@ -192,14 +194,14 @@ public function getIsDraftAttribute(): bool
192194

193195
public function getIsPeriodActiveAttribute(): bool
194196
{
195-
return $this->start->isPast() && $this->end->isFuture();
197+
return $this->start?->isPast() && $this->end?->isFuture();
196198
}
197199

198200
public function getIsActiveAttribute(): bool
199201
{
200202
return $this->status == ProjectStatus::approved
201-
&& $this->start->isPast()
202-
&& $this->end->isFuture();
203+
&& $this->start?->isPast()
204+
&& $this->end?->isFuture();
203205
}
204206

205207
public function getCanBeArchivedAttribute(): bool
@@ -249,7 +251,7 @@ public function markAsApproved(): bool
249251
$slug .= '-' . ($count + 1);
250252
}
251253

252-
$this->activities->map(fn (Activity $activity) => $activity->approve());
254+
$this->activities->map(fn(Activity $activity) => $activity->approve());
253255

254256
return $this->update([
255257
'status' => ProjectStatus::approved,
@@ -280,10 +282,10 @@ public function getEmbeddedVideosAttribute(): array
280282
->pluck('url')
281283
->filter()
282284
->map(
283-
fn (string $videoUrl) => Cache::remember(
285+
fn(string $videoUrl) => Cache::remember(
284286
'video-' . hash('sha256', $videoUrl),
285287
MONTH_IN_SECONDS,
286-
fn () => rescue(fn () => (new Embed)->get($videoUrl)->code, report: false)
288+
fn() => rescue(fn() => (new Embed)->get($videoUrl)->code, report: false)
287289
)
288290
)
289291
->filter()

app/Traits/HasValidDates.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Traits;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
7+
trait HasValidDates
8+
{
9+
public function scopeWhereHasValidDates(Builder $query, string $startColumn = 'start_date', string $endColumn = 'end_date'): Builder
10+
{
11+
return $query
12+
->whereNotNull($startColumn)
13+
->whereNotNull($endColumn)
14+
->whereColumn($startColumn, '<=', $endColumn);
15+
}
16+
}

0 commit comments

Comments
 (0)