Commit 0a8bf962 by Vladislav

#20794 Сделать чтобы в целевых РК оставалась одна карточка

1 parent 909272f7
...@@ -7,6 +7,7 @@ use App\Models\Tokens; ...@@ -7,6 +7,7 @@ use App\Models\Tokens;
use App\Service\Requests\Direct\AddAdGroups; use App\Service\Requests\Direct\AddAdGroups;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
class AdGroupsAdd extends Command class AdGroupsAdd extends Command
...@@ -42,14 +43,18 @@ class AdGroupsAdd extends Command ...@@ -42,14 +43,18 @@ class AdGroupsAdd extends Command
*/ */
public function handle() public function handle()
{ {
$tokens = Tokens::whereHas('dictionaryCampaignsEnabledForExternalSynchronized.groupsForNotExternalForNotReserveCreate.group') $tokens = Tokens::whereHas('dictionaryCampaignsEnabledForExternalSynchronized.groupsForNotExternalForNotReserveCreate.group', function (Builder $query) {
return $query->has('keywords')->orHas('bidModifiers')->orHas('advertisements');
})
->where('type', '!=', Tokens::MAIN) ->where('type', '!=', Tokens::MAIN)
->get(); ->get();
foreach ($tokens as $token) { foreach ($tokens as $token) {
$token->load([ $token->load([
'dictionaryCampaignsEnabledForExternalSynchronized.groupsForNotExternalForNotReserveCreate' => function (HasMany $query) { 'dictionaryCampaignsEnabledForExternalSynchronized.groupsForNotExternalForNotReserveCreate' => function (HasMany $query) {
return $query->has('group'); return $query->whereHas('group', function (Builder $query) {
return $query->has('keywords')->orHas('bidModifiers')->orHas('advertisements');
});
}, },
'dictionaryCampaignsEnabledForExternalSynchronized.groupsForNotExternalForNotReserveCreate.group', 'dictionaryCampaignsEnabledForExternalSynchronized.groupsForNotExternalForNotReserveCreate.group',
'dictionaryCampaignsEnabledForExternalSynchronized.groupsForNotExternalForNotReserveCreate.goalNegativeKeywordSharedSets', 'dictionaryCampaignsEnabledForExternalSynchronized.groupsForNotExternalForNotReserveCreate.goalNegativeKeywordSharedSets',
......
...@@ -209,6 +209,16 @@ class AdGroup extends Model ...@@ -209,6 +209,16 @@ class AdGroup extends Model
return $this->hasMany(Keyword::class, 'ad_group_id'); return $this->hasMany(Keyword::class, 'ad_group_id');
} }
public function bidModifiers()
{
return $this->hasMany(BidModifier::class, 'ad_group_id');
}
public function advertisements()
{
return $this->hasMany(Advertisement::class, 'ad_group_id');
}
public function negativeKeywordSharedSets() public function negativeKeywordSharedSets()
{ {
return $this->belongsToMany(NegativeKeywordSharedSet::class, AdGroupNegativeKeywordSharedSet::getModel()->getTable(), 'ad_group_id', 'negative_keyword_shared_set_id') return $this->belongsToMany(NegativeKeywordSharedSet::class, AdGroupNegativeKeywordSharedSet::getModel()->getTable(), 'ad_group_id', 'negative_keyword_shared_set_id')
......
...@@ -259,6 +259,11 @@ class GoalAdvertisement extends Pivot ...@@ -259,6 +259,11 @@ class GoalAdvertisement extends Pivot
return $this->belongsTo(GoalVCard::class, 'goal_v_card_id'); return $this->belongsTo(GoalVCard::class, 'goal_v_card_id');
} }
public function goalAdGroup()
{
return $this->belongsTo(GoalAdGroup::class, 'goal_ad_group_id');
}
public function goalAdExtensions() public function goalAdExtensions()
{ {
return $this->belongsToMany(GoalAdExtension::class, GoalAdvertisementGoalAdExtension::getModel()->getTable(), 'goal_advertisement_id', 'goal_ad_extension_id') return $this->belongsToMany(GoalAdExtension::class, GoalAdvertisementGoalAdExtension::getModel()->getTable(), 'goal_advertisement_id', 'goal_ad_extension_id')
......
...@@ -76,8 +76,14 @@ class AddAds extends DirectRequest ...@@ -76,8 +76,14 @@ class AddAds extends DirectRequest
Advertisement::where('id', $goalAd->id) Advertisement::where('id', $goalAd->id)
->forceDelete(); ->forceDelete();
} else { } else {
GoalAdvertisement::where('id', $goalAd->id) $model = GoalAdvertisement::with('goalAdGroup')->firstWhere('id', $goalAd->id);
->forceDelete();
if ($model) {
if ($model->goalAdGroup) {
$model->goalAdGroup->delete();
}
$model->forceDelete();
}
} }
continue; continue;
......
...@@ -47,7 +47,20 @@ class UpdateAds extends DirectRequest ...@@ -47,7 +47,20 @@ class UpdateAds extends DirectRequest
if (isset($update_result['Errors'][0]['Details']) && $update_result['Errors'][0]['Details'] === 'You cannot update an archived ad') { if (isset($update_result['Errors'][0]['Details']) && $update_result['Errors'][0]['Details'] === 'You cannot update an archived ad') {
GoalAdvertisement::whereExternalId($this->getParams()['Ads'][$key]['Id']) GoalAdvertisement::whereExternalId($this->getParams()['Ads'][$key]['Id'])
->forceDelete(); ->update([
'deleted_need' => Carbon::now(),
'archived_need' => null,
'reserve_archive_at' => null,
'reserve_update_at' => null,
]);
continue;
} elseif (isset($update_result['Errors'][0]['Details']) && $update_result['Errors'][0]['Details'] === 'Ad not found') {
GoalAdvertisement::whereExternalId($this->getParams()['Ads'][$key]['Id'])
->update([
'reserve_update_at' => null,
]);
GoalAdvertisement::whereExternalId($this->getParams()['Ads'][$key]['Id'])
->delete();
continue; continue;
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!