92 lines
2.5 KiB
JavaScript
92 lines
2.5 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const {
|
|
fetchCampaigns,
|
|
fetchAccountInsights,
|
|
updateCampaignBudget,
|
|
updateCampaignStatus,
|
|
} = require('../services/meta.service');
|
|
|
|
// Get all campaigns with insights
|
|
router.get('/campaigns', async (req, res) => {
|
|
try {
|
|
const { since, until } = req.query;
|
|
|
|
if (!since || !until) {
|
|
return res.status(400).json({ error: 'Date range is required (since, until)' });
|
|
}
|
|
|
|
const campaigns = await fetchCampaigns(since, until);
|
|
res.json(campaigns);
|
|
} catch (error) {
|
|
console.error('Campaign fetch error:', error);
|
|
res.status(500).json({
|
|
error: 'Failed to fetch campaigns',
|
|
details: error.response?.data?.error?.message || error.message,
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get account insights
|
|
router.get('/account-insights', async (req, res) => {
|
|
try {
|
|
const { since, until } = req.query;
|
|
|
|
if (!since || !until) {
|
|
return res.status(400).json({ error: 'Date range is required (since, until)' });
|
|
}
|
|
|
|
const insights = await fetchAccountInsights(since, until);
|
|
res.json(insights);
|
|
} catch (error) {
|
|
console.error('Account insights fetch error:', error);
|
|
res.status(500).json({
|
|
error: 'Failed to fetch account insights',
|
|
details: error.response?.data?.error?.message || error.message,
|
|
});
|
|
}
|
|
});
|
|
|
|
// Update campaign budget
|
|
router.patch('/campaigns/:campaignId/budget', async (req, res) => {
|
|
try {
|
|
const { campaignId } = req.params;
|
|
const { budget } = req.body;
|
|
|
|
if (!budget) {
|
|
return res.status(400).json({ error: 'Budget is required' });
|
|
}
|
|
|
|
const result = await updateCampaignBudget(campaignId, budget);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('Budget update error:', error);
|
|
res.status(500).json({
|
|
error: 'Failed to update campaign budget',
|
|
details: error.response?.data?.error?.message || error.message,
|
|
});
|
|
}
|
|
});
|
|
|
|
// Update campaign status (pause/unpause)
|
|
router.post('/campaigns/:campaignId/:action', async (req, res) => {
|
|
try {
|
|
const { campaignId, action } = req.params;
|
|
|
|
if (!['pause', 'unpause'].includes(action)) {
|
|
return res.status(400).json({ error: 'Invalid action. Use "pause" or "unpause"' });
|
|
}
|
|
|
|
const result = await updateCampaignStatus(campaignId, action);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('Status update error:', error);
|
|
res.status(500).json({
|
|
error: 'Failed to update campaign status',
|
|
details: error.response?.data?.error?.message || error.message,
|
|
});
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|