Add campaigns component and services
This commit is contained in:
72
dashboard-server/klaviyo-server/routes/reporting.routes.js
Normal file
72
dashboard-server/klaviyo-server/routes/reporting.routes.js
Normal file
@@ -0,0 +1,72 @@
|
||||
import express from 'express';
|
||||
import { ReportingService } from '../services/reporting.service.js';
|
||||
import { TimeManager } from '../utils/time.utils.js';
|
||||
|
||||
export function createReportingRouter(apiKey, apiRevision) {
|
||||
const router = express.Router();
|
||||
const timeManager = new TimeManager();
|
||||
const reportingService = new ReportingService(apiKey, apiRevision);
|
||||
|
||||
// Get campaign reports with optional filtering
|
||||
router.get('/campaigns', async (req, res) => {
|
||||
try {
|
||||
const { timeRange, startDate, endDate } = req.query;
|
||||
|
||||
let params = {};
|
||||
if (timeRange === 'custom') {
|
||||
if (!startDate || !endDate) {
|
||||
return res.status(400).json({ error: 'Custom range requires startDate and endDate' });
|
||||
}
|
||||
params = { startDate, endDate };
|
||||
} else {
|
||||
params = { timeRange: timeRange || 'last30days' };
|
||||
}
|
||||
|
||||
console.log('[Reporting Route] Fetching campaign reports with params:', params);
|
||||
const data = await reportingService.getEnrichedCampaignReports(params);
|
||||
console.log('[Reporting Route] Success:', {
|
||||
count: data.data?.length || 0
|
||||
});
|
||||
|
||||
res.json(data);
|
||||
} catch (error) {
|
||||
console.error('[Reporting Route] Error:', error);
|
||||
res.status(500).json({
|
||||
status: 'error',
|
||||
message: error.message,
|
||||
details: error.response?.data || null
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Get campaign reports by time range
|
||||
router.get('/campaigns/:timeRange', async (req, res) => {
|
||||
try {
|
||||
const { timeRange } = req.params;
|
||||
|
||||
let result;
|
||||
if (timeRange === 'custom') {
|
||||
const { startDate, endDate } = req.query;
|
||||
if (!startDate || !endDate) {
|
||||
return res.status(400).json({ error: 'Custom range requires startDate and endDate' });
|
||||
}
|
||||
|
||||
result = await reportingService.getEnrichedCampaignReports({
|
||||
startDate,
|
||||
endDate
|
||||
});
|
||||
} else {
|
||||
result = await reportingService.getEnrichedCampaignReports({
|
||||
timeRange
|
||||
});
|
||||
}
|
||||
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
console.error("[Reporting Route] Error:", error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
Reference in New Issue
Block a user