Skip to content

Commit deb32c6

Browse files
authored
refactor(currencies): refactor currencies to use QueueableJob (WO-5)
* refactor(currencies): refactor currencies to use QueueableJob * chore(workflows): fix filament suport version
1 parent e1e3371 commit deb32c6

6 files changed

Lines changed: 60 additions & 98 deletions

File tree

resources/lang/en/currencies.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@
5858
'import' => [
5959
'action_label' => 'Import Currencies',
6060
'modal_heading' => 'Import Currencies',
61-
'success_title' => 'Import Currencies',
62-
'success_message' => 'The import currencies job has been queued.',
61+
'job_name' => 'Import Currencies',
6362
],
6463

6564
'notifications' => [
66-
'success' => [
67-
'title' => 'Currencies Import Completed',
68-
'message' => 'All currencies have been successfully imported and updated.',
65+
'queued' => [
66+
'title' => 'Currencies Import queued',
67+
],
68+
'completed' => [
69+
'title' => 'Currencies Import completed',
6970
],
7071
'failed' => [
71-
'title' => 'Currencies Import Failed',
72-
'message' => 'Failed to import currencies data.',
72+
'title' => 'Currencies Import failed',
7373
],
7474
],
7575
];

resources/lang/hr/currencies.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@
5858
'import' => [
5959
'action_label' => 'Uvezi valute',
6060
'modal_heading' => 'Uvezi valute',
61-
'success_title' => 'Uvezi valute',
62-
'success_message' => 'Posao uvoza valuta je dodan u red za izvršavanje.',
61+
'job_name' => 'Uvezi valute',
6362
],
6463

6564
'notifications' => [
66-
'success' => [
65+
'queued' => [
66+
'title' => 'Uvoz valuta je u redu čekanja',
67+
],
68+
'completed' => [
6769
'title' => 'Uvoz valuta je završen',
68-
'message' => 'Sve valute su uspješno uvezene i ažurirane.',
6970
],
7071
'failed' => [
7172
'title' => 'Uvoz valuta nije uspješan',
72-
'message' => 'Uvoz podataka o valutama nije uspješan.',
7373
],
7474
],
7575
];

resources/lang/sl/currencies.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@
5858
'import' => [
5959
'action_label' => 'Uvozi valute',
6060
'modal_heading' => 'Uvozi valute',
61-
'success_title' => 'Uvozi valute',
62-
'success_message' => 'Opravilo uvoza valut je bilo dodano v čakalno vrsto.',
61+
'job_name' => 'Uvozi valute',
6362
],
6463

6564
'notifications' => [
66-
'success' => [
65+
'queued' => [
66+
'title' => 'Uvoz valut je v čakalni vrsti',
67+
],
68+
'completed' => [
6769
'title' => 'Uvoz valut je končan',
68-
'message' => 'Vse valute so bile uspešno uvožene in posodobljene.',
6970
],
7071
'failed' => [
7172
'title' => 'Uvoz valut ni uspel',
72-
'message' => 'Uvoz podatkov o valutah ni uspel.',
7373
],
7474
],
7575
];

resources/lang/sr/currencies.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@
5858
'import' => [
5959
'action_label' => 'Uvezi valute',
6060
'modal_heading' => 'Uvoz valuta',
61-
'success_title' => 'Uvoz valuta',
62-
'success_message' => 'Zadatak za uvoz valuta je dodat u red čekanja.',
61+
'job_name' => 'Uvezi valute',
6362
],
6463

6564
'notifications' => [
66-
'success' => [
65+
'queued' => [
66+
'title' => 'Uvoz valuta je u redu čekanja',
67+
],
68+
'completed' => [
6769
'title' => 'Uvoz valuta završen',
68-
'message' => 'Sve valute su uspešno uvežene i ažurirane.',
6970
],
7071
'failed' => [
7172
'title' => 'Uvoz valuta neuspešan',
72-
'message' => 'Neuspešan uvoz podataka valuta.',
7373
],
7474
],
7575
];

src/Filament/Clusters/World/Resources/CurrencyResource/Pages/ListCurrencies.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
use Eclipse\World\Jobs\ImportCurrencies;
77
use Filament\Actions\Action;
88
use Filament\Actions\CreateAction;
9-
use Filament\Notifications\Notification;
109
use Filament\Resources\Pages\ListRecords;
11-
use Illuminate\Support\Facades\App;
1210

1311
class ListCurrencies extends ListRecords
1412
{
@@ -25,14 +23,7 @@ protected function getHeaderActions(): array
2523
->icon('heroicon-o-arrow-down-tray')
2624
->action(function () {
2725
// Dispatch the job
28-
ImportCurrencies::dispatch(auth()->id(), App::getLocale());
29-
30-
// Show notification
31-
Notification::make()
32-
->title(__('eclipse-world::currencies.import.success_title'))
33-
->body(__('eclipse-world::currencies.import.success_message'))
34-
->success()
35-
->send();
26+
ImportCurrencies::dispatch();
3627
})
3728
->requiresConfirmation(),
3829
];

src/Jobs/ImportCurrencies.php

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,94 +2,65 @@
22

33
namespace Eclipse\World\Jobs;
44

5-
use Eclipse\Core\Models\User;
5+
use Eclipse\Common\Foundation\Jobs\QueueableJob;
66
use Eclipse\World\Models\Currency;
7-
use Eclipse\World\Notifications\ImportFinishedNotification;
87
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;
158

16-
class ImportCurrencies implements ShouldQueue
9+
class ImportCurrencies extends QueueableJob
1710
{
18-
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
19-
2011
public int $timeout = 60;
2112

2213
public bool $failOnTimeout = true;
2314

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
3216
{
33-
$this->userId = $userId;
34-
$this->locale = $locale;
35-
}
17+
// Load existing currencies into an associative array
18+
$existingCurrencies = Currency::withTrashed()->get()->keyBy('id');
3619

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);
4022

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+
}
4626

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 = [];
4928

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;
5232
}
5333

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])) {
5837
continue;
5938
}
6039

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+
];
7245

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);
8050
}
81-
}
8251

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;
9153
}
92-
throw $e;
9354
}
9455
}
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+
}
9566
}

0 commit comments

Comments
 (0)