fix: run prisma db push and seed db on startup
Build and Deploy (Gitea) / build-and-deploy (push) Successful in 1m15s
Details
Build and Deploy (Gitea) / build-and-deploy (push) Successful in 1m15s
Details
This commit is contained in:
parent
4e28601237
commit
159ae1e8db
66
server.ts
66
server.ts
|
|
@ -4,6 +4,7 @@ import jwt from 'jsonwebtoken';
|
|||
import bcrypt from 'bcryptjs';
|
||||
import multer from 'multer';
|
||||
import axios from 'axios';
|
||||
import { execSync } from 'child_process';
|
||||
import { S3Client, PutObjectCommand, CreateBucketCommand, HeadBucketCommand, PutBucketPolicyCommand } from '@aws-sdk/client-s3';
|
||||
import { createServer as createViteServer } from 'vite';
|
||||
import { prisma } from './src/db/prisma';
|
||||
|
|
@ -1824,6 +1825,71 @@ app.put('/api/admin/courses/:id/price', authenticateToken, requireAdmin, async (
|
|||
|
||||
// --- VITE MIDDLEWARE / SPA FALLBACK ---
|
||||
async function startServer() {
|
||||
try {
|
||||
console.log('🔄 Sincronizando banco de dados com Prisma...');
|
||||
execSync('npx prisma db push --accept-data-loss', { stdio: 'inherit' });
|
||||
console.log('✅ Banco de dados sincronizado!');
|
||||
|
||||
// Seed test users if DB is empty
|
||||
const adminCount = await prisma.user.count({ where: { email: 'sidney.gomes1989@gmail.com' } });
|
||||
if (adminCount === 0) {
|
||||
console.log('🌱 Semeando usuários de teste...');
|
||||
const defaultPassword = bcrypt.hashSync('admin123', 10);
|
||||
const studentPassword = bcrypt.hashSync('aluno123', 10);
|
||||
|
||||
await prisma.user.createMany({
|
||||
data: [
|
||||
{
|
||||
email: 'sidney.gomes1989@gmail.com',
|
||||
password: defaultPassword,
|
||||
name: 'Administrador Sidney',
|
||||
role: 'admin',
|
||||
subscriptionStatus: 'ACTIVE',
|
||||
},
|
||||
{
|
||||
email: 'aluno@microtecflix.com',
|
||||
password: studentPassword,
|
||||
name: 'Aluno Teste',
|
||||
role: 'student',
|
||||
subscriptionStatus: 'ACTIVE',
|
||||
},
|
||||
{
|
||||
email: 'joao@microtecflix.com',
|
||||
password: studentPassword,
|
||||
name: 'João Silva (Excel)',
|
||||
role: 'student',
|
||||
subscriptionStatus: 'ACTIVE',
|
||||
},
|
||||
{
|
||||
email: 'maria@microtecflix.com',
|
||||
password: studentPassword,
|
||||
name: 'Maria Ativa',
|
||||
role: 'student',
|
||||
subscriptionStatus: 'ACTIVE',
|
||||
},
|
||||
{
|
||||
email: 'pedro@microtecflix.com',
|
||||
password: studentPassword,
|
||||
name: 'Pedro Atrasado',
|
||||
role: 'student',
|
||||
subscriptionStatus: 'OVERDUE',
|
||||
},
|
||||
{
|
||||
email: 'ana@microtecflix.com',
|
||||
password: studentPassword,
|
||||
name: 'Ana Cancelada',
|
||||
role: 'student',
|
||||
subscriptionStatus: 'CANCELED',
|
||||
}
|
||||
],
|
||||
skipDuplicates: true
|
||||
});
|
||||
console.log('✅ Usuários de teste semeados com sucesso!');
|
||||
}
|
||||
} catch (dbError) {
|
||||
console.error('❌ Falha ao inicializar banco de dados:', dbError);
|
||||
}
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
const vite = await createViteServer({
|
||||
server: { middlewareMode: true },
|
||||
|
|
|
|||
Loading…
Reference in New Issue