email marketing
This commit is contained in:
55
src/app/(main)/(marketing)/unsubscribe/page.tsx
Normal file
55
src/app/(main)/(marketing)/unsubscribe/page.tsx
Normal file
@@ -0,0 +1,55 @@
|
||||
'use client';
|
||||
|
||||
import { useSearchParams } from 'next/navigation';
|
||||
import { useState } from 'react';
|
||||
|
||||
export default function UnsubscribePage() {
|
||||
const searchParams = useSearchParams();
|
||||
const token = searchParams.get('token');
|
||||
const [status, setStatus] = useState<'idle' | 'saving' | 'success' | 'error'>('idle');
|
||||
|
||||
async function unsubscribe() {
|
||||
setStatus('saving');
|
||||
|
||||
const response = await fetch('/api/marketing/unsubscribe', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ token }),
|
||||
});
|
||||
|
||||
setStatus(response.ok ? 'success' : 'error');
|
||||
}
|
||||
|
||||
return (
|
||||
<main className="mx-auto flex min-h-[60vh] max-w-xl items-center px-6 py-20">
|
||||
<section className="w-full border border-slate-200 bg-white p-8 text-center shadow-sm">
|
||||
{status === 'success' ? (
|
||||
<>
|
||||
<h1 className="text-3xl font-semibold text-slate-900">You’re unsubscribed.</h1>
|
||||
<p className="mt-4 text-slate-600">
|
||||
You will no longer receive QR Master product and upgrade emails.
|
||||
</p>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<h1 className="text-3xl font-semibold text-slate-900">Unsubscribe from product updates?</h1>
|
||||
<p className="mt-4 text-slate-600">
|
||||
You will still receive essential account, billing, and security emails.
|
||||
</p>
|
||||
<button
|
||||
type="button"
|
||||
onClick={unsubscribe}
|
||||
disabled={!token || status === 'saving'}
|
||||
className="mt-8 bg-slate-900 px-5 py-3 font-medium text-white disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
{status === 'saving' ? 'Unsubscribing…' : 'Unsubscribe'}
|
||||
</button>
|
||||
{status === 'error' && (
|
||||
<p className="mt-4 text-sm text-red-700">This link is invalid or has expired.</p>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</section>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
51
src/app/(main)/api/marketing/designer-broadcast/route.ts
Normal file
51
src/app/(main)/api/marketing/designer-broadcast/route.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { cookies } from 'next/headers';
|
||||
import { db } from '@/lib/db';
|
||||
import { sendDesignerAnnouncementEmail } from '@/lib/email';
|
||||
import { createMarketingUnsubscribeUrl } from '@/lib/marketingEmail';
|
||||
|
||||
export const maxDuration = 300;
|
||||
|
||||
/** Sends the Designer announcement to account holders who have not opted out. */
|
||||
export async function POST() {
|
||||
if (cookies().get('newsletter-admin')?.value !== 'authenticated') {
|
||||
return NextResponse.json({ error: 'Unauthorized.' }, { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
const suppressions = await db.newsletterSubscription.findMany({
|
||||
where: { status: 'unsubscribed' },
|
||||
select: { email: true },
|
||||
});
|
||||
const suppressedEmails = new Set(suppressions.map((entry) => entry.email.toLowerCase()));
|
||||
const accountUsers = await db.user.findMany({
|
||||
select: { email: true },
|
||||
orderBy: { createdAt: 'asc' },
|
||||
});
|
||||
const recipients = accountUsers.filter(
|
||||
(recipient) => !suppressedEmails.has(recipient.email.toLowerCase())
|
||||
);
|
||||
|
||||
let sent = 0;
|
||||
const failed: string[] = [];
|
||||
|
||||
// Sequential sending respects the configured Resend free-tier rate limit.
|
||||
for (const recipient of recipients) {
|
||||
try {
|
||||
await sendDesignerAnnouncementEmail(
|
||||
recipient.email,
|
||||
createMarketingUnsubscribeUrl(recipient.email)
|
||||
);
|
||||
sent++;
|
||||
} catch (error) {
|
||||
failed.push(recipient.email);
|
||||
console.error('Designer announcement failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({ sent, failed: failed.length, total: recipients.length });
|
||||
} catch (error) {
|
||||
console.error('Designer announcement broadcast error:', error);
|
||||
return NextResponse.json({ error: 'Unable to send Designer announcement.' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
25
src/app/(main)/api/marketing/unsubscribe/route.ts
Normal file
25
src/app/(main)/api/marketing/unsubscribe/route.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { getUnsubscribeEmail } from '@/lib/marketingEmail';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const { token } = await request.json();
|
||||
const email = getUnsubscribeEmail(typeof token === 'string' ? token : null);
|
||||
|
||||
if (!email) {
|
||||
return NextResponse.json({ error: 'This unsubscribe link is invalid or expired.' }, { status: 400 });
|
||||
}
|
||||
|
||||
await db.newsletterSubscription.upsert({
|
||||
where: { email },
|
||||
update: { status: 'unsubscribed', source: 'marketing-unsubscribe' },
|
||||
create: { email, status: 'unsubscribed', source: 'marketing-unsubscribe' },
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Marketing unsubscribe error:', error);
|
||||
return NextResponse.json({ error: 'Unable to update your email preferences.' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,7 @@ export async function POST(request: NextRequest) {
|
||||
where: { email },
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
if (existing?.status === 'subscribed') {
|
||||
// If already subscribed, return success (idempotent)
|
||||
// Don't reveal if email exists for privacy
|
||||
return NextResponse.json({
|
||||
@@ -58,7 +58,20 @@ export async function POST(request: NextRequest) {
|
||||
message: 'Successfully subscribed to AI features newsletter!',
|
||||
alreadySubscribed: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (existing?.status === 'unsubscribed') {
|
||||
await db.newsletterSubscription.update({
|
||||
where: { email },
|
||||
data: { status: 'subscribed', source: 'ai-coming-soon' },
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Successfully subscribed to AI features newsletter!',
|
||||
alreadySubscribed: false,
|
||||
});
|
||||
}
|
||||
|
||||
// Create new subscription
|
||||
await db.newsletterSubscription.create({
|
||||
|
||||
Reference in New Issue
Block a user