118 lines
3.2 KiB
JavaScript
118 lines
3.2 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const {
|
|
bankAccountRepository,
|
|
bankStatementRepository,
|
|
bankStatementTransactionRepository,
|
|
bankReconciliationRepository
|
|
} = require('../repositories');
|
|
|
|
/**
|
|
* GET /api/plugins/financials/reconciliation/accounts
|
|
* Get all bank accounts for site
|
|
*/
|
|
router.get('/accounts', async (req, res) => {
|
|
try {
|
|
const { site_id } = req.query;
|
|
|
|
if (!site_id) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'Missing required parameter: site_id'
|
|
});
|
|
}
|
|
|
|
const accounts = await bankAccountRepository.findBySiteId(site_id);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: accounts,
|
|
message: 'Bank accounts retrieved successfully'
|
|
});
|
|
} catch (error) {
|
|
console.error('Failed to get bank accounts:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to retrieve bank accounts',
|
|
message: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
/**
|
|
* GET /api/plugins/financials/reconciliation/accounts/:accountId/statements
|
|
* Get bank statements for an account
|
|
*/
|
|
router.get('/accounts/:accountId/statements', async (req, res) => {
|
|
try {
|
|
const { accountId } = req.params;
|
|
const statements = await bankStatementRepository.findByBankAccountId(accountId);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: statements,
|
|
message: 'Bank statements retrieved successfully'
|
|
});
|
|
} catch (error) {
|
|
console.error('Failed to get bank statements:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to retrieve bank statements',
|
|
message: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
/**
|
|
* GET /api/plugins/financials/reconciliation/statements/:statementId/transactions
|
|
* Get transactions for a statement
|
|
*/
|
|
router.get('/statements/:statementId/transactions', async (req, res) => {
|
|
try {
|
|
const { statementId } = req.params;
|
|
const transactions = await bankStatementTransactionRepository.findByStatementId(statementId);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: transactions,
|
|
message: 'Statement transactions retrieved successfully'
|
|
});
|
|
} catch (error) {
|
|
console.error('Failed to get statement transactions:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to retrieve statement transactions',
|
|
message: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
/**
|
|
* GET /api/plugins/financials/reconciliation/accounts/:accountId/metrics
|
|
* Get reconciliation metrics for an account
|
|
*/
|
|
router.get('/accounts/:accountId/metrics', async (req, res) => {
|
|
try {
|
|
const { accountId } = req.params;
|
|
const { as_of_date } = req.query;
|
|
|
|
const asOfDate = as_of_date || new Date().toISOString().split('T')[0];
|
|
const metrics = await bankReconciliationRepository.calculateReconciliationMetrics(accountId, asOfDate);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: metrics,
|
|
message: 'Reconciliation metrics retrieved successfully'
|
|
});
|
|
} catch (error) {
|
|
console.error('Failed to get reconciliation metrics:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Failed to retrieve reconciliation metrics',
|
|
message: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|
|
|