🚀 **Automate Meetup Activity Recalculation**

- Introduced `recalculateActivity` method in `Meetup` model to centralize activity and event timestamp updates.
- Added `MeetupEventObserver` to trigger activity recalculation on event save/delete.
- Updated `/meetups:update-activity` command to leverage the new model method for cleanup.
- Enhanced tests to cover various `MeetupEvent` scenarios affecting activity states.
This commit is contained in:
HolgerHatGarKeineNode
2026-05-17 18:13:37 +02:00
parent 71a4898303
commit 308cd8a611
5 changed files with 104 additions and 42 deletions
@@ -3,12 +3,9 @@
namespace App\Console\Commands\Database; namespace App\Console\Commands\Database;
use App\Models\Meetup; use App\Models\Meetup;
use App\Models\MeetupEvent;
use Carbon\CarbonInterface;
use Illuminate\Console\Attributes\Description; use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature; use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\Date;
#[Signature('meetups:update-activity')] #[Signature('meetups:update-activity')]
#[Description('Recalculate is_active and last_event_at for every meetup based on its events.')] #[Description('Recalculate is_active and last_event_at for every meetup based on its events.')]
@@ -16,11 +13,9 @@ class UpdateMeetupActivity extends Command
{ {
public function handle(): int public function handle(): int
{ {
$threshold = now()->subYear(); Meetup::query()->chunkById(200, function ($meetups) {
Meetup::query()->chunkById(200, function ($meetups) use ($threshold) {
foreach ($meetups as $meetup) { foreach ($meetups as $meetup) {
$this->updateMeetup($meetup, $threshold); $meetup->recalculateActivity();
} }
}); });
@@ -28,37 +23,4 @@ class UpdateMeetupActivity extends Command
return Command::SUCCESS; return Command::SUCCESS;
} }
private function updateMeetup(Meetup $meetup, CarbonInterface $threshold): void
{
$lastEventAt = MeetupEvent::query()
->where('meetup_id', $meetup->id)
->where('start', '<=', now())
->max('start');
$lastEventAt = $lastEventAt ? Date::parse($lastEventAt) : null;
$hasFutureEvent = MeetupEvent::query()
->where('meetup_id', $meetup->id)
->where('start', '>', now())
->exists();
$hasActiveRecurrence = MeetupEvent::query()
->where('meetup_id', $meetup->id)
->whereNotNull('recurrence_type')
->where(function ($query) {
$query->whereNull('recurrence_end_date')
->orWhere('recurrence_end_date', '>=', now());
})
->exists();
$isActive = ($lastEventAt && $lastEventAt->greaterThanOrEqualTo($threshold))
|| $hasFutureEvent
|| $hasActiveRecurrence;
$meetup->forceFill([
'is_active' => $isActive,
'last_event_at' => $lastEventAt,
])->saveQuietly();
}
} }
+34
View File
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Cookie; use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Spatie\Image\Enums\Fit; use Spatie\Image\Enums\Fit;
use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\HasMedia;
@@ -166,4 +167,37 @@ class Meetup extends Model implements HasMedia
{ {
return $this->hasMany(MeetupEvent::class); return $this->hasMany(MeetupEvent::class);
} }
public function recalculateActivity(): void
{
$threshold = now()->subYear();
$lastEventAt = MeetupEvent::query()
->where('meetup_id', $this->id)
->where('start', '<=', now())
->max('start');
$lastEventAt = $lastEventAt ? Date::parse($lastEventAt) : null;
$hasFutureEvent = MeetupEvent::query()
->where('meetup_id', $this->id)
->where('start', '>', now())
->exists();
$hasActiveRecurrence = MeetupEvent::query()
->where('meetup_id', $this->id)
->whereNotNull('recurrence_type')
->whereNotNull('recurrence_end_date')
->where('recurrence_end_date', '>=', now())
->exists();
$isActive = ($lastEventAt && $lastEventAt->greaterThanOrEqualTo($threshold))
|| $hasFutureEvent
|| $hasActiveRecurrence;
$this->forceFill([
'is_active' => $isActive,
'last_event_at' => $lastEventAt,
])->saveQuietly();
}
} }
+3
View File
@@ -3,10 +3,13 @@
namespace App\Models; namespace App\Models;
use App\Enums\RecurrenceType; use App\Enums\RecurrenceType;
use App\Observers\MeetupEventObserver;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
#[ObservedBy([MeetupEventObserver::class])]
class MeetupEvent extends Model class MeetupEvent extends Model
{ {
use HasFactory; use HasFactory;
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace App\Observers;
use App\Models\MeetupEvent;
class MeetupEventObserver
{
public function saved(MeetupEvent $meetupEvent): void
{
$meetupEvent->meetup?->recalculateActivity();
}
public function deleted(MeetupEvent $meetupEvent): void
{
$meetupEvent->meetup?->recalculateActivity();
}
}
@@ -49,8 +49,8 @@ it('keeps a meetup active when it only has a future event', function () {
->and($meetup->last_event_at)->toBeNull(); ->and($meetup->last_event_at)->toBeNull();
}); });
it('keeps a meetup active when a recurring event has no end date', function () { it('marks a meetup as inactive when a recurring event has no end date but is older than a year', function () {
$meetup = Meetup::factory()->create(['is_active' => false, 'last_event_at' => null]); $meetup = Meetup::factory()->create(['is_active' => true, 'last_event_at' => now()]);
MeetupEvent::factory()->create([ MeetupEvent::factory()->create([
'meetup_id' => $meetup->id, 'meetup_id' => $meetup->id,
'start' => now()->subYears(3), 'start' => now()->subYears(3),
@@ -60,6 +60,21 @@ it('keeps a meetup active when a recurring event has no end date', function () {
$this->artisan('meetups:update-activity')->assertSuccessful(); $this->artisan('meetups:update-activity')->assertSuccessful();
$meetup->refresh();
expect($meetup->is_active)->toBeFalse();
});
it('keeps a meetup active when a recurring event has an end date in the future', function () {
$meetup = Meetup::factory()->create(['is_active' => false, 'last_event_at' => null]);
MeetupEvent::factory()->create([
'meetup_id' => $meetup->id,
'start' => now()->subYears(3),
'recurrence_type' => RecurrenceType::Monthly,
'recurrence_end_date' => now()->addMonths(6),
]);
$this->artisan('meetups:update-activity')->assertSuccessful();
$meetup->refresh(); $meetup->refresh();
expect($meetup->is_active)->toBeTrue(); expect($meetup->is_active)->toBeTrue();
}); });
@@ -74,6 +89,36 @@ it('marks a meetup as inactive when no events exist at all', function () {
->and($meetup->last_event_at)->toBeNull(); ->and($meetup->last_event_at)->toBeNull();
}); });
it('flips a meetup from inactive to active immediately when a future event is created', function () {
$meetup = Meetup::factory()->create(['is_active' => false, 'last_event_at' => null]);
MeetupEvent::factory()->create([
'meetup_id' => $meetup->id,
'start' => now()->addDays(7),
'recurrence_type' => null,
]);
$meetup->refresh();
expect($meetup->is_active)->toBeTrue();
});
it('flips a meetup back to inactive when its only future event is deleted', function () {
$meetup = Meetup::factory()->create(['is_active' => false, 'last_event_at' => null]);
$event = MeetupEvent::factory()->create([
'meetup_id' => $meetup->id,
'start' => now()->addDays(7),
'recurrence_type' => null,
]);
$meetup->refresh();
expect($meetup->is_active)->toBeTrue();
$event->delete();
$meetup->refresh();
expect($meetup->is_active)->toBeFalse();
});
it('marks a meetup as inactive when a recurring event has ended more than a year ago', function () { it('marks a meetup as inactive when a recurring event has ended more than a year ago', function () {
$meetup = Meetup::factory()->create(['is_active' => true, 'last_event_at' => now()]); $meetup = Meetup::factory()->create(['is_active' => true, 'last_event_at' => now()]);
MeetupEvent::factory()->create([ MeetupEvent::factory()->create([