@@ -23,7 +23,7 @@ class ImportStaticDataJob implements ShouldQueue
2323
2424 public function handle ()
2525 {
26- Log::info ('Starting import of static data from REST Countries API... ' );
26+ Log::channel ( ' daily ' )-> info ('Starting import of static data from REST Countries API... ' );
2727
2828 $ alpha3ToAlpha2 = [
2929 'deu ' => 'de ' ,
@@ -44,113 +44,129 @@ public function handle()
4444 ];
4545
4646 try {
47- $ response = Http::get ('https://restcountries.com/v3.1/all ' );
48- Log::info ('API Response status: ' .$ response ->status ());
47+ Log::channel ('daily ' )->info ('Attempting to fetch data from REST Countries API... ' );
48+ $ response = Http::timeout (60 )->get ('https://restcountries.com/v3.1/all ' );
49+ Log::channel ('daily ' )->info ('API Response status: ' .$ response ->status ());
4950
5051 if ($ response ->failed ()) {
51- Log::error ('Failed to fetch data from REST Countries API. Status: ' .$ response ->status ());
52+ Log::channel ('daily ' )->error ('Failed to fetch data from REST Countries API. Status: ' .$ response ->status ());
53+ Log::channel ('daily ' )->error ('Response body: ' .$ response ->body ());
5254
5355 return ;
5456 }
5557
5658 $ countries = $ response ->json ();
57- Log::info ('Fetched ' .count ($ countries ).' countries from API ' );
59+ Log::channel ( ' daily ' )-> info ('Fetched ' .count ($ countries ).' countries from API ' );
5860
5961 foreach ($ countries as $ countryData ) {
60- Log::info ('Processing country: ' .$ countryData ['cca2 ' ]);
61- // Insert or update country
62- $ country = StaticCountry::updateOrCreate (
63- ['alpha2 ' => $ countryData ['cca2 ' ]],
64- [
65- 'alpha3_b ' => $ countryData ['cca3 ' ] ?? null ,
66- 'common_name ' => $ countryData ['name ' ]['common ' ],
67- 'native_name ' => json_encode ($ countryData ['name ' ]['nativeName ' ] ?? []),
68- 'exonyms ' => json_encode ($ countryData ['translations ' ] ?? []),
69- 'region ' => $ countryData ['region ' ] ?? null ,
70- 'subregion ' => $ countryData ['subregion ' ] ?? null ,
71- 'calling_code ' => $ countryData ['idd ' ]['root ' ] ?? null ,
72- 'capital ' => $ countryData ['capital ' ][0 ] ?? null ,
73- 'population ' => $ countryData ['population ' ] ?? null ,
74- 'area ' => $ countryData ['area ' ] ?? null ,
75- 'tlds ' => json_encode ($ countryData ['tld ' ] ?? []),
76- 'membership ' => json_encode ($ countryData ['regionalBlocs ' ] ?? []),
77- 'postal_code_regex ' => $ countryData ['postalCode ' ]['format ' ] ?? null ,
78- 'dialing_prefix ' => $ countryData ['idd ' ]['root ' ] ?? null ,
79- 'date_format ' => 'YYYY-MM-DD ' ,
80- ]
81- );
82-
83- // Insert or update currencies
84- if (! empty ($ countryData ['currencies ' ])) {
85- foreach ($ countryData ['currencies ' ] as $ code => $ currencyData ) {
86- $ currency = StaticCurrency::updateOrCreate (
87- ['code ' => $ code ],
88- [
89- 'common_name ' => $ currencyData ['name ' ] ?? '' ,
90- 'symbol ' => $ currencyData ['symbol ' ] ?? null ,
91- ]
92- );
93- StaticCountriesStaticCurrencies::updateOrCreate ([
94- 'country_id ' => $ country ->id ,
95- 'currency_id ' => $ currency ->id ,
96- 'is_primary ' => true ,
97- ]);
62+ try {
63+ Log::channel ('daily ' )->info ('Processing country: ' .($ countryData ['cca2 ' ] ?? 'unknown ' ));
64+
65+ if (! isset ($ countryData ['cca2 ' ])) {
66+ Log::channel ('daily ' )->warning ('Skipping country - missing cca2 code ' );
67+
68+ continue ;
9869 }
99- }
10070
101- // Insert or update languages
102- if (! empty ($ countryData ['languages ' ])) {
103- Log::info ("Languages for {$ country ->alpha2 }: " , $ countryData ['languages ' ]);
104- foreach ($ countryData ['languages ' ] as $ code => $ name ) {
105- Log::info ("Processing language: {$ code } ( {$ name }) for country {$ country ->alpha2 }" );
106-
107- $ alpha2 = $ alpha3ToAlpha2 [$ code ] ?? $ code ;
108- Log::info ("Converted {$ code } to {$ alpha2 }" );
109-
110- $ language = StaticLanguage::updateOrCreate (
111- ['alpha2 ' => $ alpha2 ],
112- ['common_name ' => $ name ]
113- );
114-
115- $ locale = $ alpha2 .'_ ' .$ country ->alpha2 ;
116- Log::info ("Generated locale: {$ locale }" );
117-
118- StaticLocale::where ('country_id ' , $ country ->id )
119- ->where ('language_id ' , $ language ->id )
120- ->update (['locale ' => $ locale ]);
121-
122- StaticLocale::updateOrCreate (
123- [
124- 'country_id ' => $ country ->id ,
125- 'language_id ' => $ language ->id ,
126- ],
127- [
128- 'locale ' => $ locale ,
129- 'name ' => $ name ,
130- 'is_official_language ' => true ,
131- ]
132- );
71+ $ country = StaticCountry::updateOrCreate (
72+ ['alpha2 ' => $ countryData ['cca2 ' ]],
73+ [
74+ 'alpha3_b ' => $ countryData ['cca3 ' ] ?? null ,
75+ 'common_name ' => $ countryData ['name ' ]['common ' ] ?? null ,
76+ 'native_name ' => json_encode ($ countryData ['name ' ]['nativeName ' ] ?? []),
77+ 'exonyms ' => json_encode ($ countryData ['translations ' ] ?? []),
78+ 'region ' => $ countryData ['region ' ] ?? null ,
79+ 'subregion ' => $ countryData ['subregion ' ] ?? null ,
80+ 'calling_code ' => $ countryData ['idd ' ]['root ' ] ?? null ,
81+ 'capital ' => is_array ($ countryData ['capital ' ]) ? ($ countryData ['capital ' ][0 ] ?? null ) : $ countryData ['capital ' ],
82+ 'population ' => $ countryData ['population ' ] ?? null ,
83+ 'area ' => $ countryData ['area ' ] ?? null ,
84+ 'tlds ' => json_encode ($ countryData ['tld ' ] ?? []),
85+ 'membership ' => json_encode ($ countryData ['regionalBlocs ' ] ?? []),
86+ 'postal_code_regex ' => $ countryData ['postalCode ' ]['format ' ] ?? null ,
87+ 'dialing_prefix ' => $ countryData ['idd ' ]['root ' ] ?? null ,
88+ 'date_format ' => 'YYYY-MM-DD ' ,
89+ ]
90+ );
91+ Log::channel ('daily ' )->info ('Created/Updated country: ' .$ country ->alpha2 );
92+
93+ if (! empty ($ countryData ['currencies ' ])) {
94+ foreach ($ countryData ['currencies ' ] as $ code => $ currencyData ) {
95+ try {
96+ $ currency = StaticCurrency::updateOrCreate (
97+ ['code ' => $ code ],
98+ [
99+ 'common_name ' => $ currencyData ['name ' ] ?? '' ,
100+ 'symbol ' => $ currencyData ['symbol ' ] ?? null ,
101+ ]
102+ );
103+ StaticCountriesStaticCurrencies::updateOrCreate ([
104+ 'country_id ' => $ country ->id ,
105+ 'currency_id ' => $ currency ->id ,
106+ 'is_primary ' => true ,
107+ ]);
108+ Log::channel ('daily ' )->info ("Added currency {$ code } for country {$ country ->alpha2 }" );
109+ } catch (\Exception $ e ) {
110+ Log::channel ('daily ' )->error ("Error processing currency {$ code } for country {$ country ->alpha2 }: " .$ e ->getMessage ());
111+ }
112+ }
113+ }
114+
115+ if (! empty ($ countryData ['languages ' ])) {
116+ foreach ($ countryData ['languages ' ] as $ code => $ name ) {
117+ try {
118+ $ alpha2 = $ alpha3ToAlpha2 [$ code ] ?? $ code ;
119+ $ language = StaticLanguage::updateOrCreate (
120+ ['alpha2 ' => $ alpha2 ],
121+ ['common_name ' => $ name ]
122+ );
123+
124+ $ locale = $ alpha2 .'_ ' .$ country ->alpha2 ;
125+ StaticLocale::updateOrCreate (
126+ [
127+ 'country_id ' => $ country ->id ,
128+ 'language_id ' => $ language ->id ,
129+ ],
130+ [
131+ 'locale ' => $ locale ,
132+ 'name ' => $ name ,
133+ 'is_official_language ' => true ,
134+ ]
135+ );
136+ Log::channel ('daily ' )->info ("Added language {$ code } for country {$ country ->alpha2 }" );
137+ } catch (\Exception $ e ) {
138+ Log::channel ('daily ' )->error ("Error processing language {$ code } for country {$ country ->alpha2 }: " .$ e ->getMessage ());
139+ }
140+ }
133141 }
134- }
135142
136- // Insert or update timezones
137- if (! empty ($ countryData ['timezones ' ])) {
138- foreach ($ countryData ['timezones ' ] as $ timezoneName ) {
139- $ timezone = StaticTimezone::updateOrCreate (
140- ['name ' => $ timezoneName ],
141- ['offset_standard ' => '' , 'dst ' => false ]
142- );
143- StaticCountriesStaticTimezones::updateOrCreate ([
144- 'country_id ' => $ country ->id ,
145- 'timezone_id ' => $ timezone ->id ,
146- ]);
143+ if (! empty ($ countryData ['timezones ' ])) {
144+ foreach ($ countryData ['timezones ' ] as $ timezoneName ) {
145+ try {
146+ $ timezone = StaticTimezone::updateOrCreate (
147+ ['name ' => $ timezoneName ],
148+ ['offset_standard ' => '' , 'dst ' => false ]
149+ );
150+ StaticCountriesStaticTimezones::updateOrCreate ([
151+ 'country_id ' => $ country ->id ,
152+ 'timezone_id ' => $ timezone ->id ,
153+ ]);
154+ Log::channel ('daily ' )->info ("Added timezone {$ timezoneName } for country {$ country ->alpha2 }" );
155+ } catch (\Exception $ e ) {
156+ Log::channel ('daily ' )->error ("Error processing timezone {$ timezoneName } for country {$ country ->alpha2 }: " .$ e ->getMessage ());
157+ }
158+ }
147159 }
160+ } catch (\Exception $ e ) {
161+ Log::channel ('daily ' )->error ("Error processing country {$ countryData ['cca2 ' ]}: " .$ e ->getMessage ());
148162 }
149163 }
150164
151- Log::info ('Finished importing static data from REST Countries API. ' );
165+ Log::channel ( ' daily ' )-> info ('Finished importing static data from REST Countries API. ' );
152166 } catch (\Exception $ e ) {
153- Log::error ('Error during import: ' .$ e ->getMessage ());
167+ Log::channel ('daily ' )->error ('Error during import: ' .$ e ->getMessage ());
168+ Log::channel ('daily ' )->error ('Stack trace: ' .$ e ->getTraceAsString ());
169+ throw $ e ;
154170 }
155171 }
156172}
0 commit comments