Skip to content

Commit

Permalink
challenges complete on the 30th day
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammadmp97 committed Sep 17, 2023
1 parent 42c7255 commit a2c8c81
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/Actions/Challenge/ContinueChallengeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public function execute(Challenge $challenge): Challenge

$challenge->timestamps = false;
$challenge->continued_at = now();

if ($challenge->passedDays() === 30) {
$challenge->status = ChallengeStatus::COMPLETED->value;
}

$challenge->save();

ChallengeContinued::dispatch($challenge);
Expand Down
33 changes: 33 additions & 0 deletions tests/Feature/ContinueChallengeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\Country;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;

class ContinueChallengeControllerTest extends TestCase
Expand Down Expand Up @@ -75,4 +76,36 @@ public function test_user_continues_the_challenge()
$this->assertTrue($challenge->continued_at->diffInDays() === 0);
$this->assertTrue($challenge->updated_at->diffInDays() > 0);
}

public function test_user_completes_a_challenge()
{
Notification::fake();

Challenge::factory()->create([
'status' => ChallengeStatus::ONGOING->value,
'continued_at' => '2022-01-01 21:00:00',
'created_at' => '2022-01-01 21:00:00',
]);


$this->travelTo('2023-01-31 21:01:00');

$this->postJson('api/challenges/1/continue');

$this
->getJson('api/users/1/achievements')
->assertJson([
'data' => [
[
'name' => 'hero',
],
],
]);

$this->assertDatabaseHas('challenges', [
'status' => ChallengeStatus::COMPLETED->value,
]);

Notification::assertCount(1);
}
}

0 comments on commit a2c8c81

Please sign in to comment.