mirror of
https://github.com/HolgerHatGarKeineNode/einundzwanzig-app.git
synced 2026-05-18 20:34:52 +00:00
🚀 **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:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Facades\Cookie;
|
||||
use Illuminate\Support\Facades\Date;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Spatie\Image\Enums\Fit;
|
||||
use Spatie\MediaLibrary\HasMedia;
|
||||
@@ -166,4 +167,37 @@ class Meetup extends Model implements HasMedia
|
||||
{
|
||||
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,10 +3,13 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\RecurrenceType;
|
||||
use App\Observers\MeetupEventObserver;
|
||||
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[ObservedBy([MeetupEventObserver::class])]
|
||||
class MeetupEvent extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user