- Added TypeORM DataSource configuration - Added payment-terminals entities - Added PaymentTerminalsModule with bootstrap function - Registered routes: /api/payment-terminals, /api/mercadopago, /api/clip - Registered webhooks: /webhooks/mercadopago, /webhooks/clip - Updated API info with available endpoints Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
143 lines
4.2 KiB
TypeScript
143 lines
4.2 KiB
TypeScript
/**
|
|
* ERP Vidrio Templado - Backend
|
|
*
|
|
* Sistema de gestión para plantas de vidrio templado
|
|
*
|
|
* Módulos planificados:
|
|
* - VT-001: Fundamentos (Auth, Users, Tenants)
|
|
* - VT-002: Cotizaciones (Cotizador por m²)
|
|
* - VT-003: Producción (Órdenes de producción)
|
|
* - VT-004: Inventario (Stock vidrio/MP)
|
|
* - VT-005: Corte (Optimización nesting)
|
|
* - VT-006: Templado (Control de hornos)
|
|
* - VT-007: Calidad (QC y certificaciones)
|
|
* - VT-008: Despacho (Logística entregas)
|
|
*/
|
|
|
|
import 'reflect-metadata';
|
|
import express from 'express';
|
|
import cors from 'cors';
|
|
import helmet from 'helmet';
|
|
import dotenv from 'dotenv';
|
|
import { DataSource } from 'typeorm';
|
|
|
|
// Payment Terminals Module
|
|
import { PaymentTerminalsModule } from './modules/payment-terminals';
|
|
import { TenantTerminalConfig } from './modules/payment-terminals/entities/tenant-terminal-config.entity';
|
|
import { TerminalPayment } from './modules/payment-terminals/entities/terminal-payment.entity';
|
|
import { TerminalWebhookEvent } from './modules/payment-terminals/entities/terminal-webhook-event.entity';
|
|
|
|
dotenv.config();
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
// Database configuration
|
|
const AppDataSource = new DataSource({
|
|
type: 'postgres',
|
|
host: process.env.DB_HOST || 'localhost',
|
|
port: parseInt(process.env.DB_PORT || '5432', 10),
|
|
username: process.env.DB_USER || 'erp_user',
|
|
password: process.env.DB_PASSWORD || 'erp_dev_2026',
|
|
database: process.env.DB_NAME || 'erp_vidrio_templado',
|
|
schema: process.env.DB_SCHEMA || 'public',
|
|
entities: [
|
|
// Payment Terminals
|
|
TenantTerminalConfig,
|
|
TerminalPayment,
|
|
TerminalWebhookEvent,
|
|
],
|
|
synchronize: process.env.NODE_ENV === 'development',
|
|
logging: process.env.NODE_ENV === 'development',
|
|
});
|
|
|
|
// Middleware
|
|
app.use(helmet());
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
|
|
// Health check endpoint
|
|
app.get('/health', (req, res) => {
|
|
res.json({
|
|
status: 'ok',
|
|
service: 'erp-vidrio-templado-backend',
|
|
version: '0.1.0',
|
|
timestamp: new Date().toISOString(),
|
|
database: AppDataSource.isInitialized ? 'connected' : 'disconnected',
|
|
});
|
|
});
|
|
|
|
// API info endpoint
|
|
app.get('/api', (req, res) => {
|
|
res.json({
|
|
name: 'ERP Vidrio Templado API',
|
|
version: '0.1.0',
|
|
description: 'Sistema de gestión para plantas de vidrio templado',
|
|
modules: [
|
|
'VT-001: Fundamentos',
|
|
'VT-002: Cotizaciones',
|
|
'VT-003: Producción',
|
|
'VT-004: Inventario',
|
|
'VT-005: Corte',
|
|
'VT-006: Templado',
|
|
'VT-007: Calidad',
|
|
'VT-008: Despacho',
|
|
'Payment Terminals (MercadoPago, Clip)',
|
|
],
|
|
endpoints: {
|
|
health: '/health',
|
|
paymentTerminals: '/api/payment-terminals',
|
|
mercadopago: '/api/mercadopago',
|
|
clip: '/api/clip',
|
|
},
|
|
status: 'scaffold - en desarrollo',
|
|
});
|
|
});
|
|
|
|
// Bootstrap function
|
|
async function bootstrap() {
|
|
try {
|
|
// Initialize database
|
|
await AppDataSource.initialize();
|
|
console.log('📦 Database connection established');
|
|
|
|
// Initialize Payment Terminals Module
|
|
const paymentTerminals = new PaymentTerminalsModule({ dataSource: AppDataSource });
|
|
app.use('/api', paymentTerminals.router);
|
|
app.use('/webhooks', paymentTerminals.webhookRouter);
|
|
console.log('💳 Payment Terminals module initialized');
|
|
|
|
// 404 handler
|
|
app.use((req, res) => {
|
|
res.status(404).json({ error: 'Not Found' });
|
|
});
|
|
|
|
// Error handler
|
|
app.use((err: Error, _req: express.Request, res: express.Response, _next: express.NextFunction) => {
|
|
console.error(err.stack);
|
|
res.status(500).json({
|
|
error: 'Internal Server Error',
|
|
message: process.env.NODE_ENV === 'development' ? err.message : undefined,
|
|
});
|
|
});
|
|
|
|
// Start server
|
|
app.listen(PORT, () => {
|
|
console.log(`🔷 ERP Vidrio Templado backend running on port ${PORT}`);
|
|
console.log(`📊 Environment: ${process.env.NODE_ENV || 'development'}`);
|
|
console.log(`🏥 Health check: http://localhost:${PORT}/health`);
|
|
console.log(`📚 API Root: http://localhost:${PORT}/api`);
|
|
});
|
|
} catch (error) {
|
|
console.error('Failed to start server:', error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// Start application
|
|
if (require.main === module) {
|
|
bootstrap();
|
|
}
|
|
|
|
export { app, AppDataSource };
|