|
| 1 | +import type { NextApiRequest, NextApiResponse } from 'next'; |
| 2 | + |
| 3 | +import { CREATE_THREAD_MUTATION, UPSERT_CUSTOMER_MUTATION } from './plain-mutations'; |
| 4 | + |
| 5 | +const apiKey = process.env.PLAIN_API_KEY; |
| 6 | +if (!apiKey) throw new Error('PLAIN_API_KEY env variable is missing'); |
| 7 | + |
| 8 | +const isEmail = (v: string) => /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(v.trim()); |
| 9 | + |
| 10 | +const makeGraphQLRequest = async (query: string, variables: Record<string, unknown>) => { |
| 11 | + const response = await fetch('https://core-api.uk.plain.com/graphql/v1', { |
| 12 | + method: 'POST', |
| 13 | + headers: { |
| 14 | + 'Content-Type': 'application/json', |
| 15 | + Authorization: `Bearer ${apiKey}`, |
| 16 | + }, |
| 17 | + body: JSON.stringify({ |
| 18 | + query, |
| 19 | + variables, |
| 20 | + }), |
| 21 | + }); |
| 22 | + |
| 23 | + if (!response.ok) { |
| 24 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 25 | + } |
| 26 | + |
| 27 | + return response.json(); |
| 28 | +}; |
| 29 | +export default async function handler(req: NextApiRequest, res: NextApiResponse) { |
| 30 | + const allowedOrigins = ['https://app.aave.com', 'https://aave.com']; |
| 31 | + const origin = req.headers.origin; |
| 32 | + |
| 33 | + const isOriginAllowed = (origin: string | undefined): boolean => { |
| 34 | + if (!origin) return false; |
| 35 | + |
| 36 | + if (allowedOrigins.includes(origin)) return true; |
| 37 | + |
| 38 | + // Match any subdomain ending with avaraxyz.vercel.app for deployment urls |
| 39 | + const allowedPatterns = [/^https:\/\/.*avaraxyz\.vercel\.app$/]; |
| 40 | + |
| 41 | + return allowedPatterns.some((pattern) => pattern.test(origin)); |
| 42 | + }; |
| 43 | + |
| 44 | + if (origin && isOriginAllowed(origin)) { |
| 45 | + res.setHeader('Access-Control-Allow-Origin', origin); |
| 46 | + } |
| 47 | + |
| 48 | + res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS'); |
| 49 | + res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); |
| 50 | + |
| 51 | + if (req.method === 'OPTIONS') { |
| 52 | + return res.status(200).end(); |
| 53 | + } |
| 54 | + |
| 55 | + if (req.method !== 'POST') { |
| 56 | + return res.status(405).json({ error: 'Method not allowed' }); |
| 57 | + } |
| 58 | + |
| 59 | + try { |
| 60 | + const { email, text } = req.body; |
| 61 | + |
| 62 | + if (!email || !text) { |
| 63 | + return res.status(400).json({ message: 'Email and text are required.' }); |
| 64 | + } |
| 65 | + |
| 66 | + if (!isEmail(email)) { |
| 67 | + return res.status(400).json({ message: 'Invalid email format.' }); |
| 68 | + } |
| 69 | + |
| 70 | + if (!text?.trim()) { |
| 71 | + return res.status(400).json({ error: 'Missing inquiry' }); |
| 72 | + } |
| 73 | + |
| 74 | + const upsertCustomerVariables = { |
| 75 | + input: { |
| 76 | + identifier: { |
| 77 | + emailAddress: email, |
| 78 | + }, |
| 79 | + onCreate: { |
| 80 | + fullName: email, |
| 81 | + email: { |
| 82 | + email: email, |
| 83 | + isVerified: true, |
| 84 | + }, |
| 85 | + }, |
| 86 | + onUpdate: {}, |
| 87 | + }, |
| 88 | + }; |
| 89 | + |
| 90 | + const customerRes = await makeGraphQLRequest(UPSERT_CUSTOMER_MUTATION, upsertCustomerVariables); |
| 91 | + |
| 92 | + if (customerRes.errors) { |
| 93 | + console.error('GraphQL errors:', customerRes.errors); |
| 94 | + return res |
| 95 | + .status(400) |
| 96 | + .json({ message: 'Failed to create support ticket', error: customerRes.errors }); |
| 97 | + } |
| 98 | + const customerResult = customerRes.data?.upsertCustomer; |
| 99 | + if (customerResult?.error) { |
| 100 | + console.error('Error upserting Customer:', customerResult.error); |
| 101 | + return res |
| 102 | + .status(400) |
| 103 | + .json({ message: 'Failed to create support ticket', error: customerResult.error }); |
| 104 | + } |
| 105 | + |
| 106 | + const createThreadVariables = { |
| 107 | + input: { |
| 108 | + title: 'New Support Inquiry', |
| 109 | + customerIdentifier: { |
| 110 | + emailAddress: email, |
| 111 | + }, |
| 112 | + components: [ |
| 113 | + { |
| 114 | + componentText: { |
| 115 | + text: 'Support inquiry from aave.com', |
| 116 | + }, |
| 117 | + }, |
| 118 | + { |
| 119 | + componentDivider: { |
| 120 | + dividerSpacingSize: 'M', |
| 121 | + }, |
| 122 | + }, |
| 123 | + { |
| 124 | + componentText: { |
| 125 | + textSize: 'S', |
| 126 | + textColor: 'MUTED', |
| 127 | + text: 'Contact email', |
| 128 | + }, |
| 129 | + }, |
| 130 | + { |
| 131 | + componentText: { |
| 132 | + text: email, |
| 133 | + }, |
| 134 | + }, |
| 135 | + { |
| 136 | + componentSpacer: { |
| 137 | + spacerSize: 'M', |
| 138 | + }, |
| 139 | + }, |
| 140 | + { |
| 141 | + componentText: { |
| 142 | + textSize: 'S', |
| 143 | + textColor: 'MUTED', |
| 144 | + text: 'Message', |
| 145 | + }, |
| 146 | + }, |
| 147 | + { |
| 148 | + componentPlainText: { |
| 149 | + plainText: text, |
| 150 | + }, |
| 151 | + }, |
| 152 | + { |
| 153 | + componentSpacer: { |
| 154 | + spacerSize: 'M', |
| 155 | + }, |
| 156 | + }, |
| 157 | + ], |
| 158 | + labelTypeIds: ['lt_01K36FQ2J7ZGXQ55RV769TJHYN'], |
| 159 | + }, |
| 160 | + }; |
| 161 | + const result = await makeGraphQLRequest(CREATE_THREAD_MUTATION, createThreadVariables); |
| 162 | + |
| 163 | + if (result.errors) { |
| 164 | + console.error('GraphQL errors in createThread:', result.errors); |
| 165 | + return res |
| 166 | + .status(400) |
| 167 | + .json({ message: 'Failed to create support ticket', error: result.errors }); |
| 168 | + } |
| 169 | + |
| 170 | + const threadResult = result.data?.createThread; |
| 171 | + if (threadResult?.error) { |
| 172 | + console.error('Error creating support ticket:', threadResult.error); |
| 173 | + return res |
| 174 | + .status(400) |
| 175 | + .json({ message: 'Failed to create support ticket', error: threadResult.error }); |
| 176 | + } |
| 177 | + |
| 178 | + return res.status(200).json({ |
| 179 | + message: 'Support Ticket Created Successfully', |
| 180 | + data: threadResult.thread.id, |
| 181 | + ok: true, |
| 182 | + }); |
| 183 | + } catch (error) { |
| 184 | + console.error('Support ticket backend error:', error); |
| 185 | + return res.status(500).json({ message: 'Internal Server Error' }); |
| 186 | + } |
| 187 | +} |
0 commit comments