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
18 changes: 18 additions & 0 deletions resources/lang/en/countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,22 @@
'helper' => 'Numeric code (ISO-3166)',
],
],

'import' => [
'action_label' => 'Import Countries',
'modal_heading' => 'Import Countries',
'success_title' => 'Import Countries',
'success_message' => 'The import countries job has been queued.',
],

'notifications' => [
'success' => [
'title' => 'Countries Import Completed',
'message' => 'All countries have been successfully imported and updated.',
],
'failed' => [
'title' => 'Countries Import Failed',
'message' => 'Failed to import countries data.',
],
],
];
11 changes: 11 additions & 0 deletions resources/lang/en/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,15 @@
'HR' => 'Croatia',
],
],

'notifications' => [
'success' => [
'title' => 'Posts Import Completed',
'message' => 'Postal data for :country has been successfully imported.',
],
'failed' => [
'title' => 'Posts Import Failed',
'message' => 'Failed to import postal data for :country.',
],
],
];
18 changes: 18 additions & 0 deletions resources/lang/hr/countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,22 @@
'helper' => 'Numerička oznaka (ISO-3166)',
],
],

'import' => [
'action_label' => 'Uvezi države',
'modal_heading' => 'Uvoz država',
'success_title' => 'Uvoz država',
'success_message' => 'Zadatak za uvoz država je dodan u red čekanja.',
],

'notifications' => [
'success' => [
'title' => 'Uvoz država završen',
'message' => 'Sve države su uspješno uvežene i ažurirane.',
],
'failed' => [
'title' => 'Uvoz država neuspješan',
'message' => 'Neuspješan uvoz podataka država.',
],
],
];
11 changes: 11 additions & 0 deletions resources/lang/hr/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,15 @@
'HR' => 'Hrvatska',
],
],

'notifications' => [
'success' => [
'title' => 'Uvoz pošta završen',
'message' => 'Poštanski podaci za :country su uspješno uveženi.',
],
'failed' => [
'title' => 'Uvoz pošta neuspješan',
'message' => 'Neuspješan uvoz poštanskih podataka za :country.',
],
],
];
18 changes: 18 additions & 0 deletions resources/lang/sl/countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,22 @@
'helper' => 'Numerična šifra (ISO-3166)',
],
],

'import' => [
'action_label' => 'Uvozi države',
'modal_heading' => 'Uvoz držav',
'success_title' => 'Uvoz držav',
'success_message' => 'Naloga za uvoz držav je bila dodana v čakalno vrsto.',
],

'notifications' => [
'success' => [
'title' => 'Uvoz držav uspešno končan',
'message' => 'Vse države so bile uspešno uvožene in posodobljene.',
],
'failed' => [
'title' => 'Uvoz držav neuspešen',
'message' => 'Uvoz podatkov držav ni uspel.',
],
],
];
11 changes: 11 additions & 0 deletions resources/lang/sl/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,15 @@
'HR' => 'Hrvaška',
],
],

'notifications' => [
'success' => [
'title' => 'Uvoz pošt uspešno končan',
'message' => 'Poštni podatki za :country so bili uspešno uvoženi.',
],
'failed' => [
'title' => 'Uvoz pošt neuspešen',
'message' => 'Uvoz poštnih podatkov za :country ni uspel.',
],
],
];
18 changes: 18 additions & 0 deletions resources/lang/sr/countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,22 @@
'helper' => 'Numerička oznaka (ISO-3166)',
],
],

'import' => [
'action_label' => 'Uvezi države',
'modal_heading' => 'Uvoz država',
'success_title' => 'Uvoz država',
'success_message' => 'Zadatak za uvoz država je dodat u red čekanja.',
],

'notifications' => [
'success' => [
'title' => 'Uvoz država završen',
'message' => 'Sve države su uspešno uvežene i ažurirane.',
],
'failed' => [
'title' => 'Uvoz država neuspešan',
'message' => 'Neuspešan uvoz podataka država.',
],
],
];
11 changes: 11 additions & 0 deletions resources/lang/sr/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,15 @@
'HR' => 'Hrvatska',
],
],

'notifications' => [
'success' => [
'title' => 'Uvoz pošta završen',
'message' => 'Poštanski podaci za :country su uspešno uveženi.',
],
'failed' => [
'title' => 'Uvoz pošta neuspešan',
'message' => 'Neuspešan uvoz poštanskih podataka za :country.',
],
],
];
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Filament\Actions\CreateAction;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Support\Facades\App;

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

// Show notification
Notification::make()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Filament\Forms\Components\Select;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Support\Facades\App;

class ListPosts extends ListRecords
{
Expand Down Expand Up @@ -36,8 +37,8 @@ protected function getHeaderActions(): array
])
->modalHeading(__('eclipse-world::posts.import.modal_heading'))
->action(function (array $data) {
// Dispatch the job with selected country
ImportPosts::dispatch($data['country_id']);
// Dispatch the job
ImportPosts::dispatch($data['country_id'], auth()->id(), App::getLocale());

// Show notification
Notification::make()
Expand Down
76 changes: 57 additions & 19 deletions src/Jobs/ImportCountries.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace Eclipse\World\Jobs;

use Eclipse\Core\Models\User;
use Eclipse\World\Models\Country;
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 ImportCountries implements ShouldQueue
{
Expand All @@ -17,32 +21,66 @@ class ImportCountries implements ShouldQueue

public bool $failOnTimeout = true;

public ?int $userId;

public string $locale;

/**
* Create a new job instance.
*/
public function __construct(?int $userId, string $locale = 'en')
{
$this->userId = $userId;
$this->locale = $locale;
}

public function handle(): void
{
// Load existing countries into an associative array
$existingCountries = Country::withTrashed()->get()->keyBy('id');
Log::info('Starting countries import');

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

// Load new country data
$countries = json_decode(file_get_contents('https://raw.githubusercontent.com/mledoze/countries/master/dist/countries.json'), true);
try {
// Load existing countries into an associative array
$existingCountries = Country::withTrashed()->get()->keyBy('id');

foreach ($countries as $rawData) {
if (! $rawData['independent']) {
continue;
// Load new country data
$countries = json_decode(file_get_contents('https://raw.githubusercontent.com/mledoze/countries/master/dist/countries.json'), true);

if (! $countries) {
throw new Exception('Failed to fetch or parse countries data');
}

foreach ($countries as $rawData) {
if (! $rawData['independent']) {
continue;
}

$data = [
'id' => $rawData['cca2'],
'a3_id' => $rawData['cca3'],
'num_code' => $rawData['ccn3'],
'name' => $rawData['name']['common'],
'flag' => $rawData['flag'],
];

if (isset($existingCountries[$data['id']])) {
$existingCountries[$data['id']]->update($data);
} else {
Country::create($data);
}
}

$data = [
'id' => $rawData['cca2'],
'a3_id' => $rawData['cca3'],
'num_code' => $rawData['ccn3'],
'name' => $rawData['name']['common'],
'flag' => $rawData['flag'],
];

if (isset($existingCountries[$data['id']])) {
$existingCountries[$data['id']]->update($data);
} else {
Country::create($data);
Log::info('Countries import completed');
if ($user) {
$user->notify(new ImportFinishedNotification('success', 'countries', null, $this->locale));
}
} catch (Exception $e) {
Log::error('Countries import failed: '.$e->getMessage());
if ($user) {
$user->notify(new ImportFinishedNotification('failed', 'countries', null, $this->locale));
}
throw $e;
}
}
}
69 changes: 45 additions & 24 deletions src/Jobs/ImportPosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Eclipse\World\Jobs;

use Eclipse\Core\Models\User;
use Eclipse\World\Models\Post;
use Eclipse\World\Notifications\ImportFinishedNotification;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -20,12 +22,18 @@ class ImportPosts implements ShouldQueue

public string $countryId;

public int $userId;

public string $locale;

/**
* Create a new job instance.
*/
public function __construct(string $countryId)
public function __construct(string $countryId, int $userId, string $locale = 'en')
{
$this->countryId = $countryId;
$this->userId = $userId;
$this->locale = $locale;
}

/**
Expand All @@ -43,37 +51,50 @@ public function handle(): void

Log::info("Starting postal data import for country: {$this->countryId}");

do {
[$totalRecords, $records] = $this->getData($batchSize, $offset);
$user = $this->userId ? User::find($this->userId) : null;

foreach ($records as $record) {
[$postalCode, $placeName] = $this->getRecordData($record);
try {
do {
[$totalRecords, $records] = $this->getData($batchSize, $offset);

if (array_key_exists($postalCode, $processedCodes)) {
continue;
}
foreach ($records as $record) {
[$postalCode, $placeName] = $this->getRecordData($record);

if (array_key_exists($postalCode, $processedCodes)) {
continue;
}

$processedCodes[$postalCode] = true;
$processedCodes[$postalCode] = true;

$existingPost = Post::where('country_id', $this->countryId)
->where('code', $postalCode)
->first();
$existingPost = Post::where('country_id', $this->countryId)
->where('code', $postalCode)
->first();

if (empty($existingPost)) {
Post::create([
'country_id' => $this->countryId,
'code' => $postalCode,
'name' => $placeName,
]);
} elseif ($existingPost->name !== $placeName) {
$existingPost->update(['name' => $placeName]);
if (empty($existingPost)) {
Post::create([
'country_id' => $this->countryId,
'code' => $postalCode,
'name' => $placeName,
]);
} elseif ($existingPost->name !== $placeName) {
$existingPost->update(['name' => $placeName]);
}
}
}

$offset += $batchSize;
} while ($offset < $totalRecords);
$offset += $batchSize;
} while ($offset < $totalRecords);

Log::info("Postal data import completed for {$this->countryId}");
Log::info("Postal data import completed for {$this->countryId}");
if ($user) {
$user->notify(new ImportFinishedNotification('success', 'posts', $this->countryId, $this->locale));
}
} catch (Exception $e) {
Log::error("Postal data import failed for {$this->countryId}: {$e->getMessage()}");
if ($user) {
$user->notify(new ImportFinishedNotification('failed', 'posts', $this->countryId, $this->locale));
}
throw $e;
}
}

/**
Expand Down
Loading
Loading