feat: real Brevo API verifier, auto-reset if campaign missing, and update existing campaign support
This commit is contained in:
1 parent
2b4d3643cb
commit
99dd0f272e
4 files changed
+250
-27
No files matched your search
@@ -0,0 +1,64 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
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: `https://app.brevo.com/campaigns/email/edit/${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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user