trypost/app/Observers/SocialAccountObserver.php
Paulo Castellano 17a091975d fix: merge-readiness — close two billing/network bugs, harden tests
Bugs (both with regression tests):
- OnboardingController::store now guards already-subscribed accounts (mirrors
  index), preventing a second Stripe Checkout / double subscription if a
  subscribed user re-POSTs /onboarding.
- SocialAccountObserver: drop the `platform_user_id != …` clause from the
  creating-time one-per-network check. On create there is no "self" to exclude,
  so it only weakened the rule — the same account connected via two network
  variants (e.g. Instagram standalone + via Facebook, same id) could slip a
  second account into the network. Now any account in the network blocks.

Robustness:
- CreateWorkspace wraps create + member attach + switchWorkspace in a
  transaction (cache-forget / quantity-sync run after), so a partial failure
  can't leave an orphan workspace that inflates the Stripe seat count — covers
  both the signup and the add-workspace paths.

Test honesty & coverage:
- Scope the ten "connect multiple <platform> accounts" tests to self-hosted
  mode (config + name); they only passed because the test env defaults
  SELF_HOSTED=true, and in cloud the one-per-network rule blocks them.
- network_taken popup now has controller-level tests on all six OAuth
  controllers (added Threads, YouTube, LinkedInPage, and a new
  InstagramFacebook test file; LinkedInPage/InstagramFacebook also exercise
  variant collapse).
- Wiring tests that creating/deleting a workspace actually calls
  syncWorkspaceQuantity (guards per-seat billing against silent breakage).
- Strengthen TrialLengthTest to assert the configured length reaches
  trial_ends_at; add a same-id network-variant block test.
2026-06-22 09:26:31 -03:00

65 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Observers;
use App\Enums\SocialAccount\Platform;
use App\Exceptions\SocialAccount\NetworkAlreadyConnectedException;
use App\Jobs\PostHog\SyncAccountUsage;
use App\Models\SocialAccount;
use App\Services\PostHogService;
class SocialAccountObserver
{
/**
* Enforce one connected account per social network per workspace. Variants
* of the same network (LinkedIn profile/page, Instagram standalone/Facebook)
* collapse via Platform::network(). Reconnecting an existing account goes
* through updateOrCreate's update path and never reaches this hook. Bypassed
* in self-hosted mode, which has no per-workspace limits.
*/
public function creating(SocialAccount $socialAccount): void
{
if (config('trypost.self_hosted')) {
return;
}
$platform = $socialAccount->platform;
if (! $platform instanceof Platform) {
return;
}
$conflict = SocialAccount::query()
->where('workspace_id', $socialAccount->workspace_id)
->whereIn('platform', $platform->networkPlatformValues())
->exists();
if ($conflict) {
throw new NetworkAlreadyConnectedException($platform);
}
}
public function created(SocialAccount $socialAccount): void
{
$this->syncUsage($socialAccount);
}
public function deleted(SocialAccount $socialAccount): void
{
$this->syncUsage($socialAccount);
}
private function syncUsage(SocialAccount $socialAccount): void
{
if (! PostHogService::isEnabled()) {
return;
}
SyncAccountUsage::dispatch(
(string) $socialAccount->workspace->account_id,
(string) $socialAccount->workspace_id,
);
}
}