Skip to content

Commit

Permalink
Merge pull request #767 from owlchester/features/import
Browse files Browse the repository at this point in the history
Campaign Import
  • Loading branch information
ilestis authored Nov 18, 2023
2 parents 0378156 + 33d0d88 commit 9afd97f
Show file tree
Hide file tree
Showing 88 changed files with 3,418 additions and 84 deletions.
48 changes: 48 additions & 0 deletions app/Console/Commands/Campaigns/ImportCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Console\Commands\Campaigns;

use App\Enums\CampaignImportStatus;
use App\Jobs\Campaigns\Import;
use App\Models\Campaign;
use App\Models\CampaignImport;
use Illuminate\Console\Command;

class ImportCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'campaigns:import {campaign}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Re-run an import job';

/**
* Execute the console command.
*/
public function handle()
{
$campaignID = $this->argument('campaign');
$campaign = Campaign::find($campaignID);

$job = CampaignImport::where('campaign_id', $campaign->id)->orderBy('created_at', 'DESC')->
where('status_id', '<>', 1)->first();
if (!$job) {
$this->info('No job for campaign ' . $campaign->id);
return;
}

$job->status_id = CampaignImportStatus::QUEUED;
$job->saveQuietly();

Import::dispatch($job);
$this->info('Re-queued campaign ' . $campaign->id);
}
}
12 changes: 12 additions & 0 deletions app/Enums/CampaignImportStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Enums;

enum CampaignImportStatus: int
{
case PREPARED = 1;
case QUEUED = 2;
case RUNNING = 3;
case FINISHED = 4;
case FAILED = 5;
}
25 changes: 25 additions & 0 deletions app/Facades/ImportIdMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

/**
* Class QuestCache
* @package App\Facades
*
* @see \App\Services\Campaign\Import\ImportIdMapper
* @mixin \App\Services\Campaign\Import\ImportIdMapper
*/
class ImportIdMapper extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'importidmapper';
}
}
59 changes: 59 additions & 0 deletions app/Http/Controllers/Campaign/ImportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace App\Http\Controllers\Campaign;

use App\Enums\CampaignImportStatus;
use App\Facades\Datagrid;
use App\Http\Controllers\Controller;
use App\Models\Campaign;
use App\Services\Campaign\Import\PrepareService;

class ImportController extends Controller
{
protected PrepareService $service;
public function __construct(PrepareService $prepareService)
{
$this->middleware('auth');
$this->service = $prepareService;
}

public function index(Campaign $campaign)
{
$this->authorize('setting', $campaign);


Datagrid::layout(\App\Renderers\Layouts\Campaign\CampaignImport::class);

$rows = $campaign->campaignImports()
->sort(request()->only(['o', 'k']))
->where('status_id', '<>', CampaignImportStatus::PREPARED)
->with(['user'])
->orderBy('updated_at', 'DESC')
->paginate();

// Ajax Datagrid
if (request()->ajax()) {
$html = view('layouts.datagrid._table')->with('rows', $rows)->render();
return response()->json([
'success' => true,
'html' => $html,
]);
}

$token = null;
if (auth()->user()->isSubscriber()) {
$token = $this->service
->campaign($campaign)
->user(auth()->user())
->token();
}



return view('campaigns.import.index')
->with('campaign', $campaign)
->with('token', $token)
->with('rows', $rows)
;
}
}
3 changes: 2 additions & 1 deletion app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ public function unmentioned(Campaign $campaign, $id)
]);
}

// @phpstan-ignore-next-line
$entities = \App\Models\Entity::unmentioned()
->inTags($widget->tags->pluck('id')->toArray())
->type($widget->conf('entity'))
->with(['updater'])
->type($widget->conf('entity'))
->paginate(10);

return view('dashboard.widgets._recent_list')
Expand Down
76 changes: 76 additions & 0 deletions app/Jobs/Campaigns/Import.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Jobs\Campaigns;

use App\Enums\CampaignImportStatus;
use App\Models\CampaignImport;
use App\Services\Campaign\Import\ImportService;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Throwable;

class Import implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 1;

protected int $jobID;

/**
* CampaignExport constructor.
*/
public function __construct(CampaignImport $campaignImport)
{
$this->jobID = $campaignImport->id;
}

/**
* Execute the job
* @throws Exception
*/
public function handle()
{
Log::info('Campaign import', ['init', 'id' => $this->jobID]);
/** @var CampaignImport $job */
$job = CampaignImport::find($this->jobID);
if (!$job) {
Log::info('Campaign import', ['empty', 'id' => $this->jobID]);
return 0;
}
if (!$job->campaign || !$job->user) {
Log::info('Campaign import', ['empty_campaign_or_user', 'id' => $this->jobID]);
return 0;
}
Log::info('Campaign import', ['running', 'id' => $this->jobID]);
$job->update(['status_id' => CampaignImportStatus::RUNNING]);

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

return 1;
}

/**
*
*/
public function failed(Throwable $exception)
{
}
}
5 changes: 5 additions & 0 deletions app/Models/Ability.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class Ability extends MiscModel
*/
protected string $entityType = 'ability';

protected array $exportFields = [
'base',
'charges'
];

/**
* Parent ID used for the Node Trait
* @return string
Expand Down
14 changes: 14 additions & 0 deletions app/Models/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,18 @@ public function listRangeText(): string
{
return implode(', ', $this->listRange);
}

public function exportFields(): array
{
return [
'id',
'type_id',
'name',
'value',
'is_private',
'default_order',
'is_pinned',
'is_hidden',
];
}
}
4 changes: 4 additions & 0 deletions app/Models/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class Calendar extends MiscModel
'parameters' => 'array'
];

protected array $foreignExport = [
'calendarWeather',
];

protected array $loadedMonths;

protected array $loadedWeekdays;
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ public function thumbnail(int $width = 400, int $height = null, string $field =
public function exportable(): bool
{
if (!app()->isProduction()) {
return $this->queuedCampaignExports->count() === 0;
return true; //$this->queuedCampaignExports->count() === 0;
}

return empty($this->export_date) || !$this->export_date->isToday() && $this->queuedCampaignExports->count() === 0;
Expand Down
80 changes: 80 additions & 0 deletions app/Models/CampaignImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace App\Models;

use App\Enums\CampaignImportStatus;
use App\Models\Concerns\SortableTrait;
use App\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Storage;

/**
* @property int $id
* @property CampaignImportStatus $status_id
* @property Campaign $campaign
* @property User $user
*/
class CampaignImport extends Model
{
use Prunable;
use SortableTrait;

public $fillable = [
'user_id',
'campaign_id',
'status_id'
];

public $casts = [
'config' => 'array',
'status_id' => CampaignImportStatus::class,
];

public $sortable = [
'status_id',
'updated_at',
'created_by',
];

public function campaign()
{
return $this->belongsTo(Campaign::class);
}

/**
* Automatically prune old elements from the db
*/
public function prunable(): Builder
{
return static::/*where('updated_at', '<=', now()->subDays(1))
->*/whereIn('status_id', [CampaignImportStatus::PREPARED, CampaignImportStatus::QUEUED]);
}

public function user()
{
return $this->belongsTo(User::class);
}

public function isPrepared(): bool
{
return $this->status_id == CampaignImportStatus::PREPARED;
}

public function isFailed(): bool
{
return $this->status_id == CampaignImportStatus::FAILED;
}
protected function pruning(): void
{
$files = Arr::get($this->config, 'files');
if (empty($files)) {
return;
}
foreach ($files as $file) {
Storage::disk('s3')->delete($file);
}
}
}
12 changes: 11 additions & 1 deletion app/Models/Character.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Character extends MiscModel
* Foreign relations to add to export
*/
protected array $foreignExport = [
'characterTraits', 'families', 'races'
'characterTraits', 'characterFamilies', 'characterRaces', 'organisationMemberships'
];

/**
Expand Down Expand Up @@ -222,6 +222,16 @@ public function families()
->with('entity');
}

public function characterFamilies()
{
return $this->hasMany(CharacterFamily::class, 'character_id');
}

public function characterRaces()
{
return $this->hasMany(CharacterRace::class, 'character_id');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
Expand Down
Loading

0 comments on commit 9afd97f

Please sign in to comment.