Namiesto napevno zadaneho zoznamu kampani (404) sa odkaz sklada z ID
kampane: app.brevo.com/marketing-campaign/edit/{id}.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getBrevoCampaignUrl } from '@/lib/brevo';
|
|
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
const { searchParams } = new URL(req.url);
|
|
const campaignId = searchParams.get('campaignId');
|
|
|
|
if (!campaignId) {
|
|
return NextResponse.json(
|
|
{ exists: false, error: 'Chýba parameter campaignId' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const apiKey = process.env.BREVO_API_KEY;
|
|
if (!apiKey) {
|
|
return NextResponse.json(
|
|
{ exists: false, error: 'BREVO_API_KEY nie je nakonfigurovaný' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
|
|
const res = await fetch(`https://api.brevo.com/v3/emailCampaigns/${encodeURIComponent(campaignId)}`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'accept': 'application/json',
|
|
'api-key': apiKey
|
|
},
|
|
cache: 'no-store'
|
|
});
|
|
|
|
if (res.status === 404) {
|
|
return NextResponse.json({
|
|
exists: false,
|
|
message: 'Kampaň s týmto ID v Brevo neexistuje alebo bola vymazaná.'
|
|
});
|
|
}
|
|
|
|
if (!res.ok) {
|
|
const errData = await res.json().catch(() => ({}));
|
|
return NextResponse.json({
|
|
exists: false,
|
|
error: errData.message || `Brevo API vrátilo status ${res.status}`
|
|
});
|
|
}
|
|
|
|
const data = await res.json();
|
|
return NextResponse.json({
|
|
exists: true,
|
|
campaignId: data.id,
|
|
name: data.name,
|
|
status: data.status,
|
|
subject: data.subject,
|
|
brevoUrl: getBrevoCampaignUrl(data.id),
|
|
brevoListingUrl: 'https://app.brevo.com/campaigns/listing'
|
|
});
|
|
} catch (err: any) {
|
|
console.error('Brevo verify error:', err);
|
|
return NextResponse.json(
|
|
{ exists: false, error: err.message || 'Chyba spojenia s Brevo' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|