-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84d8c43
commit d9e02cb
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
'abandoned_challenges_deadline' => 3, // Three days | ||
'hope_bot_mail' => '[email protected]', | ||
|
||
'celebrities_limit' => 10, | ||
|
||
'achievements' => [ | ||
1 => [ | ||
'id' => 1, | ||
|
@@ -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, | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]); | ||
} | ||
} |