|
2 | 2 |
|
3 | 3 | namespace Eclipse\World\Jobs; |
4 | 4 |
|
5 | | -use Eclipse\Core\Models\User; |
| 5 | +use Eclipse\Common\Foundation\Jobs\QueueableJob; |
6 | 6 | use Eclipse\World\Models\Currency; |
7 | | -use Eclipse\World\Notifications\ImportFinishedNotification; |
8 | 7 | use Exception; |
9 | | -use Illuminate\Bus\Queueable; |
10 | | -use Illuminate\Contracts\Queue\ShouldQueue; |
11 | | -use Illuminate\Foundation\Bus\Dispatchable; |
12 | | -use Illuminate\Queue\InteractsWithQueue; |
13 | | -use Illuminate\Queue\SerializesModels; |
14 | | -use Illuminate\Support\Facades\Log; |
15 | 8 |
|
16 | | -class ImportCurrencies implements ShouldQueue |
| 9 | +class ImportCurrencies extends QueueableJob |
17 | 10 | { |
18 | | - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
19 | | - |
20 | 11 | public int $timeout = 60; |
21 | 12 |
|
22 | 13 | public bool $failOnTimeout = true; |
23 | 14 |
|
24 | | - public ?int $userId; |
25 | | - |
26 | | - public string $locale; |
27 | | - |
28 | | - /** |
29 | | - * Create a new job instance. |
30 | | - */ |
31 | | - public function __construct(?int $userId, string $locale = 'en') |
| 15 | + protected function execute(): void |
32 | 16 | { |
33 | | - $this->userId = $userId; |
34 | | - $this->locale = $locale; |
35 | | - } |
| 17 | + // Load existing currencies into an associative array |
| 18 | + $existingCurrencies = Currency::withTrashed()->get()->keyBy('id'); |
36 | 19 |
|
37 | | - public function handle(): void |
38 | | - { |
39 | | - Log::info('Starting currencies import'); |
| 20 | + // Load new currency data from REST Countries API |
| 21 | + $countries = json_decode(file_get_contents('https://raw.githubusercontent.com/mledoze/countries/master/dist/countries.json'), true); |
40 | 22 |
|
41 | | - $user = $this->userId ? User::find($this->userId) : null; |
42 | | - |
43 | | - try { |
44 | | - // Load existing currencies into an associative array |
45 | | - $existingCurrencies = Currency::withTrashed()->get()->keyBy('id'); |
| 23 | + if (! $countries) { |
| 24 | + throw new Exception('Failed to fetch or parse countries data'); |
| 25 | + } |
46 | 26 |
|
47 | | - // Load new currency data from REST Countries API |
48 | | - $countries = json_decode(file_get_contents('https://raw.githubusercontent.com/mledoze/countries/master/dist/countries.json'), true); |
| 27 | + $processedCurrencies = []; |
49 | 28 |
|
50 | | - if (! $countries) { |
51 | | - throw new Exception('Failed to fetch or parse countries data'); |
| 29 | + foreach ($countries as $rawData) { |
| 30 | + if (! $rawData['independent'] || empty($rawData['currencies'])) { |
| 31 | + continue; |
52 | 32 | } |
53 | 33 |
|
54 | | - $processedCurrencies = []; |
55 | | - |
56 | | - foreach ($countries as $rawData) { |
57 | | - if (! $rawData['independent'] || empty($rawData['currencies'])) { |
| 34 | + foreach ($rawData['currencies'] as $currencyCode => $currencyData) { |
| 35 | + // Skip if we've already processed this currency |
| 36 | + if (isset($processedCurrencies[$currencyCode])) { |
58 | 37 | continue; |
59 | 38 | } |
60 | 39 |
|
61 | | - foreach ($rawData['currencies'] as $currencyCode => $currencyData) { |
62 | | - // Skip if we've already processed this currency |
63 | | - if (isset($processedCurrencies[$currencyCode])) { |
64 | | - continue; |
65 | | - } |
66 | | - |
67 | | - $data = [ |
68 | | - 'id' => $currencyCode, |
69 | | - 'name' => $currencyData['name'], |
70 | | - 'is_active' => true, |
71 | | - ]; |
| 40 | + $data = [ |
| 41 | + 'id' => $currencyCode, |
| 42 | + 'name' => $currencyData['name'], |
| 43 | + 'is_active' => true, |
| 44 | + ]; |
72 | 45 |
|
73 | | - if (isset($existingCurrencies[$currencyCode])) { |
74 | | - $existingCurrencies[$currencyCode]->update($data); |
75 | | - } else { |
76 | | - Currency::create($data); |
77 | | - } |
78 | | - |
79 | | - $processedCurrencies[$currencyCode] = true; |
| 46 | + if (isset($existingCurrencies[$currencyCode])) { |
| 47 | + $existingCurrencies[$currencyCode]->update($data); |
| 48 | + } else { |
| 49 | + Currency::create($data); |
80 | 50 | } |
81 | | - } |
82 | 51 |
|
83 | | - Log::info('Currencies import completed'); |
84 | | - if ($user) { |
85 | | - $user->notify(new ImportFinishedNotification('success', 'currencies', null, $this->locale)); |
86 | | - } |
87 | | - } catch (Exception $e) { |
88 | | - Log::error('Currencies import failed: '.$e->getMessage()); |
89 | | - if ($user) { |
90 | | - $user->notify(new ImportFinishedNotification('failed', 'currencies', null, $this->locale)); |
| 52 | + $processedCurrencies[$currencyCode] = true; |
91 | 53 | } |
92 | | - throw $e; |
93 | 54 | } |
94 | 55 | } |
| 56 | + |
| 57 | + protected function getJobName(): string |
| 58 | + { |
| 59 | + return __('eclipse-world::currencies.import.job_name', [], $this->locale); |
| 60 | + } |
| 61 | + |
| 62 | + protected function getNotificationTitle(): string |
| 63 | + { |
| 64 | + return __("eclipse-world::currencies.notifications.{$this->status->value}.title", [], $this->locale); |
| 65 | + } |
95 | 66 | } |
0 commit comments