Files
ladill-frontdesk/app/Services/Frontdesk/DemoLoginReseeder.php
T
isaaccladandCursor a28e8619a1
Deploy Ladill Frontdesk / deploy (push) Successful in 1m10s
Wire Frontdesk demo seeder to shared DemoWorld
Org name, branches, and visitors now come from DemoWorld so continuity
matches Care/Queue across the suite, plus Members for DemoWorld staff
with a frontdesk role. Also resolve organization by owner_ref when
metadata.frontdesk.organization_id is missing, and skip reseed-on-login
for DemoWorld staff emails.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-17 13:35:52 +00:00

95 lines
2.7 KiB
PHP

<?php
namespace App\Services\Frontdesk;
use App\Models\User;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
/**
* Reseed Frontdesk 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.
*/
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:frontdesk:'.$seedUser->public_id;
if (! Cache::add($lockKey, 1, 120)) {
return;
}
$identity = (string) ($seedUser->email ?: $seedUser->public_id);
dispatch(function () use ($identity, $plan, $lockKey) {
try {
Artisan::call('demo:seed', [
'identity' => $identity,
'--plan' => $plan,
'--reset' => true,
]);
} catch (\Throwable $e) {
Log::warning('demo_frontdesk_login_reseed_failed', [
'identity' => $identity,
'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;
}
}