P0 fixes: - Fix OrderDetail product change overwriting product_id due to React state batching (single setItems call now updates both fields) - Validate all :id route params via parseId helper; return 400 for invalid IDs instead of passing raw strings to SQLite - Product/customer delete now checks for references first, returns 409 Conflict instead of letting FK constraint produce 500 P1 fixes: - Disallow quantity_on_hand in product PUT so all stock changes go through PATCH /stock (preserves audit trail) - Add global Express error handler and unhandledRejection listener P2 fixes: - Validate report date params (YYYY-MM-DD format) and stock-history limit (positive integer, capped at 1000) - Add jsonSafe() helper to api.js for safe 204 handling - OrderNew setSubmitting now runs in finally block - Login shows specific message for 429 rate limit, generic message for other auth failures P3 fixes: - Replace brittle try/catch ALTER TABLE with schema_version migration table and versioned migrations - Fix OrderDetail useEffect missing dependency (useCallback + [load]) Also: expanded README with full production deployment instructions (PM2, nginx, backups) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
56 lines
1.8 KiB
JavaScript
56 lines
1.8 KiB
JavaScript
require('dotenv').config({ path: require('path').join(__dirname, '..', '.env') });
|
|
const express = require('express');
|
|
const cookieParser = require('cookie-parser');
|
|
const path = require('path');
|
|
|
|
const authRouter = require('./routes/auth');
|
|
const { authMiddleware } = require('./middleware/auth');
|
|
const productsRouter = require('./routes/products');
|
|
const customersRouter = require('./routes/customers');
|
|
const ordersRouter = require('./routes/orders');
|
|
const dashboardRouter = require('./routes/dashboard');
|
|
const reportsRouter = require('./routes/reports');
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3002;
|
|
|
|
app.set('trust proxy', 1);
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
const cors = require('cors');
|
|
app.use(cors({ origin: 'http://localhost:5173', credentials: true }));
|
|
}
|
|
|
|
app.use(express.json());
|
|
app.use(cookieParser());
|
|
|
|
app.use('/api/auth', authRouter);
|
|
app.use('/api', authMiddleware);
|
|
app.use('/api/products', productsRouter);
|
|
app.use('/api/customers', customersRouter);
|
|
app.use('/api/orders', ordersRouter);
|
|
app.use('/api/dashboard', dashboardRouter);
|
|
app.use('/api/reports', reportsRouter);
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
app.use(express.static(path.join(__dirname, '..', 'client', 'dist')));
|
|
app.get('*', (req, res) => {
|
|
res.sendFile(path.join(__dirname, '..', 'client', 'dist', 'index.html'));
|
|
});
|
|
}
|
|
|
|
// Global error handler
|
|
app.use((err, req, res, next) => {
|
|
console.error(err.stack || err.message || err);
|
|
if (res.headersSent) return next(err);
|
|
res.status(500).json({ error: process.env.NODE_ENV === 'production' ? 'Internal server error' : err.message });
|
|
});
|
|
|
|
process.on('unhandledRejection', (reason) => {
|
|
console.error('Unhandled rejection:', reason);
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`Server running on http://localhost:${PORT}`);
|
|
});
|