app-paqueteria-nest/src/database/seeds/auto-seed.ts
2026-09-18 20:22:24 -04:00

38 lines
1.2 KiB
TypeScript

import { INestApplication } from '@nestjs/common';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import * as bcrypt from 'bcrypt';
import { Empresa } from '../../modules/empresa/entities/empresa.entity.js';
import { Usuario } from '../../modules/usuario/entities/usuario.entity.js';
export async function runSeed(app: INestApplication): Promise<void> {
const empresaRepo = app.get<Repository<Empresa>>(getRepositoryToken(Empresa));
const usuarioRepo = app.get<Repository<Usuario>>(getRepositoryToken(Usuario));
const existeEmpresa = await empresaRepo.findOne({ where: {} });
if (existeEmpresa) return;
console.log('DB vacía — ejecutando seed...');
/*
await empresaRepo.save({
nombre: 'La Clave',
diasIncluidos: 7,
costoBase: 5,
costoDiaAdicional: 2,
diasParaAlmacen: 30,
});
*/
const passwordHash = await bcrypt.hash('admin123', 10);
await usuarioRepo.save({
nombre: 'Administrador',
usuario: 'admin@admin.com',
password: passwordHash,
pregunta: '¿Cómo se llama tu mamá?',
respuesta: 'Juanita',
rol: 'ADMIN',
});
console.log('Seed completado: Empresa "La Clave" + usuario admin creados.');
}