Skip to content

Commit

Permalink
fix: country code detection (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio authored Dec 3, 2024
1 parent d62cef0 commit 00aba83
Show file tree
Hide file tree
Showing 12 changed files with 306 additions and 489 deletions.
82 changes: 32 additions & 50 deletions app/Jobs/Y2020/M06/Parliament/Records/ImportAbroadRecordsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

namespace App\Jobs\Y2020\M06\Parliament\Records;

use App\Events\CountryCodeNotFound;
use App\Exceptions\CountryCodeNotFoundException;
use App\Exceptions\MissingSourceFileException;
use App\Models\Country;
use App\Models\Record;
use App\Models\ScheduledJob;
use App\Models\Vote;
Expand Down Expand Up @@ -55,67 +52,52 @@ public function handle(): void
);

foreach ($reader->getRecords() as $row) {
try {
$countryId = $this->getCountryId($row['uat_name']);
$countryId = RecordService::getCountryId($row['uat_name']);

$part = RecordService::getPart($row['report_stage_code']);
$part = RecordService::getPart($row['report_stage_code']);

$records->push([
'election_id' => $this->scheduledJob->election_id,
'country_id' => $countryId,
'section' => $row['precinct_nr'],
'part' => $part,
$records->push([
'election_id' => $this->scheduledJob->election_id,
'country_id' => $countryId,
'section' => $row['precinct_nr'],
'part' => $part,

'eligible_voters_permanent' => $row['a'],
'eligible_voters_special' => 0,
'eligible_voters_permanent' => $row['a'],
'eligible_voters_special' => 0,

'present_voters_permanent' => $row['b1'],
'present_voters_special' => $row['b2'],
'present_voters_supliment' => $row['b3'],
'present_voters_mail' => 0, //$row['b4'],
'present_voters_permanent' => $row['b1'],
'present_voters_special' => $row['b2'],
'present_voters_supliment' => $row['b3'],
'present_voters_mail' => 0, //$row['b4'],

'votes_valid' => $row['c'],
'votes_null' => $row['d'],
'votes_valid' => $row['c'],
'votes_null' => $row['d'],

'papers_received' => $row['e'],
'papers_unused' => $row['f'],
'papers_received' => $row['e'],
'papers_unused' => $row['f'],

'has_issues' => false,
]);
'has_issues' => false,
]);

$votes = collect();
foreach ($votables as $column => $votable) {
$votes->push([
'election_id' => $this->scheduledJob->election_id,
'country_id' => $countryId,
'section' => $row['precinct_nr'],
'part' => $part,

'votable_type' => $votable['votable_type'],
'votable_id' => $votable['votable_id'],
$votes = collect();
foreach ($votables as $column => $votable) {
$votes->push([
'election_id' => $this->scheduledJob->election_id,
'country_id' => $countryId,
'section' => $row['precinct_nr'],
'part' => $part,

'votes' => $row[$column],
]);
}
'votable_type' => $votable['votable_type'],
'votable_id' => $votable['votable_id'],

Vote::saveToTemporaryTable($votes->all());
} catch (CountryCodeNotFoundException $th) {
CountryCodeNotFound::dispatch($row['uat_name'], $this->scheduledJob->election);
'votes' => $row[$column],
]);
}
}

Record::saveToTemporaryTable($records->all());
}

protected function getCountryId(string $name): string
{
$country = Country::search($name)->first();

if (! $country) {
throw new CountryCodeNotFoundException($name);
Vote::saveToTemporaryTable($votes->all());
}

return $country->id;
Record::saveToTemporaryTable($records->all());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

namespace App\Jobs\Y2020\M06\Parliament\Records;

use App\Events\CountryCodeNotFound;
use App\Exceptions\CountryCodeNotFoundException;
use App\Jobs\SchedulableJob;
use App\Models\Country;
use App\Models\Record;
use App\Models\Vote;
use App\Services\RecordService;
Expand Down Expand Up @@ -40,67 +37,52 @@ public function execute(): void
);

foreach ($reader->getRecords() as $row) {
try {
$countryId = $this->getCountryId($row['uat_name']);
$countryId = RecordService::getCountryId($row['uat_name']);

$part = RecordService::getPart($row['report_stage_code']);
$part = RecordService::getPart($row['report_stage_code']);

$records->push([
'election_id' => $this->scheduledJob->election_id,
'country_id' => $countryId,
'section' => $row['precinct_nr'],
'part' => $part,
$records->push([
'election_id' => $this->scheduledJob->election_id,
'country_id' => $countryId,
'section' => $row['precinct_nr'],
'part' => $part,

'eligible_voters_permanent' => $row['a'],
'eligible_voters_special' => 0,
'eligible_voters_permanent' => $row['a'],
'eligible_voters_special' => 0,

'present_voters_permanent' => 0,
'present_voters_special' => 0,
'present_voters_supliment' => 0,
'present_voters_mail' => $row['c'],
'present_voters_permanent' => 0,
'present_voters_special' => 0,
'present_voters_supliment' => 0,
'present_voters_mail' => $row['c'],

'votes_valid' => $row['d1'],
'votes_null' => $row['d2'],
'votes_valid' => $row['d1'],
'votes_null' => $row['d2'],

'papers_received' => 0,
'papers_unused' => 0,
'papers_received' => 0,
'papers_unused' => 0,

'has_issues' => false,
]);
'has_issues' => false,
]);

$votes = collect();
foreach ($votables as $column => $votable) {
$votes->push([
'election_id' => $this->scheduledJob->election_id,
'country_id' => $countryId,
'section' => $row['precinct_nr'],
'part' => $part,

'votable_type' => $votable['votable_type'],
'votable_id' => $votable['votable_id'],
$votes = collect();
foreach ($votables as $column => $votable) {
$votes->push([
'election_id' => $this->scheduledJob->election_id,
'country_id' => $countryId,
'section' => $row['precinct_nr'],
'part' => $part,

'votes' => $row[$column],
]);
}
'votable_type' => $votable['votable_type'],
'votable_id' => $votable['votable_id'],

Vote::saveToTemporaryTable($votes->all());
} catch (CountryCodeNotFoundException $th) {
CountryCodeNotFound::dispatch($row['uat_name'], $this->scheduledJob->election);
'votes' => $row[$column],
]);
}
}

Record::saveToTemporaryTable($records->all());
}

protected function getCountryId(string $name): string
{
$country = Country::search($name)->first();

if (! $country) {
throw new CountryCodeNotFoundException($name);
Vote::saveToTemporaryTable($votes->all());
}

return $country->id;
Record::saveToTemporaryTable($records->all());
}

/**
Expand Down
52 changes: 17 additions & 35 deletions app/Jobs/Y2020/M06/Parliament/Turnouts/ImportAbroadTurnoutsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

namespace App\Jobs\Y2020\M06\Parliament\Turnouts;

use App\Events\CountryCodeNotFound;
use App\Exceptions\CountryCodeNotFoundException;
use App\Exceptions\MissingSourceFileException;
use App\Models\Country;
use App\Models\ScheduledJob;
use App\Models\Turnout;
use Illuminate\Bus\Batchable;
Expand Down Expand Up @@ -50,27 +47,23 @@ public function handle(): void
$segments = Turnout::segmentsMap();

foreach ($reader->getRecords() as $record) {
try {
$values->push([
'election_id' => $this->scheduledJob->election_id,
'country_id' => $this->getCountryId($record['UAT']),
'section' => $record['Nr sectie de votare'],

'initial_permanent' => $record['Votanti pe lista permanenta'],
'initial_complement' => $record['Votanti pe lista complementara'],
'permanent' => $record['LP'],
'complement' => $record['LSC'],
'supplement' => $record['LS'],
'mobile' => $record['UM'],

'area' => $record['Mediu'],
'has_issues' => $this->determineIfHasIssues($record),

...$segments->map(fn (string $segment) => $record[$segment]),
]);
} catch (CountryCodeNotFoundException $th) {
CountryCodeNotFound::dispatch($record['UAT'], $this->scheduledJob->election);
}
$values->push([
'election_id' => $this->scheduledJob->election_id,
'country_id' => RecordService::getCountryId($record['UAT']),
'section' => $record['Nr sectie de votare'],

'initial_permanent' => $record['Votanti pe lista permanenta'],
'initial_complement' => $record['Votanti pe lista complementara'],
'permanent' => $record['LP'],
'complement' => $record['LSC'],
'supplement' => $record['LS'],
'mobile' => $record['UM'],

'area' => $record['Mediu'],
'has_issues' => $this->determineIfHasIssues($record),

...$segments->map(fn (string $segment) => $record[$segment]),
]);
}

Turnout::saveToTemporaryTable($values->all());
Expand All @@ -89,17 +82,6 @@ protected function determineIfHasIssues(array $record): bool
return false;
}

protected function getCountryId(string $name): string
{
$country = Country::search($name)->first();

if (! $country) {
throw new CountryCodeNotFoundException($name);
}

return $country->id;
}

/**
* Get the tags that should be assigned to the job.
*
Expand Down
Loading

0 comments on commit 00aba83

Please sign in to comment.