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
@@ -2,11 +2,18 @@ import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const { campaignName, subject, htmlContent, scheduledAt } = await req.json();
|
||||
const {
|
||||
campaignName,
|
||||
subject,
|
||||
htmlContent,
|
||||
scheduledAt,
|
||||
updateExisting = false,
|
||||
existingCampaignId
|
||||
} = await req.json();
|
||||
|
||||
if (!campaignName || !subject || !htmlContent) {
|
||||
if (!htmlContent) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Chýbajú povinné polia (campaignName, subject, htmlContent).' },
|
||||
{ error: 'Chýba povinné pole htmlContent.' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
@@ -22,10 +29,54 @@ export async function POST(req: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
// Volanie Brevo API v3
|
||||
const payload: any = {
|
||||
name: campaignName,
|
||||
subject: subject,
|
||||
// 1. Ak je zadané updateExisting a máme existujúce ID, skúsime aktualizovať existujúcu kampaň
|
||||
if (updateExisting && existingCampaignId) {
|
||||
const updatePayload: any = {
|
||||
name: campaignName,
|
||||
subject: subject,
|
||||
sender: {
|
||||
name: senderName,
|
||||
email: senderEmail
|
||||
},
|
||||
htmlContent: htmlContent
|
||||
};
|
||||
|
||||
if (scheduledAt) {
|
||||
updatePayload.scheduledAt = scheduledAt;
|
||||
}
|
||||
|
||||
const updateRes = await fetch(
|
||||
`https://api.brevo.com/v3/emailCampaigns/${encodeURIComponent(existingCampaignId)}`,
|
||||
{
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'accept': 'application/json',
|
||||
'content-type': 'application/json',
|
||||
'api-key': apiKey
|
||||
},
|
||||
body: JSON.stringify(updatePayload)
|
||||
}
|
||||
);
|
||||
|
||||
if (updateRes.ok || updateRes.status === 204) {
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
updated: true,
|
||||
campaignId: Number(existingCampaignId),
|
||||
brevoUrl: `https://app.brevo.com/campaigns/email/edit/${existingCampaignId}`,
|
||||
brevoListingUrl: 'https://app.brevo.com/campaigns/listing',
|
||||
message: 'Kampaň bola úspešne aktualizovaná v Brevo!'
|
||||
});
|
||||
}
|
||||
|
||||
// Ak aktualizácia zlyhala (napr. kampaň už bola odoslaná alebo neexistuje), vytvoríme novú
|
||||
console.warn(`Update failed on campaign ${existingCampaignId}, creating a new one instead.`);
|
||||
}
|
||||
|
||||
// 2. Vytvorenie novej kampane v Brevo
|
||||
const createPayload: any = {
|
||||
name: campaignName || 'COOP Jednota Newsletter',
|
||||
subject: subject || 'COOP Jednota Newsletter',
|
||||
sender: {
|
||||
name: senderName,
|
||||
email: senderEmail
|
||||
@@ -35,7 +86,7 @@ export async function POST(req: NextRequest) {
|
||||
};
|
||||
|
||||
if (scheduledAt) {
|
||||
payload.scheduledAt = scheduledAt;
|
||||
createPayload.scheduledAt = scheduledAt;
|
||||
}
|
||||
|
||||
const brevoRes = await fetch('https://api.brevo.com/v3/emailCampaigns', {
|
||||
@@ -45,7 +96,7 @@ export async function POST(req: NextRequest) {
|
||||
'content-type': 'application/json',
|
||||
'api-key': apiKey
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
body: JSON.stringify(createPayload)
|
||||
});
|
||||
|
||||
const data = await brevoRes.json();
|
||||
@@ -58,9 +109,9 @@ export async function POST(req: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
// Úspech - vrátime campaignId a priame URL na editáciu a prehľad kampaní v novom Brevo dashboarde
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
updated: false,
|
||||
campaignId: data.id,
|
||||
brevoUrl: `https://app.brevo.com/campaigns/email/edit/${data.id}`,
|
||||
brevoListingUrl: 'https://app.brevo.com/campaigns/listing',
|
||||
|
||||
Reference in new issue
Block a user