Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Nov 20, 2024
1 parent de168cd commit 60ed88c
Show file tree
Hide file tree
Showing 13 changed files with 466 additions and 88 deletions.
107 changes: 107 additions & 0 deletions app/Concerns/Import/HasPlace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

namespace App\Concerns\Import;

use App\Models\Country;
use App\Models\County;
use App\Models\Locality;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Validator;
use stdClass;

trait HasPlace
{
protected ?Collection $countries = null;

protected ?Collection $counties = null;

protected ?Collection $localities = null;

protected function getCountries(): Collection
{
$countries = collect();

Country::each(function (Country $country) use ($countries) {
collect($country->old_ids)->each(
fn (int $oldId) => $countries->put($oldId, $country->id)
);
});

return $countries;
}

protected function getCounties(): Collection
{
return County::pluck('id', 'old_id');
}

protected function getLocalities(): Collection
{
$localities = collect();

Locality::query()
->whereNotNull('old_ids')
->each(function (Locality $locality) use ($localities) {
collect($locality->old_ids)->each(
fn (int $oldId) => $localities->put($oldId, $locality->id)
);
});

return $localities;
}

protected function getPlace(stdClass $row): ?array
{
if (blank($this->countries)) {
$this->countries = $this->getCountries();
}

if (blank($this->counties)) {
$this->counties = $this->getCounties();
}

if (blank($this->localities)) {
$this->localities = $this->getLocalities();
}

$place = [
'country_id' => $this->countries->get($row->CountryId),
'county_id' => $this->counties->get($row->CountyId),
'locality_id' => $this->localities->get($row->LocalityId),
];

$validation = Validator::make($place, [
'country_id' => ['required_without:county_id,locality_id'],
'county_id' => ['required_without:country_id', 'required_with:locality_id'],
'locality_id' => ['required_without:country_id', 'required_with:county_id'],
]);

if ($validation->fails()) {
if ($place['county_id'] === 403 && blank($place['locality_id'])) {
// TODO: Date doar pe București, TBD
// old ballot ids 62, 63, 64, 65, 69, 69, 71, 72, 73, 74
} elseif (
blank($place['locality_id']) &&
$row->LocalityId >= 64413 &&
$row->LocalityId <= 64497
) {
// TODO: Toate Localitatile Din Judet
// old ballot ids 71, 72, 73, 74
} else {
logger()->error('Could not determine location.', [
'BallotId' => $row->BallotId,
'TurnoutId' => $row->Id,
'CountyId' => $row->CountyId,
'LocalityId' => $row->LocalityId,
'locality_id' => $place['locality_id'],
]);

return null;
}
}

return $place;
}
}
5 changes: 5 additions & 0 deletions app/Console/Commands/Import/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,15 @@ public function handle(): int
$this->call(ImportOldIdsCommand::class, [
'--force' => $this->option('force'),
]);

$this->call(ImportTurnoutsCommand::class, [
'--force' => $this->option('force'),
]);

$this->call(ImportPartiesCandidatesCommand::class, [
'--force' => $this->option('force'),
]);

return self::SUCCESS;
}
}
15 changes: 15 additions & 0 deletions app/Console/Commands/Import/ImportElectionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ public function handle(): int
},
'date' => $row->Date,
'is_live' => false,
'has_lists' => match ($row->BallotType) {
// Referendum = 0,
// President = 1,
// Senate = 2,
// House = 3,
// LocalCouncil = 4,
// CountyCouncil = 5,
// Mayor = 6,
// EuropeanParliament = 7,
// CountyCouncilPresident = 8,
// CapitalCityMayor = 9,
// CapitalCityCouncil = 10,
0, 1, 6, 8, 9 => false,
2, 3, 4, 5, 7, 10 => true,
},
'old_id' => $row->BallotId,
]);
}, (int) $this->option('chunk'));
Expand Down
30 changes: 19 additions & 11 deletions app/Console/Commands/Import/ImportOldIdsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ protected function importCountryIds(): void
);

$query->each(function (stdClass $row) {
$country = Country::search($row->Name)->first();
$name = match ($row->Name) {
'Geneva' => 'Elveția',
default => $row->Name,
};

$country = Country::search($name)->first();

if (blank($country)) {
logger()->error("Country not found: {$row->Name}");
logger()->error("Country not found: {$name}");

return;
}
Expand Down Expand Up @@ -110,7 +115,7 @@ protected function importLocalityIds(): void
{
$query = $this->db
->table('localities')
->orderBy('localities.Siruta');
->orderBy('localities.LocalityId');

$this->createProgressBar(
'Importing locality IDs...',
Expand All @@ -120,12 +125,6 @@ protected function importLocalityIds(): void
$counties = County::pluck('id', 'old_id');

$query->each(function (stdClass $row) use ($counties) {
// $siruta = match ($row->Siruta) {
// 116921 => 61069, // Băneasa, Constanța
// 713, 21469 => 9280, // Fântânele, Arad
// default => $row->Siruta
// };

if ($row->Siruta === 0) {
$locality = $this->searchLocalities($row->Name, $counties->get($row->CountyId));
} else {
Expand All @@ -134,10 +133,10 @@ protected function importLocalityIds(): void
->firstOr(fn () => $this->searchLocalities($row->Name, $counties->get($row->CountyId)));
}

logger()->info("{$row->LocalityId} | {$row->Name} | Siruta: {$row->Siruta} => " . $locality?->name ?? 'NULL');
// logger()->info("{$row->LocalityId} | {$row->Name} | Siruta: {$row->Siruta} => " . $locality?->name ?? 'NULL');

if (blank($locality)) {
logger()->error("Locality not found: {$row->Name}");
logger()->error("Locality not found: {$row->LocalityId} | {$row->Name} | Siruta: {$row->Siruta}");

return;
}
Expand All @@ -157,8 +156,17 @@ protected function importLocalityIds(): void

protected function searchLocalities(string $name, int $county_id): ?Locality
{
$name = match (true) {
$county_id === 118 && $name === 'Pescari' => 'Coronini',
$county_id === 136 && $name === 'Basarabi' => 'Murfatlar',
$county_id === 207 && $name === 'Unirea' => 'General Berthelot',
default => $name,
};

return Locality::search($name)
->where('county_id', $county_id)
->get()
->sortBy('parent_id')
->first();
}
}
Loading

0 comments on commit 60ed88c

Please sign in to comment.