Skip to content

Commit 22994b2

Browse files
committed
fix-demo-signup
1 parent d9cf3ae commit 22994b2

1 file changed

Lines changed: 84 additions & 45 deletions

File tree

server/scripts/seed.ts

Lines changed: 84 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -365,18 +365,20 @@ async function seed() {
365365
// ─────────────────────────────────────────────
366366
// Clean up legacy applirank.com seed data
367367
// ─────────────────────────────────────────────
368-
const [legacyOrg] = await db
369-
.select({ id: schema.organization.id })
370-
.from(schema.organization)
371-
.where(eq(schema.organization.slug, LEGACY_ORG_SLUG))
372-
.limit(1)
373-
374-
const [legacyUser] = await db
375-
.select({ id: schema.user.id })
376-
.from(schema.user)
377-
.where(eq(schema.user.email, LEGACY_DEMO_EMAIL))
378-
.limit(1)
379-
368+
const [legacyOrgResult, legacyUserResult] = await Promise.all([
369+
db
370+
.select({ id: schema.organization.id })
371+
.from(schema.organization)
372+
.where(eq(schema.organization.slug, LEGACY_ORG_SLUG))
373+
.limit(1),
374+
db
375+
.select({ id: schema.user.id })
376+
.from(schema.user)
377+
.where(eq(schema.user.email, LEGACY_DEMO_EMAIL))
378+
.limit(1),
379+
])
380+
const [legacyOrg] = legacyOrgResult
381+
const [legacyUser] = legacyUserResult
380382
if (legacyOrg || legacyUser) {
381383
console.log('🧹 Removing legacy applirank.com demo data...')
382384

@@ -394,47 +396,84 @@ async function seed() {
394396
}
395397
}
396398

397-
// Check if demo org already exists
398-
const existingOrg = await db
399-
.select()
399+
// ─────────────────────────────────────────────
400+
// Upsert demo user (handles email rename scenarios)
401+
// ─────────────────────────────────────────────
402+
const [existingDemoUser] = await db
403+
.select({ id: schema.user.id })
404+
.from(schema.user)
405+
.where(eq(schema.user.email, DEMO_EMAIL))
406+
.limit(1)
407+
408+
const hashedPassword = await hashPassword(DEMO_PASSWORD)
409+
let userId: string
410+
411+
if (existingDemoUser) {
412+
userId = existingDemoUser.id
413+
// Ensure password is up to date in case it changed
414+
await db
415+
.update(schema.account)
416+
.set({ password: hashedPassword, updatedAt: new Date() })
417+
.where(eq(schema.account.userId, userId))
418+
console.log(`✅ Demo user already exists: ${DEMO_EMAIL}`)
419+
}
420+
else {
421+
userId = id()
422+
await db.insert(schema.user).values({
423+
id: userId,
424+
name: 'Demo Recruiter',
425+
email: DEMO_EMAIL,
426+
emailVerified: true,
427+
createdAt: daysAgo(30),
428+
updatedAt: daysAgo(30),
429+
})
430+
await db.insert(schema.account).values({
431+
id: id(),
432+
userId,
433+
accountId: userId,
434+
providerId: 'credential',
435+
password: hashedPassword,
436+
createdAt: daysAgo(30),
437+
updatedAt: daysAgo(30),
438+
})
439+
console.log(`✅ Created demo user: ${DEMO_EMAIL}`)
440+
}
441+
442+
// ─────────────────────────────────────────────
443+
// Upsert demo org (handles partial migration)
444+
// ─────────────────────────────────────────────
445+
const [existingOrg] = await db
446+
.select({ id: schema.organization.id })
400447
.from(schema.organization)
401448
.where(eq(schema.organization.slug, DEMO_ORG_SLUG))
402449
.limit(1)
403450

404-
if (existingOrg.length > 0) {
405-
console.log('⚠️ Demo organization already exists. Skipping seed.')
406-
console.log(' To re-seed, delete the organization first or reset the database.')
451+
if (existingOrg) {
452+
// Org exists — ensure the demo user is a member, then stop
453+
const [existingMember] = await db
454+
.select({ id: schema.member.id })
455+
.from(schema.member)
456+
.where(eq(schema.member.userId, userId))
457+
.limit(1)
458+
459+
if (!existingMember) {
460+
await db.insert(schema.member).values({
461+
id: id(),
462+
userId,
463+
organizationId: existingOrg.id,
464+
role: 'owner',
465+
createdAt: daysAgo(30),
466+
})
467+
console.log('✅ Linked demo user to existing org as owner')
468+
}
469+
470+
console.log('⚠️ Demo organization already exists. Skipping full seed.')
471+
console.log(' To re-seed all data, delete the organization first or reset the database.')
407472
await client.end()
408473
return
409474
}
410475

411-
// 1. Create demo user
412-
const userId = id()
413-
const hashedPassword = await hashPassword(DEMO_PASSWORD)
414-
415-
await db.insert(schema.user).values({
416-
id: userId,
417-
name: 'Demo Recruiter',
418-
email: DEMO_EMAIL,
419-
emailVerified: true,
420-
createdAt: daysAgo(30),
421-
updatedAt: daysAgo(30),
422-
})
423-
424-
// Create account (email/password provider)
425-
await db.insert(schema.account).values({
426-
id: id(),
427-
userId,
428-
accountId: userId,
429-
providerId: 'credential',
430-
password: hashedPassword,
431-
createdAt: daysAgo(30),
432-
updatedAt: daysAgo(30),
433-
})
434-
435-
console.log(`✅ Created demo user: ${DEMO_EMAIL}`)
436-
437-
// 2. Create organization
476+
// 2. Create organization (fresh seed path)
438477
const orgId = id()
439478

440479
await db.insert(schema.organization).values({

0 commit comments

Comments
 (0)