diff --git a/app/Actions/Post/CreatePost.php b/app/Actions/Post/CreatePost.php index 11c5a1f4..26535cec 100644 --- a/app/Actions/Post/CreatePost.php +++ b/app/Actions/Post/CreatePost.php @@ -6,7 +6,6 @@ use App\Enums\Post\CreatedVia; use App\Enums\Post\Status as PostStatus; -use App\Events\PostCreated; use App\Models\Post; use App\Models\User; use App\Models\Workspace; @@ -91,8 +90,6 @@ public static function execute(Workspace $workspace, User $user, array $data): P return $post; }); - PostCreated::dispatch($post); - return $post; } diff --git a/app/Actions/Post/DuplicatePost.php b/app/Actions/Post/DuplicatePost.php index 53b5d134..1e40854e 100644 --- a/app/Actions/Post/DuplicatePost.php +++ b/app/Actions/Post/DuplicatePost.php @@ -7,7 +7,6 @@ use App\Enums\Post\CreatedVia; use App\Enums\Post\Status as PostStatus; use App\Enums\PostPlatform\Status as PostPlatformStatus; -use App\Events\PostCreated; use App\Models\Post; use App\Models\User; use Illuminate\Support\Facades\DB; @@ -21,7 +20,7 @@ class DuplicatePost { public static function execute(Post $original, User $user): Post { - $copy = DB::transaction(function () use ($original, $user): Post { + return DB::transaction(function () use ($original, $user): Post { $copy = $original->workspace->posts()->create([ 'user_id' => $user->id, 'content' => $original->content, @@ -57,9 +56,5 @@ public static function execute(Post $original, User $user): Post return $copy; }); - - PostCreated::dispatch($copy); - - return $copy; } } diff --git a/app/Observers/PostObserver.php b/app/Observers/PostObserver.php index 33456b27..c9cfd84a 100644 --- a/app/Observers/PostObserver.php +++ b/app/Observers/PostObserver.php @@ -6,11 +6,18 @@ use App\Enums\Automation\Trigger\Type as TriggerType; use App\Enums\Post\Status as PostStatus; +use App\Events\PostCreated; use App\Jobs\Automation\DispatchPostTriggerAutomationsJob; use App\Models\Post; +use Illuminate\Contracts\Events\ShouldHandleEventsAfterCommit; -class PostObserver +class PostObserver implements ShouldHandleEventsAfterCommit { + public function created(Post $post): void + { + PostCreated::dispatch($post); + } + public function saved(Post $post): void { if (! $post->wasChanged('status')) { @@ -27,6 +34,6 @@ public function saved(Post $post): void return; } - DispatchPostTriggerAutomationsJob::dispatch($post, $triggerType)->afterCommit(); + DispatchPostTriggerAutomationsJob::dispatch($post, $triggerType); } } diff --git a/tests/Feature/Actions/Post/CreatePostTest.php b/tests/Feature/Actions/Post/CreatePostTest.php index 410257dd..3a50dcee 100644 --- a/tests/Feature/Actions/Post/CreatePostTest.php +++ b/tests/Feature/Actions/Post/CreatePostTest.php @@ -9,7 +9,7 @@ use App\Models\Workspace; use Illuminate\Support\Facades\Event; -test('execute dispatches PostCreated with the persisted post', function () { +test('execute relies on the observer to dispatch PostCreated', function () { Event::fake([PostCreated::class]); $user = User::factory()->create(); diff --git a/tests/Feature/Actions/Post/DuplicatePostTest.php b/tests/Feature/Actions/Post/DuplicatePostTest.php index 93f6705c..13e43daf 100644 --- a/tests/Feature/Actions/Post/DuplicatePostTest.php +++ b/tests/Feature/Actions/Post/DuplicatePostTest.php @@ -32,9 +32,7 @@ ->and($copy->published_at)->toBeNull(); }); -test('execute dispatches PostCreated for the duplicated post', function () { - Event::fake([PostCreated::class]); - +test('execute relies on the observer to dispatch PostCreated for the duplicate', function () { $user = User::factory()->create(); $workspace = Workspace::factory()->create(['user_id' => $user->id]); $original = Post::factory()->create([ @@ -42,6 +40,8 @@ 'user_id' => $user->id, ]); + Event::fake([PostCreated::class]); + $copy = DuplicatePost::execute($original, $user); Event::assertDispatched( diff --git a/tests/Feature/Observers/PostObserverTest.php b/tests/Feature/Observers/PostObserverTest.php new file mode 100644 index 00000000..ebda452b --- /dev/null +++ b/tests/Feature/Observers/PostObserverTest.php @@ -0,0 +1,40 @@ +create(); + $workspace = Workspace::factory()->create(['user_id' => $user->id]); + + $post = Post::factory()->create([ + 'workspace_id' => $workspace->id, + 'user_id' => $user->id, + ]); + + Event::assertDispatched( + PostCreated::class, + fn (PostCreated $event) => $event->post->id === $post->id, + ); +}); + +test('creating a post quietly does not dispatch PostCreated', function () { + Event::fake([PostCreated::class]); + + $user = User::factory()->create(); + $workspace = Workspace::factory()->create(['user_id' => $user->id]); + + Post::factory()->createQuietly([ + 'workspace_id' => $workspace->id, + 'user_id' => $user->id, + ]); + + Event::assertNotDispatched(PostCreated::class); +});