From 00aba8310063f1304c4fbae83530a0d1e29f3244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20Ioni=C8=9B=C4=83?= Date: Tue, 3 Dec 2024 17:02:22 +0200 Subject: [PATCH] fix: country code detection (#151) --- .../Records/ImportAbroadRecordsJob.php | 82 ++++++++----------- .../ImportCorrespondenceRecordsJob.php | 82 ++++++++----------- .../Turnouts/ImportAbroadTurnoutsJob.php | 52 ++++-------- .../Records/ImportAbroadRecordsJob.php | 80 +++++++----------- .../Turnouts/ImportAbroadTurnoutsJob.php | 52 ++++-------- .../Records/ImportAbroadRecordsJob.php | 82 ++++++++----------- .../ImportCorrespondenceRecordsJob.php | 82 ++++++++----------- .../Turnouts/ImportAbroadTurnoutsJob.php | 53 ++++-------- .../Records/ImportAbroadRecordsJob.php | 82 ++++++++----------- .../ImportCorrespondenceRecordsJob.php | 82 ++++++++----------- .../Turnouts/ImportAbroadTurnoutsJob.php | 53 ++++-------- app/Services/RecordService.php | 13 +++ 12 files changed, 306 insertions(+), 489 deletions(-) diff --git a/app/Jobs/Y2020/M06/Parliament/Records/ImportAbroadRecordsJob.php b/app/Jobs/Y2020/M06/Parliament/Records/ImportAbroadRecordsJob.php index 6d31b8b..254b477 100644 --- a/app/Jobs/Y2020/M06/Parliament/Records/ImportAbroadRecordsJob.php +++ b/app/Jobs/Y2020/M06/Parliament/Records/ImportAbroadRecordsJob.php @@ -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; @@ -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()); } /** diff --git a/app/Jobs/Y2020/M06/Parliament/Records/ImportCorrespondenceRecordsJob.php b/app/Jobs/Y2020/M06/Parliament/Records/ImportCorrespondenceRecordsJob.php index 7b38fb8..464abda 100644 --- a/app/Jobs/Y2020/M06/Parliament/Records/ImportCorrespondenceRecordsJob.php +++ b/app/Jobs/Y2020/M06/Parliament/Records/ImportCorrespondenceRecordsJob.php @@ -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; @@ -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()); } /** diff --git a/app/Jobs/Y2020/M06/Parliament/Turnouts/ImportAbroadTurnoutsJob.php b/app/Jobs/Y2020/M06/Parliament/Turnouts/ImportAbroadTurnoutsJob.php index e77e339..43278b1 100644 --- a/app/Jobs/Y2020/M06/Parliament/Turnouts/ImportAbroadTurnoutsJob.php +++ b/app/Jobs/Y2020/M06/Parliament/Turnouts/ImportAbroadTurnoutsJob.php @@ -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; @@ -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()); @@ -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. * diff --git a/app/Jobs/Y2024/M06/EuropeanParliament/Records/ImportAbroadRecordsJob.php b/app/Jobs/Y2024/M06/EuropeanParliament/Records/ImportAbroadRecordsJob.php index 9a3489e..7f009cd 100644 --- a/app/Jobs/Y2024/M06/EuropeanParliament/Records/ImportAbroadRecordsJob.php +++ b/app/Jobs/Y2024/M06/EuropeanParliament/Records/ImportAbroadRecordsJob.php @@ -4,10 +4,7 @@ namespace App\Jobs\Y2024\M06\EuropeanParliament\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; @@ -64,65 +61,50 @@ 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['a1'], - 'eligible_voters_special' => $row['a2'], + 'eligible_voters_permanent' => $row['a1'], + 'eligible_voters_special' => $row['a2'], - 'present_voters_permanent' => $row['b1'], - 'present_voters_special' => $row['b2'], - 'present_voters_supliment' => $row['b3'], + 'present_voters_permanent' => $row['b1'], + 'present_voters_special' => $row['b2'], + 'present_voters_supliment' => $row['b3'], - 'papers_received' => $row['c'], - 'papers_unused' => $row['d'], - 'votes_valid' => $row['e'], - 'votes_null' => $row['f'], + 'papers_received' => $row['c'], + 'papers_unused' => $row['d'], + 'votes_valid' => $row['e'], + 'votes_null' => $row['f'], - 'has_issues' => RecordService::checkRecord($row), - ]); + 'has_issues' => RecordService::checkRecord($row), + ]); - $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()); } /** diff --git a/app/Jobs/Y2024/M06/EuropeanParliament/Turnouts/ImportAbroadTurnoutsJob.php b/app/Jobs/Y2024/M06/EuropeanParliament/Turnouts/ImportAbroadTurnoutsJob.php index 1705361..9c49604 100644 --- a/app/Jobs/Y2024/M06/EuropeanParliament/Turnouts/ImportAbroadTurnoutsJob.php +++ b/app/Jobs/Y2024/M06/EuropeanParliament/Turnouts/ImportAbroadTurnoutsJob.php @@ -4,10 +4,7 @@ namespace App\Jobs\Y2024\M06\EuropeanParliament\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 App\Services\RecordService; @@ -51,27 +48,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['Înscriși pe liste permanente'], - 'initial_complement' => 0, - 'permanent' => $record['LP'], - 'complement' => $record['LC'], - 'supplement' => $record['LS'], - 'mobile' => $record['UM'], - - 'area' => $record['Mediu'], - 'has_issues' => RecordService::checkTurnout($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['Înscriși pe liste permanente'], + 'initial_complement' => 0, + 'permanent' => $record['LP'], + 'complement' => $record['LC'], + 'supplement' => $record['LS'], + 'mobile' => $record['UM'], + + 'area' => $record['Mediu'], + 'has_issues' => RecordService::checkTurnout($record), + + ...$segments->map(fn (string $segment) => $record[$segment]), + ]); } Turnout::saveToTemporaryTable($values->all()); @@ -90,17 +83,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. * diff --git a/app/Jobs/Y2024/M11/President/Records/ImportAbroadRecordsJob.php b/app/Jobs/Y2024/M11/President/Records/ImportAbroadRecordsJob.php index a8aaf59..49872d0 100644 --- a/app/Jobs/Y2024/M11/President/Records/ImportAbroadRecordsJob.php +++ b/app/Jobs/Y2024/M11/President/Records/ImportAbroadRecordsJob.php @@ -4,10 +4,7 @@ namespace App\Jobs\Y2024\M11\President\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; @@ -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()); } /** diff --git a/app/Jobs/Y2024/M11/President/Records/ImportCorrespondenceRecordsJob.php b/app/Jobs/Y2024/M11/President/Records/ImportCorrespondenceRecordsJob.php index 928158c..25a57bd 100644 --- a/app/Jobs/Y2024/M11/President/Records/ImportCorrespondenceRecordsJob.php +++ b/app/Jobs/Y2024/M11/President/Records/ImportCorrespondenceRecordsJob.php @@ -4,10 +4,7 @@ namespace App\Jobs\Y2024\M11\President\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; @@ -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()); } /** diff --git a/app/Jobs/Y2024/M11/President/Turnouts/ImportAbroadTurnoutsJob.php b/app/Jobs/Y2024/M11/President/Turnouts/ImportAbroadTurnoutsJob.php index bedef79..5546b8d 100644 --- a/app/Jobs/Y2024/M11/President/Turnouts/ImportAbroadTurnoutsJob.php +++ b/app/Jobs/Y2024/M11/President/Turnouts/ImportAbroadTurnoutsJob.php @@ -4,12 +4,10 @@ namespace App\Jobs\Y2024\M11\President\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 App\Services\RecordService; use Illuminate\Bus\Batchable; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -50,27 +48,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['Înscriși pe liste permanente'], - 'initial_complement' => 0, - '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['Înscriși pe liste permanente'], + 'initial_complement' => 0, + '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()); @@ -89,17 +83,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. * diff --git a/app/Jobs/Y2024/M12/Parliament/Records/ImportAbroadRecordsJob.php b/app/Jobs/Y2024/M12/Parliament/Records/ImportAbroadRecordsJob.php index b36d786..7783886 100644 --- a/app/Jobs/Y2024/M12/Parliament/Records/ImportAbroadRecordsJob.php +++ b/app/Jobs/Y2024/M12/Parliament/Records/ImportAbroadRecordsJob.php @@ -4,10 +4,7 @@ namespace App\Jobs\Y2024\M12\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; @@ -58,67 +55,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['e'], - 'votes_null' => $row['f'], + 'votes_valid' => $row['e'], + 'votes_null' => $row['f'], - 'papers_received' => $row['c'], - 'papers_unused' => $row['d'], + 'papers_received' => $row['c'], + 'papers_unused' => $row['d'], - '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()); } public function uniqueId(): string diff --git a/app/Jobs/Y2024/M12/Parliament/Records/ImportCorrespondenceRecordsJob.php b/app/Jobs/Y2024/M12/Parliament/Records/ImportCorrespondenceRecordsJob.php index 4cd5019..79cf9bd 100644 --- a/app/Jobs/Y2024/M12/Parliament/Records/ImportCorrespondenceRecordsJob.php +++ b/app/Jobs/Y2024/M12/Parliament/Records/ImportCorrespondenceRecordsJob.php @@ -4,10 +4,7 @@ namespace App\Jobs\Y2024\M12\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; @@ -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()); } /** diff --git a/app/Jobs/Y2024/M12/Parliament/Turnouts/ImportAbroadTurnoutsJob.php b/app/Jobs/Y2024/M12/Parliament/Turnouts/ImportAbroadTurnoutsJob.php index d630ea8..acbde88 100644 --- a/app/Jobs/Y2024/M12/Parliament/Turnouts/ImportAbroadTurnoutsJob.php +++ b/app/Jobs/Y2024/M12/Parliament/Turnouts/ImportAbroadTurnoutsJob.php @@ -4,12 +4,10 @@ namespace App\Jobs\Y2024\M12\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 App\Services\RecordService; use Illuminate\Bus\Batchable; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -52,27 +50,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['Înscriși pe liste permanente'], - 'initial_complement' => 0, - '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['Înscriși pe liste permanente'], + 'initial_complement' => 0, + '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()); @@ -91,17 +85,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. * diff --git a/app/Services/RecordService.php b/app/Services/RecordService.php index 72d0c18..960edc5 100644 --- a/app/Services/RecordService.php +++ b/app/Services/RecordService.php @@ -6,7 +6,9 @@ use App\Enums\Part; use App\Enums\Time; +use App\Exceptions\CountryCodeNotFoundException; use App\Models\Candidate; +use App\Models\Country; use App\Models\Party; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Cache; @@ -61,6 +63,17 @@ public static function getPart(string $code): Part }; } + public static function getCountryId(string $name): string + { + $country = Country::search($name)->first(); + + if (! $country) { + throw new CountryCodeNotFoundException($name); + } + + return $country->id; + } + public static function isIndependentCandidate(string $name): bool { return Str::contains($name, config('import.independent_candidate_designation'), ignoreCase: true);