Files
ladill-queue/app/Services/Qms/DemoLoginReseeder.php
T
isaaccladandCursor 8062cce8d1
Deploy Ladill Queue / deploy (push) Successful in 1m19s
Create demo org shell before Queue login redirect.
Match Care: guarantee onboarded org on the SSO request path so launcher/login never flashes onboarding during async reseed.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-17 19:33:12 +00:00

119 lines
3.6 KiB
PHP

<?php
namespace App\Services\Qms;
use App\Models\User;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
/**
* Reseed Queue demo tenants after a fresh SSO login.
*
* Only owner demo accounts trigger a full reseed. DemoWorld staff logins
* (e.g. receptionist@ladill.com variants) share the owner tenant but must
* never reseed 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
{
public function maybeResetAfterLogin(User $user): void
{
if (! (bool) config('demo_accounts.enabled', false)
|| ! (bool) config('demo_accounts.reset_on_login', true)) {
return;
}
if (\App\Support\DemoWorld::isStaffDemoEmail($user->email)) {
return;
}
$plan = $this->planForEmail($user->email);
if ($plan === null) {
return;
}
$seedUser = $this->resolveSeedUser($user);
if (! $seedUser) {
return;
}
$lockKey = 'demo-reseed:queue:'.$seedUser->public_id;
if (! Cache::add($lockKey, 1, 120)) {
try {
app(DemoSeeder::class)->ensureOnboardedShell($seedUser, $plan);
} catch (\Throwable $e) {
Log::warning('demo_queue_login_shell_failed', [
'user_id' => $seedUser->id,
'plan' => $plan,
'error' => $e->getMessage(),
]);
}
return;
}
try {
app(DemoSeeder::class)->ensureOnboardedShell($seedUser, $plan);
} catch (\Throwable $e) {
Cache::forget($lockKey);
Log::warning('demo_queue_login_shell_failed', [
'user_id' => $seedUser->id,
'plan' => $plan,
'error' => $e->getMessage(),
]);
return;
}
$identity = (string) ($seedUser->email ?: $seedUser->public_id);
$userId = $seedUser->id;
dispatch(function () use ($identity, $plan, $lockKey, $userId) {
try {
if (User::query()->find($userId)) {
app(DemoSeeder::class)->seed($identity, $plan, true);
}
} catch (\Throwable $e) {
Log::warning('demo_queue_login_reseed_failed', [
'user_id' => $userId,
'plan' => $plan,
'error' => $e->getMessage(),
]);
} finally {
Cache::forget($lockKey);
}
})->afterResponse();
}
private function planForEmail(?string $email): ?string
{
$email = strtolower(trim((string) $email));
if ($email === '') {
return null;
}
$accounts = (array) config('demo_accounts.accounts', []);
if (isset($accounts[$email])) {
return $accounts[$email];
}
$members = (array) config('demo_accounts.member_emails', []);
$ownerEmail = $members[$email] ?? null;
return $ownerEmail ? ($accounts[$ownerEmail] ?? null) : null;
}
private function resolveSeedUser(User $user): ?User
{
$email = strtolower((string) $user->email);
$ownerEmail = ((array) config('demo_accounts.member_emails', []))[$email] ?? null;
if (! $ownerEmail) {
return $user;
}
return User::query()->where('email', $ownerEmail)->first() ?? $user;
}
}