Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ node_modules
.php-cs-fixer.cache
.vscode
tests.txt
composer-local.json
composer-local.lock
26 changes: 26 additions & 0 deletions src/Console/GoogleMerchant/ProductsExprirationRenewCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace DescomMarket\Feeds\Console\GoogleMerchant;

use Illuminate\Console\Command;
use DescomMarket\Feeds\Google\Merchant\Services\Products\ProductsExpirationRenewService;

class ProductsExprirationRenewCommand extends Command
{
protected $signature = 'dm360:google-merchant:products:expiration:renew {--min-expiration-days=14}';

protected $description = 'Renew Google Merchant products expiration';

public function handle()
{
$minExpirationDays = $this->option('min-expiration-days');

$success = ProductsExpirationRenewService::run($minExpirationDays);

if ($success) {
$this->info('Renewing Google Merchant products expiration completed.');
} else {
$this->error('Renewing Google Merchant products expiration failed.');
}
}
}
4 changes: 3 additions & 1 deletion src/DescomMarketFeedsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DescomMarket\Feeds;

use DescomMarket\Feeds\Console\GoogleMerchant\ProductsExprirationRenewCommand;
use DescomMarket\Feeds\Console\IndexUrlCommand;
use DescomMarket\Feeds\Providers\EventServiceProvider;
use Illuminate\Console\Scheduling\Schedule;
Expand All @@ -26,6 +27,7 @@ public function boot()

$this->commands([
IndexUrlCommand::class,
ProductsExprirationRenewCommand::class,
]);

$this->registerScheduler();
Expand All @@ -34,7 +36,7 @@ public function boot()

private function registerScheduler()
{
if (! config('feeds-google.index.enabled')) {
if (!config('feeds-google.index.enabled')) {
return;
}

Expand Down
6 changes: 2 additions & 4 deletions src/Google/GoogleServiceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,18 @@ private static function service(string $serviceClassName, string|array $scopes)
{
$credentials = config('feeds-google.api.credentials.path');

if (! $credentials) {
if (!$credentials) {
throw new \Exception('No can connect to Google without credentials json file');
}

if (! self::$client) {
if (!self::$client) {
self::$client = new Client();

self::$client->setAuthConfig($credentials);

self::$client->addScope($scopes);

}


return new $serviceClassName(self::$client);
}
}
41 changes: 41 additions & 0 deletions src/Google/Merchant/Jobs/InsertProductInGoogleMerchantJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace DescomMarket\Feeds\Google\Merchant\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use DescomMarket\Common\Repositories\Catalog\Products\ProductRepository;
use DescomMarket\Feeds\Google\Merchant\Services\Products\ProductInsertService;


class InsertProductInGoogleMerchantJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public $delay;
public $tries;

public function __construct(private int $productId)
{
$this->delay = config('feeds-google.merchant.queue.delay', 60);
$this->tries = config('feeds-google.merchant.queue.tries', 10);
$this->queue = config('feeds-google.merchant.queue.name', 'google_merchant');
$this->connection = config('feeds-google.merchant.queue.connection', 'sync');
}

public function handle()
{
$product = ProductRepository::get($this->productId);

if (!$product) {
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Falta verificar los filtros


$command = new ProductInsertService();

$command->run($product);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace DescomMarket\Feeds\Google\Merchant\Services\Products;

use Carbon\Carbon;
use DescomMarket\Feeds\Google\GoogleServiceBuilder;
use DescomMarket\Feeds\Google\Merchant\Jobs\InsertProductInGoogleMerchantJob;

class ProductsExpirationRenewService
{
private string $merchantId;
private int $maxResults;
private ?string $pageToken;
private ?int $minExpirationDays;

public function __construct()
{
$this->merchantId = config('feeds-google.merchant.id') ?? '';
$this->maxResults = 250;
$this->pageToken = null;
}

public static function run($minExpirationDays = null): bool
{
$self = new self();
$self->minExpirationDays = $minExpirationDays;

if (!$self->merchantId || !$self->minExpirationDays) {
return false;
}

do {
$productsStatuses = $self->getProductsStatuses();
$self->renewProductExpiration($productsStatuses);
} while ($self->pageToken !== null);

return true;
}

private function getProductsStatuses()
{
$productsStatuses = GoogleServiceBuilder::googleMerchant()
->productstatuses
->listProductstatuses($this->merchantId, [
'maxResults' => $this->maxResults,
'pageToken' => $this->pageToken,
]);

$this->pageToken = $productsStatuses->getNextPageToken() ?? null;

return $productsStatuses->getResources();
}

private function renewProductExpiration($productsStatuses = [])
{
foreach ($productsStatuses as $productStatus) {

$googleExpirationDate = Carbon::parse($productStatus->getGoogleExpirationDate());

if (Carbon::now()->diffInDays($googleExpirationDate) < $this->minExpirationDays) {
$productId = $this->cleanProductId($productStatus->getProductId());
InsertProductInGoogleMerchantJob::dispatch($productId);
}
}
}

private function cleanProductId(string $productId): int
{
return str_replace('online:es:ES:', '', $productId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function transform(array $productData): Product

$offer = self::offer($productData);

if (! is_null($offer)) {
if (!is_null($offer)) {
$price = new Price();
$price->setValue($offer);
$price->setCurrency('EUR');
Expand Down