Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Nov 11, 2024
1 parent 2bd1388 commit 50d1678
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 31 deletions.
11 changes: 7 additions & 4 deletions app/Concerns/BelongsToElection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 10 additions & 2 deletions app/Filament/Imports/SimpleCandidateImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
65 changes: 46 additions & 19 deletions app/Jobs/Europarl240609/Records/ImportAbroadRecordsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions app/Jobs/Europarl240609/Records/ImportCountyRecordsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'],
Expand Down
9 changes: 4 additions & 5 deletions app/Models/Scopes/BelongsToElectionScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion app/Models/Vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Vote extends Model implements TemporaryTable
'county_id',
'locality_id',
'section',
// 'part',
'part',
'votable_type',
'votable_id',
'votes',
Expand Down

0 comments on commit 50d1678

Please sign in to comment.