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 <cursoragent@cursor.com>
This commit is contained in:
Paulo Castellano 2026-07-24 11:56:44 -03:00
parent 2c50a60790
commit 8b1ce057c3
3 changed files with 27 additions and 2 deletions

View file

@ -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;
}
}

View file

@ -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) {

View file

@ -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,
);
});