Async reseed alone still left the first Care request without an onboarded org; ensure the shell synchronously, then refresh demo data afterResponse(). Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -11,6 +11,10 @@ use Illuminate\Support\Facades\Log;
|
||||
*
|
||||
* Only owner demo accounts (and legacy enterprise org members) trigger a full
|
||||
* reseed. Staff training logins never wipe mid-demo.
|
||||
*
|
||||
* The onboarded org shell is created synchronously before the login redirect so
|
||||
* EnsureOrganizationSetup never flashes the blank onboarding form. Heavy demo
|
||||
* data still refreshes afterResponse().
|
||||
*/
|
||||
class DemoLoginReseeder
|
||||
{
|
||||
@@ -38,6 +42,30 @@ class DemoLoginReseeder
|
||||
|
||||
$lockKey = 'demo-reseed:care:'.$seedUser->public_id;
|
||||
if (! Cache::add($lockKey, 1, 120)) {
|
||||
// Another login is already reseeding — still guarantee the shell exists.
|
||||
try {
|
||||
app(DemoTenantSeeder::class)->ensureOnboardedShell($seedUser, $plan);
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('demo_care_login_shell_failed', [
|
||||
'user_id' => $seedUser->id,
|
||||
'plan' => $plan,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
app(DemoTenantSeeder::class)->ensureOnboardedShell($seedUser, $plan);
|
||||
} catch (\Throwable $e) {
|
||||
Cache::forget($lockKey);
|
||||
Log::warning('demo_care_login_shell_failed', [
|
||||
'user_id' => $seedUser->id,
|
||||
'plan' => $plan,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -91,8 +91,13 @@ class DemoTenantSeeder
|
||||
$ownerRef = $user->ownerRef();
|
||||
$volumes = $this->volumes($plan);
|
||||
|
||||
// Always materialize the onboarded org shell before any wipe so concurrent
|
||||
// SSO redirects never land on the blank onboarding form.
|
||||
$this->ensureOnboardedShell($user, $plan);
|
||||
|
||||
if ($reset) {
|
||||
DB::transaction(fn () => $this->resetTenant($ownerRef));
|
||||
$this->ensureOnboardedShell($user, $plan);
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($user, $ownerRef, $plan, $volumes) {
|
||||
@@ -182,6 +187,24 @@ class DemoTenantSeeder
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the demo tenant has an onboarded organization + owner member.
|
||||
* Safe to call on the SSO request path before redirecting.
|
||||
*/
|
||||
public function ensureOnboardedShell(User $user, string $plan): Organization
|
||||
{
|
||||
$plan = $this->normalizePlan($plan);
|
||||
$ownerRef = $user->ownerRef();
|
||||
$volumes = $this->volumes($plan);
|
||||
|
||||
return DB::transaction(function () use ($user, $ownerRef, $plan, $volumes) {
|
||||
$organization = $this->upsertOrganization($user, $ownerRef, $plan, $volumes['branches']);
|
||||
$this->upsertOwnerMember($organization, $ownerRef);
|
||||
|
||||
return $organization;
|
||||
});
|
||||
}
|
||||
|
||||
public function resetTenant(string $ownerRef): void
|
||||
{
|
||||
$orgIds = DB::table('care_organizations')->where('owner_ref', $ownerRef)->pluck('id');
|
||||
|
||||
@@ -17,7 +17,10 @@ class OrganizationResolver
|
||||
|
||||
$member = Member::where('user_ref', $ref)->first();
|
||||
if ($member) {
|
||||
return Organization::find($member->organization_id);
|
||||
$organization = Organization::query()->find($member->organization_id);
|
||||
if ($organization) {
|
||||
return $organization;
|
||||
}
|
||||
}
|
||||
|
||||
return Organization::owned($ref)->first();
|
||||
|
||||
@@ -34,6 +34,10 @@ class DemoLoginReseederTest extends TestCase
|
||||
]);
|
||||
|
||||
$seeder = $this->mock(DemoTenantSeeder::class);
|
||||
$seeder->shouldReceive('ensureOnboardedShell')
|
||||
->once()
|
||||
->withArgs(fn (User $u, string $plan) => $u->is($user) && $plan === 'free')
|
||||
->andReturn(new \App\Models\Organization);
|
||||
$seeder->shouldReceive('seed')
|
||||
->once()
|
||||
->withArgs(fn (User $u, string $plan, bool $reset) => $u->is($user) && $plan === 'free' && $reset === true)
|
||||
@@ -53,6 +57,7 @@ class DemoLoginReseederTest extends TestCase
|
||||
]);
|
||||
|
||||
$seeder = $this->mock(DemoTenantSeeder::class);
|
||||
$seeder->shouldReceive('ensureOnboardedShell')->never();
|
||||
$seeder->shouldReceive('seed')->never();
|
||||
|
||||
app(DemoLoginReseeder::class)->maybeResetAfterLogin($user);
|
||||
@@ -70,6 +75,7 @@ class DemoLoginReseederTest extends TestCase
|
||||
]);
|
||||
|
||||
$seeder = $this->mock(DemoTenantSeeder::class);
|
||||
$seeder->shouldReceive('ensureOnboardedShell')->never();
|
||||
$seeder->shouldReceive('seed')->never();
|
||||
|
||||
app(DemoLoginReseeder::class)->maybeResetAfterLogin($user);
|
||||
@@ -85,9 +91,23 @@ class DemoLoginReseederTest extends TestCase
|
||||
]);
|
||||
|
||||
$seeder = $this->mock(DemoTenantSeeder::class);
|
||||
$seeder->shouldReceive('ensureOnboardedShell')->never();
|
||||
$seeder->shouldReceive('seed')->never();
|
||||
|
||||
app(DemoLoginReseeder::class)->maybeResetAfterLogin($user);
|
||||
$this->app->terminate();
|
||||
}
|
||||
|
||||
public function test_login_shell_is_onboarded_before_async_reseed(): void
|
||||
{
|
||||
$user = User::create([
|
||||
'public_id' => 'demo-shell-pid',
|
||||
'name' => 'Ladill Demo (Pro)',
|
||||
'email' => 'demo-pro@ladill.com',
|
||||
]);
|
||||
|
||||
app(DemoLoginReseeder::class)->maybeResetAfterLogin($user);
|
||||
|
||||
$this->assertTrue(app(\App\Services\Care\OrganizationResolver::class)->isOnboarded($user));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user