mirror of
https://git.meshkee.com/BaloutPastry/backend.git
synced 2026-08-11 22:31:00 +04:30
66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
/**
|
|
* Create the first superAdmin (no seed data).
|
|
*
|
|
* Usage:
|
|
* npx ts-node -r tsconfig-paths/register scripts/create-super-admin.ts \
|
|
* --phone 09120000000 --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', 'بلوط');
|
|
const title = arg('title', 'جناب آقای');
|
|
|
|
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: {
|
|
title,
|
|
firstName,
|
|
lastName,
|
|
cellNumber,
|
|
passwordHash,
|
|
role: UserRole.superAdmin,
|
|
},
|
|
});
|
|
|
|
console.log('Created superAdmin:', {
|
|
id: user.id,
|
|
cellNumber: user.cellNumber,
|
|
name: `${user.title} ${user.firstName} ${user.lastName}`,
|
|
});
|
|
}
|
|
|
|
main()
|
|
.catch((err) => {
|
|
console.error(err.message || err);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|