-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.js
118 lines (106 loc) · 4.18 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// app.js
const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const fs = require("fs");
// Initialize dotenv to load environment variables
dotenv.config();
// Initialize the Express app
const app = express();
app.use(express.json());
// Determine if the environment is a test environment
const isTestEnv = process.env.NODE_ENV === "test";
// Conditionally connect to MongoDB unless in a test environment
if (!isTestEnv) {
const mongoURI = process.env.MONGODB_URI || "mongodb://mongo:27017/opencap";
mongoose.connect(mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("MongoDB connected"))
.catch(err => console.error("MongoDB connection error:", err));
}
// Function to safely require routes
const safeRequire = (path) => {
try {
return fs.existsSync(path) ? require(path) : null;
} catch (err) {
console.warn(`Warning: Could not load route file: ${path}`);
return null;
}
};
// Import route modules
const routes = {
// Core routes that should always exist
financialReportRoutes: require("./routes/financialReportingRoutes"),
userRoutes: require("./routes/userRoutes"),
shareClassRoutes: require("./routes/shareClassRoutes"),
stakeholderRoutes: require("./routes/stakeholderRoutes"),
documentRoutes: require("./routes/documentRoutes"),
fundraisingRoundRoutes: require("./routes/fundraisingRoundRoutes"),
equityPlanRoutes: require("./routes/equityPlanRoutes"),
documentEmbeddingRoutes: require("./routes/documentEmbeddingRoutes"),
employeeRoutes: require("./routes/employeeRoutes"),
activityRoutes: require("./routes/activityRoutes"),
investmentRoutes: require("./routes/investmentTrackerRoutes"),
adminRoutes: require("./routes/adminRoutes"),
documentAccessRoutes: require("./routes/documentAccessRoutes"),
investorRoutes: require("./routes/investorRoutes"),
companyRoutes: require("./routes/companyRoutes"),
authRoutes: require("./routes/authRoutes"),
// Optional routes that might not exist in all environments
communicationRoutes: safeRequire("./routes/communicationRoutes"),
notificationRoutes: safeRequire("./routes/notificationRoutes"),
inviteManagementRoutes: safeRequire("./routes/inviteManagementRoutes"),
spvRoutes: safeRequire("./routes/spvRoutes"),
spvAssetRoutes: safeRequire("./routes/spvAssetRoutes"),
complianceCheckRoutes: safeRequire("./routes/complianceCheckRoutes"),
integrationModuleRoutes: safeRequire("./routes/integrationModuleRoutes"),
taxCalculatorRoutes: safeRequire("./routes/taxCalculatorRoutes")
};
// Route mapping with paths
const routeMappings = {
'/api/financial-reports': 'financialReportRoutes',
'/api/users': 'userRoutes',
'/api/shareClasses': 'shareClassRoutes',
'/api/stakeholders': 'stakeholderRoutes',
'/api/documents': 'documentRoutes',
'/api/fundraisingRounds': 'fundraisingRoundRoutes',
'/api/equityPlans': 'equityPlanRoutes',
'/api/documentEmbeddings': 'documentEmbeddingRoutes',
'/api/employees': 'employeeRoutes',
'/api/activities': 'activityRoutes',
'/api/investments': 'investmentRoutes',
'/api/admins': 'adminRoutes',
'/api/documentAccesses': 'documentAccessRoutes',
'/api/investors': 'investorRoutes',
'/api/companies': 'companyRoutes',
'/auth': 'authRoutes',
'/api/communications': 'communicationRoutes',
'/api/notifications': 'notificationRoutes',
'/api/invites': 'inviteManagementRoutes',
'/api/spv': 'spvRoutes',
'/api/spv-assets': 'spvAssetRoutes',
'/api/compliance-checks': 'complianceCheckRoutes',
'/api/integration-modules': 'integrationModuleRoutes',
'/api/taxCalculations': 'taxCalculatorRoutes'
};
// Mount routes only if they exist
Object.entries(routeMappings).forEach(([path, routeName]) => {
if (routes[routeName]) {
app.use(path, routes[routeName]);
}
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error("Error:", err.message);
res.status(err.statusCode || 500).json({
error: err.message || "Internal Server Error",
stack: process.env.NODE_ENV === 'development' ? err.stack : undefined
});
});
// 404 handler
app.use('*', (req, res) => {
res.status(404).json({ error: 'Route not found' });
});
module.exports = app;