72 lines
1.7 KiB
TypeScript
72 lines
1.7 KiB
TypeScript
import { PrismaClient } from '@prisma/client'
|
|
import { PrismaPg } from '@prisma/adapter-pg'
|
|
import { Pool } from 'pg'
|
|
import { config } from 'dotenv'
|
|
|
|
config() // Load .env file
|
|
|
|
const pool = new Pool({ connectionString: process.env.DATABASE_URL })
|
|
const adapter = new PrismaPg(pool)
|
|
const prisma = new PrismaClient({ adapter })
|
|
|
|
async function createAdminUser(email: string, displayName: string) {
|
|
console.log(`Creating admin user: ${email}`)
|
|
|
|
// Create or update the user
|
|
const user = await prisma.user.upsert({
|
|
where: { email },
|
|
update: {
|
|
displayName,
|
|
isActive: true,
|
|
},
|
|
create: {
|
|
email,
|
|
displayName,
|
|
isActive: true,
|
|
},
|
|
})
|
|
console.log(`✅ User created/updated: ${user.id}`)
|
|
|
|
// Find the Admin role
|
|
const adminRole = await prisma.role.findUnique({
|
|
where: { name: 'Admin' },
|
|
})
|
|
|
|
if (!adminRole) {
|
|
console.error('❌ Admin role not found. Run seed first: npx prisma db seed')
|
|
process.exit(1)
|
|
}
|
|
|
|
// Assign Admin role to user
|
|
await prisma.userRole.upsert({
|
|
where: {
|
|
userId_roleId: {
|
|
userId: user.id,
|
|
roleId: adminRole.id,
|
|
},
|
|
},
|
|
update: {},
|
|
create: {
|
|
userId: user.id,
|
|
roleId: adminRole.id,
|
|
},
|
|
})
|
|
console.log(`✅ Admin role assigned to ${email}`)
|
|
|
|
console.log('\n🎉 Admin user setup complete!')
|
|
console.log(` Email: ${email}`)
|
|
console.log(` User ID: ${user.id}`)
|
|
}
|
|
|
|
const email = process.argv[2] || 'lorentz@wulfconsulting.com'
|
|
const displayName = process.argv[3] || 'Lorentz'
|
|
|
|
createAdminUser(email, displayName)
|
|
.catch((e) => {
|
|
console.error('❌ Failed:', e)
|
|
process.exit(1)
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect()
|
|
await pool.end()
|
|
})
|