resolveOffer((string) $this->argument('offer')); if (! $offer instanceof ProductOffer) { $this->error('Offerta Amazon non trovata.'); return self::FAILURE; } try { $result = $publisher->publishOffer( $offer->loadMissing('product'), trim((string) $this->option('chat')), (bool) $this->option('dry-run') ); } catch (RuntimeException $e) { $this->error($e->getMessage()); return self::FAILURE; } $this->table( ['Campo', 'Valore'], [ ['Offer ID', (string) $offer->id], ['Prodotto', trim((string) ($offer->product?->internal_code ?? '')) . ' ยท ' . trim((string) ($offer->title ?: $offer->product?->name ?: 'Prodotto Amazon'))], ['Dry run', ! empty($result['dry_run']) ? 'si' : 'no'], ['Chat', (string) ($result['chat_id'] ?? '')], ['Canale', (string) ($result['channel_name'] ?? '')], ['Metodo', (string) ($result['method'] ?? 'preview')], ['Esito', ! empty($result['ok']) ? 'ok' : 'errore'], ['Immagine', (string) ($result['image_url'] ?? '')], ] ); $this->line(''); $this->line((string) ($result['message'] ?? '')); if (empty($result['ok'])) { $this->line(''); $this->line(json_encode($result['response'] ?? [], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) ?: '{}'); return self::FAILURE; } return self::SUCCESS; } private function resolveOffer(string $value): ?ProductOffer { $value = trim($value); if ($value === '') { return null; } $query = ProductOffer::query() ->with('product') ->where('source_type', 'amazon_creators_api') ->latest('last_seen_at') ->latest('id'); if (is_numeric($value)) { $offer = (clone $query)->find((int) $value); if ($offer instanceof ProductOffer) { return $offer; } } return (clone $query) ->where('external_product_id', $value) ->orWhereHas('product', static function ($productQuery) use ($value): void { $productQuery->where('internal_code', $value); }) ->first(); } }