-
Notifications
You must be signed in to change notification settings - Fork 2
Gm product expiration renew #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
llorensjj
wants to merge
4
commits into
master
Choose a base branch
from
gm_product_expiration_renew
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
26
src/Console/GoogleMerchant/ProductsExprirationRenewCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.'); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/Google/Merchant/Jobs/InsertProductInGoogleMerchantJob.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| $command = new ProductInsertService(); | ||
|
|
||
| $command->run($product); | ||
| } | ||
| } | ||
71 changes: 71 additions & 0 deletions
71
src/Google/Merchant/Services/Products/ProductsExpirationRenewService.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Falta verificar los filtros