81 lines
2.3 KiB
JavaScript
81 lines
2.3 KiB
JavaScript
const express = require('express');
|
|
const { query, validationResult } = require('express-validator');
|
|
const { authorize } = require('../middleware/auth');
|
|
const reportsController = require('../controllers/reportsController');
|
|
|
|
const router = express.Router();
|
|
|
|
// Validation middleware
|
|
const handleValidationErrors = (req, res, next) => {
|
|
const errors = validationResult(req);
|
|
if (!errors.isEmpty()) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
message: 'Validation failed',
|
|
errors: errors.array()
|
|
});
|
|
}
|
|
next();
|
|
};
|
|
|
|
// Date range validation
|
|
const dateRangeValidation = [
|
|
query('startDate')
|
|
.optional()
|
|
.isISO8601()
|
|
.withMessage('Start date must be a valid ISO 8601 date'),
|
|
query('endDate')
|
|
.optional()
|
|
.isISO8601()
|
|
.withMessage('End date must be a valid ISO 8601 date'),
|
|
query('groupBy')
|
|
.optional()
|
|
.isIn(['day', 'week', 'month', 'quarter', 'year'])
|
|
.withMessage('Group by must be day, week, month, quarter, or year'),
|
|
query('format')
|
|
.optional()
|
|
.isIn(['json', 'csv'])
|
|
.withMessage('Format must be json or csv')
|
|
];
|
|
|
|
// Export validation
|
|
const exportValidation = [
|
|
query('reportType')
|
|
.isIn(['sales', 'usage', 'commission'])
|
|
.withMessage('Report type must be sales, usage, or commission'),
|
|
query('format')
|
|
.optional()
|
|
.isIn(['json', 'csv', 'pdf'])
|
|
.withMessage('Format must be json, csv, or pdf')
|
|
];
|
|
|
|
// Sales report
|
|
router.get('/sales', dateRangeValidation, handleValidationErrors, reportsController.getSalesReport);
|
|
|
|
// Usage report
|
|
router.get('/usage', [
|
|
...dateRangeValidation,
|
|
query('customerId')
|
|
.optional()
|
|
.isUUID()
|
|
.withMessage('Customer ID must be a valid UUID'),
|
|
query('productId')
|
|
.optional()
|
|
.isUUID()
|
|
.withMessage('Product ID must be a valid UUID')
|
|
], handleValidationErrors, reportsController.getUsageReport);
|
|
|
|
// Commission report
|
|
router.get('/commission', [
|
|
...dateRangeValidation,
|
|
query('status')
|
|
.optional()
|
|
.isIn(['all', 'pending', 'approved', 'paid', 'disputed', 'cancelled'])
|
|
.withMessage('Invalid commission status')
|
|
], handleValidationErrors, reportsController.getCommissionReport);
|
|
|
|
// Export reports
|
|
router.get('/export', exportValidation, handleValidationErrors, reportsController.exportReport);
|
|
|
|
module.exports = router;
|