Onboarding/lifecycle workflows in PostHog (and downstream tools like SendKit) need to segment users by how many social accounts they've connected and how many posts they've created. The existing SyncUser job only re-emitted these counts on signup and billing changes, so the values went stale the moment a user did anything meaningful. This wires up two new paths that refresh the account group automatically: - SocialAccountObserver (#[ObservedBy] on the model) fires SyncAccountUsage on created/deleted, covering all 14 OAuth callback paths in one hook. - SyncUsageOnPostCreated / SyncUsageOnPostDeleted listeners (auto-discovered) fire SyncAccountUsage on the corresponding events dispatched by CreatePost and DeletePost. SyncAccountUsage is the new dedicated job for group properties only (groupIdentify account + workspace). SyncUser was slimmed to just identify the user and delegate the group sync, removing the duplicated property mapping between the two jobs. All entry points (observer + both listeners) short-circuit when PostHog is disabled, so self-hosted instances without PostHog configured see zero queued jobs and zero overhead. posts_count cache is invalidated before each sync so the job reads fresh counts from the database instead of stale cached values.
66 lines
2 KiB
PHP
66 lines
2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Jobs\PostHog\SyncAccountUsage;
|
|
use App\Models\Account;
|
|
use App\Models\SocialAccount;
|
|
use App\Models\User;
|
|
use App\Models\Workspace;
|
|
use Illuminate\Support\Facades\Bus;
|
|
|
|
beforeEach(function () {
|
|
config(['services.posthog.enabled' => true, 'services.posthog.api_key' => 'phc_test_key']);
|
|
|
|
$this->account = Account::factory()->create();
|
|
$this->user = User::factory()->create(['account_id' => $this->account->id]);
|
|
$this->account->update(['owner_id' => $this->user->id]);
|
|
$this->workspace = Workspace::factory()->create([
|
|
'account_id' => $this->account->id,
|
|
'user_id' => $this->user->id,
|
|
]);
|
|
});
|
|
|
|
test('creating a social account dispatches SyncAccountUsage', function () {
|
|
Bus::fake();
|
|
|
|
SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]);
|
|
|
|
Bus::assertDispatched(SyncAccountUsage::class, function ($job) {
|
|
return $job->accountId === (string) $this->account->id
|
|
&& $job->workspaceId === (string) $this->workspace->id;
|
|
});
|
|
});
|
|
|
|
test('deleting a social account dispatches SyncAccountUsage', function () {
|
|
$socialAccount = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]);
|
|
|
|
Bus::fake();
|
|
|
|
$socialAccount->delete();
|
|
|
|
Bus::assertDispatched(SyncAccountUsage::class, function ($job) {
|
|
return $job->accountId === (string) $this->account->id
|
|
&& $job->workspaceId === (string) $this->workspace->id;
|
|
});
|
|
});
|
|
|
|
test('updating a social account does not dispatch SyncAccountUsage', function () {
|
|
$socialAccount = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]);
|
|
|
|
Bus::fake();
|
|
|
|
$socialAccount->update(['is_active' => false]);
|
|
|
|
Bus::assertNotDispatched(SyncAccountUsage::class);
|
|
});
|
|
|
|
test('does not dispatch when PostHog is disabled', function () {
|
|
config(['services.posthog.enabled' => false]);
|
|
|
|
Bus::fake();
|
|
|
|
SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]);
|
|
|
|
Bus::assertNotDispatched(SyncAccountUsage::class);
|
|
});
|