Skip to content

Commit

Permalink
new achievement added: celebrity
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammadmp97 committed Dec 10, 2023
1 parent 84d8c43 commit d9e02cb
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
45 changes: 45 additions & 0 deletions app/Console/Commands/UpdateCelebritiesList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Console\Commands;

use App\Actions\Achievement\CreateUserAchievement;
use App\Models\User;
use App\Models\UserAchievement;
use App\Services\Achievement;
use Illuminate\Console\Command;

class UpdateCelebritiesList extends Command
{
protected $signature = 'app:update-celebrities-list';

protected $description = 'Update celebrities list and achievements';

public function handle(CreateUserAchievement $createUserAchievement)
{
$topTenFollowersCount = User::query()
->withCount(['followers'])
->distinct('followers_count')
->where('followers_count', '>', 0)
->orderBy('followers_count', 'desc')
->limit(config('hope.celebrities_limit'))
->pluck('followers_count');

$mostFollowedUsers = User::query()
->withCount(['followers'])
->orderBy('followers_count', 'desc')
->whereIn('followers_count', $topTenFollowersCount)
->get();

$achievement = Achievement::getById(8);

UserAchievement::query()
->where('achievement_id', $achievement['id'])
->delete();

foreach ($mostFollowedUsers as $user) {
$createUserAchievement->execute($user, $achievement);
}

return $this->comment('Done!');
}
}
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ protected function schedule(Schedule $schedule): void
$schedule->command('app:handle-deactivation-requests')->daily();

$schedule->command('app:delete-seen-notifications')->daily();

$schedule->command('app:update-celebrities-list')->weekly();
}

/**
Expand Down
8 changes: 8 additions & 0 deletions config/hope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
'abandoned_challenges_deadline' => 3, // Three days
'hope_bot_mail' => '[email protected]',

'celebrities_limit' => 10,

'achievements' => [
1 => [
'id' => 1,
Expand Down Expand Up @@ -54,5 +56,11 @@
'score' => 300,
'after_n_days' => 30,
],
[
'id' => 8,
'name' => 'celebrity',
'description' => 'The user earns this if is followed by many people',
'score' => 50,
],
],
];
27 changes: 27 additions & 0 deletions tests/Unit/Commands/UpdateCelebritiesListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Tests\Unit;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class UpdateCelebritiesListTest extends TestCase
{
use RefreshDatabase;

public function test_most_followed_users_get_an_achievement()
{
$setOfUsers = User::factory()->count(5)->create();

$this->user
->followers()
->attach($setOfUsers->pluck('id'));

$this->artisan('app:update-celebrities-list');

$this->assertDatabaseHas('user_achievements', [
'user_id' => 1,
]);
}
}

0 comments on commit d9e02cb

Please sign in to comment.