-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from Open-Cap-Stack/feature/456-financial-rep…
…ort-api Feature/456 Add Financial Report API with Controller, Model, Route, and Test Suite
- Loading branch information
Showing
5 changed files
with
154 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,38 @@ | ||
require('dotenv').config(); // Load the .env file | ||
const mongoose = require("mongoose"); | ||
const { Client } = require('pg'); | ||
require('dotenv').config(); // Ensure dotenv loads | ||
const mongoose = require('mongoose'); | ||
|
||
module.exports = async () => { | ||
// Set up MongoDB | ||
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/opencap_test'; // Fallback to hardcoded URI | ||
async function connectToMongoDB() { | ||
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/opencap_test'; | ||
if (!mongoUri) { | ||
throw new Error('MongoDB URI is not defined.'); | ||
} | ||
|
||
await mongoose.connect(mongoUri, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
}); | ||
|
||
// Drop the MongoDB database to clean up before tests | ||
await mongoose.connection.dropDatabase(); | ||
|
||
// Set up PostgreSQL for metadata management | ||
const pgClient = new Client({ | ||
user: process.env.PG_USER || 'lakehouse_user', | ||
host: process.env.PG_HOST || 'localhost', | ||
database: process.env.PG_DATABASE || 'lakehouse_metadata', | ||
password: process.env.PG_PASSWORD || 'password', | ||
port: process.env.PG_PORT || 5432, | ||
}); | ||
|
||
try { | ||
await pgClient.connect(); | ||
console.log('Connected to PostgreSQL database.'); | ||
|
||
// Clean up PostgreSQL data before tests (if necessary) | ||
await pgClient.query('TRUNCATE TABLE datasets, dataset_schema, ingestion_logs RESTART IDENTITY CASCADE;'); | ||
console.log('PostgreSQL tables truncated.'); | ||
|
||
} catch (error) { | ||
console.error('Error connecting to PostgreSQL:', error); | ||
throw new Error('PostgreSQL connection failed.'); | ||
} finally { | ||
await pgClient.end(); | ||
await mongoose.connect(mongoUri, { useNewUrlParser: true, useUnifiedTopology: true }); | ||
console.log('Connected to MongoDB for setup.'); | ||
} | ||
|
||
async function dropDatabaseWithRetry() { | ||
let attempts = 3; | ||
while (attempts > 0) { | ||
try { | ||
await mongoose.connection.dropDatabase(); | ||
console.log('MongoDB test database dropped successfully.'); | ||
return; | ||
} catch (error) { | ||
if (error.codeName === 'DatabaseDropPending') { | ||
console.log('Database drop pending, retrying...'); | ||
attempts -= 1; | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
} | ||
throw new Error('Failed to drop MongoDB test database.'); | ||
} | ||
|
||
// Set up MinIO client (mock or real) | ||
process.env.MINIO_ENDPOINT = process.env.MINIO_ENDPOINT || 'localhost'; | ||
process.env.MINIO_PORT = process.env.MINIO_PORT || '9000'; | ||
process.env.MINIO_ACCESS_KEY = process.env.MINIO_ACCESS_KEY || 'your-access-key'; | ||
process.env.MINIO_SECRET_KEY = process.env.MINIO_SECRET_KEY || 'your-secret-key'; | ||
console.log('MinIO environment variables set.'); | ||
|
||
// Set up Airflow (mock or real) | ||
process.env.AIRFLOW_BASE_URL = process.env.AIRFLOW_BASE_URL || 'http://localhost:8080'; | ||
process.env.AIRFLOW_USERNAME = process.env.AIRFLOW_USERNAME || 'admin'; | ||
process.env.AIRFLOW_PASSWORD = process.env.AIRFLOW_PASSWORD || 'admin_password'; | ||
console.log('Airflow environment variables set.'); | ||
|
||
// Close the MongoDB connection | ||
module.exports = async () => { | ||
await connectToMongoDB(); | ||
await dropDatabaseWithRetry(); | ||
await mongoose.connection.close(); | ||
console.log('MongoDB connection closed after setup.'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// models/financialReport.js | ||
|
||
const mongoose = require('mongoose'); | ||
const { Schema } = mongoose; | ||
const { v4: uuidv4 } = require('uuid'); | ||
|
||
const FinancialReportSchema = new Schema({ | ||
ReportID: { | ||
type: String, | ||
default: uuidv4, // Generate a UUID by default | ||
unique: true, | ||
required: true, | ||
}, | ||
Type: { | ||
type: String, | ||
enum: ['Annual', 'Quarterly'], // Enum values | ||
required: true, | ||
}, | ||
Data: { | ||
type: Schema.Types.Mixed, // Stores JSON data | ||
required: true, | ||
}, | ||
TotalRevenue: { | ||
type: Schema.Types.Decimal128, // Decimal for monetary values | ||
required: true, | ||
}, | ||
TotalExpenses: { | ||
type: Schema.Types.Decimal128, | ||
required: true, | ||
}, | ||
NetIncome: { | ||
type: Schema.Types.Decimal128, | ||
required: true, | ||
}, | ||
EquitySummary: { | ||
type: [String], // Array of UUIDs (shareClassId) | ||
default: [], // Optional field | ||
}, | ||
Timestamp: { | ||
type: Date, | ||
default: Date.now, | ||
required: true, | ||
}, | ||
}); | ||
|
||
module.exports = mongoose.model('FinancialReport', FinancialReportSchema); |