🚀 **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;
use App\Models\Meetup;
use App\Models\MeetupEvent;
use Carbon\CarbonInterface;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Date;
#[Signature('meetups:update-activity')]
#[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
{
$threshold = now()->subYear();
Meetup::query()->chunkById(200, function ($meetups) use ($threshold) {
Meetup::query()->chunkById(200, function ($meetups) {
foreach ($meetups as $meetup) {
$this->updateMeetup($meetup, $threshold);
$meetup->recalculateActivity();
}
});
@@ -28,37 +23,4 @@ class UpdateMeetupActivity extends Command
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();
}
}