64 lines
1.6 KiB
TypeScript
64 lines
1.6 KiB
TypeScript
/**
|
|
* Create the first superAdmin.
|
|
*
|
|
* Usage:
|
|
* npx ts-node -r tsconfig-paths/register scripts/create-super-admin.ts \
|
|
* --phone 09127004945 --password secret123 --first علیرضا --last حسنی
|
|
*/
|
|
import { PrismaClient, UserRole } from '@prisma/client'
|
|
import * as bcrypt from 'bcrypt'
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
function arg(name: string, fallback?: string): string {
|
|
const idx = process.argv.indexOf(`--${name}`)
|
|
if (idx >= 0 && process.argv[idx + 1]) return process.argv[idx + 1]
|
|
if (fallback !== undefined) return fallback
|
|
throw new Error(`Missing --${name}`)
|
|
}
|
|
|
|
async function main() {
|
|
const cellNumber = arg('phone')
|
|
const password = arg('password')
|
|
const firstName = arg('first', 'علیرضا')
|
|
const lastName = arg('last', 'حسنی')
|
|
|
|
if (!/^09\d{9}$/.test(cellNumber)) {
|
|
throw new Error('phone must match 09xxxxxxxxx')
|
|
}
|
|
if (password.length < 4) {
|
|
throw new Error('password min length 4')
|
|
}
|
|
|
|
const existing = await prisma.user.findUnique({ where: { cellNumber } })
|
|
if (existing) {
|
|
throw new Error(`User already exists: ${cellNumber}`)
|
|
}
|
|
|
|
const passwordHash = await bcrypt.hash(password, 10)
|
|
const user = await prisma.user.create({
|
|
data: {
|
|
firstName,
|
|
lastName,
|
|
cellNumber,
|
|
passwordHash,
|
|
role: UserRole.superAdmin,
|
|
},
|
|
})
|
|
|
|
console.log('Created superAdmin:', {
|
|
id: user.id,
|
|
cellNumber: user.cellNumber,
|
|
name: `${user.firstName} ${user.lastName}`,
|
|
})
|
|
}
|
|
|
|
main()
|
|
.catch((err) => {
|
|
console.error(err.message || err)
|
|
process.exit(1)
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect()
|
|
})
|