Skip to content

Commit 0dacf1f

Browse files
feat:[lar-140] add transaction and add feature test to remove point
1 parent 34e3a36 commit 0dacf1f

File tree

7 files changed

+107
-50
lines changed

7 files changed

+107
-50
lines changed

app/Livewire/Pages/Discussions/SingleDiscussion.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Filament\Forms\Concerns\InteractsWithForms;
1515
use Filament\Forms\Contracts\HasForms;
1616
use Illuminate\Contracts\View\View;
17+
use Illuminate\Support\Facades\DB;
1718
use Livewire\Component;
1819

1920
final class SingleDiscussion extends Component implements HasActions, HasForms
@@ -81,8 +82,17 @@ public function deleteAction(): Action
8182
->authorize('delete', $this->discussion)
8283
->requiresConfirmation()
8384
->successNotificationTitle(__('notifications.discussion.deleted'))
84-
->successRedirectUrl(route('discussions.index'))
85-
->after(fn ($record) => undoPoint(new DiscussionCreated($record)));
85+
->action(function (): void {
86+
DB::beginTransaction();
87+
88+
undoPoint(new DiscussionCreated($this->discussion));
89+
90+
$this->discussion->delete();
91+
92+
DB::commit();
93+
94+
$this->redirectRoute('discussions.index', navigate: true);
95+
});
8696
}
8797

8898
public function render(): View

app/Livewire/Pages/Forum/DetailThread.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Filament\Forms\Concerns\InteractsWithForms;
1313
use Filament\Forms\Contracts\HasForms;
1414
use Illuminate\Contracts\View\View;
15+
use Illuminate\Support\Facades\DB;
1516
use Livewire\Attributes\Layout;
1617
use Livewire\Attributes\On;
1718
use Livewire\Component;
@@ -54,9 +55,12 @@ public function deleteAction(): Action
5455
->authorize('delete', $this->thread)
5556
->requiresConfirmation()
5657
->action(function (): void {
57-
$this->thread->delete();
58+
DB::beginTransaction();
5859

5960
undoPoint(new ThreadCreated($this->thread));
61+
$this->thread->delete();
62+
63+
DB::commit();
6064

6165
$this->redirectRoute('forum.index', navigate: true);
6266
});

composer.lock

Lines changed: 44 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Feature/Livewire/Components/Slideovers/DiscussionFormTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
->assertSuccessful();
2323
});
2424

25-
it('user can create a new thread', function (): void {
25+
it('user can create a new discussion', function (): void {
2626
$user = $this->login();
2727
$tags = Tag::factory()->count(3)->create();
2828

@@ -36,10 +36,13 @@
3636
->assertHasNoFormErrors();
3737

3838
$discussion = Discussion::query()->first();
39+
$user->refresh();
3940

4041
expect($discussion?->user)->toBeInstanceOf(User::class)
4142
->and($discussion?->user->is($user))
42-
->toBeTrue();
43+
->toBeTrue()
44+
->and($user->getPoints())
45+
->toBe(20);
4346
});
4447

4548
it('validate forms input', function (): void {

tests/Feature/Livewire/Components/Slideovers/ThreadFormTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,13 @@
7575
->assertHasNoFormErrors();
7676

7777
$thread = Thread::query()->first();
78+
$user->refresh();
7879

7980
expect($thread?->user)->toBeInstanceOf(User::class)
8081
->and($thread?->user->is($user))
81-
->toBeTrue();
82+
->toBeTrue()
83+
->and($user->getPoints())
84+
->toBe(55);
8285
});
8386

8487
it('user cannot create thread with and unverified email address', function (): void {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use App\Gamify\Points\DiscussionCreated;
6+
use App\Livewire\Pages\Discussions\SingleDiscussion;
7+
use App\Models\Discussion;
8+
use App\Models\Tag;
9+
use Livewire\Livewire;
10+
11+
it('delete user action can remove discussion point ', function (): void {
12+
$user = $this->login();
13+
$discussion = Discussion::factory()->create(['user_id' => $user->id]);
14+
$tags = Tag::factory()->count(3)->create();
15+
16+
$discussion->tags()->attach($tags->modelKeys());
17+
18+
givePoint(new DiscussionCreated($discussion));
19+
20+
Livewire::test(SingleDiscussion::class, ['discussion' => $discussion])
21+
->callAction('deleteAction')
22+
->assertStatus(200);
23+
24+
$user->refresh();
25+
26+
expect($user->getPoints())
27+
->toBe(0);
28+
29+
});

0 commit comments

Comments
 (0)