Commit 76d05e25 by Vladislav

#20794 Сделать чтобы в целевых РК оставалась одна карточка (Удаление объявлений)

1 parent df9dfe11
......@@ -103,6 +103,8 @@ class Advertisement extends Model
'reserve_archive_at',
'deleted_need',
'reserve_delete_at',
'deleted_at',
];
protected $casts = [
......
......@@ -35,6 +35,7 @@ use Illuminate\Support\Collection;
* @property bool $manage
* @property bool $enabled
* @property \Illuminate\Support\Carbon|null $groups_loaded_at
* @property \Illuminate\Support\Carbon|null $ads_loaded_at
* @property \Illuminate\Support\Carbon|null $bid_modifiers_loaded_at
* @property \Illuminate\Support\Carbon|null $disabled_at
* @property \Illuminate\Support\Carbon|null $created_at
......@@ -113,6 +114,7 @@ class Campaigns extends Model
'attribution_model',
'priority_goals',
'groups_loaded_at',
'ads_loaded_at',
'bid_modifiers_loaded_at',
'updated_self',
'updated_children',
......@@ -133,6 +135,7 @@ class Campaigns extends Model
'daily_budget' => 'array',
'priority_goals' => 'array',
'groups_loaded_at' => 'datetime',
'ads_loaded_at' => 'datetime',
'bid_modifiers_loaded_at' => 'datetime',
'updated_self' => 'datetime',
'updated_children' => 'datetime',
......
......@@ -6,6 +6,7 @@ use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Advertisement;
use App\Models\Pivots\GoalAdvertisement;
use App\Service\Contract\APIRequest;
use App\Service\DirectResponseHelper;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
......@@ -39,91 +40,88 @@ class ArchiveAds extends DirectRequest
return;
}
foreach (DirectResponseHelper::getExternalIdsChunkByResult($response['result']['ArchiveResults']) as $external_ids) {
if ($this->getToken()->isMain()) {
Advertisement::whereIn('external_id', $external_ids)
->update([
'state' => Advertisement::STATE_ARCHIVED,
'status_clarification' => 'Archived.',
'archived_need' => null,
'reserve_archive_at' => null,
]);
} else {
GoalAdvertisement::whereIn('external_id', $external_ids)
->update([
'archive_at' => Carbon::now(),
'archived_need' => null,
'reserve_archive_at' => null,
]);
}
}
foreach ($response['result']['ArchiveResults'] as $key => $archive_result) {
if (!isset($archive_result['Id'])) {
$external_id = $this->getParams()['SelectionCriteria']['Ids'][$key];
/*
* array (
* 0 =>
* array (
* 'Code' => 8300,
* 'Message' => 'Invalid object status',
* 'Details' => 'Ad is a draft and cannot be archived',
* ),
* )
*/
if (isset($archive_result['Errors'][0]['Code']) == 8300) {
if ($this->getToken()->isMain()) {
Advertisement::whereExternalId($external_id)
->update([
'deleted_need' => Carbon::now(),
'archived_need' => null,
'reserve_archive_at' => null,
]);
} else {
GoalAdvertisement::whereExternalId($external_id)
->update([
'deleted_need' => Carbon::now(),
'archived_need' => null,
'reserve_archive_at' => null,
]);
}
} elseif (isset($archive_result['Errors']) && count($archive_result['Errors'])) {
if ($this->getToken()->isMain()) {
$model = Advertisement::whereExternalId($external_id)->first();
} else {
$model = GoalAdvertisement::whereExternalId($external_id)->first();
}
if ($model) {
$model->errors()->create([
'token_id' => $this->getToken()->getKey(),
'service' => $this->getService(),
'method' => $this->getMethod(),
'params' => $external_id,
'errors' => $archive_result['Errors'],
]);
}
} else {
Log::debug("ArchiveAds, empty Id, token_id {$this->getToken()->getKey()}");
Log::debug($archive_result);
Log::debug($external_id);
}
if (isset($archive_result['Id'])) {
continue;
}
$external_id = $this->getParams()['SelectionCriteria']['Ids'][$key];
/*
* array (
* 0 =>
* array (
* 'Code' => 8300,
* 'Message' => 'Invalid object status',
* 'Details' => 'Ad is a draft and cannot be archived',
* ),
* )
*/
if (isset($archive_result['Errors'][0]['Code']) == 8300) {
if ($this->getToken()->isMain()) {
Advertisement::whereExternalId($external_id)
->update([
'deleted_need' => Carbon::now(),
'archived_need' => null,
'reserve_archive_at' => null,
]);
} else {
GoalAdvertisement::whereExternalId($external_id)
->update([
'deleted_need' => Carbon::now(),
'archived_need' => null,
'reserve_archive_at' => null,
]);
}
} elseif (isset($archive_result['Errors']) && count($archive_result['Errors'])) {
if ($this->getToken()->isMain()) {
$model = Advertisement::whereExternalId($external_id)->first();
} else {
$model = GoalAdvertisement::whereExternalId($external_id)->first();
}
continue;
if ($model) {
$model->errors()->create([
'token_id' => $this->getToken()->getKey(),
'service' => $this->getService(),
'method' => $this->getMethod(),
'params' => $external_id,
'errors' => $archive_result['Errors'],
]);
}
} else {
Log::debug("ArchiveAds, empty Id, token_id {$this->getToken()->getKey()}");
Log::debug($archive_result);
Log::debug($external_id);
}
$external_id = (string)$archive_result['Id'];
if ($this->getToken()->isMain()) {
Advertisement::needArchived()
->where('external_id', $external_id)
Advertisement::whereExternalId($external_id)
->update([
'state' => Advertisement::STATE_ARCHIVED,
'status_clarification' => 'Archived.',
'archived_need' => null,
'reserve_archive_at' => null,
]);
} else {
GoalAdvertisement::forExternal()->needArchived()
->where('external_id', $external_id)
GoalAdvertisement::whereExternalId($external_id)
->update([
'archive_at' => Carbon::now(),
'archived_need' => null,
'reserve_archive_at' => null,
]);
}
......
......@@ -6,9 +6,11 @@ use App\Jobs\ProcessCallLimitedAPI;
use App\Models\AdExtension;
use App\Models\AdGroup;
use App\Models\Advertisement;
use App\Models\Campaigns;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class GetAds extends DirectRequest
{
......@@ -127,6 +129,7 @@ class GetAds extends DirectRequest
'turbo_page_moderation' => $ad['TextAd']['TurboPageModeration'],
'business_id' => $ad['TextAd']['BusinessId'],
'prefer_v_card_over_business' => isset($ad['TextAd']['PreferVCardOverBusiness']) ? $ad['TextAd']['PreferVCardOverBusiness'] === 'YES' : null,
'deleted_at' => null,
]);
$ad_extensions_array = [];
......@@ -187,6 +190,31 @@ class GetAds extends DirectRequest
}
}
if ($this->getToken()->isMain()) {
if (
!empty($this->getParams()['SelectionCriteria']['CampaignIds'])
&&
!isset($response['result']['LimitedBy'])
) {
$campaign_ids = $this->getParams()['SelectionCriteria']['CampaignIds'];
DB::update("UPDATE advertisements ad
INNER JOIN campaigns ca ON ad.campaign_id = ca.id
SET ad.deleted_at = now()
WHERE ca.ads_loaded_at IS NOT NULL
AND ad.updated_at <= ca.ads_loaded_at
AND ad.deleted_at is null
AND ca.external_id in (" . implode(", ", $campaign_ids) . ")");
Campaigns::whereIn('external_id', $campaign_ids)
->update([
'ads_loaded_at' => Carbon::now(),
]);
}
}
}
private function requestPrepare($filter)
......
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddAdsLoadedAtColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('campaigns', function (Blueprint $table) {
$table->timestamp('ads_loaded_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!