Open-Ledger-MCP-Server

Official
import { Request, Response } from 'express'; import { logger } from '../utils/logger.js'; // OpenLedger Controller export const openLedgerController = { // Company endpoints createCompany: (req: Request, res: Response) => { try { logger.info('Creating company'); // In a real implementation, this would interact with a database const company = { id: `comp-${Date.now()}`, ...req.body, date_created: new Date().toISOString(), status: 'ACTIVE' }; res.status(201).json(company); } catch (error) { logger.error(`Error creating company: ${error}`); res.status(500).json({ error: 'Failed to create company' }); } }, getCompany: (req: Request, res: Response) => { try { const { id } = req.params; logger.info(`Getting company with ID: ${id}`); // Mock company data - in a real app, fetch from database const company = { id, legal_name: 'Sample Company', tin: '12-3456789', us_state: 'CA', entity_type: 'LLC', status: 'ACTIVE', date_created: '2023-01-01T00:00:00Z' }; res.status(200).json(company); } catch (error) { logger.error(`Error getting company: ${error}`); res.status(500).json({ error: 'Failed to get company' }); } }, updateCompany: (req: Request, res: Response) => { try { const { id } = req.params; logger.info(`Updating company with ID: ${id}`); // Mock company data - in a real app, update in database const company = { id, ...req.body, updated_at: new Date().toISOString() }; res.status(200).json(company); } catch (error) { logger.error(`Error updating company: ${error}`); res.status(500).json({ error: 'Failed to update company' }); } }, deleteCompany: (req: Request, res: Response) => { try { const { id } = req.params; logger.info(`Deleting company with ID: ${id}`); // In a real app, delete from database res.status(200).json({ message: 'Company deleted successfully' }); } catch (error) { logger.error(`Error deleting company: ${error}`); res.status(500).json({ error: 'Failed to delete company' }); } }, // Transaction endpoints getTransactions: (req: Request, res: Response) => { try { const { id } = req.params; logger.info(`Getting transactions for company ID: ${id}`); // Mock transaction data - in a real app, fetch from database const transactions = [ { id: 'tx1', date: '2023-01-15T10:30:00Z', amount: 1000.00, currency: 'USD', description: 'Client payment', status: 'CLEARED', company_id: id }, { id: 'tx2', date: '2023-01-20T14:45:00Z', amount: -250.00, currency: 'USD', description: 'Office supplies', status: 'CLEARED', company_id: id } ]; res.status(200).json({ transactions, totalCount: transactions.length }); } catch (error) { logger.error(`Error getting transactions: ${error}`); res.status(500).json({ error: 'Failed to get transactions' }); } }, createTransaction: (req: Request, res: Response) => { try { const { id } = req.params; logger.info(`Creating transaction for company ID: ${id}`); const transaction = { id: `tx-${Date.now()}`, ...req.body, company_id: id, created_at: new Date().toISOString() }; res.status(201).json(transaction); } catch (error) { logger.error(`Error creating transaction: ${error}`); res.status(500).json({ error: 'Failed to create transaction' }); } }, updateTransaction: (req: Request, res: Response) => { try { const { id, transactionId } = req.params; logger.info(`Updating transaction ${transactionId} for company ID: ${id}`); const transaction = { id: transactionId, ...req.body, company_id: id, updated_at: new Date().toISOString() }; res.status(200).json(transaction); } catch (error) { logger.error(`Error updating transaction: ${error}`); res.status(500).json({ error: 'Failed to update transaction' }); } }, exportTransactions: (req: Request, res: Response) => { try { const { id } = req.params; const { format = 'csv', startDate, endDate } = req.query; logger.info(`Exporting transactions for company ID: ${id} in ${format} format`); // In a real app, generate the export file const exportInfo = { url: `https://example.com/exports/${id}_${Date.now()}.${format}`, filename: `transactions_${id}.${format}`, count: 25 }; res.status(200).json(exportInfo); } catch (error) { logger.error(`Error exporting transactions: ${error}`); res.status(500).json({ error: 'Failed to export transactions' }); } }, getTransactionsByMonth: (req: Request, res: Response) => { try { const { id, month } = req.params; logger.info(`Getting transactions for company ID: ${id} for month: ${month}`); // Mock transaction data - in a real app, fetch from database const transactions = [ { id: 'tx1', date: `${month}-15T10:30:00Z`, amount: 1000.00, currency: 'USD', description: 'Client payment', status: 'CLEARED', company_id: id } ]; res.status(200).json({ transactions, totalCount: transactions.length, month }); } catch (error) { logger.error(`Error getting transactions by month: ${error}`); res.status(500).json({ error: 'Failed to get transactions' }); } }, promptTransaction: (req: Request, res: Response) => { try { const { id } = req.params; logger.info(`Prompting transaction for company ID: ${id}`); // In a real app, this might use AI to generate a transaction prompt res.status(200).json({ prompt: 'What was the purpose of this transaction?', suggestions: ['Client Payment', 'Office Expense', 'Utility Bill'] }); } catch (error) { logger.error(`Error prompting transaction: ${error}`); res.status(500).json({ error: 'Failed to prompt transaction' }); } }, classifyTransactions: (req: Request, res: Response) => { try { const { id } = req.params; logger.info(`Classifying transactions for company ID: ${id}`); // Mock classified transactions - in a real app, use AI or rules to classify const classifiedTransactions = [ { id: 'tx1', description: 'Client payment', category: 'REVENUE', confidence: 0.95 }, { id: 'tx2', description: 'Office supplies', category: 'SGA_EXPENSE', confidence: 0.87 } ]; res.status(200).json(classifiedTransactions); } catch (error) { logger.error(`Error classifying transactions: ${error}`); res.status(500).json({ error: 'Failed to classify transactions' }); } }, generateGeneralLedger: (req: Request, res: Response) => { try { const { id } = req.params; logger.info(`Generating general ledger for company ID: ${id}`); // Mock general ledger - in a real app, generate from transactions const generalLedger = { company_id: id, period: 'January 2023', accounts: [ { account_code: 1000, name: 'Cash', type: 'ASSET', opening_balance: 5000.00, closing_balance: 5750.00, transactions: [ { id: 'tx1', date: '2023-01-15', amount: 1000.00, description: 'Client payment' }, { id: 'tx2', date: '2023-01-20', amount: -250.00, description: 'Office supplies' } ] } ] }; res.status(200).json(generalLedger); } catch (error) { logger.error(`Error generating general ledger: ${error}`); res.status(500).json({ error: 'Failed to generate general ledger' }); } }, bulkCreateTransactions: (req: Request, res: Response) => { try { const { id } = req.params; const transactions = req.body; logger.info(`Bulk creating ${transactions.length} transactions for company ID: ${id}`); // Process each transaction - in a real app, save to database const createdTransactions = transactions.map((tx: any, index: number) => ({ id: `tx-bulk-${Date.now()}-${index}`, ...tx, company_id: id, created_at: new Date().toISOString() })); res.status(201).json(createdTransactions); } catch (error) { logger.error(`Error bulk creating transactions: ${error}`); res.status(500).json({ error: 'Failed to bulk create transactions' }); } }, suggestTransactionCategories: (req: Request, res: Response) => { try { const { id } = req.params; const { transactions } = req.body; logger.info(`Suggesting categories for ${transactions.length} transactions for company ID: ${id}`); // Generate suggestions - in a real app, use AI or rules const suggestions = transactions.map((tx: any) => ({ transaction_id: tx.id, suggested_categories: [ 'REVENUE', 'EXPENSE', 'ASSET' ] })); res.status(200).json(suggestions); } catch (error) { logger.error(`Error suggesting transaction categories: ${error}`); res.status(500).json({ error: 'Failed to suggest categories' }); } }, // Category endpoints createCategory: (req: Request, res: Response) => { try { const { id } = req.params; logger.info(`Creating category for company ID: ${id}`); const category = { id: Date.now(), ...req.body, companyId: id }; res.status(201).json(category); } catch (error) { logger.error(`Error creating category: ${error}`); res.status(500).json({ error: 'Failed to create category' }); } }, getCategories: (req: Request, res: Response) => { try { const { id } = req.params; logger.info(`Getting categories for company ID: ${id}`); // Mock categories - in a real app, fetch from database const categories = [ { id: 1, name: 'Revenue', type: 'REVENUE', companyId: id, account_code: 4000 }, { id: 2, name: 'Office Expenses', type: 'SGA_EXPENSE', companyId: id, account_code: 6000 } ]; res.status(200).json(categories); } catch (error) { logger.error(`Error getting categories: ${error}`); res.status(500).json({ error: 'Failed to get categories' }); } } };