← back to Dear Bubbe Nextjs
app/api/discount/route.ts
133 lines
import { NextRequest, NextResponse } from 'next/server'
import fs from 'fs/promises'
import path from 'path'
// Store discount codes (in production, use a database)
const DISCOUNTS_DIR = path.join(process.cwd(), 'data', 'discounts')
async function ensureDiscountsDir() {
try {
await fs.mkdir(DISCOUNTS_DIR, { recursive: true })
} catch (error) {
console.error('Failed to create discounts directory:', error)
}
}
export async function POST(request: NextRequest) {
try {
const { email, code, plan } = await request.json()
if (!email || !code || !plan) {
return NextResponse.json({ error: 'Missing required fields' }, { status: 400 })
}
await ensureDiscountsDir()
// Create discount record
const discount = {
email,
code,
plan,
discount: 15, // 15% off
createdAt: new Date().toISOString(),
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString(), // 24 hours
used: false
}
// Save discount code
const discountFile = path.join(DISCOUNTS_DIR, `${code}.json`)
await fs.writeFile(discountFile, JSON.stringify(discount, null, 2))
// In production, you would send an actual email here
// For now, we'll just log it
console.log(`[DISCOUNT] Code ${code} sent to ${email} for plan ${plan}`)
console.log(`[DISCOUNT] 15% off, expires in 24 hours`)
// Create a simple email record
const emailRecord = {
to: email,
subject: '🎉 Your 15% OFF Bubbe Discount Code!',
body: `
Mazel tov! Here's your special discount code:
Code: ${code}
Discount: 15% OFF your first month
Plan: ${plan}
Expires: 24 hours from now
Use this code at checkout to save on your Bubbe subscription!
Remember: This offer expires in 24 hours, so don't be a schmuck and miss out!
Love,
Bubbe 🥯
`,
sentAt: new Date().toISOString()
}
// Save email record
const emailFile = path.join(DISCOUNTS_DIR, `email_${Date.now()}.json`)
await fs.writeFile(emailFile, JSON.stringify(emailRecord, null, 2))
return NextResponse.json({
success: true,
code,
message: 'Discount code sent to email!'
})
} catch (error) {
console.error('Error creating discount:', error)
return NextResponse.json({ error: 'Failed to create discount' }, { status: 500 })
}
}
// Verify discount code
export async function GET(request: NextRequest) {
try {
const searchParams = request.nextUrl.searchParams
const code = searchParams.get('code')
if (!code) {
return NextResponse.json({ error: 'No code provided' }, { status: 400 })
}
const discountFile = path.join(DISCOUNTS_DIR, `${code}.json`)
try {
const data = await fs.readFile(discountFile, 'utf-8')
const discount = JSON.parse(data)
// Check if expired
const expiryDate = new Date(discount.expiresAt)
const now = new Date()
if (now > expiryDate) {
return NextResponse.json({
valid: false,
message: 'This discount code has expired'
})
}
if (discount.used) {
return NextResponse.json({
valid: false,
message: 'This discount code has already been used'
})
}
return NextResponse.json({
valid: true,
discount: discount.discount,
plan: discount.plan,
expiresAt: discount.expiresAt
})
} catch (error) {
return NextResponse.json({
valid: false,
message: 'Invalid discount code'
})
}
} catch (error) {
console.error('Error verifying discount:', error)
return NextResponse.json({ error: 'Failed to verify discount' }, { status: 500 })
}
}