Skip to content

feat: (LAR-105) create TransfertDiscutionToThreadAction file #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions app/Actions/Discussion/ConvertDiscussionToThreadAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace App\Actions\Discussion;

use App\Models\Discussion;
use App\Models\Thread;
use Illuminate\Support\Facades\DB;

final class ConvertDiscussionToThreadAction
{
public function execute(Discussion $discussion): Thread
{
return DB::transaction(function () use ($discussion) {
$thread = Thread::create([
'title' => $discussion->title,
'slug' => $discussion->slug,
'body' => $discussion->body,
'user_id' => $discussion->user_id,
'last_posted_at' => null,
]);

$discussion->replies()->update([
'replyable_type' => 'thread',
'replyable_id' => $thread->id,
]);

$discussion->delete();

app(NotifyUsersOfThreadConversion::class)->execute($thread);

return $thread;
});
}
}
25 changes: 25 additions & 0 deletions app/Actions/Discussion/NotifyUsersOfThreadConversion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\Actions\Discussion;

use App\Models\Thread;
use App\Models\User;
use App\Notifications\ThreadConvertedByAdmin;
use App\Notifications\ThreadConvertedByCreator;
use Illuminate\Support\Facades\Auth;

final class NotifyUsersOfThreadConversion
{
public function execute(Thread $thread): void
{
$usersToNotify = $thread->replies()->pluck('user_id')->unique()->toArray();

User::whereIn('id', $usersToNotify)->get()->each->notify(new ThreadConvertedByCreator($thread));

if (Auth::check() && Auth::user()->isAdmin()) {
$thread->user->notify(new ThreadConvertedByAdmin($thread));
}
}
}
31 changes: 31 additions & 0 deletions app/Livewire/Modals/ConvertDiscussion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Livewire\Modals;

use App\Actions\Discussion\ConvertDiscussionToThreadAction;
use App\Models\Discussion;
use Illuminate\Contracts\View\View;
use LivewireUI\Modal\ModalComponent;

final class ConvertDiscussion extends ModalComponent
{
public int $discussionId;

public function save(): void
{
$discussion = Discussion::findOrFail($this->discussionId);

$this->authorize('convertedToThread', $discussion);

$thread = app(ConvertDiscussionToThreadAction::class)->execute($discussion);

Check failure on line 22 in app/Livewire/Modals/ConvertDiscussion.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $discussion of method App\Actions\Discussion\ConvertDiscussionToThreadAction::execute() expects App\Models\Discussion, Illuminate\Database\Eloquent\Model given.

$this->redirectRoute('forum.show', $thread, navigate: true);
}

public function render(): View
{
return view('livewire.modals.convert-discussion');
}
}
41 changes: 41 additions & 0 deletions app/Notifications/ThreadConvertedByAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace App\Notifications;

use App\Models\Thread;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

final class ThreadConvertedByAdmin extends Notification
{
use Queueable;

public function __construct(public Thread $thread) {}

/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->subject('pages/discussion.converted_by_admin')
->greeting('Hello!')
->line('An admin has converted a discussion to a thread.')
->line('Thread Title: '.$this->thread->title)
->action('View Thread', route('forum.show', $this->thread))
->line('This action was performed by an administrator.');
}
}
40 changes: 40 additions & 0 deletions app/Notifications/ThreadConvertedByCreator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Notifications;

use App\Models\Thread;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

final class ThreadConvertedByCreator extends Notification
{
use Queueable;

public function __construct(public Thread $thread) {}

/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->subject(__('pages/discussion.converted_by_creator'))
->line('A discussion you participated in has been converted to a thread.')
->line('Thread Title: '.$this->thread->title)
->action('View Thread', route('forum.show', $this->thread))
->line('Thank you for your participation!');
}
}
5 changes: 5 additions & 0 deletions app/Policies/DiscussionPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ public function report(User $user, Discussion $discussion): bool
{
return $user->hasVerifiedEmail() && ! $discussion->isAuthoredBy($user);
}

public function convertedToThread(User $user, Discussion $discussion): bool
{
return $discussion->isAuthoredBy($user) || $user->isAdmin();
}
}
1 change: 1 addition & 0 deletions lang/en/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
'save' => 'Save',
'ban' => 'Ban',
'unban' => 'Cancel ban',
'confirm' => 'Confirm',

];
28 changes: 28 additions & 0 deletions lang/en/pages/discussion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

return [

'title' => 'Tous les sujets de discussion',
'contributors' => [
'top' => 'Top Contributeurs',
'description' => 'Les personnes qui ont lancé le plus de discussions sur le site.',
],
'empty' => 'Discussions sans commentaires',
'empty_description' => 'Les discussions / sujets qui n’ont pas encore eu de commentaires. Soyez le premier à apporter votre contribution.',
'total_answer' => 'total réponses',
'new_discussion' => 'Nouveau discussion',
'filter' => [
'recent' => 'Récent',
'popular' => 'Populaire',
'active' => 'Actif',
],
'comments_count' => 'Commentaires (:count)',
'convert_to_thread' => 'Convert to thread',
'confirm_conversion' => 'Confirm conversion',
'text_confirmation' => 'Do you really want to turn this discussion into a topic?',
'converted_by_creator' => 'Discussion Converted to Thread',
'converted_by_admin' => 'Discussion Converted to Thread by the administrator',

];
1 change: 1 addition & 0 deletions lang/fr/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
'save' => 'Enregistrer',
'ban' => 'Bannir',
'unban' => 'Dé-bannir',
'confirm' => 'Confirmer',

];
5 changes: 5 additions & 0 deletions lang/fr/pages/discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@
'active' => 'Actif',
],
'comments_count' => 'Commentaires (:count)',
'convert_to_thread' => 'Convertir en sujet',
'confirm_conversion' => 'Confirmez la conversion',
'text_confirmation' => 'Voulez-vous vraiment transformer cette discussion en sujet de forum?',
'converted_by_creator' => 'Discussion convertie en sujet par le createur',
'converted_by_admin' => 'Discussion convertie en sujet par l\'administrateur',

];
35 changes: 35 additions & 0 deletions resources/views/livewire/modals/convert-discussion.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<x-modal
header-classes="p-4 border-b border-gray-100 sm:px-6 sm:py-4"
content-classes="relative p-4 flex-1 sm:max-h-[500px] sm:px-6 sm:px-5"
footer-classes="px-4 py-3 border-t border-gray-100 sm:px-6 sm:flex sm:flex-row-reverse"
form-action="save"
>
<x-slot name="title">
{{ __("pages/discussion.confirm_conversion") }}
</x-slot>

<div class="space-y-4 pb-5">
<div class="grid grid-cols-2 gap-4">
<div class="space-y-2">
<x-slot name="content">
{{ __("pages/discussion.text_confirmation") }}
</x-slot>
</div>
</div>
</div>

<x-slot name="buttons">
<x-buttons.submit
:title="__('action.confirm')"
wire:loading.attr="data-loading"
class="w-full sm:ml-3 sm:w-auto"
/>
<x-buttons.default
type="button"
wire:click="$dispatch('closeModal')"
class="w-full px-4 py-2 mt-3 text-sm sm:mt-0 sm:w-auto"
>
{{ __('action.cancel') }}
</x-buttons.default>
</x-slot>
</x-modal>
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,28 @@ class="mx-auto mt-6 text-sm prose prose-sm prose-green max-w-none dark:prose-inv
<div class="flex items-center mt-2 space-x-2">
<x-link
href="{{ route('discussions.edit', $discussion) }}"
class="font-sans text-sm leading-5 text-gray-500 dark:text-gray-400 hover:underline focus:outline-none"
class="text-sm leading-5 text-gray-500 dark:text-gray-400 hover:underline focus:outline-none"
>
{{ __('Éditer') }}
{{ __('action.edit') }}
</x-link>
<span class="font-medium text-gray-500 dark:text-gray-400">·</span>
<button
onclick="Livewire.dispatch('openModal', {component: 'modals.delete-discussion', arguments: {{ json_encode([$discussion->id]) }})"
onclick="Livewire.dispatch('openModal', {component: 'modals.delete-discussion', arguments: {{ json_encode([$discussion->id]) }}})"
type="button"
class="font-sans text-sm leading-5 text-red-500 hover:underline focus:outline-none"
class="text-sm leading-5 text-red-500 hover:underline focus:outline-none"
>
{{ __('Supprimer') }}
{{ __('actions.delete') }}
</button>
@can('convertedToThread', $discussion)
<span class="font-medium text-gray-500 dark:text-gray-400">·</span>
<button
onclick="Livewire.dispatch('openModal', {component: 'modals.convert-discussion', arguments: { discussionId: {{ $discussion->id }} }})"
class="text-sm leading-5 text-gray-500 dark:text-gray-400 hover:underline focus:outline-none"
>
{{ __('pages/discussion.convert_to_thread') }}
{{ __('pages/home.description') }}
</button>
@endcan
</div>
@endcan
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

use App\Actions\Discussion\ConvertDiscussionToThreadAction;
use App\Models\Discussion;
use App\Models\Reply;
use App\Models\Thread;
use Illuminate\Support\Facades\Notification;
use Spatie\Permission\Models\Role;

beforeEach(function (): void {
$this->user = $this->login();
$this->discussion = Discussion::factory()->create(['user_id' => $this->user->id]);
Role::create(['name' => 'admin']);
Notification::fake();
});

describe(ConvertDiscussionToThreadAction::class, function (): void {
it('allows discussion author to convert his discussion to a forum topic', function (): void {
$replies = Reply::factory()->count(3)->create([
'replyable_type' => 'discussion',
'replyable_id' => $this->discussion->id,
]);

$thread = app(ConvertDiscussionToThreadAction::class)->execute(discussion: $this->discussion);

expect($thread)->toBeInstanceOf(Thread::class)
->and(Discussion::find($this->discussion->id))->toBeNull();

$replies->each(function ($reply) use ($thread): void {
$updatedReply = Reply::find($reply->id);

expect($updatedReply->replyable_type)->toBe('thread')
->and($updatedReply->replyable_id)->toBe($thread->id);
});

Notification::assertCount(3);
});

it('allows admin users to convert any discussion to a forum topic', function (): void {
$this->user->assignRole('admin');

Reply::factory()->count(3)->create([
'replyable_type' => 'discussion',
'replyable_id' => $this->discussion->id,
]);

$thread = app(ConvertDiscussionToThreadAction::class)->execute(discussion: $this->discussion);

expect($thread)->toBeInstanceOf(Thread::class);

Notification::assertCount(4);
});
});
30 changes: 30 additions & 0 deletions tests/Feature/Livewire/Modal/ConvertDiscussionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

use App\Livewire\Modals\ConvertDiscussion;
use App\Models\Discussion;
use App\Models\User;
use Livewire\Livewire;

beforeEach(function (): void {
$this->user = $this->login();
});

describe(ConvertDiscussion::class, function (): void {
it('requires authorization for user to convert discussion', function (): void {
$user = User::factory()->create();
$discussion = Discussion::factory()->create(['user_id' => $user->id]);

Livewire::test(ConvertDiscussion::class)
->set('discussionId', $discussion->id)
->call('save')
->assertForbidden();
});

it('throws exception for non-existent discussion', function (): void {
Livewire::test(ConvertDiscussion::class)
->set('discussionId', 9)
->call('save');
})->throws(Illuminate\Database\Eloquent\ModelNotFoundException::class);
});
Loading