Commit 2900b73c by Vladislav

#19500 Синхронизация модификаторов

1 parent 5364c394
...@@ -5,6 +5,7 @@ namespace App\Console\Commands; ...@@ -5,6 +5,7 @@ namespace App\Console\Commands;
use App\Models\AdGroup; use App\Models\AdGroup;
use App\Models\Tokens; use App\Models\Tokens;
use App\Service\Requests\Direct\GetAdGroups; use App\Service\Requests\Direct\GetAdGroups;
use App\Service\Requests\Direct\GetBidModifiers;
use App\Service\Requests\Direct\GetKeywords; use App\Service\Requests\Direct\GetKeywords;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
...@@ -65,6 +66,12 @@ class AdGroupsLoadUpdated extends Command ...@@ -65,6 +66,12 @@ class AdGroupsLoadUpdated extends Command
'Ids' => $ids, 'Ids' => $ids,
]); ]);
$request = new GetBidModifiers();
$request->setToken($token)
->call([
'AdGroupIds' => $ids,
]);
foreach (array_chunk($ids, 1000) as $ids_limit) { foreach (array_chunk($ids, 1000) as $ids_limit) {
$request = new GetKeywords(); $request = new GetKeywords();
$request->setToken($token) $request->setToken($token)
......
...@@ -101,8 +101,12 @@ class AdvertisementsUpdate extends Command ...@@ -101,8 +101,12 @@ class AdvertisementsUpdate extends Command
->groupBy('goal_advertisements.id') ->groupBy('goal_advertisements.id')
->get(); ->get();
foreach (array_chunk($goalAds->pluck('id')->toArray(), 1000) as $items) { if (!$goalAds->count()) {
GoalAdvertisement::whereIn('id', $items) continue;
}
foreach (array_chunk($goalAds->pluck('external_id')->toArray(), 1000) as $items) {
GoalAdvertisement::whereIn('external_id', $items)
->update([ ->update([
'reserve_update_at' => Carbon::now(), 'reserve_update_at' => Carbon::now(),
]); ]);
......
<?php
namespace App\Console\Commands;
use App\Models\Pivots\GoalSitelink;
use App\Models\Tokens;
use App\Service\Requests\Direct\AddSitelinks;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasMany;
class RetargetinglistsAdd extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'retargetinglists:add';
/**
* 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
* @throws \Exception
*/
public function handle()
{
$tokens = Tokens::whereHas('goalRetargetinglistsForNotExternalForNotReserveCreate.retargetinglist')
->where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$token->load([
'goalRetargetinglistsForNotExternalForNotReserveCreate' => function (HasMany $query) {
return $query->has('retargetinglist');
},
'goalRetargetinglistsForNotExternalForNotReserveCreate.retargetinglist',
]);
$goalRetargetinglists = $token->goalRetargetinglistsForNotExternalForNotReserveCreate;
foreach (array_chunk($goalRetargetinglists->pluck('id')->toArray(), 1000) as $items) {
GoalSitelink::whereIn('id', $items)
->update([
'reserve_create_at' => Carbon::now(),
]);
}
$request = new AddSitelinks();
$request->setToken($token)
->call([
'goalRetargetinglists' => $goalRetargetinglists,
]);
}
return 0;
}
}
<?php
namespace App\Console\Commands;
use App\Models\Tokens;
use App\Service\Requests\Direct\GetRetargetinglists;
use Illuminate\Console\Command;
class RetargetinglistsLoad extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'retargetinglists:load';
/**
* 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
* @throws \Exception
*/
public function handle()
{
$token = Tokens::where('type', Tokens::MAIN)->first();
if (!$token) {
throw new \Exception('Не найден токен блин');
}
$request = new GetRetargetinglists();
$request->setToken($token)
->call();
return 0;
}
}
...@@ -29,6 +29,8 @@ use App\Console\Commands\NegativeKeywordSharedSetsAdd; ...@@ -29,6 +29,8 @@ use App\Console\Commands\NegativeKeywordSharedSetsAdd;
use App\Console\Commands\NegativeKeywordSharedSetsLoad; use App\Console\Commands\NegativeKeywordSharedSetsLoad;
use App\Console\Commands\NegativeKeywordSharedSetsUpdate; use App\Console\Commands\NegativeKeywordSharedSetsUpdate;
use App\Console\Commands\RefreshLimits; use App\Console\Commands\RefreshLimits;
use App\Console\Commands\RetargetinglistsAdd;
use App\Console\Commands\RetargetinglistsLoad;
use App\Console\Commands\SitelinksAdd; use App\Console\Commands\SitelinksAdd;
use App\Console\Commands\SitelinksLoad; use App\Console\Commands\SitelinksLoad;
use App\Console\Commands\VCardsAdd; use App\Console\Commands\VCardsAdd;
...@@ -64,6 +66,9 @@ class Kernel extends ConsoleKernel ...@@ -64,6 +66,9 @@ class Kernel extends ConsoleKernel
$schedule->command(CampaignsLoadUpdated::class)->hourlyAt(10); $schedule->command(CampaignsLoadUpdated::class)->hourlyAt(10);
$schedule->command(CampaignsCheckUpdatedChildren::class)->hourlyAt(10); $schedule->command(CampaignsCheckUpdatedChildren::class)->hourlyAt(10);
$schedule->command(RetargetinglistsLoad::class)->hourlyAt(5);
$schedule->command(RetargetinglistsAdd::class)->hourlyAt(10);
$schedule->command(NegativeKeywordSharedSetsLoad::class)->hourlyAt(10); $schedule->command(NegativeKeywordSharedSetsLoad::class)->hourlyAt(10);
$schedule->command(NegativeKeywordSharedSetsAdd::class)->hourlyAt(15); $schedule->command(NegativeKeywordSharedSetsAdd::class)->hourlyAt(15);
$schedule->command(NegativeKeywordSharedSetsUpdate::class)->hourlyAt(15); $schedule->command(NegativeKeywordSharedSetsUpdate::class)->hourlyAt(15);
......
...@@ -34,6 +34,7 @@ use Illuminate\Support\Collection; ...@@ -34,6 +34,7 @@ use Illuminate\Support\Collection;
* @property array|null $cpm_video_ad_group * @property array|null $cpm_video_ad_group
* @property array|null $smart_ad_group * @property array|null $smart_ad_group
* @property \Illuminate\Support\Carbon|null $keywords_loaded_at * @property \Illuminate\Support\Carbon|null $keywords_loaded_at
* @property \Illuminate\Support\Carbon|null $bid_modifiers_loaded_at
* @property \Illuminate\Support\Carbon|null $updated_self * @property \Illuminate\Support\Carbon|null $updated_self
* @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at * @property \Illuminate\Support\Carbon|null $updated_at
...@@ -125,6 +126,7 @@ class AdGroup extends Model ...@@ -125,6 +126,7 @@ class AdGroup extends Model
'restricted_region_ids', 'restricted_region_ids',
'updated_self', 'updated_self',
'keywords_loaded_at', 'keywords_loaded_at',
'bid_modifiers_loaded_at',
]; ];
protected $casts = [ protected $casts = [
...@@ -140,6 +142,7 @@ class AdGroup extends Model ...@@ -140,6 +142,7 @@ class AdGroup extends Model
'restricted_region_ids' => 'json', 'restricted_region_ids' => 'json',
'updated_self' => 'datetime', 'updated_self' => 'datetime',
'keywords_loaded_at' => 'datetime', 'keywords_loaded_at' => 'datetime',
'bid_modifiers_loaded_at' => 'datetime',
]; ];
/** /**
......
...@@ -51,6 +51,32 @@ class BidModifier extends Model ...@@ -51,6 +51,32 @@ class BidModifier extends Model
'updated_self' => 'datetime', 'updated_self' => 'datetime',
]; ];
public function getBidModifierValueAttribute()
{
if ($this->mobile_adjustment) {
return $this->mobile_adjustment['BidModifier'];
} elseif ($this->desktop_adjustment) {
return $this->desktop_adjustment['BidModifier'];
} elseif ($this->demographics_adjustment) {
return $this->demographics_adjustment['BidModifier'];
} elseif ($this->retargeting_adjustment) {
return $this->retargeting_adjustment['BidModifier'];
}
}
public function getOriginalBidModifierValueAttribute()
{
if ($this->mobile_adjustment) {
return $this->getOriginal('mobile_adjustment')['BidModifier'];
} elseif ($this->desktop_adjustment) {
return $this->getOriginal('desktop_adjustment')['BidModifier'];
} elseif ($this->demographics_adjustment) {
return $this->getOriginal('demographics_adjustment')['BidModifier'];
} elseif ($this->retargeting_adjustment) {
return $this->getOriginal('retargeting_adjustment')['BidModifier'];
}
}
/** /**
* @param Builder $query * @param Builder $query
* @return Builder * @return Builder
......
...@@ -35,6 +35,7 @@ use Illuminate\Support\Collection; ...@@ -35,6 +35,7 @@ use Illuminate\Support\Collection;
* @property bool $manage * @property bool $manage
* @property bool $enabled * @property bool $enabled
* @property \Illuminate\Support\Carbon|null $groups_loaded_at * @property \Illuminate\Support\Carbon|null $groups_loaded_at
* @property \Illuminate\Support\Carbon|null $bid_modifiers_loaded_at
* @property \Illuminate\Support\Carbon|null $disabled_at * @property \Illuminate\Support\Carbon|null $disabled_at
* @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at * @property \Illuminate\Support\Carbon|null $updated_at
...@@ -112,6 +113,7 @@ class Campaigns extends Model ...@@ -112,6 +113,7 @@ class Campaigns extends Model
'attribution_model', 'attribution_model',
'priority_goals', 'priority_goals',
'groups_loaded_at', 'groups_loaded_at',
'bid_modifiers_loaded_at',
'updated_self', 'updated_self',
'updated_children', 'updated_children',
'manage', 'manage',
...@@ -131,6 +133,7 @@ class Campaigns extends Model ...@@ -131,6 +133,7 @@ class Campaigns extends Model
'daily_budget' => 'array', 'daily_budget' => 'array',
'priority_goals' => 'array', 'priority_goals' => 'array',
'groups_loaded_at' => 'datetime', 'groups_loaded_at' => 'datetime',
'bid_modifiers_loaded_at' => 'datetime',
'updated_self' => 'datetime', 'updated_self' => 'datetime',
'updated_children' => 'datetime', 'updated_children' => 'datetime',
'manage' => 'boolean', 'manage' => 'boolean',
......
<?php
namespace App\Models\Pivots;
use App\Models\Retargetinglist;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;
class GoalRetargetinglist extends Pivot
{
use SoftDeletes;
protected $table = 'goal_retargetinglists';
protected $fillable = [
'external_id',
'retargetinglist_id',
'token_id',
'external_upload_at',
'external_updated_at',
'updated_need',
'reserve_create_at',
'reserve_update_at',
];
protected $casts = [
'external_upload_at' => 'datetime',
'external_updated_at' => 'datetime',
'updated_need' => 'datetime',
'reserve_create_at' => 'datetime',
'reserve_update_at' => 'datetime',
];
public $incrementing = true;
static public function getWithPivot()
{
return [
'id',
'external_id',
'retargetinglist_id',
'token_id',
'external_upload_at',
'external_updated_at',
'updated_need',
'reserve_create_at',
'reserve_update_at',
];
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForExternal($query)
{
return $query->whereNotNull('external_id');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotExternal($query)
{
return $query->whereNull('external_id');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotReserveCreate($query)
{
return $query->whereNull('reserve_create_at');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotReserveUpdate($query)
{
return $query->whereNull('reserve_update_at');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeNeedUpdated($query)
{
return $query->whereNotNull('updated_need');
}
public function retargetinglist()
{
return $this->belongsTo(Retargetinglist::class, 'retargetinglist_id');
}
}
<?php
namespace App\Models;
use App\Models\Pivots\GoalRetargetinglist;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
class Retargetinglist extends Model
{
use SoftDeletes;
const TYPE_RETARGETING = 'RETARGETING';
const TYPE_AUDIENCE = 'AUDIENCE';
protected $table = 'retargetinglists';
protected $fillable = [
'type',
'name',
'description',
'rules',
];
protected $casts = [
'rules' => 'json',
];
/**
* @return Collection
*/
static public function getPropertiesWatch()
{
return collect([
'type',
'name',
'description',
'rules',
]);
}
public function goalRetargetinglists()
{
return $this->hasMany(GoalRetargetinglist::class, 'retargetinglist_id');
}
}
...@@ -5,6 +5,7 @@ namespace App\Models; ...@@ -5,6 +5,7 @@ namespace App\Models;
use App\Models\Pivots\DictionaryCampaign; use App\Models\Pivots\DictionaryCampaign;
use App\Models\Pivots\GoalAdExtension; use App\Models\Pivots\GoalAdExtension;
use App\Models\Pivots\GoalNegativeKeywordSharedSet; use App\Models\Pivots\GoalNegativeKeywordSharedSet;
use App\Models\Pivots\GoalRetargetinglist;
use App\Models\Pivots\GoalSitelink; use App\Models\Pivots\GoalSitelink;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
...@@ -298,4 +299,14 @@ class Tokens extends Model ...@@ -298,4 +299,14 @@ class Tokens extends Model
return $this->goalSitelinks()->forNotExternal()->forNotReserveCreate(); return $this->goalSitelinks()->forNotExternal()->forNotReserveCreate();
} }
public function goalRetargetinglists()
{
return $this->hasMany(GoalRetargetinglist::class, 'token_id');
}
public function goalRetargetinglistsForNotExternalForNotReserveCreate()
{
return $this->goalRetargetinglists()->forNotExternal()->forNotReserveCreate();
}
} }
...@@ -65,7 +65,7 @@ class AddAds extends DirectRequest ...@@ -65,7 +65,7 @@ class AddAds extends DirectRequest
Log::debug($add_result); Log::debug($add_result);
Log::debug($this->getParams()['Ads'][$key]); Log::debug($this->getParams()['Ads'][$key]);
GoalAdvertisement::whereId($goalAd->getKey()) GoalAdvertisement::whereId($goalAd->id)
->update([ ->update([
'reserve_create_at' => null, 'reserve_create_at' => null,
]); ]);
...@@ -75,7 +75,7 @@ class AddAds extends DirectRequest ...@@ -75,7 +75,7 @@ class AddAds extends DirectRequest
$external_id = (string)$add_result['Id']; $external_id = (string)$add_result['Id'];
GoalAdvertisement::whereId($goalAd->getKey()) GoalAdvertisement::whereId($goalAd->id)
->update([ ->update([
'external_id' => $external_id, 'external_id' => $external_id,
'external_upload_at' => Carbon::now(), 'external_upload_at' => Carbon::now(),
......
...@@ -119,14 +119,15 @@ class AddBidModifiers extends DirectRequest ...@@ -119,14 +119,15 @@ class AddBidModifiers extends DirectRequest
/* @var $goalBidModifier \stdClass */ /* @var $goalBidModifier \stdClass */
$data = [ $data = [];
'CampaignId' => $goalBidModifier->dictionary_campaign_external_id,
];
if ($goalBidModifier->goal_ad_group_external_id) { if ($goalBidModifier->goal_ad_group_external_id) {
$data['AdGroupId'] = $goalBidModifier->goal_ad_group_external_id; $data['AdGroupId'] = $goalBidModifier->goal_ad_group_external_id;
} else {
$data['CampaignId'] = $goalBidModifier->dictionary_campaign_external_id;
} }
if ($mobile_adjustment = @json_decode($goalBidModifier->mobile_adjustment, true)) { if ($mobile_adjustment = @json_decode($goalBidModifier->mobile_adjustment, true)) {
$data['MobileAdjustment'] = $mobile_adjustment; $data['MobileAdjustment'] = $mobile_adjustment;
......
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Pivots\GoalRetargetinglist;
use App\Models\Pivots\GoalSitelink;
use App\Models\Variable;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use App\Service\StrReplaceByVariables;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log;
class AddRetargetinglists extends DirectRequest
{
protected $max_count = 1000;
protected $timestamp;
/* @var Collection|GoalRetargetinglist[] */
protected $goalRetargetinglists;
public function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
public function getObjectsCount()
{
return count($this->getParams()['RetargetingLists']);
}
public function slice($maxObjects): ?APIRequest
{
$splinter = $this->sliceByKey($maxObjects, 'RetargetingLists');
$splinter->putParams([
'goalRetargetinglists' => $this->goalRetargetinglists->slice($maxObjects)->values(),
]);
$this->putParams([
'goalRetargetinglists' => $this->goalRetargetinglists->slice(0, $maxObjects),
]);
return $splinter;
}
public function handle($response)
{
try {
if (!isset($response['result']['AddResults'])) {
return;
}
foreach ($response['result']['AddResults'] as $key => $add_result) {
$goalSitelink = $this->goalRetargetinglists->get($key);
if (!isset($add_result['Id'])) {
Log::debug("AddSitelink, empty Id");
Log::debug($add_result);
Log::debug($this->getParams()['RetargetingLists'][$key]);
$goalSitelink->update([
'reserve_create_at' => null,
]);
continue;
}
$external_id = (string)$add_result['Id'];
$goalSitelink->update([
'external_id' => $external_id,
'external_upload_at' => Carbon::now(),
'reserve_create_at' => null,
]);
}
} catch (\Exception $e) {
Log::debug($e);
throw $e;
}
}
public function failed()
{
GoalSitelink::whereIn('id', $this->goalRetargetinglists->pluck('id')->toArray())
->update([
'reserve_create_at' => null,
]);
}
public function putParams($params)
{
$this->goalRetargetinglists = $params['goalRetargetinglists'];
}
private function requestPrepare($params)
{
$this->setService('RetargetingLists');
$this->setMethod('add');
$this->putParams($params);
$variables = Variable::all();
$lists = [];
$this->setParams([
'RetargetingLists' => $this->goalRetargetinglists->map(function (GoalRetargetinglist $goalRetargetinglist) use ($variables, &$lists) {
if (!isset($lists[$goalRetargetinglist->dictionary_campaign_id])) {
$list = Variable::getListVariablesByDictionaryCampaign($goalRetargetinglist->dictionary_campaign_id, $variables);
$lists[$goalRetargetinglist->dictionary_campaign_id] = $list;
} else {
$list = $lists[$goalRetargetinglist->dictionary_campaign_id];
}
return [
'Type' => $goalRetargetinglist->retargetinglist->type,
'Name' => StrReplaceByVariables::getInstance($goalRetargetinglist->retargetinglist->name, $list)->get(),
'Description' => StrReplaceByVariables::getInstance($goalRetargetinglist->retargetinglist->description, $list)->get(),
'rules' => $goalRetargetinglist->retargetinglist->rules,
];
})->toArray(),
]);
}
}
...@@ -10,6 +10,7 @@ use App\Models\Campaigns; ...@@ -10,6 +10,7 @@ use App\Models\Campaigns;
use App\Service\Contract\APIRequest; use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest; use App\Service\Requests\DirectRequest;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
class GetBidModifiers extends DirectRequest class GetBidModifiers extends DirectRequest
...@@ -133,17 +134,11 @@ class GetBidModifiers extends DirectRequest ...@@ -133,17 +134,11 @@ class GetBidModifiers extends DirectRequest
'external_id' => $external_id 'external_id' => $external_id
], $data); ], $data);
/* if (!$bid_modifier->wasRecentlyCreated && $bid_modifier->bidModifierValue !== $bid_modifier->originalBidModifierValue) {
if ($bid_modifier->wasRecentlyCreated) {
$campaign_ids_synced_need[$bid_modifier->campaign_id] = true;
} elseif ($bid_modifier->wasChanged(['campaign_id'])) {
$campaign_ids_synced_need[$bid_modifier->campaign_id] = true;
} elseif ($bid_modifier->wasChanged($bid_modifier::getPropertiesWatch()->toArray())) {
$bid_modifier->goalBidModifiers()->has('dictionaryCampaign')->forExternal()->update([ $bid_modifier->goalBidModifiers()->has('dictionaryCampaign')->forExternal()->update([
'updated_need' => Carbon::now(), 'updated_need' => Carbon::now(),
]); ]);
} }
*/
} else { } else {
...@@ -152,6 +147,41 @@ class GetBidModifiers extends DirectRequest ...@@ -152,6 +147,41 @@ class GetBidModifiers extends DirectRequest
} }
if (!empty($this->getParams()['SelectionCriteria']['AdGroupIds'])) {
if (!isset($response['result']['LimitedBy'])) {
$ag_group_external_ids = $this->getParams()['SelectionCriteria']['AdGroupIds'];
$sql = "UPDATE bid_modifiers bm
INNER JOIN ad_groups ag ON bm.ad_group_id = ag.id
SET bm.deleted_at = now()
WHERE bm.updated_at <= ag.bid_modifiers_loaded_at
AND bm.deleted_at is null
AND ag.external_id in (" . implode(", ", $ag_group_external_ids) . ")";
DB::update($sql);
AdGroup::whereIn('external_id', $ag_group_external_ids)->update([
'bid_modifiers_loaded_at' => Carbon::now()
]);
}
} elseif (!empty($this->getParams()['SelectionCriteria']['CampaignIds'])) {
$campaign_external_ids = $this->getParams()['SelectionCriteria']['CampaignIds'];
$sql = "UPDATE bid_modifiers bm
INNER JOIN campaigns ca ON bm.campaign_id = ca.id
SET bm.deleted_at = now()
WHERE bm.updated_at <= ca.bid_modifiers_loaded_at
AND bm.deleted_at is null
AND ca.external_id in (" . implode(", ", $campaign_external_ids) . ")";
DB::update($sql);
Campaigns::whereIn('external_id', $campaign_external_ids)->update([
'bid_modifiers_loaded_at' => Carbon::now()
]);
}
} catch (\Exception $e) { } catch (\Exception $e) {
Log::debug($e); Log::debug($e);
throw $e; throw $e;
......
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Retargetinglist;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class GetRetargetinglists extends DirectRequest
{
protected $max_count = -1;
protected $max_count_Ids = 10000;
function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
public function getObjectsCount()
{
$params = $this->getParams();
if (isset($params['SelectionCriteria']['Ids'])) {
return count($params['SelectionCriteria']['Ids']);
}
return -1;
}
public function slice($maxObjects): ?APIRequest
{
$params = $this->getParams();
if (isset($params['SelectionCriteria']['Ids'])) {
return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'Ids']);
}
return null;
}
function handle($response)
{
try {
if (isset($response['result']['VCards'])) {
foreach ($response['result']['RetargetingLists'] as $retargeting_list) {
$external_id = (string)$retargeting_list['Id'];
if ($this->getToken()->isMain()) {
$data = [
'external_id' => $external_id,
'type' => $retargeting_list['Type'],
'name' => $retargeting_list['Name'],
'description' => $retargeting_list['Description'],
'rules' => $retargeting_list['Rules'],
];
$adExtension = Retargetinglist::updateOrCreate([
'external_id' => $external_id
], $data);
if ($adExtension->wasChanged($adExtension::getPropertiesWatch()->toArray())) {
$adExtension->goalRetargetinglists()->forExternal()->update([
'updated_need' => Carbon::now(),
]);
}
} else {
//
}
}
}
} catch (\Exception $e) {
Log::debug($e);
throw $e;
}
}
private function requestPrepare($filter)
{
$this->setService('RetargetingLists');
$this->setMethod('get');
$params = [
"FieldNames" => [
"Id", "Type", "Name",
"Description", "Rules",
],
];
if (isset($filter['Ids'])) {
$this->max_count = $this->max_count_Ids;
$params['SelectionCriteria'] = [
'Ids' => $filter['Ids'],
];
}
$this->setParams($params);
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddBidModifiersLoadedAtColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('ad_groups', function (Blueprint $table) {
$table->timestamp('bid_modifiers_loaded_at')->nullable();
});
Schema::table('campaigns', function (Blueprint $table) {
$table->timestamp('bid_modifiers_loaded_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
<?php
use App\Models\Retargetinglist;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRetargetinglistsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('retargetinglists', function (Blueprint $table) {
$table->id();
$table->enum('type', [
Retargetinglist::TYPE_RETARGETING,
Retargetinglist::TYPE_AUDIENCE,
]);
$table->string('name')->nullable();
$table->string('description')->nullable();
$table->json('rules')->nullable();
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('retargetinglists');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateGoalRetargetinglistsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('goal_retargetinglists', function (Blueprint $table) {
$table->id();
$table->bigInteger('external_id')->nullable();
$table->bigInteger('retargetinglist_id')->unsigned();
$table->bigInteger('token_id')->unsigned();
$table->timestamp('external_upload_at')->nullable();
$table->timestamp('external_updated_at')->nullable();
$table->timestamp('updated_need')->nullable();
$table->timestamp('reserve_create_at')->nullable();
$table->foreign('retargetinglist_id')->references('id')->on('retargetinglists')
->cascadeOnDelete();
$table->foreign('token_id')->references('id')->on('tokens')
->cascadeOnDelete();
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('goal_retargetinglists');
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!