Skip to content

Commit

Permalink
wip (#76)
Browse files Browse the repository at this point in the history
* wip

* wip

---------

Co-authored-by: Andrei Ioniță <[email protected]>
  • Loading branch information
gheorghelupu17 and andreiio authored Nov 23, 2024
1 parent c0756d3 commit 54fa089
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 2 deletions.
10 changes: 10 additions & 0 deletions app/Http/Controllers/Api/V1/TurnoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public function total(Election $election)//: JsonResource
toBase: true,
);

$result->last_updated_at = $election->turnouts_updated_at;

return TurnoutResource::make($result);
}

Expand Down Expand Up @@ -102,6 +104,8 @@ public function diaspora(Election $election): JsonResource
toBase: true,
);

$result->last_updated_at = $election->turnouts_updated_at;

return TurnoutDiasporaAggregatedResource::make($result);
}

Expand Down Expand Up @@ -135,6 +139,8 @@ public function country(Election $election, Country $country): JsonResource
toBase: true,
);

$result->last_updated_at = $election->turnouts_updated_at;

return TurnoutDiasporaResource::make($result);
}

Expand Down Expand Up @@ -189,6 +195,8 @@ public function national(Election $election): JsonResource
toBase: true,
);

$result->last_updated_at = $election->turnouts_updated_at;

return TurnoutNationalAggregatedResource::make($result);
}

Expand Down Expand Up @@ -255,6 +263,8 @@ public function county(Election $election, County $county): JsonResource
toBase: true,
);

$result->last_updated_at = $election->turnouts_updated_at;

return TurnoutNationalAggregatedResource::make($result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class TurnoutDiasporaAggregatedResource extends JsonResource
public function toArray(Request $request): array
{
return [
/*
* Last time the data was updated in Y-m-d H:i:s format.
*
* @var string
*/
'last_updated_at' => $this->last_updated_at?->toDateTimeString(),

/*
* Total number of voters subscribed to the election permanent list .
* @var integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class TurnoutDiasporaResource extends JsonResource
public function toArray(Request $request): array
{
return [
/*
* Last time the data was updated in Y-m-d H:i:s format.
*
* @var string|null
*/
'last_updated_at' => data_get($this->resource, 'last_updated_at')?->toDateTimeString(),

/*
* Total number of voters subscribed to the election permanent list .
* @var integer
Expand Down Expand Up @@ -46,6 +53,7 @@ public function toArray(Request $request): array
'demographics' => DemographicsResource::make($this->demographics),

'areas' => AreaResource::make($this->areas),

];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class TurnoutNationalAggregatedResource extends JsonResource
public function toArray(Request $request): array
{
return [
/*
* Last time the data was updated in Y-m-d H:i:s format.
*
* @var string
*/
'last_updated_at' => $this->last_updated_at?->toDateTimeString(),

/*
* Total number of voters subscribed to the election permanent list .
* @var integer
Expand Down Expand Up @@ -48,7 +55,6 @@ public function toArray(Request $request): array
* @var AreaResource
*/
'areas' => AreaResource::make($this->areas),

];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class TurnoutNationalResource extends JsonResource
public function toArray(Request $request): array
{
return [
/*
* Last time the data was updated in Y-m-d H:i:s format.
*
* @var string|null
*/
'last_updated_at' => data_get($this->resource, 'last_updated_at')?->toDateTimeString(),

/*
* Total number of voters subscribed to the election permanent list .
* @var integer
Expand Down
8 changes: 8 additions & 0 deletions app/Http/Resources/Turnout/TurnoutResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class TurnoutResource extends JsonResource
public function toArray(Request $request): array
{
return [
/*
* Last time the data was updated in Y-m-d H:i:s format.
*
* @var string
*/
'last_updated_at' => $this->last_updated_at?->toDateTimeString(),

/*
* Total number of voters subscribed to the election permanent list .
* @var integer
Expand All @@ -32,6 +39,7 @@ public function toArray(Request $request): array
'demographics' => DemographicsResource::make($this->demographics),

'areas' => AreaResource::make($this->areas),

];
}
}
2 changes: 2 additions & 0 deletions app/Jobs/Europarl240609/Records/FetchRecordsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Jobs\DeleteTemporaryTableData;
use App\Jobs\PersistTemporaryTableData;
use App\Jobs\SchedulableJob;
use App\Jobs\UpdateElectionRecordsTimestamp;
use App\Models\County;
use App\Models\Record;
use App\Models\Vote;
Expand Down Expand Up @@ -43,6 +44,7 @@ public function execute(): void
Bus::batch($jobs)
->catch($persistAndClean)
->then($persistAndClean)
->then(fn () => UpdateElectionRecordsTimestamp::dispatch($electionId))
->name("$electionName / Rezultate / $time")
->allowFailures()
->dispatch();
Expand Down
2 changes: 2 additions & 0 deletions app/Jobs/Europarl240609/Turnouts/FetchTurnoutsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Jobs\DeleteTemporaryTableData;
use App\Jobs\PersistTemporaryTableData;
use App\Jobs\SchedulableJob;
use App\Jobs\UpdateElectionTurnoutsTimestamp;
use App\Models\County;
use App\Models\Turnout;
use Illuminate\Support\Facades\Bus;
Expand Down Expand Up @@ -74,6 +75,7 @@ public function execute(): void
Bus::batch($jobs)
->catch($persistAndClean)
->then($persistAndClean)
->then(fn () => UpdateElectionTurnoutsTimestamp::dispatch($electionId))
->name("$electionName / Prezență / $time")
->allowFailures()
->dispatch();
Expand Down
2 changes: 2 additions & 0 deletions app/Jobs/Presidential241124/Turnouts/FetchTurnoutsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Jobs\DeleteTemporaryTableData;
use App\Jobs\PersistTemporaryTableData;
use App\Jobs\SchedulableJob;
use App\Jobs\UpdateElectionTurnoutsTimestamp;
use App\Models\County;
use App\Models\Turnout;
use Illuminate\Support\Facades\Bus;
Expand Down Expand Up @@ -74,6 +75,7 @@ public function execute(): void
Bus::batch($jobs)
->catch($persistAndClean)
->then($persistAndClean)
->then(fn () => UpdateElectionTurnoutsTimestamp::dispatch($electionId))
->name("$electionName / Prezență / $time")
->allowFailures()
->dispatch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Jobs\DeleteTemporaryTableData;
use App\Jobs\PersistTemporaryTableData;
use App\Jobs\SchedulableJob;
use App\Jobs\UpdateElectionTurnoutsTimestamp;
use App\Models\County;
use App\Models\Turnout;
use Illuminate\Support\Facades\Bus;
Expand Down Expand Up @@ -67,7 +68,7 @@ public function execute(): void

Bus::batch([new ImportTurnoutsJob($this->scheduledJob, County::find(403))])
->catch($persistAndClean)
->then($persistAndClean)
->then(fn () => UpdateElectionTurnoutsTimestamp::dispatch($electionId))
->name("$electionName / Prezență / $time")
->allowFailures()
->dispatch();
Expand Down
34 changes: 34 additions & 0 deletions app/Jobs/UpdateElectionRecordsTimestamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Models\Election;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;

class UpdateElectionRecordsTimestamp implements ShouldQueue
{
use Dispatchable;
use Queueable;

public Election $election;

/**
* Create a new job instance.
*/
public function __construct(int $electionId)
{
$this->election = Election::find($electionId);
}

/**
* Execute the job.
*/
public function handle(): void
{
$this->election->touch('records_updated_at');
}
}
34 changes: 34 additions & 0 deletions app/Jobs/UpdateElectionTurnoutsTimestamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Models\Election;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;

class UpdateElectionTurnoutsTimestamp implements ShouldQueue
{
use Dispatchable;
use Queueable;

public Election $election;

/**
* Create a new job instance.
*/
public function __construct(int $electionId)
{
$this->election = Election::find($electionId);
}

/**
* Execute the job.
*/
public function handle(): void
{
$this->election->touch('turnouts_updated_at');
}
}
4 changes: 4 additions & 0 deletions app/Models/Election.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Election extends Model implements HasName, HasAvatar
'has_lists',
'properties',
'old_id',
'turnouts_updated_at',
'records_updated_at',
];

protected function casts(): array
Expand All @@ -46,6 +48,8 @@ protected function casts(): array
'has_lists' => 'boolean',
'properties' => 'collection',
'old_id' => 'int',
'turnouts_updated_at' => 'datetime',
'records_updated_at' => 'datetime',
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('elections', function (Blueprint $table) {
$table->timestamp('turnouts_updated_at')->nullable();
$table->timestamp('records_updated_at')->nullable();
});
}
};

0 comments on commit 54fa089

Please sign in to comment.