init
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { AppModule } from './app.module';
|
||||
import { UsersService } from './users/users.service';
|
||||
import { UserRole } from './common/enums';
|
||||
|
||||
/**
|
||||
* Seed script — creates the initial super admin user.
|
||||
*
|
||||
* Usage:
|
||||
* npx ts-node -r tsconfig-paths/register src/seed.ts
|
||||
*
|
||||
* Or via npm script:
|
||||
* npm run seed
|
||||
*
|
||||
* Environment variables (or defaults):
|
||||
* ADMIN_EMAIL=admin@cloudhost.local
|
||||
* ADMIN_PASSWORD=Admin123!
|
||||
*/
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.createApplicationContext(AppModule);
|
||||
const usersService = app.get(UsersService);
|
||||
|
||||
const email = process.env.ADMIN_EMAIL || 'admin@cloudhost.local';
|
||||
const password = process.env.ADMIN_PASSWORD || 'Admin123!';
|
||||
|
||||
const existing = await usersService.findByEmail(email);
|
||||
if (existing) {
|
||||
console.log(`⚠️ Admin user already exists: ${email} (role: ${existing.role})`);
|
||||
if (existing.role !== UserRole.ADMIN) {
|
||||
await usersService.update(existing.id, { role: UserRole.ADMIN });
|
||||
console.log(`✅ Promoted ${email} to admin`);
|
||||
}
|
||||
} else {
|
||||
const hashedPassword = await bcrypt.hash(password, 12);
|
||||
await usersService.create({
|
||||
email,
|
||||
password: hashedPassword,
|
||||
firstName: 'Super',
|
||||
lastName: 'Admin',
|
||||
role: UserRole.ADMIN,
|
||||
});
|
||||
console.log(`✅ Admin user created: ${email}`);
|
||||
}
|
||||
|
||||
console.log(`\n📋 Admin credentials:`);
|
||||
console.log(` Email: ${email}`);
|
||||
console.log(` Password: ${password}`);
|
||||
|
||||
await app.close();
|
||||
}
|
||||
|
||||
bootstrap().catch((err) => {
|
||||
console.error('❌ Seed failed:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user