From 50d16789737d2c6e0a5f888db90f01f1870c29fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20Ioni=C8=9B=C4=83?= Date: Mon, 11 Nov 2024 16:31:35 +0000 Subject: [PATCH] fix --- app/Concerns/BelongsToElection.php | 11 ++-- .../Imports/SimpleCandidateImporter.php | 12 +++- .../Records/ImportAbroadRecordsJob.php | 65 +++++++++++++------ .../Records/ImportCountyRecordsJob.php | 6 ++ app/Models/Scopes/BelongsToElectionScope.php | 9 ++- app/Models/Vote.php | 2 +- 6 files changed, 74 insertions(+), 31 deletions(-) diff --git a/app/Concerns/BelongsToElection.php b/app/Concerns/BelongsToElection.php index 556829d..ad4a16f 100644 --- a/app/Concerns/BelongsToElection.php +++ b/app/Concerns/BelongsToElection.php @@ -19,15 +19,18 @@ public function initializeBelongsToElection(): void protected static function bootBelongsToElection(): void { static::creating(function (self $model) { - if (! Filament::auth()->check()) { + // No need to change the election id if it's already set. + if (filled($model->election_id)) { return; } - if (! Filament::hasTenancy()) { + if (! Filament::auth()->check() || ! Filament::hasTenancy()) { return; } - - $model->election()->associate(Filament::getTenant()); + // There's no tenant outside of Filament. + if (filled($election = Filament::getTenant())) { + $model->election()->associate($election); + } }); static::addGlobalScope(new BelongsToElectionScope); diff --git a/app/Filament/Imports/SimpleCandidateImporter.php b/app/Filament/Imports/SimpleCandidateImporter.php index e4e4368..ee95eff 100644 --- a/app/Filament/Imports/SimpleCandidateImporter.php +++ b/app/Filament/Imports/SimpleCandidateImporter.php @@ -50,10 +50,18 @@ protected function afterValidate(): void public static function getCompletedNotificationBody(Import $import): string { - $body = 'Your candidate import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; + $body = \sprintf( + 'Your candidate import has completed and %d %s imported.', + number_format($import->successful_rows), + str('row')->plural($import->successful_rows) + ); if ($failedRowsCount = $import->getFailedRowsCount()) { - $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.'; + $body .= \sprintf( + ' %d %s failed to import.', + number_format($failedRowsCount), + str('row')->plural($failedRowsCount) + ); } return $body; diff --git a/app/Jobs/Europarl240609/Records/ImportAbroadRecordsJob.php b/app/Jobs/Europarl240609/Records/ImportAbroadRecordsJob.php index 1044c2f..8dd1cc0 100644 --- a/app/Jobs/Europarl240609/Records/ImportAbroadRecordsJob.php +++ b/app/Jobs/Europarl240609/Records/ImportAbroadRecordsJob.php @@ -5,12 +5,15 @@ namespace App\Jobs\Europarl240609\Records; use App\Actions\CheckRecordHasIssues; +use App\Actions\GenerateMappedVotablesList; +use App\Enums\Part; 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; use Illuminate\Bus\Batchable; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -44,7 +47,7 @@ public function __construct(ScheduledJob $scheduledJob) $this->scheduledJob = $scheduledJob; } - public function handle(CheckRecordHasIssues $checker): void + public function handle(CheckRecordHasIssues $checker, GenerateMappedVotablesList $generator): void { $disk = $this->scheduledJob->disk(); $path = $this->scheduledJob->getSourcePath('sr.csv'); @@ -63,36 +66,60 @@ public function handle(CheckRecordHasIssues $checker): void $reader = Reader::createFromStream($disk->readStream($path)); $reader->setHeaderOffset(0); - $values = collect(); + $records = collect(); - $records = $reader->getRecords(); - foreach ($records as $record) { + $votables = $generator->votables($reader->getHeader()); + + foreach ($reader->getRecords() as $row) { try { - $values->push([ + $countryId = $this->getCountryId($row['uat_name']); + + $records->push([ 'election_id' => $this->scheduledJob->election_id, - 'country_id' => $this->getCountryId($record['uat_name']), - 'section' => $record['precinct_nr'], + 'country_id' => $countryId, + 'section' => $row['precinct_nr'], - 'eligible_voters_permanent' => $record['a1'], - 'eligible_voters_special' => $record['a2'], + 'eligible_voters_permanent' => $row['a1'], + 'eligible_voters_special' => $row['a2'], - 'present_voters_permanent' => $record['b1'], - 'present_voters_special' => $record['b2'], - 'present_voters_supliment' => $record['b3'], + 'present_voters_permanent' => $row['b1'], + 'present_voters_special' => $row['b2'], + 'present_voters_supliment' => $row['b3'], - 'papers_received' => $record['c'], - 'papers_unused' => $record['d'], - 'votes_valid' => $record['e'], - 'votes_null' => $record['f'], + 'papers_received' => $row['c'], + 'papers_unused' => $row['d'], + 'votes_valid' => $row['e'], + 'votes_null' => $row['f'], - 'has_issues' => $checker->checkRecord($record), + 'has_issues' => $checker->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' => match ($row['report_stage_code']) { + 'FINAL' => Part::FINAL, + 'PART' => Part::PART, + 'PROV' => Part::PROV, + }, + + 'votable_type' => $votable['votable_type'], + 'votable_id' => $votable['votable_id'], + + 'votes' => $row[$column], + ]); + } + + Vote::saveToTemporaryTable($votes->all()); } catch (CountryCodeNotFoundException $th) { - CountryCodeNotFound::dispatch($record['uat_name'], $this->scheduledJob->election); + CountryCodeNotFound::dispatch($row['uat_name'], $this->scheduledJob->election); } } - Record::saveToTemporaryTable($values->all()); + Record::saveToTemporaryTable($records->all()); } protected function getCountryId(string $name): string diff --git a/app/Jobs/Europarl240609/Records/ImportCountyRecordsJob.php b/app/Jobs/Europarl240609/Records/ImportCountyRecordsJob.php index 332d5c9..e983fe6 100644 --- a/app/Jobs/Europarl240609/Records/ImportCountyRecordsJob.php +++ b/app/Jobs/Europarl240609/Records/ImportCountyRecordsJob.php @@ -6,6 +6,7 @@ use App\Actions\CheckRecordHasIssues; use App\Actions\GenerateMappedVotablesList; +use App\Enums\Part; use App\Exceptions\MissingSourceFileException; use App\Models\County; use App\Models\Record; @@ -101,6 +102,11 @@ public function handle(CheckRecordHasIssues $checker, GenerateMappedVotablesList 'county_id' => $this->county->id, 'locality_id' => $row['uat_siruta'], 'section' => $row['precinct_nr'], + 'part' => match ($row['report_stage_code']) { + 'FINAL' => Part::FINAL, + 'PART' => Part::PART, + 'PROV' => Part::PROV, + }, 'votable_type' => $votable['votable_type'], 'votable_id' => $votable['votable_id'], diff --git a/app/Models/Scopes/BelongsToElectionScope.php b/app/Models/Scopes/BelongsToElectionScope.php index dd880ae..647df45 100644 --- a/app/Models/Scopes/BelongsToElectionScope.php +++ b/app/Models/Scopes/BelongsToElectionScope.php @@ -16,14 +16,13 @@ class BelongsToElectionScope implements Scope */ public function apply(Builder $builder, Model $model): void { - if (! Filament::auth()->check()) { + if (! Filament::auth()->check() || ! Filament::hasTenancy()) { return; } - if (! Filament::hasTenancy()) { - return; + // There's no tenant outside of Filament. + if (filled($election = Filament::getTenant())) { + $builder->whereBelongsTo($election); } - - $builder->whereBelongsTo(Filament::getTenant()); } } diff --git a/app/Models/Vote.php b/app/Models/Vote.php index 25e6c23..07f9c9a 100644 --- a/app/Models/Vote.php +++ b/app/Models/Vote.php @@ -32,7 +32,7 @@ class Vote extends Model implements TemporaryTable 'county_id', 'locality_id', 'section', - // 'part', + 'part', 'votable_type', 'votable_id', 'votes',