-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #767 from owlchester/features/import
Campaign Import
- Loading branch information
Showing
88 changed files
with
3,418 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.