Skip to content
Merged
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: 1 addition & 1 deletion .github/workflows/test-runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "filament/support" --no-interaction --no-update
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "filament/support:^3.3" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-progress --no-interaction

- name: Run test suite
Expand Down
14 changes: 7 additions & 7 deletions resources/lang/en/currencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@
'import' => [
'action_label' => 'Import Currencies',
'modal_heading' => 'Import Currencies',
'success_title' => 'Import Currencies',
'success_message' => 'The import currencies job has been queued.',
'job_name' => 'Import Currencies',
],

'notifications' => [
'success' => [
'title' => 'Currencies Import Completed',
'message' => 'All currencies have been successfully imported and updated.',
'queued' => [
'title' => 'Currencies Import queued',
],
'completed' => [
'title' => 'Currencies Import completed',
],
'failed' => [
'title' => 'Currencies Import Failed',
'message' => 'Failed to import currencies data.',
'title' => 'Currencies Import failed',
],
],
];
10 changes: 5 additions & 5 deletions resources/lang/hr/currencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@
'import' => [
'action_label' => 'Uvezi valute',
'modal_heading' => 'Uvezi valute',
'success_title' => 'Uvezi valute',
'success_message' => 'Posao uvoza valuta je dodan u red za izvršavanje.',
'job_name' => 'Uvezi valute',
],

'notifications' => [
'success' => [
'queued' => [
'title' => 'Uvoz valuta je u redu čekanja',
],
'completed' => [
'title' => 'Uvoz valuta je završen',
'message' => 'Sve valute su uspješno uvezene i ažurirane.',
],
'failed' => [
'title' => 'Uvoz valuta nije uspješan',
'message' => 'Uvoz podataka o valutama nije uspješan.',
],
],
];
10 changes: 5 additions & 5 deletions resources/lang/sl/currencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@
'import' => [
'action_label' => 'Uvozi valute',
'modal_heading' => 'Uvozi valute',
'success_title' => 'Uvozi valute',
'success_message' => 'Opravilo uvoza valut je bilo dodano v čakalno vrsto.',
'job_name' => 'Uvozi valute',
],

'notifications' => [
'success' => [
'queued' => [
'title' => 'Uvoz valut je v čakalni vrsti',
],
'completed' => [
'title' => 'Uvoz valut je končan',
'message' => 'Vse valute so bile uspešno uvožene in posodobljene.',
],
'failed' => [
'title' => 'Uvoz valut ni uspel',
'message' => 'Uvoz podatkov o valutah ni uspel.',
],
],
];
10 changes: 5 additions & 5 deletions resources/lang/sr/currencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@
'import' => [
'action_label' => 'Uvezi valute',
'modal_heading' => 'Uvoz valuta',
'success_title' => 'Uvoz valuta',
'success_message' => 'Zadatak za uvoz valuta je dodat u red čekanja.',
'job_name' => 'Uvezi valute',
],

'notifications' => [
'success' => [
'queued' => [
'title' => 'Uvoz valuta je u redu čekanja',
],
'completed' => [
'title' => 'Uvoz valuta završen',
'message' => 'Sve valute su uspešno uvežene i ažurirane.',
],
'failed' => [
'title' => 'Uvoz valuta neuspešan',
'message' => 'Neuspešan uvoz podataka valuta.',
],
],
];
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
use Eclipse\World\Jobs\ImportCurrencies;
use Filament\Actions\Action;
use Filament\Actions\CreateAction;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Support\Facades\App;

class ListCurrencies extends ListRecords
{
Expand All @@ -25,14 +23,7 @@ protected function getHeaderActions(): array
->icon('heroicon-o-arrow-down-tray')
->action(function () {
// Dispatch the job
ImportCurrencies::dispatch(auth()->id(), App::getLocale());

// Show notification
Notification::make()
->title(__('eclipse-world::currencies.import.success_title'))
->body(__('eclipse-world::currencies.import.success_message'))
->success()
->send();
ImportCurrencies::dispatch();
})
->requiresConfirmation(),
];
Expand Down
103 changes: 37 additions & 66 deletions src/Jobs/ImportCurrencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,65 @@

namespace Eclipse\World\Jobs;

use Eclipse\Core\Models\User;
use Eclipse\Common\Foundation\Jobs\QueueableJob;
use Eclipse\World\Models\Currency;
use Eclipse\World\Notifications\ImportFinishedNotification;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class ImportCurrencies implements ShouldQueue
class ImportCurrencies extends QueueableJob
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public int $timeout = 60;

public bool $failOnTimeout = true;

public ?int $userId;

public string $locale;

/**
* Create a new job instance.
*/
public function __construct(?int $userId, string $locale = 'en')
protected function execute(): void
{
$this->userId = $userId;
$this->locale = $locale;
}
// Load existing currencies into an associative array
$existingCurrencies = Currency::withTrashed()->get()->keyBy('id');

public function handle(): void
{
Log::info('Starting currencies import');
// Load new currency data from REST Countries API
$countries = json_decode(file_get_contents('https://raw.githubusercontent.com/mledoze/countries/master/dist/countries.json'), true);

$user = $this->userId ? User::find($this->userId) : null;

try {
// Load existing currencies into an associative array
$existingCurrencies = Currency::withTrashed()->get()->keyBy('id');
if (! $countries) {
throw new Exception('Failed to fetch or parse countries data');
}

// Load new currency data from REST Countries API
$countries = json_decode(file_get_contents('https://raw.githubusercontent.com/mledoze/countries/master/dist/countries.json'), true);
$processedCurrencies = [];

if (! $countries) {
throw new Exception('Failed to fetch or parse countries data');
foreach ($countries as $rawData) {
if (! $rawData['independent'] || empty($rawData['currencies'])) {
continue;
}

$processedCurrencies = [];

foreach ($countries as $rawData) {
if (! $rawData['independent'] || empty($rawData['currencies'])) {
foreach ($rawData['currencies'] as $currencyCode => $currencyData) {
// Skip if we've already processed this currency
if (isset($processedCurrencies[$currencyCode])) {
continue;
}

foreach ($rawData['currencies'] as $currencyCode => $currencyData) {
// Skip if we've already processed this currency
if (isset($processedCurrencies[$currencyCode])) {
continue;
}

$data = [
'id' => $currencyCode,
'name' => $currencyData['name'],
'is_active' => true,
];
$data = [
'id' => $currencyCode,
'name' => $currencyData['name'],
'is_active' => true,
];

if (isset($existingCurrencies[$currencyCode])) {
$existingCurrencies[$currencyCode]->update($data);
} else {
Currency::create($data);
}

$processedCurrencies[$currencyCode] = true;
if (isset($existingCurrencies[$currencyCode])) {
$existingCurrencies[$currencyCode]->update($data);
} else {
Currency::create($data);
}
}

Log::info('Currencies import completed');
if ($user) {
$user->notify(new ImportFinishedNotification('success', 'currencies', null, $this->locale));
}
} catch (Exception $e) {
Log::error('Currencies import failed: '.$e->getMessage());
if ($user) {
$user->notify(new ImportFinishedNotification('failed', 'currencies', null, $this->locale));
$processedCurrencies[$currencyCode] = true;
}
throw $e;
}
}

protected function getJobName(): string
{
return __('eclipse-world::currencies.import.job_name', [], $this->locale);
}

protected function getNotificationTitle(): string
{
return __("eclipse-world::currencies.notifications.{$this->status->value}.title", [], $this->locale);
}
}