// Grainy.ai — Support Form API // Fires a Loops event to focus@grainy.ai with ticket details // // Env vars needed: // LOOPS_API_KEY (already set up from your other email flows) // // Loops setup: // 1. Create contact: focus@grainy.ai // 2. Create event: support_ticket // 3. Create flow triggered by support_ticket → send email // 4. In the email template use {{fromEmail}}, {{subject}}, {{message}}, etc. module.exports = async function handler(req, res) { if (req.method !== 'POST') { return res.status(405).json({ error: 'Method not allowed' }); } const { email, category, subject, message, clerkUserId, plan } = req.body || {}; if (!email || !subject || !message) { return res.status(400).json({ error: 'Missing required fields' }); } const loopsKey = process.env.LOOPS_API_KEY; if (!loopsKey) { console.error('LOOPS_API_KEY not configured'); return res.status(500).json({ error: 'Email service not configured' }); } const categoryLabels = { general: 'General', billing: 'Billing & Payments', bug: 'Bug Report', feature: 'Feature Request', account: 'Account Issue', cancel: 'Cancellation / Refund', other: 'Other', }; try { const response = await fetch('https://app.loops.so/api/v1/events/send', { method: 'POST', headers: { 'Authorization': `Bearer ${loopsKey}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ eventName: 'support_ticket', email: 'focus@grainy.ai', eventProperties: { fromEmail: email, category: categoryLabels[category] || category, subject: subject, message: message, plan: plan || 'unknown', clerkUserId: clerkUserId || '', }, }), }); if (!response.ok) { const errText = await response.text(); console.error('Loops API error:', response.status, errText); return res.status(500).json({ error: 'Failed to send support ticket' }); } console.log(`Support ticket sent: [${category}] ${subject} from ${email}`); return res.status(200).json({ success: true }); } catch (err) { console.error('Support ticket error:', err.message); return res.status(500).json({ error: 'Failed to send support ticket' }); } };