Skip to content

Commit

Permalink
Purge empty campaigns + fix magical campaign_id
Browse files Browse the repository at this point in the history
  • Loading branch information
ilestis committed Jun 29, 2023
1 parent e2f7559 commit c8f286b
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 8 deletions.
48 changes: 48 additions & 0 deletions app/Console/Commands/CleanupCampaigns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Console\Commands;

use App\Services\Campaign\PurgeService;
use Carbon\Carbon;
use Illuminate\Console\Command;

class CleanupCampaigns extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'campaigns:cleanup {dry=1}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete empty campaigns';

/**
* Execute the console command.
*/
public function handle()
{
$this->info(Carbon::now());

/** @var PurgeService $service */
$service = app()->make(PurgeService::class);

$dry = $this->argument('dry');
if ($dry === '0') {
$service->real();
}

$count = $service->purgeEmpty();

if ($dry === '0') {
$this->info(Carbon::now() . ': Deleted ' . $count . ' empty campaigns.');
} else {
$this->info(Carbon::now() . ': There are ' . $count . ' empty campaigns that can be deleted.');
}
}
}
1 change: 1 addition & 0 deletions app/Models/Ability.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Ability extends MiscModel

/** @var string[] */
protected $fillable = [
'campaign_id',
'name',
'slug',
'type',
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Family.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
*/
class Family extends MiscModel
{
use Acl
;
use Acl;
use CampaignTrait;
use ExportableTrait;
use Nested;
Expand All @@ -38,6 +37,7 @@ class Family extends MiscModel

/** @var string[] */
protected $fillable = [
'campaign_id',
'name',
'slug',
'entry',
Expand Down
1 change: 1 addition & 0 deletions app/Models/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Map extends MiscModel

/** @var string[] */
protected $fillable = [
'campaign_id',
'name',
'slug',
'type',
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
*/
class Note extends MiscModel
{
use Acl
;
use Acl;
use CampaignTrait;
use ExportableTrait;
use Nested;
Expand All @@ -31,6 +30,7 @@ class Note extends MiscModel

/** @var string[] */
protected $fillable = [
'campaign_id',
'name',
'slug',
'entry',
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Organisation.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
*/
class Organisation extends MiscModel
{
use Acl
;
use Acl;
use CampaignTrait;
use ExportableTrait;
use Nested;
Expand All @@ -39,6 +38,7 @@ class Organisation extends MiscModel

/** @var string[] */
protected $fillable = [
'campaign_id',
'name',
'slug',
'entry',
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Timeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ class Timeline extends MiscModel
use Acl;
use CampaignTrait;
use ExportableTrait;
use Nested
;
use Nested;
use SoftDeletes;
use SortableTrait;

public $fillable = [
'campaign_id',
'name',
'type',
'calendar_id',
Expand Down
50 changes: 50 additions & 0 deletions app/Services/Campaign/PurgeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Services\Campaign;

use App\Models\Campaign;
use App\Services\ImageService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Log;

class PurgeService
{
protected int $count = 0;
protected bool $dry = true;

public function real(): self
{
$this->dry = false;
return $this;
}

public function count(): int
{
return $this->baseAll()
->count();
}

public function purgeEmpty(): int
{
$this->baseAll()
->chunk(500, function ($campaigns) {
/** @var Campaign $campaign */
foreach ($campaigns as $campaign) {
if (!$this->dry) {
ImageService::cleanup($campaign);
$campaign->delete();
Log::info('Services\Campaigns\PurgeService', ['campaign' => $campaign->id]);
}
$this->count++;
}
});
return $this->count;
}

protected function baseAll(): Builder
{
return Campaign::select('campaigns.id')
->leftJoin('campaign_user as cu', 'cu.campaign_id', 'campaigns.id')
->whereNull('cu.id');
}
}

0 comments on commit c8f286b

Please sign in to comment.