Skip to content

Commit

Permalink
Merge pull request #223 from Traewelling/dev-ks-221
Browse files Browse the repository at this point in the history
🚑 Fixed points calculation
  • Loading branch information
jeyemwey authored Feb 20, 2021
2 parents b2e78d3 + dea7313 commit 21dd3e5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
4 changes: 0 additions & 4 deletions app/Http/Controllers/StatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@ public static function DeleteStatus($user, $statusId): ?bool {
$user->train_distance -= $trainCheckin->distance;
$user->train_duration -= $trainCheckin->duration;

//Don't subtract points, if status outside of current point calculation
if ($trainCheckin->departure->isAfter(Carbon::parse('last thursday 3:14am'))) {
$user->points -= $trainCheckin->points;
}
$user->update();
$status->delete();
return true;
Expand Down
1 change: 0 additions & 1 deletion app/Http/Controllers/TransportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ public static function TrainCheckin($tripId,

$user->train_distance += $trainCheckin->distance;
$user->train_duration += $trainCheckin->duration;
$user->points += $trainCheckin->points;

$user->update();

Expand Down
12 changes: 10 additions & 2 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
Expand All @@ -25,10 +26,10 @@ class User extends Authenticatable implements MustVerifyEmail
];
protected $casts = [
'email_verified_at' => 'datetime',
'private_profile' => 'boolean'
'private_profile' => 'boolean'
];
protected $appends = [
'averageSpeed'
'averageSpeed', 'points'
];

public function getAverageSpeedAttribute(): float {
Expand Down Expand Up @@ -67,4 +68,11 @@ public function icsTokens(): HasMany {
return $this->hasMany(IcsToken::class, 'user_id', 'id');
}

public function getPointsAttribute(): int {
return TrainCheckin::whereIn('status_id', $this->statuses()->select('id'))
->where('departure', '>=', Carbon::now()->subDays(7)->toIso8601String())
->select('points')
->sum('points');
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

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

class RemoveStaticPointsFromUsers extends Migration
{

public function up(): void {
Schema::table('users', function(Blueprint $table) {
$table->dropColumn('points');
});
}

public function down(): void {
Schema::table('users', function(Blueprint $table) {
$table->integer('points')
->unsigned()
->default(0)
->after('train_duration');
});
}
}

0 comments on commit 21dd3e5

Please sign in to comment.