Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubKermes committed Mar 21, 2024
1 parent 0e7d1e7 commit f9fe3d6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
6 changes: 6 additions & 0 deletions app/Http/Controllers/RulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ public function getRules($country, $city): array

return $data;
}

public function importRules(bool $force): void
{
$importer = new OpenAIService();
$importer->importRulesForAllCities($force);
}
}
2 changes: 2 additions & 0 deletions app/Jobs/ImportCityRulesJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Jobs;

use App\Services\OpenAIService;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
Expand All @@ -15,6 +16,7 @@ class ImportCityRulesJob implements ShouldQueue
use Dispatchable;
use InteractsWithQueue;
use SerializesModels;
use Batchable;

public function __construct(
private array $cityData,
Expand Down
21 changes: 20 additions & 1 deletion app/Services/OpenAIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

use App\Jobs\ImportCityRulesJob;
use App\Models\City;
use App\Models\ImportInfo;
use App\Models\Rules;
use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Bus;
use OpenAI;

class OpenAIService implements ShouldQueue
Expand All @@ -17,7 +20,11 @@ class OpenAIService implements ShouldQueue

public function __construct()
{
$this->client = OpenAI::client(env("OPENAI_API_KEY"));
try {
$this->client = OpenAI::client(env("OPENAI_API_KEY"));
} catch (Exception $e) {
throw new Exception("OpenAI API key is not set");
}
$this->countriesKnownToHaveUniformRules = [
"Poland", "Germany", "France", "Spain", "Italy", "Portugal", "Austria", "Czech Republic", "Slovakia", "Hungary",
"Romania", "Bulgaria", "Greece", "Sweden", "Finland", "Norway", "Denmark", "Netherlands", "Belgium", "Switzerland",
Expand Down Expand Up @@ -50,15 +57,27 @@ public function importRulesForAllCities(bool $force): void
{
$cities = City::query()->whereHas("cityProviders")->orderBy("country_id")->get();

$importInfo = ImportInfo::query()->create([
"who_runs_it" => "admin",
"status" => "running",
]);
$jobs = [];

foreach ($cities as $city) {
$cityData = [
"city_id" => $city->id,
"country_id" => $city->country_id,
"city_name" => $city->name,
"country_name" => $city->country->name,
];
$jobs[] = new ImportCityRulesJob($cityData, $force);
ImportCityRulesJob::dispatch($cityData, $force);
}
Bus::batch($jobs)->finally(function () use ($importInfo): void {
ImportInfo::query()->where("id", $importInfo->id)->update([
"status" => "finished",
]);
})->dispatch();
}

public function importRulesForCity(array $cityData, bool $force): array
Expand Down
4 changes: 2 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use App\Http\Controllers\CityWithoutAssignedCountryController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\FavoritesController;
use App\Services\OpenAIService;
use App\Http\Controllers\RulesController;
use Illuminate\Support\Facades\Route;

Route::middleware("guest")->group(function (): void {
Expand Down Expand Up @@ -48,7 +48,7 @@
Route::delete("/delete-city-without-assigned-country/{city}", [CityWithoutAssignedCountryController::class, "destroy"]);
Route::post("/delete-all-cities-without-assigned-country", [CityWithoutAssignedCountryController::class, "destroyAll"]);

Route::get("/import-rules/{force}", [OpenAIService::class, "importRulesForAllCities"]);
Route::get("/import-rules/{force}", [RulesController::class, "importRules"]);
});
});

Expand Down

0 comments on commit f9fe3d6

Please sign in to comment.