From 8b1ce057c31376960d7ade415c5462be2c349348 Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Fri, 24 Jul 2026 11:56:44 -0300 Subject: [PATCH] Fire PostCreated on duplicate and drop unused PostHog eager load. Duplicates now sync usage and track post.created like CreatePost; TrackPost only eager-loads what capture needs. Co-authored-by: Cursor --- app/Actions/Post/DuplicatePost.php | 7 ++++++- app/Jobs/PostHog/TrackPost.php | 2 +- .../Actions/Post/DuplicatePostTest.php | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app/Actions/Post/DuplicatePost.php b/app/Actions/Post/DuplicatePost.php index 1e40854e..53b5d134 100644 --- a/app/Actions/Post/DuplicatePost.php +++ b/app/Actions/Post/DuplicatePost.php @@ -7,6 +7,7 @@ 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; @@ -20,7 +21,7 @@ class DuplicatePost { public static function execute(Post $original, User $user): Post { - return DB::transaction(function () use ($original, $user): Post { + $copy = DB::transaction(function () use ($original, $user): Post { $copy = $original->workspace->posts()->create([ 'user_id' => $user->id, 'content' => $original->content, @@ -56,5 +57,9 @@ public static function execute(Post $original, User $user): Post return $copy; }); + + PostCreated::dispatch($copy); + + return $copy; } } diff --git a/app/Jobs/PostHog/TrackPost.php b/app/Jobs/PostHog/TrackPost.php index 15137b0b..bd31ff04 100644 --- a/app/Jobs/PostHog/TrackPost.php +++ b/app/Jobs/PostHog/TrackPost.php @@ -33,7 +33,7 @@ public function handle(PostHogService $postHog): void } $post = Post::query() - ->with(['workspace.account.plan', 'user']) + ->with(['workspace.account.plan']) ->find($this->postId); if (! $post) { diff --git a/tests/Feature/Actions/Post/DuplicatePostTest.php b/tests/Feature/Actions/Post/DuplicatePostTest.php index 628c6943..93f6705c 100644 --- a/tests/Feature/Actions/Post/DuplicatePostTest.php +++ b/tests/Feature/Actions/Post/DuplicatePostTest.php @@ -5,9 +5,11 @@ use App\Actions\Post\DuplicatePost; 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; +use Illuminate\Support\Facades\Event; test('execute clones the post as a draft created via web', function () { $user = User::factory()->create(); @@ -29,3 +31,21 @@ ->and($copy->scheduled_at)->toBeNull() ->and($copy->published_at)->toBeNull(); }); + +test('execute dispatches PostCreated for the duplicated post', function () { + Event::fake([PostCreated::class]); + + $user = User::factory()->create(); + $workspace = Workspace::factory()->create(['user_id' => $user->id]); + $original = Post::factory()->create([ + 'workspace_id' => $workspace->id, + 'user_id' => $user->id, + ]); + + $copy = DuplicatePost::execute($original, $user); + + Event::assertDispatched( + PostCreated::class, + fn (PostCreated $event) => $event->post->id === $copy->id, + ); +});