Files
cloud-host/backend/src/seed.ts
T
keyhan d87b50c6a4 fix(build): use kubectl cp for reliable source upload and fix Python/Django CMD
- Replace k8s.Exec WebSocket with kubectl cp for binary file transfer to PVC
- Fix Python Dockerfile CMD: switch from JSON exec form to shell form to avoid
  invalid JSON escape sequences (\|) causing shell parse errors
- Fix Django Dockerfile CMD with same shell form approach
- Add retry logic for ECONNRESET during build job polling
- Update seed.ts default admin email to match actual database

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-14 00:10:04 +03:30

57 lines
1.6 KiB
TypeScript

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=test@example.com
* ADMIN_PASSWORD=Admin123!
*/
async function bootstrap() {
const app = await NestFactory.createApplicationContext(AppModule);
const usersService = app.get(UsersService);
const email = process.env.ADMIN_EMAIL || 'test@example.com';
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);
});