Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Письменов Дмитрий Иванович
/
yourroomads
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 5f316da6
authored
May 21, 2021
by
Vladislav
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#19456 Запрос измененных РК и сохранение инфы об этом
1 parent
2cf96902
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
345 additions
and
125 deletions
app/Console/Commands/CampaignsCheckChange.php
app/Console/Commands/CampaignsLoadUpdated.php
app/Models/Campaigns.php
app/Models/Pivots/DictionaryCampaign.php
app/Models/Tokens.php
app/Service/Requests/Direct/CheckCampaignsChange.php
app/Service/Requests/Direct/GetCampaigns.php
database/migrations/2021_05_17_141740_create_dictionary_campaigns_table.php
tests/Unit/CheckCampaignsTest.php
tests/Unit/GetCampaignsTest.php
app/Console/Commands/CampaignsCheckChange.php
View file @
5f316da
...
@@ -40,16 +40,26 @@ class CampaignsCheckChange extends Command
...
@@ -40,16 +40,26 @@ class CampaignsCheckChange extends Command
*/
*/
public
function
handle
()
public
function
handle
()
{
{
$token
=
Tokens
::
where
(
'type'
,
Tokens
::
MAIN
)
->
first
();
$token_main
=
Tokens
::
where
(
'type'
,
Tokens
::
MAIN
)
->
first
();
if
(
!
$token
){
throw
new
\Exception
(
'Не найден токен блин'
);
if
(
$token_main
){
$factory
=
APIRequest
::
getInstance
(
API
::
YANDEX
);
$factory
->
setToken
(
$token_main
);
$factory
->
getRequest
(
'change'
,
'checkCampaigns'
)
->
call
();
}
}
$
factory
=
APIRequest
::
getInstance
(
API
::
YANDEX
);
$
tokens
=
Tokens
::
has
(
'dictionaryCampaignsForExternal'
)
$factory
->
setToken
(
$token
);
->
where
(
'type'
,
'!='
,
Tokens
::
MAIN
)
->
get
(
);
$factory
->
getRequest
(
'change'
,
'checkCampaigns'
)
foreach
(
$tokens
as
$token
)
{
->
call
();
$factory
=
APIRequest
::
getInstance
(
API
::
YANDEX
);
$factory
->
setToken
(
$token
);
$factory
->
getRequest
(
'change'
,
'checkCampaigns'
)
->
call
();
}
return
0
;
return
0
;
}
}
...
...
app/Console/Commands/CampaignsLoadUpdated.php
View file @
5f316da
...
@@ -4,6 +4,7 @@ namespace App\Console\Commands;
...
@@ -4,6 +4,7 @@ namespace App\Console\Commands;
use
App\Jobs\ProcessCallLimitedAPI
;
use
App\Jobs\ProcessCallLimitedAPI
;
use
App\Models\Campaigns
;
use
App\Models\Campaigns
;
use
App\Models\Pivots\DictionaryCampaign
;
use
App\Models\Tokens
;
use
App\Models\Tokens
;
use
App\Service\API\API
;
use
App\Service\API\API
;
use
App\Service\Direct\CheckDictionaries
;
use
App\Service\Direct\CheckDictionaries
;
...
@@ -46,12 +47,12 @@ class CampaignsLoadUpdated extends Command
...
@@ -46,12 +47,12 @@ class CampaignsLoadUpdated extends Command
public
function
handle
()
public
function
handle
()
{
{
$campaigns
=
Campaigns
::
forUpdatedSelf
()
->
get
();
$campaigns
=
Campaigns
::
forUpdatedSelf
()
->
get
();
if
(
!
$campaigns
->
count
()){
if
(
!
$campaigns
->
count
())
{
return
;
return
;
}
}
$token
=
Tokens
::
where
(
'type'
,
Tokens
::
MAIN
)
->
first
();
$token
=
Tokens
::
where
(
'type'
,
Tokens
::
MAIN
)
->
first
();
if
(
!
$token
){
if
(
!
$token
)
{
throw
new
\Exception
(
'Не найден токен блин'
);
throw
new
\Exception
(
'Не найден токен блин'
);
}
}
...
@@ -59,12 +60,29 @@ class CampaignsLoadUpdated extends Command
...
@@ -59,12 +60,29 @@ class CampaignsLoadUpdated extends Command
$factory
->
setToken
(
$token
);
$factory
->
setToken
(
$token
);
$ids
=
[];
$ids
=
[];
foreach
(
$campaigns
as
$campaign
){
foreach
(
$campaigns
as
$campaign
)
{
$ids
[]
=
$campaign
[
'external_id'
];
$ids
[]
=
$campaign
[
'external_id'
];
}
}
if
(
!
empty
(
$ids
)){
$factory
->
getRequest
(
'campaigns'
,
'get'
)
->
call
([
$factory
->
getRequest
(
'campaigns'
,
'get'
)
->
call
([
'ids'
=>
$ids
]);
'ids'
=>
$campaigns
->
map
(
function
(
Campaigns
$campaign
)
{
return
$campaign
->
external_id
;
})
]);
$tokens
=
Tokens
::
has
(
'dictionaryCampaignsForExternalSynchronizedUpdatedSelf'
)
->
with
(
'dictionaryCampaignsForExternalSynchronizedUpdatedSelf'
)
->
where
(
'type'
,
'!='
,
Tokens
::
MAIN
)
->
get
();
foreach
(
$tokens
as
$token
)
{
$factory
=
APIRequest
::
getInstance
(
API
::
YANDEX
);
$factory
->
setToken
(
$token
);
$factory
->
getRequest
(
'change'
,
'checkCampaigns'
)
->
call
([
'ids'
=>
$token
->
dictionaryCampaignsForExternalSynchronizedUpdatedSelf
->
map
(
function
(
DictionaryCampaign
$dictionaryCampaign
)
{
return
$dictionaryCampaign
->
external_id
;
})
]);
}
}
return
0
;
return
0
;
...
...
app/Models/Campaigns.php
View file @
5f316da
...
@@ -4,6 +4,7 @@ namespace App\Models;
...
@@ -4,6 +4,7 @@ namespace App\Models;
use
App\Models\Pivots\DictionaryCampaignVariable
;
use
App\Models\Pivots\DictionaryCampaignVariable
;
use
App\Models\Pivots\DictionaryCampaign
;
use
App\Models\Pivots\DictionaryCampaign
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Factories\HasFactory
;
use
Illuminate\Database\Eloquent\Factories\HasFactory
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Model
;
...
@@ -42,15 +43,13 @@ use Illuminate\Database\Eloquent\Model;
...
@@ -42,15 +43,13 @@ use Illuminate\Database\Eloquent\Model;
* @property-read int|null $dictionary_campaign_variables_count
* @property-read int|null $dictionary_campaign_variables_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Variable[] $dictionaryCampaigns
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Variable[] $dictionaryCampaigns
* @property-read int|null $dictionary_campaigns_count
* @property-read int|null $dictionary_campaigns_count
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns forEnabled()
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns forGroupsLoadable()
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns forGroupsLoadable()
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns forManaged()
* @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 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()
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns notForEnabled()
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns notForManaged()
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns query()
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns query()
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns whereAttributionModel($value)
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns whereAttributionModel($value)
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns whereBlockedIps($value)
* @method static \Illuminate\Database\Eloquent\Builder|Campaigns whereBlockedIps($value)
...
@@ -126,8 +125,7 @@ class Campaigns extends Model
...
@@ -126,8 +125,7 @@ class Campaigns extends Model
{
{
parent
::
boot
();
parent
::
boot
();
static
::
created
(
function
(
Campaigns
$campaign
)
static
::
created
(
function
(
Campaigns
$campaign
)
{
{
if
(
$campaign
->
manage
)
{
if
(
$campaign
->
manage
)
{
Dictionary
::
whereNotNull
(
'token_id'
)
->
each
(
function
(
Dictionary
$dictionary
)
use
(
$campaign
)
{
Dictionary
::
whereNotNull
(
'token_id'
)
->
each
(
function
(
Dictionary
$dictionary
)
use
(
$campaign
)
{
$campaign
->
dictionaries
()
->
syncWithoutDetaching
([
$campaign
->
dictionaries
()
->
syncWithoutDetaching
([
...
@@ -137,8 +135,7 @@ class Campaigns extends Model
...
@@ -137,8 +135,7 @@ class Campaigns extends Model
}
}
});
});
static
::
updated
(
function
(
Campaigns
$campaign
)
static
::
updated
(
function
(
Campaigns
$campaign
)
{
{
if
(
$campaign
->
manage
!==
$campaign
->
getOriginal
(
'manage'
))
{
if
(
$campaign
->
manage
!==
$campaign
->
getOriginal
(
'manage'
))
{
if
(
$campaign
->
manage
)
{
if
(
$campaign
->
manage
)
{
Dictionary
::
whereNotNull
(
'token_id'
)
->
each
(
function
(
Dictionary
$dictionary
)
use
(
$campaign
)
{
Dictionary
::
whereNotNull
(
'token_id'
)
->
each
(
function
(
Dictionary
$dictionary
)
use
(
$campaign
)
{
...
@@ -150,6 +147,13 @@ class Campaigns extends Model
...
@@ -150,6 +147,13 @@ class Campaigns extends Model
$campaign
->
dictionaries
()
->
detach
();
$campaign
->
dictionaries
()
->
detach
();
}
}
}
}
if
(
DictionaryCampaign
::
getPropertiesCopyWithPivot
()
->
first
(
function
(
$property_name
)
use
(
$campaign
)
{
return
$campaign
->
{
$property_name
}
!==
$campaign
->
getOriginal
(
$property_name
);
}))
{
$campaign
->
dictionaryCampaigns
()
->
synchronized
()
->
update
(
DictionaryCampaign
::
copyPropertyInCampaign
(
$campaign
)
);
}
});
});
}
}
...
@@ -176,38 +180,29 @@ class Campaigns extends Model
...
@@ -176,38 +180,29 @@ class Campaigns extends Model
->
withTimestamps
();
->
withTimestamps
();
}
}
public
function
scopeForUpdatedSelf
(
$query
)
public
function
scopeForUpdatedSelf
(
Builder
$query
)
{
{
$query
->
whereNotNull
(
'updated_self'
);
$query
->
whereNotNull
(
'updated_self'
);
}
}
public
function
scopeForUpdatedChildren
(
$query
)
public
function
scopeForUpdatedChildren
(
Builder
$query
)
{
{
$query
->
whereNotNull
(
'updated_children'
);
$query
->
whereNotNull
(
'updated_children'
);
}
}
public
function
scopeForGroupsLoadable
(
$query
)
public
function
scopeForGroupsLoadable
(
Builder
$query
)
{
{
$query
->
whereNull
(
'groups_loaded_at'
);
$query
->
whereNull
(
'groups_loaded_at'
);
}
}
public
function
scopeForManaged
(
$query
)
public
function
scopeForManaged
(
Builder
$query
,
$manage
=
true
)
{
$query
->
where
(
'manage'
,
true
);
}
public
function
scopeNotForManaged
(
$query
)
{
{
$query
->
where
(
'manage'
,
fals
e
);
$query
->
where
(
'manage'
,
$manag
e
);
}
}
public
function
scopeForEnabled
(
$query
)
public
function
scopeForEnabled
(
Builder
$query
,
$enabled
=
true
)
{
{
$query
->
where
(
'enabled'
,
true
);
$query
->
where
(
'enabled'
,
$enabled
);
}
}
public
function
scopeNotForEnabled
(
$query
)
{
$query
->
where
(
'enabled'
,
false
);
}
}
}
app/Models/Pivots/DictionaryCampaign.php
View file @
5f316da
...
@@ -8,6 +8,7 @@ use App\Models\Limits;
...
@@ -8,6 +8,7 @@ use App\Models\Limits;
use
App\Models\Variable
;
use
App\Models\Variable
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Relations\Pivot
;
use
Illuminate\Database\Eloquent\Relations\Pivot
;
use
Illuminate\Support\Collection
;
/**
/**
* App\Models\Pivots\DictionaryCampaign
* App\Models\Pivots\DictionaryCampaign
...
@@ -24,13 +25,22 @@ use Illuminate\Database\Eloquent\Relations\Pivot;
...
@@ -24,13 +25,22 @@ use Illuminate\Database\Eloquent\Relations\Pivot;
* @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 int $updated
* @property bool $updated
* @property \Illuminate\Support\Carbon|null $created_at
* @property bool $synced
* @property \Illuminate\Support\Carbon|null $external_upload_at
* @property \Illuminate\Support\Carbon|null $created_atd
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @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|Limits forNotExternal()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign forExternal()
* @method static \Illuminate\Database\Eloquent\Builder|Limits joinDictionaries()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign forNotExternal()
* @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 synchronized()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign notExternalUpdated()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign forUpdatedSelf()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign forUpdatedChildren()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign joinDictionaries()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign query()
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign query()
...
@@ -44,7 +54,6 @@ use Illuminate\Database\Eloquent\Relations\Pivot;
...
@@ -44,7 +54,6 @@ use Illuminate\Database\Eloquent\Relations\Pivot;
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign whereNegativeKeywords($value)
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign whereNegativeKeywords($value)
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign whereUpdated($value)
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign whereUpdated($value)
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|DictionaryCampaign whereUpdatedAt($value)
* @property string|null $external_upload_at
* @method static Builder|DictionaryCampaign whereExternalUploadAt($value)
* @method static Builder|DictionaryCampaign whereExternalUploadAt($value)
*/
*/
class
DictionaryCampaign
extends
Pivot
class
DictionaryCampaign
extends
Pivot
...
@@ -60,6 +69,9 @@ class DictionaryCampaign extends Pivot
...
@@ -60,6 +69,9 @@ class DictionaryCampaign extends Pivot
'negative_keywords'
,
'negative_keywords'
,
'excluded_sites'
,
'excluded_sites'
,
'external_upload_at'
,
'external_upload_at'
,
'external_updated_at'
,
'updated_self'
,
'updated_children'
,
'updated'
,
'updated'
,
'synced'
,
'synced'
,
];
];
...
@@ -71,50 +83,108 @@ class DictionaryCampaign extends Pivot
...
@@ -71,50 +83,108 @@ class DictionaryCampaign extends Pivot
'negative_keywords'
=>
'array'
,
'negative_keywords'
=>
'array'
,
'excluded_sites'
=>
'array'
,
'excluded_sites'
=>
'array'
,
'external_upload_at'
=>
'datetime'
,
'external_upload_at'
=>
'datetime'
,
'external_updated_at'
=>
'datetime'
,
'updated_self'
=>
'datetime'
,
'updated_children'
=>
'datetime'
,
'updated'
=>
'boolean'
,
'updated'
=>
'boolean'
,
'synced'
=>
'boolean'
,
'synced'
=>
'boolean'
,
];
];
static
public
function
get
ExcludeCopy
WithPivot
()
static
public
function
getWithPivot
()
{
{
return
[
return
[
'name'
,
'negative_keywords'
,
'excluded_sites'
,
'external_upload_at'
,
'external_upload_at'
,
'external_updated_at'
,
'updated_self'
,
'updated_children'
,
'updated'
,
'updated'
,
'synced'
,
'synced'
,
];
];
}
}
static
public
function
getWithPivot
()
/**
* @return Collection
*/
static
public
function
getPropertiesCopyWithPivot
()
{
{
return
[
return
collect
(
[
'name'
,
'name'
,
'negative_keywords'
,
'negative_keywords'
,
'excluded_sites'
,
'excluded_sites'
,
'external_upload_at'
,
]);
'updated'
,
'synced'
,
];
}
}
static
public
function
copyPropertyInCampaign
(
Campaigns
$campaign
)
/**
* @param Campaigns|array $campaign
*/
static
public
function
copyPropertyInCampaign
(
$campaign
)
{
{
return
collect
(
self
::
getWithPivot
())
return
self
::
getPropertiesCopyWithPivot
()
->
filter
(
function
(
$property_name
){
return
!
in_array
(
$property_name
,
self
::
getExcludeCopyWithPivot
());
})
->
transform
(
function
(
$property_name
)
use
(
$campaign
)
{
->
transform
(
function
(
$property_name
)
use
(
$campaign
)
{
$value
=
null
;
if
(
$campaign
instanceof
Campaigns
)
{
$value
=
$campaign
->
{
$property_name
};
}
elseif
(
is_array
(
$campaign
)
&&
isset
(
$campaign
[
$property_name
]))
{
$value
=
$campaign
[
$property_name
];
}
return
[
return
[
$property_name
=>
$campaign
->
{
$property_name
}
$property_name
=>
$campaign
->
{
$property_name
}
];
];
})
->
flatMap
(
function
(
$val
){
})
->
flatMap
(
function
(
$val
)
{
return
$val
;
return
$val
;
})
->
all
();
})
->
all
();
}
}
public
function
scopeForExternal
(
Builder
$query
)
{
return
$query
->
whereNotNull
(
'external_id'
);
}
public
function
scopeForNotExternal
(
Builder
$query
)
public
function
scopeForNotExternal
(
Builder
$query
)
{
{
return
$query
->
whereNull
(
'external_id'
);
return
$query
->
whereNull
(
'external_id'
);
}
}
public
function
scopeUpdated
(
Builder
$query
,
$updated
=
true
)
{
return
$query
->
where
(
'updated'
,
$updated
);
}
public
function
scopeSynced
(
Builder
$query
,
$synced
=
true
)
{
return
$query
->
where
(
'synced'
,
$synced
);
}
public
function
scopeSynchronized
(
Builder
$query
)
{
return
$query
->
synced
()
->
where
(
function
(
Builder
$query
)
{
$query
->
updated
()
->
orWhere
(
function
(
Builder
$query
)
{
$query
->
updated
(
false
)
->
notExternalUpdated
();
});
});
}
public
function
scopeNotExternalUpdated
(
Builder
$query
)
{
$query
->
whereNull
(
'external_updated_at'
);
}
public
function
scopeForUpdatedSelf
(
Builder
$query
)
{
$query
->
whereNotNull
(
'updated_self'
);
}
public
function
scopeForUpdatedChildren
(
Builder
$query
)
{
$query
->
whereNotNull
(
'updated_children'
);
}
public
function
scopeJoinDictionaries
(
Builder
$query
)
public
function
scopeJoinDictionaries
(
Builder
$query
)
{
{
$dictionary_model
=
Dictionary
::
getModel
();
$dictionary_model
=
Dictionary
::
getModel
();
...
...
app/Models/Tokens.php
View file @
5f316da
...
@@ -50,7 +50,12 @@ class Tokens extends Model
...
@@ -50,7 +50,12 @@ class Tokens extends Model
CONST
MAIN
=
'main'
;
CONST
MAIN
=
'main'
;
CONST
GOAL
=
'goal'
;
CONST
GOAL
=
'goal'
;
protected
$fillable
=
[
'token'
,
'login'
,
'type'
];
protected
$fillable
=
[
'token'
,
'login'
,
'type'
,
'timestamp'
,
];
public
function
scopeFilter
(
$query
,
array
$filters
)
public
function
scopeFilter
(
$query
,
array
$filters
)
{
{
...
@@ -90,15 +95,22 @@ class Tokens extends Model
...
@@ -90,15 +95,22 @@ class Tokens extends Model
{
{
return
$this
->
hasManyThrough
(
DictionaryCampaign
::
class
,
Dictionary
::
class
,
'token_id'
,
'dictionary_id'
)
return
$this
->
hasManyThrough
(
DictionaryCampaign
::
class
,
Dictionary
::
class
,
'token_id'
,
'dictionary_id'
)
->
orderBy
(
DictionaryCampaign
::
getModel
()
->
getTable
()
.
'.created_at'
,
'ASC'
)
->
orderBy
(
DictionaryCampaign
::
getModel
()
->
getTable
()
.
'.created_at'
,
'ASC'
)
->
forNotExternal
(
);
->
orderBy
(
DictionaryCampaign
::
getModel
()
->
getTable
()
.
'.'
.
DictionaryCampaign
::
getModel
()
->
getKeyName
(),
'ASC'
);
}
}
public
function
dictionaryCampaignsForNotExternal
()
public
function
dictionaryCampaignsForNotExternal
()
{
{
return
$this
->
dictionaryCampaigns
()
return
$this
->
dictionaryCampaigns
()
->
forNotExternal
();
->
orderBy
(
DictionaryCampaign
::
getModel
()
->
getTable
()
.
'.created_at'
,
'ASC'
)
}
->
orderBy
(
DictionaryCampaign
::
getModel
()
->
getTable
()
.
'.'
.
DictionaryCampaign
::
getModel
()
->
getKeyName
(),
'ASC'
)
->
forNotExternal
();
public
function
dictionaryCampaignsForExternal
()
{
return
$this
->
dictionaryCampaigns
()
->
forExternal
();
}
public
function
dictionaryCampaignsForExternalSynchronizedUpdatedSelf
()
{
return
$this
->
dictionaryCampaignsForExternal
()
->
synchronized
()
->
forUpdatedSelf
();
}
}
public
function
campaignsForManaged
()
public
function
campaignsForManaged
()
...
@@ -108,6 +120,6 @@ class Tokens extends Model
...
@@ -108,6 +120,6 @@ class Tokens extends Model
public
function
campaignsNotForManaged
()
public
function
campaignsNotForManaged
()
{
{
return
$this
->
campaigns
()
->
notForManaged
(
);
return
$this
->
campaigns
()
->
forManaged
(
false
);
}
}
}
}
app/Service/Requests/Direct/CheckCampaignsChange.php
View file @
5f316da
...
@@ -4,6 +4,7 @@ namespace App\Service\Requests\Direct;
...
@@ -4,6 +4,7 @@ namespace App\Service\Requests\Direct;
use
App\Jobs\ProcessCallLimitedAPI
;
use
App\Jobs\ProcessCallLimitedAPI
;
use
App\Models\Campaigns
;
use
App\Models\Campaigns
;
use
App\Models\Pivots\DictionaryCampaign
;
use
App\Service\API\API
;
use
App\Service\API\API
;
use
App\Service\Requests\APIRequest
;
use
App\Service\Requests\APIRequest
;
use
App\Service\Requests\DirectRequest
;
use
App\Service\Requests\DirectRequest
;
...
@@ -26,27 +27,39 @@ class CheckCampaignsChange extends DirectRequest
...
@@ -26,27 +27,39 @@ class CheckCampaignsChange extends DirectRequest
function
handle
(
$response
)
function
handle
(
$response
)
{
{
foreach
(
$response
[
'result'
][
'Campaigns'
]
as
$campaign
)
{
foreach
(
$response
[
'result'
][
'Campaigns'
]
as
$campaign_data
)
{
if
(
in_array
(
self
::
SELF
,
$campaign
[
'ChangesIn'
])
||
in_array
(
self
::
CHILDREN
,
$campaign
[
'ChangesIn'
]))
{
if
(
in_array
(
self
::
SELF
,
$campaign_data
[
'ChangesIn'
])
||
in_array
(
self
::
CHILDREN
,
$campaign_data
[
'ChangesIn'
]))
{
$data
=
[
$data
=
[];
'token'
=>
$this
->
getToken
()
->
id
,
];
if
(
in_array
(
self
::
SELF
,
$campaign
[
'ChangesIn'
]))
{
if
(
in_array
(
self
::
SELF
,
$campaign
_data
[
'ChangesIn'
]))
{
$data
[
'updated_self'
]
=
Carbon
::
now
();
$data
[
'updated_self'
]
=
Carbon
::
now
();
}
}
if
(
in_array
(
self
::
CHILDREN
,
$campaign
[
'ChangesIn'
]))
{
if
(
in_array
(
self
::
CHILDREN
,
$campaign
_data
[
'ChangesIn'
]))
{
$data
[
'updated_children'
]
=
Carbon
::
now
();
$data
[
'updated_children'
]
=
Carbon
::
now
();
}
}
Campaigns
::
firstOrCreate
([
$external_id
=
$campaign_data
[
'CampaignId'
];
'external_id'
=>
$campaign
[
'CampaignId'
]
],
$data
);
if
(
$this
->
getToken
()
->
isMain
())
{
$data
[
'token'
]
=
$this
->
getToken
()
->
getKey
();
Campaigns
::
firstOrCreate
([
'external_id'
=>
$external_id
],
$data
);
}
else
{
$dictionaryCampaign
=
DictionaryCampaign
::
synchronized
()
->
find
(
$external_id
);
if
(
$dictionaryCampaign
)
{
$data
[
'external_updated_at'
]
=
Carbon
::
now
();
$dictionaryCampaign
->
update
(
$data
);
}
}
}
}
}
}
$this
->
getToken
()
->
timestamp
=
$response
[
'result'
][
'Timestamp'
];
$this
->
getToken
()
->
update
([
$this
->
getToken
()
->
save
();
'timestamp'
=>
$response
[
'result'
][
'Timestamp'
],
]);
$this
->
setToken
(
$this
->
getToken
()
->
refresh
());
}
}
private
function
requestPrepare
(
$params
)
private
function
requestPrepare
(
$params
)
...
...
app/Service/Requests/Direct/GetCampaigns.php
View file @
5f316da
...
@@ -4,6 +4,7 @@ namespace App\Service\Requests\Direct;
...
@@ -4,6 +4,7 @@ namespace App\Service\Requests\Direct;
use
App\Jobs\ProcessCallLimitedAPI
;
use
App\Jobs\ProcessCallLimitedAPI
;
use
App\Models\Campaigns
;
use
App\Models\Campaigns
;
use
App\Models\Pivots\DictionaryCampaign
;
use
App\Service\Requests\DirectRequest
;
use
App\Service\Requests\DirectRequest
;
use
Illuminate\Support\Facades\Log
;
use
Illuminate\Support\Facades\Log
;
...
@@ -21,45 +22,67 @@ class GetCampaigns extends DirectRequest
...
@@ -21,45 +22,67 @@ class GetCampaigns extends DirectRequest
function
handle
(
$response
)
function
handle
(
$response
)
{
{
try
{
try
{
$data
=
[];
$
campaigns_
data
=
[];
foreach
(
$response
[
'result'
][
'Campaigns'
]
as
$campaign
)
{
foreach
(
$response
[
'result'
][
'Campaigns'
]
as
$campaign
_data
)
{
$data
[]
=
[
$data
=
[
'external_id'
=>
$campaign
[
'Id'
],
'external_id'
=>
$campaign
_data
[
'Id'
],
'token'
=>
$this
->
getToken
()
->
id
,
'token'
=>
$this
->
getToken
()
->
id
,
'name'
=>
$campaign
[
'Name'
],
'name'
=>
$campaign_data
[
'Name'
],
'time_targeting'
=>
json_encode
(
$campaign
[
'TimeTargeting'
]),
'time_targeting'
=>
json_encode
(
$campaign_data
[
'TimeTargeting'
]),
'negative_keywords'
=>
json_encode
(
$campaign
[
'NegativeKeywords'
][
'Items'
]
??
[]),
'negative_keywords'
=>
json_encode
(
$campaign_data
[
'NegativeKeywords'
][
'Items'
]
??
[]),
'blocked_ips'
=>
json_encode
(
$campaign
[
'BlockedIps'
][
'Items'
]
??
[]),
'blocked_ips'
=>
json_encode
(
$campaign_data
[
'BlockedIps'
][
'Items'
]
??
[]),
'excluded_sites'
=>
json_encode
(
$campaign
[
'ExcludedSites'
][
'Items'
]
??
[]),
'excluded_sites'
=>
json_encode
(
$campaign_data
[
'ExcludedSites'
][
'Items'
]
??
[]),
'daily_budget'
=>
json_encode
(
$campaign
[
'DailyBudget'
]
??
[]),
'daily_budget'
=>
json_encode
(
$campaign_data
[
'DailyBudget'
]
??
[]),
'text_campaign_strategy_search'
=>
$campaign
[
'TextCampaign'
][
'BiddingStrategy'
][
'Search'
][
'BiddingStrategyType'
],
'text_campaign_strategy_search'
=>
$campaign_data
[
'TextCampaign'
][
'BiddingStrategy'
][
'Search'
][
'BiddingStrategyType'
],
'text_campaign_strategy_network'
=>
$campaign
[
'TextCampaign'
][
'BiddingStrategy'
][
'Network'
][
'BiddingStrategyType'
],
'text_campaign_strategy_network'
=>
$campaign_data
[
'TextCampaign'
][
'BiddingStrategy'
][
'Network'
][
'BiddingStrategyType'
],
'settings'
=>
json_encode
(
$campaign
[
'TextCampaign'
][
'Settings'
]
??
[]),
'settings'
=>
json_encode
(
$campaign_data
[
'TextCampaign'
][
'Settings'
]
??
[]),
'counter_ids'
=>
json_encode
(
$campaign
[
'TextCampaign'
][
'CounterIds'
][
'Items'
]
??
[]),
'counter_ids'
=>
json_encode
(
$campaign_data
[
'TextCampaign'
][
'CounterIds'
][
'Items'
]
??
[]),
'relevant_keywords_setting_budget_percent'
=>
$campaign
[
'TextCampaign'
][
'RelevantKeywords'
][
'BudgetPercent'
],
'relevant_keywords_setting_budget_percent'
=>
$campaign_data
[
'TextCampaign'
][
'RelevantKeywords'
][
'BudgetPercent'
],
'relevant_keywords_setting_optimize_goal_id'
=>
$campaign
[
'TextCampaign'
][
'RelevantKeywords'
][
'OptimizeGoalId'
],
'relevant_keywords_setting_optimize_goal_id'
=>
$campaign_data
[
'TextCampaign'
][
'RelevantKeywords'
][
'OptimizeGoalId'
],
'attribution_model'
=>
$campaign
[
'TextCampaign'
][
'AttributionModel'
],
'attribution_model'
=>
$campaign_data
[
'TextCampaign'
][
'AttributionModel'
],
'priority_goals'
=>
json_encode
(
$campaign
[
'TextCampaign'
][
'PriorityGoals'
]),
'priority_goals'
=>
json_encode
(
$campaign_data
[
'TextCampaign'
][
'PriorityGoals'
]),
'updated_self'
=>
null
,
'updated_children'
=>
null
,
];
];
$campaigns_data
[]
=
$data
;
}
if
(
$this
->
getToken
()
->
isMain
())
{
foreach
(
$campaigns_data
as
$campaign_data
)
{
$dictionaryCampaign
=
DictionaryCampaign
::
synchronized
()
->
find
(
$campaign_data
[
'external_id'
]);
if
(
!
$dictionaryCampaign
)
continue
;
$dictionaryCampaign
->
update
(
$dictionaryCampaign
::
copyPropertyInCampaign
(
$campaign_data
)
);
}
Campaigns
::
upsert
(
$campaigns_data
,
[
'external_id'
],
[
'token'
,
'name'
,
'time_targeting'
,
'negative_keywords'
,
'blocked_ips'
,
'excluded_sites'
,
'daily_budget'
,
'text_campaign_strategy_search'
,
'text_campaign_strategy_network'
,
'settings'
,
'counter_ids'
,
'relevant_keywords_setting_budget_percent'
,
'relevant_keywords_setting_optimize_goal_id'
,
'attribution_model'
,
'priority_goals'
,
'updated_self'
,
'updated_children'
,
]);
}
else
{
}
}
Campaigns
::
upsert
(
$data
,
[
'external_id'
],
[
'token'
,
'name'
,
'time_targeting'
,
'negative_keywords'
,
'blocked_ips'
,
'excluded_sites'
,
'daily_budget'
,
'text_campaign_strategy_search'
,
'text_campaign_strategy_network'
,
'settings'
,
'counter_ids'
,
'relevant_keywords_setting_budget_percent'
,
'relevant_keywords_setting_optimize_goal_id'
,
'attribution_model'
,
]);
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
Log
::
debug
(
$e
);
Log
::
debug
(
$e
);
}
}
...
...
database/migrations/2021_05_17_141740_create_dictionary_campaigns_table.php
View file @
5f316da
...
@@ -23,7 +23,10 @@ class CreateDictionaryCampaignsTable extends Migration
...
@@ -23,7 +23,10 @@ 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
->
dateTime
(
'external_upload_at'
)
->
nullable
();
$table
->
timestamp
(
'external_upload_at'
)
->
nullable
();
$table
->
timestamp
(
'external_updated_at'
)
->
nullable
();
$table
->
timestamp
(
'updated_self'
)
->
nullable
();
$table
->
timestamp
(
'updated_children'
)
->
nullable
();
$table
->
timestamps
();
$table
->
timestamps
();
...
...
tests/Unit/CheckCampaignsTest.php
View file @
5f316da
...
@@ -5,6 +5,7 @@ namespace Tests\Unit;
...
@@ -5,6 +5,7 @@ namespace Tests\Unit;
use
App\Jobs\ProcessCallLimitedAPI
;
use
App\Jobs\ProcessCallLimitedAPI
;
use
App\Models\Account
;
use
App\Models\Account
;
use
App\Models\Campaigns
;
use
App\Models\Campaigns
;
use
App\Models\Dictionary
;
use
App\Models\Tokens
;
use
App\Models\Tokens
;
use
App\Models\User
;
use
App\Models\User
;
use
App\Service\Contract\API
;
use
App\Service\Contract\API
;
...
@@ -18,6 +19,10 @@ class CheckCampaignsTest extends TestCase
...
@@ -18,6 +19,10 @@ class CheckCampaignsTest extends TestCase
use
RefreshDatabase
;
use
RefreshDatabase
;
private
$request
;
private
$request
;
private
$user
;
private
$token_main
;
private
$token
;
private
$dictionary
;
protected
function
setUp
()
:
void
protected
function
setUp
()
:
void
{
{
...
@@ -33,47 +38,115 @@ class CheckCampaignsTest extends TestCase
...
@@ -33,47 +38,115 @@ class CheckCampaignsTest extends TestCase
'owner'
=>
true
,
'owner'
=>
true
,
]);
]);
$this
->
token_main
=
factory
(
Tokens
::
class
)
->
create
([
'type'
=>
Tokens
::
MAIN
,
'created_by'
=>
$this
->
user
->
id
]);
$this
->
token
=
factory
(
Tokens
::
class
)
->
create
([
$this
->
token
=
factory
(
Tokens
::
class
)
->
create
([
'created_by'
=>
$this
->
user
->
id
'created_by'
=>
$this
->
user
->
id
]);
$this
->
dictionary
=
factory
(
Dictionary
::
class
)
->
create
([
'token_id'
=>
$this
->
token
,
'type'
=>
Dictionary
::
CITY
,
]);
]);
$this
->
request_main
=
APIRequest
::
getInstance
(
API
::
YANDEX
)
->
setToken
(
$this
->
token_main
)
->
getRequest
(
'change'
,
'CheckCampaigns'
);
$this
->
request
=
APIRequest
::
getInstance
(
API
::
YANDEX
)
$this
->
request
=
APIRequest
::
getInstance
(
API
::
YANDEX
)
->
setToken
(
$this
->
token
)
->
setToken
(
$this
->
token
)
->
getRequest
(
'change'
,
'CheckCampaigns'
);
->
getRequest
(
'change'
,
'CheckCampaigns'
);
}
}
public
function
testCallApi
(){
public
function
testCallApi
()
{
Queue
::
fake
();
Queue
::
fake
();
Queue
::
assertNothingPushed
();
Queue
::
assertNothingPushed
();
$this
->
request_main
->
call
();
$this
->
request
->
call
();
$this
->
request
->
call
();
Queue
::
assertPushed
(
ProcessCallLimitedAPI
::
class
);
Queue
::
assertPushed
(
ProcessCallLimitedAPI
::
class
);
}
}
public
function
testHandleApi
(){
public
function
testHandleApi
()
{
$this
->
assertEquals
(
0
,
Campaigns
::
forUpdatedSelf
()
->
count
());
$this
->
assertEquals
(
0
,
Campaigns
::
forUpdatedSelf
()
->
count
());
$this
->
assertEquals
(
0
,
Campaigns
::
forUpdatedChildren
()
->
count
());
$this
->
assertEquals
(
0
,
Campaigns
::
forUpdatedChildren
()
->
count
());
$this
->
request
->
handle
([
$this
->
request
_main
->
handle
([
'result'
=>
[
'result'
=>
[
'Timestamp'
=>
'test time'
,
'Timestamp'
=>
'test time'
,
'Campaigns'
=>
[
'Campaigns'
=>
[
[
[
'ChangesIn'
=>
[
'SELF'
,
'CHILDREN'
],
'ChangesIn'
=>
[
'SELF'
,
'CHILDREN'
],
'CampaignId'
=>
1
'CampaignId'
=>
1
],
],
[
[
'ChangesIn'
=>
[
'CHILDREN'
],
'ChangesIn'
=>
[
'CHILDREN'
],
'CampaignId'
=>
2
'CampaignId'
=>
2
]
]
]
]
]
]
]);
]);
$this
->
assertEquals
(
$this
->
request
->
getToken
()
->
timestamp
,
'test time'
);
$this
->
assertEquals
(
$this
->
request
_main
->
getToken
()
->
timestamp
,
'test time'
);
$this
->
assertEquals
(
1
,
Campaigns
::
forUpdatedSelf
()
->
count
());
$this
->
assertEquals
(
1
,
Campaigns
::
forUpdatedSelf
()
->
count
());
$this
->
assertEquals
(
2
,
Campaigns
::
forUpdatedChildren
()
->
count
());
$this
->
assertEquals
(
2
,
Campaigns
::
forUpdatedChildren
()
->
count
());
$campaign
=
Campaigns
::
first
();
$this
->
actingAs
(
$this
->
user
)
->
post
(
route
(
'token.campaign.managed'
,
[
$this
->
token_main
->
getKey
(),
$campaign
->
getKey
(),
]),
[
'managed'
=>
true
,
])
->
assertStatus
(
302
);
$campaign
->
update
([
'external_id'
=>
1
,
]);
$campaign
->
refresh
();
$this
->
assertEquals
(
true
,
$campaign
->
manage
);
$this
->
assertEquals
(
1
,
$campaign
->
external_id
);
$this
->
actingAs
(
$this
->
user
)
->
post
(
route
(
'token.city.store'
,
[
$this
->
token
->
getKey
(),
$this
->
dictionary
->
getKey
(),
]))
->
assertStatus
(
302
);
$this
->
assertEquals
(
1
,
$this
->
dictionary
->
campaigns
()
->
count
());
$this
->
request
->
handle
([
'result'
=>
[
'Timestamp'
=>
'test time 2'
,
'Campaigns'
=>
[
[
'ChangesIn'
=>
[
'SELF'
,
'CHILDREN'
],
'CampaignId'
=>
1
]
]
]
]);
$this
->
assertEquals
(
$this
->
request
->
getToken
()
->
timestamp
,
'test time 2'
);
$campaign
=
$this
->
dictionary
->
campaigns
()
->
first
();
$this
->
assertNotNull
(
$campaign
->
pivot
->
external_updated_at
);
$this
->
assertNotNull
(
$campaign
->
pivot
->
updated_self
);
$this
->
assertNotNull
(
$campaign
->
pivot
->
updated_children
);
}
}
}
}
tests/Unit/GetCampaignsTest.php
View file @
5f316da
...
@@ -18,6 +18,8 @@ class GetCampaignsTest extends TestCase
...
@@ -18,6 +18,8 @@ class GetCampaignsTest extends TestCase
use
RefreshDatabase
;
use
RefreshDatabase
;
private
$request
;
private
$request
;
private
$user
;
private
$token_main
;
protected
function
setUp
()
:
void
protected
function
setUp
()
:
void
{
{
...
@@ -33,12 +35,13 @@ class GetCampaignsTest extends TestCase
...
@@ -33,12 +35,13 @@ class GetCampaignsTest extends TestCase
'owner'
=>
true
,
'owner'
=>
true
,
]);
]);
$this
->
token
=
factory
(
Tokens
::
class
)
->
create
([
$this
->
token_main
=
factory
(
Tokens
::
class
)
->
create
([
'created_by'
=>
$this
->
user
->
id
'type'
=>
Tokens
::
MAIN
,
'created_by'
=>
$this
->
user
->
id
,
]);
]);
$this
->
request
=
APIRequest
::
getInstance
(
API
::
YANDEX
)
$this
->
request
=
APIRequest
::
getInstance
(
API
::
YANDEX
)
->
setToken
(
$this
->
token
)
->
setToken
(
$this
->
token
_main
)
->
getRequest
(
'campaigns'
,
'get'
);
->
getRequest
(
'campaigns'
,
'get'
);
}
}
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment