Commit 309600cd by Vladislav

#19466 Остановка РК

1 parent 1359384c
...@@ -42,12 +42,12 @@ class CampaignsAdd extends Command ...@@ -42,12 +42,12 @@ class CampaignsAdd extends Command
*/ */
public function handle() public function handle()
{ {
$tokens = Tokens::whereHas('dictionaryCampaignsForNotExternal') $tokens = Tokens::whereHas('dictionaryCampaignsEnabledForNotExternal')
->with([ ->with([
'dictionaryCampaignsForNotExternal' => function (HasManyThrough $query) { 'dictionaryCampaignsEnabledForNotExternal' => function (HasManyThrough $query) {
return $query->limit(10); return $query->limit(10);
}, },
'dictionaryCampaignsForNotExternal.campaign' 'dictionaryCampaignsEnabledForNotExternal.campaign'
])->where('type', '!=', Tokens::MAIN) ])->where('type', '!=', Tokens::MAIN)
->get(); ->get();
...@@ -57,7 +57,7 @@ class CampaignsAdd extends Command ...@@ -57,7 +57,7 @@ class CampaignsAdd extends Command
$factory->getRequest('campaigns', 'add') $factory->getRequest('campaigns', 'add')
->call([ ->call([
'dictionaryCampaigns' => $token->dictionaryCampaignsForNotExternal, 'dictionaryCampaigns' => $token->dictionaryCampaignsEnabledForNotExternal,
'variables' => Variable::all(), 'variables' => Variable::all(),
]); ]);
} }
......
...@@ -11,6 +11,7 @@ use App\Service\Direct\CheckDictionaries; ...@@ -11,6 +11,7 @@ use App\Service\Direct\CheckDictionaries;
use App\Service\Direct\GetCampaigns; use App\Service\Direct\GetCampaigns;
use App\Service\Requests\APIRequest; use App\Service\Requests\APIRequest;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Support\Facades\Bus; use Illuminate\Support\Facades\Bus;
class CampaignsLoadUpdated extends Command class CampaignsLoadUpdated extends Command
...@@ -70,16 +71,21 @@ class CampaignsLoadUpdated extends Command ...@@ -70,16 +71,21 @@ class CampaignsLoadUpdated extends Command
}) })
]); ]);
$tokens = Tokens::has('dictionaryCampaignsForExternalSynchronizedUpdatedSelf') $tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf')
->with('dictionaryCampaignsForExternalSynchronizedUpdatedSelf') ->with([
->where('type', '!=', Tokens::MAIN)->get(); 'dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf' => function (HasManyThrough $query) {
return $query->limit(10);
},
])
->where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) { foreach ($tokens as $token) {
$factory = APIRequest::getInstance(API::YANDEX); $factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token); $factory->setToken($token);
$factory->getRequest('change', 'checkCampaigns')->call([ $factory->getRequest('change', 'checkCampaigns')->call([
'ids' => $token->dictionaryCampaignsForExternalSynchronizedUpdatedSelf->map(function (DictionaryCampaign $dictionaryCampaign) { 'ids' => $token->dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf->map(function (DictionaryCampaign $dictionaryCampaign) {
return $dictionaryCampaign->external_id; return $dictionaryCampaign->external_id;
}) })
]); ]);
......
<?php
namespace App\Console\Commands;
use App\Models\Tokens;
use App\Models\Variable;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
class CampaignsResume extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'campaigns:resume';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Останавливает показы на уровне кампании';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$token_main = Tokens::whereHas('campaignsEnabledDisabled')
->with([
'campaignsEnabledDisabled' => function (HasMany $query) {
return $query->limit(1000);
},
])->where('type', Tokens::MAIN)->first();
if ($token_main){
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token_main);
$factory->getRequest('campaigns', 'resume')
->call([
'ids' => $token_main->campaignsEnabledDisabled->pluck('external_id'),
]);
}
$tokens = Tokens::whereHas('dictionaryCampaignsEnabledForExternalDisabled')
->with([
'dictionaryCampaignsEnabledForExternalDisabled' => function (HasManyThrough $query) {
return $query->limit(1000);
},
])->where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('campaigns', 'resume')
->call([
'ids' => $token->dictionaryCampaignsEnabledForExternalDisabled->pluck('external_id'),
]);
}
return 0;
}
}
<?php
namespace App\Console\Commands;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
class CampaignsSuspend extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'campaigns:suspend';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Останавливает показы на уровне кампании';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$token_main = Tokens::whereHas('campaignsNotEnabledNotDisabled')
->with([
'campaignsNotEnabledNotDisabled' => function (HasMany $query) {
return $query->limit(1000);
},
])->where('type', Tokens::MAIN)->first();
if ($token_main){
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token_main);
$factory->getRequest('campaigns', 'suspend')
->call([
'ids' => $token_main->campaignsNotEnabledNotDisabled->pluck('external_id'),
]);
}
$tokens = Tokens::whereHas('dictionaryCampaignsNotEnabledForExternalNotDisabled')
->with([
'dictionaryCampaignsNotEnabledForExternalNotDisabled' => function (HasManyThrough $query) {
return $query->limit(1000);
},
])->where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('campaigns', 'suspend')
->call([
'ids' => $token->dictionaryCampaignsNotEnabledForExternalNotDisabled->pluck('external_id'),
]);
}
return 0;
}
}
...@@ -42,12 +42,12 @@ class CampaignsUpdate extends Command ...@@ -42,12 +42,12 @@ class CampaignsUpdate extends Command
*/ */
public function handle() public function handle()
{ {
$tokens = Tokens::whereHas('dictionaryCampaignsForExternalNeedUpdated') $tokens = Tokens::whereHas('dictionaryCampaignsEnabledForExternalNeedUpdated')
->with([ ->with([
'dictionaryCampaignsForExternalNeedUpdated' => function (HasManyThrough $query) { 'dictionaryCampaignsEnabledForExternalNeedUpdated' => function (HasManyThrough $query) {
return $query->limit(10); return $query->limit(10);
}, },
'dictionaryCampaignsForExternalNeedUpdated.campaign' 'dictionaryCampaignsEnabledForExternalNeedUpdated.campaign'
])->where('type', '!=', Tokens::MAIN) ])->where('type', '!=', Tokens::MAIN)
->get(); ->get();
...@@ -57,7 +57,7 @@ class CampaignsUpdate extends Command ...@@ -57,7 +57,7 @@ class CampaignsUpdate extends Command
$factory->getRequest('campaigns', 'update') $factory->getRequest('campaigns', 'update')
->call([ ->call([
'dictionaryCampaigns' => $token->dictionaryCampaignsForExternalNeedUpdated, 'dictionaryCampaigns' => $token->dictionaryCampaignsEnabledForExternalNeedUpdated,
'variables' => Variable::all(), 'variables' => Variable::all(),
]); ]);
} }
......
...@@ -201,7 +201,7 @@ class TokensController extends Controller ...@@ -201,7 +201,7 @@ class TokensController extends Controller
'updated' => !!request('updated'), 'updated' => !!request('updated'),
]); ]);
return Redirect::route('token.edit', $token->getKey())->with('success', 'Campaign updated.'); return Redirect::route('token.edit', $token->getKey())->with('success', 'City campaign updated.');
} }
public function syncedCityCampaign(Tokens $token, Dictionary $dictionary, $campaign_id) public function syncedCityCampaign(Tokens $token, Dictionary $dictionary, $campaign_id)
...@@ -220,7 +220,26 @@ class TokensController extends Controller ...@@ -220,7 +220,26 @@ class TokensController extends Controller
'synced' => !!request('synced'), 'synced' => !!request('synced'),
]); ]);
return Redirect::route('token.edit', $token->getKey())->with('success', 'City synced.'); return Redirect::route('token.edit', $token->getKey())->with('success', 'City campaign synced.');
}
public function enabledCityCampaign(Tokens $token, Dictionary $dictionary, $campaign_id)
{
if ($token->isMain()) {
return Redirect::back();
}
$campaign = $dictionary->campaigns()->find($campaign_id);
if (!$campaign) {
return Redirect::back();
}
$campaign->pivot->update([
'enabled' => !!request('enabled'),
]);
return Redirect::route('token.edit', $token->getKey())->with('success', 'City campaign ' . ($campaign->pivot->enabled ? 'enabled' : 'disabled') . '.');
} }
public function destroyCity(Tokens $token, Dictionary $dictionary) public function destroyCity(Tokens $token, Dictionary $dictionary)
......
...@@ -48,6 +48,8 @@ use Illuminate\Support\Collection; ...@@ -48,6 +48,8 @@ use Illuminate\Support\Collection;
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns forGroupsLoadable() * @method static \Illuminate\Database\Eloquent\Builder|Campaigns forGroupsLoadable()
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns forManaged($value = true) * @method static \Illuminate\Database\Eloquent\Builder|Campaigns forManaged($value = true)
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns forEnabled($value = true) * @method static \Illuminate\Database\Eloquent\Builder|Campaigns forEnabled($value = true)
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns notDisabled()
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns disabled()
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns forUpdatedSelf() * @method static \Illuminate\Database\Eloquent\Builder|Campaigns forUpdatedSelf()
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns forUpdatedChildren() * @method static \Illuminate\Database\Eloquent\Builder|Campaigns forUpdatedChildren()
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Campaigns newModelQuery()
...@@ -104,6 +106,7 @@ class Campaigns extends Model ...@@ -104,6 +106,7 @@ class Campaigns extends Model
'updated_children', 'updated_children',
'manage', 'manage',
'enabled', 'enabled',
'disabled_at',
]; ];
protected $casts = [ protected $casts = [
...@@ -121,6 +124,7 @@ class Campaigns extends Model ...@@ -121,6 +124,7 @@ class Campaigns extends Model
'updated_children' => 'datetime', 'updated_children' => 'datetime',
'manage' => 'boolean', 'manage' => 'boolean',
'enabled' => 'boolean', 'enabled' => 'boolean',
'disabled_at' => 'datetime',
]; ];
/** /**
...@@ -195,7 +199,7 @@ class Campaigns extends Model ...@@ -195,7 +199,7 @@ class Campaigns extends Model
if (DictionaryCampaign::getPropertiesCopyWithPivot()->first(function ($property_name) use ($campaign) { if (DictionaryCampaign::getPropertiesCopyWithPivot()->first(function ($property_name) use ($campaign) {
return $campaign->{$property_name} !== $campaign->getOriginal($property_name); return $campaign->{$property_name} !== $campaign->getOriginal($property_name);
})) { })) {
$campaign->dictionaryCampaigns()->synchronized()->update( $campaign->dictionaryCampaigns()->enabled()->synchronized()->update(
DictionaryCampaign::copyPropertyInCampaign($campaign) DictionaryCampaign::copyPropertyInCampaign($campaign)
); );
} }
...@@ -203,7 +207,7 @@ class Campaigns extends Model ...@@ -203,7 +207,7 @@ class Campaigns extends Model
if (self::getPropertiesWatch()->first(function ($property_name) use ($campaign) { if (self::getPropertiesWatch()->first(function ($property_name) use ($campaign) {
return $campaign->{$property_name} !== $campaign->getOriginal($property_name); return $campaign->{$property_name} !== $campaign->getOriginal($property_name);
})) { })) {
$campaign->dictionaryCampaigns()->synchronized()->forExternal()->update([ $campaign->dictionaryCampaigns()->enabled()->synchronized()->forExternal()->update([
'updated_need' => Carbon::now(), 'updated_need' => Carbon::now(),
]); ]);
} }
...@@ -259,4 +263,14 @@ class Campaigns extends Model ...@@ -259,4 +263,14 @@ class Campaigns extends Model
$query->where('enabled', $enabled); $query->where('enabled', $enabled);
} }
public function scopeDisabled(Builder $query)
{
return $query->whereNotNull('disabled_at');
}
public function scopeNotDisabled(Builder $query)
{
return $query->whereNull('disabled_at');
}
} }
...@@ -25,6 +25,7 @@ use Illuminate\Support\Collection; ...@@ -25,6 +25,7 @@ use Illuminate\Support\Collection;
* @property string|null $name * @property string|null $name
* @property array|null $negative_keywords * @property array|null $negative_keywords
* @property array|null $excluded_sites * @property array|null $excluded_sites
* @property bool $enabled
* @property bool $updated * @property bool $updated
* @property bool $synced * @property bool $synced
* @property \Illuminate\Support\Carbon|null $external_upload_at * @property \Illuminate\Support\Carbon|null $external_upload_at
...@@ -33,8 +34,11 @@ use Illuminate\Support\Collection; ...@@ -33,8 +34,11 @@ use Illuminate\Support\Collection;
* @property-read int|null $dictionary_campaign_variables_count * @property-read int|null $dictionary_campaign_variables_count
* @property-read int|null $variables_count * @property-read int|null $variables_count
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign forExternal() * @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign forExternal()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign notDisabled()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign disabled()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign forNotExternal() * @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign forNotExternal()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign needUpdated() * @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign needUpdated()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign enabled($value = true)
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign updated($value = true) * @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign updated($value = true)
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign synced($value = true) * @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign synced($value = true)
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign synchronized() * @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign synchronized()
...@@ -75,8 +79,10 @@ class DictionaryCampaign extends Pivot ...@@ -75,8 +79,10 @@ class DictionaryCampaign extends Pivot
'updated_need', 'updated_need',
'updated_self', 'updated_self',
'updated_children', 'updated_children',
'enabled',
'updated', 'updated',
'synced', 'synced',
'disabled_at',
]; ];
protected $casts = [ protected $casts = [
...@@ -90,8 +96,10 @@ class DictionaryCampaign extends Pivot ...@@ -90,8 +96,10 @@ class DictionaryCampaign extends Pivot
'updated_need' => 'datetime', 'updated_need' => 'datetime',
'updated_self' => 'datetime', 'updated_self' => 'datetime',
'updated_children' => 'datetime', 'updated_children' => 'datetime',
'enabled' => 'boolean',
'updated' => 'boolean', 'updated' => 'boolean',
'synced' => 'boolean', 'synced' => 'boolean',
'disabled_at' => 'datetime',
]; ];
static public function getWithPivot() static public function getWithPivot()
...@@ -107,8 +115,10 @@ class DictionaryCampaign extends Pivot ...@@ -107,8 +115,10 @@ class DictionaryCampaign extends Pivot
'updated_need', 'updated_need',
'updated_self', 'updated_self',
'updated_children', 'updated_children',
'enabled',
'updated', 'updated',
'synced', 'synced',
'disabled_at',
]; ];
} }
...@@ -147,6 +157,16 @@ class DictionaryCampaign extends Pivot ...@@ -147,6 +157,16 @@ class DictionaryCampaign extends Pivot
})->all(); })->all();
} }
public function scopeDisabled(Builder $query)
{
return $query->whereNotNull('disabled_at');
}
public function scopeNotDisabled(Builder $query)
{
return $query->whereNull('disabled_at');
}
public function scopeForExternal(Builder $query) public function scopeForExternal(Builder $query)
{ {
return $query->whereNotNull('external_id'); return $query->whereNotNull('external_id');
...@@ -157,6 +177,11 @@ class DictionaryCampaign extends Pivot ...@@ -157,6 +177,11 @@ class DictionaryCampaign extends Pivot
return $query->whereNull('external_id'); return $query->whereNull('external_id');
} }
public function scopeEnabled(Builder $query, $enabled = true)
{
return $query->where('enabled', $enabled);
}
public function scopeUpdated(Builder $query, $updated = true) public function scopeUpdated(Builder $query, $updated = true)
{ {
return $query->where('updated', $updated); return $query->where('updated', $updated);
......
...@@ -20,14 +20,18 @@ use Illuminate\Database\Eloquent\Model; ...@@ -20,14 +20,18 @@ use Illuminate\Database\Eloquent\Model;
* @property int $limit * @property int $limit
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaigns * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaigns
* @property-read int|null $campaigns_count * @property-read int|null $campaigns_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaignsNotEnabledNotDisabled
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaignsEnabledDisabled
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $cities * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $cities
* @property-read int|null $cities_count * @property-read int|null $cities_count
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaigns * @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaigns
* @property-read int|null $dictionary_campaigns_count * @property-read int|null $dictionary_campaigns_count
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsForNotExternal
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsForExternal * @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsForExternal
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsForExternalNeedUpdated * @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForNotExternal
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsForExternalSynchronizedUpdatedSelf * @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsNotEnabledForExternalNotDisabled
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalDisabled
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalNeedUpdated
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $campaignsForManaged * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $campaignsForManaged
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $campaignsNotForManaged * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $campaignsNotForManaged
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Limits[] $limits * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Limits[] $limits
...@@ -88,6 +92,16 @@ class Tokens extends Model ...@@ -88,6 +92,16 @@ class Tokens extends Model
->orderBy('updated_at', 'DESC'); ->orderBy('updated_at', 'DESC');
} }
public function campaignsNotEnabledNotDisabled()
{
return $this->campaigns()->forEnabled(false)->notDisabled();
}
public function campaignsEnabledDisabled()
{
return $this->campaigns()->forEnabled()->disabled();
}
public function cities() public function cities()
{ {
return $this->hasMany(Dictionary::class, 'token_id', 'id') return $this->hasMany(Dictionary::class, 'token_id', 'id')
...@@ -101,24 +115,34 @@ class Tokens extends Model ...@@ -101,24 +115,34 @@ class Tokens extends Model
->orderBy(DictionaryCampaign::getModel()->getTable() . '.' . DictionaryCampaign::getModel()->getKeyName(), 'ASC'); ->orderBy(DictionaryCampaign::getModel()->getTable() . '.' . DictionaryCampaign::getModel()->getKeyName(), 'ASC');
} }
public function dictionaryCampaignsForNotExternal() public function dictionaryCampaignsForExternal()
{
return $this->dictionaryCampaigns()->forExternal();
}
public function dictionaryCampaignsEnabledForNotExternal()
{ {
return $this->dictionaryCampaigns()->forNotExternal(); return $this->dictionaryCampaigns()->enabled()->forNotExternal();
} }
public function dictionaryCampaignsForExternal() public function dictionaryCampaignsNotEnabledForExternalNotDisabled()
{ {
return $this->dictionaryCampaigns()->forExternal(); return $this->dictionaryCampaigns()->enabled(false)->forExternal()->notDisabled();
}
public function dictionaryCampaignsEnabledForExternalDisabled()
{
return $this->dictionaryCampaigns()->enabled()->forExternal()->disabled();
} }
public function dictionaryCampaignsForExternalNeedUpdated() public function dictionaryCampaignsEnabledForExternalNeedUpdated()
{ {
return $this->dictionaryCampaigns()->forExternal()->needUpdated(); return $this->dictionaryCampaigns()->enabled()->forExternal()->needUpdated();
} }
public function dictionaryCampaignsForExternalSynchronizedUpdatedSelf() public function dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf()
{ {
return $this->dictionaryCampaignsForExternal()->synchronized()->forUpdatedSelf(); return $this->dictionaryCampaignsForExternal()->enabled()->synchronized()->forUpdatedSelf();
} }
public function campaignsForManaged() public function campaignsForManaged()
......
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Campaigns;
use App\Models\Pivots\DictionaryCampaign;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class ResumeCampaigns extends DirectRequest
{
function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
function handle($response)
{
try {
foreach ($response['result']['ResumeResults'] as $resume_result) {
$external_id = $resume_result['Id'] ?? '';
if (!$external_id) {
Log::debug("resumeCampaigns, empty Id", $resume_result);
continue;
}
if ($this->getToken()->isMain()) {
Campaigns::where('external_id', $external_id)
->update([
'disabled_at' => null,
]);
} else {
DictionaryCampaign::where('external_id', $external_id)
->update([
'disabled_at' => null,
]);
}
}
} catch (\Exception $e) {
Log::debug($e);
}
}
private function requestPrepare($filter)
{
$this->setService('campaigns');
$this->setMethod('resume');
$params = [
'SelectionCriteria' => [
'Ids' => $filter['ids'],
],
];
$this->setParams($params);
}
}
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Campaigns;
use App\Models\Pivots\DictionaryCampaign;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class SuspendCampaigns extends DirectRequest
{
function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
function handle($response)
{
try {
foreach ($response['result']['SuspendResults'] as $suspend_result) {
$external_id = $suspend_result['Id'] ?? '';
if (!$external_id) {
Log::debug("SuspendCampaigns, empty Id", $suspend_result);
continue;
}
if ($this->getToken()->isMain()) {
Campaigns::where('external_id', $external_id)
->update([
'disabled_at' => Carbon::now(),
]);
} else {
DictionaryCampaign::where('external_id', $external_id)
->update([
'disabled_at' => Carbon::now(),
]);
}
}
} catch (\Exception $e) {
Log::debug($e);
}
}
private function requestPrepare($filter)
{
$this->setService('campaigns');
$this->setMethod('suspend');
$params = [
'SelectionCriteria' => [
'Ids' => $filter['ids'],
],
];
$this->setParams($params);
}
}
...@@ -30,17 +30,16 @@ class UpdateCampaigns extends DirectRequest ...@@ -30,17 +30,16 @@ class UpdateCampaigns extends DirectRequest
public function handle($response) public function handle($response)
{ {
try { try {
Log::debug($response);
foreach ($response['result']['UpdateResults'] as $key => $add_result) { foreach ($response['result']['UpdateResults'] as $key => $add_result) {
$id = $add_result['Id'] ?? ''; $external_id = $add_result['Id'] ?? '';
if (!$id) { if (!$external_id) {
Log::debug("AddCampaigns, empty Id, [dictionary_campaigns.id = {$this->dictionaryCampaigns->get($key)->getKey()}]", $add_result); Log::debug("AddCampaigns, empty Id, [dictionary_campaigns.id = {$this->dictionaryCampaigns->get($key)->getKey()}]", $add_result);
continue; continue;
} }
DictionaryCampaign::forExternal()->needUpdated() DictionaryCampaign::forExternal()->needUpdated()
->where('external_id', $id) ->where('external_id', $external_id)
->update([ ->update([
'updated_need' => null, 'updated_need' => null,
]); ]);
......
...@@ -36,6 +36,7 @@ class CreateCampaignsTable extends Migration ...@@ -36,6 +36,7 @@ class CreateCampaignsTable extends Migration
$table->boolean('manage')->default(0); $table->boolean('manage')->default(0);
$table->boolean('enabled')->default(1); $table->boolean('enabled')->default(1);
$table->timestamp('groups_loaded_at')->nullable(); $table->timestamp('groups_loaded_at')->nullable();
$table->timestamp('disabled_at')->nullable();
$table->timestamps(); $table->timestamps();
$table->foreign('token')->references('id')->on('tokens'); $table->foreign('token')->references('id')->on('tokens');
......
...@@ -23,11 +23,13 @@ class CreateDictionaryCampaignsTable extends Migration ...@@ -23,11 +23,13 @@ class CreateDictionaryCampaignsTable extends Migration
$table->text('excluded_sites')->nullable(); $table->text('excluded_sites')->nullable();
$table->boolean('synced')->default(1); $table->boolean('synced')->default(1);
$table->boolean('updated')->default(0); $table->boolean('updated')->default(0);
$table->boolean('enabled')->default(1);
$table->timestamp('external_upload_at')->nullable(); $table->timestamp('external_upload_at')->nullable();
$table->timestamp('external_updated_at')->nullable(); $table->timestamp('external_updated_at')->nullable();
$table->timestamp('updated_need')->nullable(); $table->timestamp('updated_need')->nullable();
$table->timestamp('updated_self')->nullable(); $table->timestamp('updated_self')->nullable();
$table->timestamp('updated_children')->nullable(); $table->timestamp('updated_children')->nullable();
$table->timestamp('disabled_at')->nullable();
$table->timestamps(); $table->timestamps();
......
...@@ -39,13 +39,16 @@ ...@@ -39,13 +39,16 @@
type="checkbox" type="checkbox"
> >
<label :for="'campaign-enabled-' + campaign.id">Enabled</label> <label :for="'campaign-enabled-' + campaign.id">Enabled</label>
<button class="text-red-600 hover:underline" </td>
tabindex="-1" <td class="border-t w-px">
type="button" <div class="px-6 py-4">
@click="destroy(campaign.id)" <button class="px-4 flex items-center"
> tabindex="-1"
Delete Campaign @click="destroy(campaign.id)"
</button> >
<icon name="trash" class="block w-6 h-6 fill-gray-400"/>
</button>
</div>
</td> </td>
</tr> </tr>
<tr v-if="token.campaigns.length === 0"> <tr v-if="token.campaigns.length === 0">
......
...@@ -48,7 +48,8 @@ ...@@ -48,7 +48,8 @@
<th class="px-6 pt-6 pb-4">Кампания</th> <th class="px-6 pt-6 pb-4">Кампания</th>
<th class="px-6 pt-6 pb-4">Обновлять?</th> <th class="px-6 pt-6 pb-4">Обновлять?</th>
<th class="px-6 pt-6 pb-4">Синхронизировать?</th> <th class="px-6 pt-6 pb-4">Синхронизировать?</th>
<th class="px-6 pt-6 pb-4" colspan="2"></th> <th class="px-6 pt-6 pb-4">Включено?</th>
<th class="px-6 pt-6 pb-4"></th>
</tr> </tr>
<tr v-if="city.dictionary_campaigns.length" :key="dictionary_campaign.id" v-for="dictionary_campaign in city.dictionary_campaigns"> <tr v-if="city.dictionary_campaigns.length" :key="dictionary_campaign.id" v-for="dictionary_campaign in city.dictionary_campaigns">
<td class="border-t"></td> <td class="border-t"></td>
...@@ -75,7 +76,13 @@ ...@@ -75,7 +76,13 @@
type="checkbox" type="checkbox"
> >
</td> </td>
<td class="border-t" colspan="2"></td> <td class="border-t text-center">
<input :checked="dictionary_campaign.enabled"
@change="enabled(city.id, dictionary_campaign.campaign_id, !dictionary_campaign.enabled)"
type="checkbox"
>
</td>
<td class="border-t"></td>
</tr> </tr>
</template> </template>
<tr v-if="token.cities.length === 0"> <tr v-if="token.cities.length === 0">
...@@ -99,7 +106,6 @@ ...@@ -99,7 +106,6 @@
main_token_campaigns: Array, main_token_campaigns: Array,
}, },
data() { data() {
console.log(this.token.cities[0]);
return { return {
city: false city: false
} }
...@@ -121,6 +127,9 @@ ...@@ -121,6 +127,9 @@
updated(city_id, campaign_id, updated) { updated(city_id, campaign_id, updated) {
this.$emit('updated', city_id, campaign_id, updated) this.$emit('updated', city_id, campaign_id, updated)
}, },
enabled(city_id, campaign_id, updated) {
this.$emit('enabled', city_id, campaign_id, updated)
},
} }
} }
</script> </script>
...@@ -51,8 +51,9 @@ ...@@ -51,8 +51,9 @@
:main_token_campaigns="main_token_campaigns" :main_token_campaigns="main_token_campaigns"
v-on:add="cityAdd" v-on:add="cityAdd"
v-on:delete="cityDelete" v-on:delete="cityDelete"
v-on:updated="campaignUpdated" v-on:updated="cityUpdated"
v-on:synced="campaignSynced" v-on:synced="citySynced"
v-on:enabled="cityEnabled"
></city-settings> ></city-settings>
</div> </div>
</template> </template>
...@@ -104,16 +105,21 @@ export default { ...@@ -104,16 +105,21 @@ export default {
cityAdd(id) { cityAdd(id) {
this.$inertia.post(this.route('token.city.store', [this.token.id, id])) this.$inertia.post(this.route('token.city.store', [this.token.id, id]))
}, },
campaignUpdated(city_id, campaign_id, updated) { cityUpdated(city_id, campaign_id, updated) {
this.$inertia.post(this.route('token.city.campaign.updated', [this.token.id, city_id, campaign_id]), { this.$inertia.post(this.route('token.city.campaign.updated', [this.token.id, city_id, campaign_id]), {
updated: updated ? 1 : 0, updated: updated ? 1 : 0,
}) })
}, },
campaignSynced(city_id, campaign_id, synced) { citySynced(city_id, campaign_id, synced) {
this.$inertia.post(this.route('token.city.campaign.synced', [this.token.id, city_id, campaign_id]), { this.$inertia.post(this.route('token.city.campaign.synced', [this.token.id, city_id, campaign_id]), {
updated: synced ? 1 : 0, updated: synced ? 1 : 0,
}) })
}, },
cityEnabled(city_id, campaign_id, enabled) {
this.$inertia.post(this.route('token.city.campaign.enabled', [this.token.id, city_id, campaign_id]), {
enabled: enabled ? 1 : 0,
})
},
cityDelete(id) { cityDelete(id) {
if (confirm('Are you sure you want to delete this city?')) { if (confirm('Are you sure you want to delete this city?')) {
this.$inertia.delete(this.route('token.city.destroy', [this.token.id, id])) this.$inertia.delete(this.route('token.city.destroy', [this.token.id, id]))
......
...@@ -193,6 +193,8 @@ Route::group(['middleware' => 'auth'], function () { ...@@ -193,6 +193,8 @@ Route::group(['middleware' => 'auth'], function () {
->name('token.city.campaign.updated'); ->name('token.city.campaign.updated');
Route::post('token/city/campaign/synced/{token}/{dictionary}/{campaign_id}', [TokensController::class, 'syncedCityCampaign']) Route::post('token/city/campaign/synced/{token}/{dictionary}/{campaign_id}', [TokensController::class, 'syncedCityCampaign'])
->name('token.city.campaign.synced'); ->name('token.city.campaign.synced');
Route::post('token/city/campaign/enabled/{token}/{dictionary}/{campaign_id}', [TokensController::class, 'enabledCityCampaign'])
->name('token.city.campaign.enabled');
Route::delete('token/city/{token}/{dictionary}', [TokensController::class, 'destroyCity']) Route::delete('token/city/{token}/{dictionary}', [TokensController::class, 'destroyCity'])
->name('token.city.destroy'); ->name('token.city.destroy');
......
...@@ -74,7 +74,7 @@ class AddCampaignsTest extends TestCase ...@@ -74,7 +74,7 @@ class AddCampaignsTest extends TestCase
$this->assertEquals(1, $this->dictionary->campaigns->count()); $this->assertEquals(1, $this->dictionary->campaigns->count());
$this->params = [ $this->params = [
'dictionaryCampaigns' => $this->token->dictionaryCampaignsForNotExternal, 'dictionaryCampaigns' => $this->token->dictionaryCampaignsEnabledForNotExternal,
'variables' => Variable::all(), 'variables' => Variable::all(),
]; ];
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!