@@ -2,17 +2,35 @@ import type { NextApiRequest, NextApiResponse } from 'next';
22
33import { CREATE_THREAD_MUTATION , UPSERT_CUSTOMER_MUTATION } from './plain-mutations' ;
44
5- const apiKey = process . env . PLAIN_API_KEY ;
6- if ( ! apiKey ) throw new Error ( 'PLAIN_API_KEY env variable is missing' ) ;
5+ const apiKeyProd = process . env . PLAIN_API_KEY ;
6+ const apiKeyTest = process . env . PLAIN_TEST_API_KEY ;
7+ if ( ! apiKeyProd ) throw new Error ( 'PLAIN_API_KEY env variable is missing' ) ;
8+ if ( ! apiKeyTest ) throw new Error ( 'PLAIN_TEST_API_KEY env variable is missing' ) ;
79
810const isEmail = ( v : string ) => / ^ [ ^ \s @ ] + @ [ ^ \s @ ] + \. [ ^ \s @ ] { 2 , } $ / . test ( v . trim ( ) ) ;
911
10- const makeGraphQLRequest = async ( query : string , variables : Record < string , unknown > ) => {
12+ const getPlainConfig = ( env : 'testnet' | 'production' ) : string => {
13+ const apiKey = env === 'testnet' ? apiKeyTest : apiKeyProd ;
14+
15+ if ( ! apiKey ) {
16+ throw new Error (
17+ `Missing Plain credentials for ${ env } environment. Check PLAIN_API_KEY[_TEST].`
18+ ) ;
19+ }
20+
21+ return apiKey ;
22+ } ;
23+
24+ const makeGraphQLRequest = async (
25+ query : string ,
26+ variables : Record < string , unknown > ,
27+ plainConfig : string
28+ ) => {
1129 const response = await fetch ( 'https://core-api.uk.plain.com/graphql/v1' , {
1230 method : 'POST' ,
1331 headers : {
1432 'Content-Type' : 'application/json' ,
15- Authorization : `Bearer ${ apiKey } ` ,
33+ Authorization : `Bearer ${ plainConfig } ` ,
1634 } ,
1735 body : JSON . stringify ( {
1836 query,
@@ -57,10 +75,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
5775 }
5876
5977 try {
60- const { email, text } = req . body ;
78+ const { email, text, environment } = req . body ;
6179
62- if ( ! email || ! text ) {
63- return res . status ( 400 ) . json ( { message : 'Email and text are required.' } ) ;
80+ if ( ! email || ! text || ! environment ) {
81+ return res . status ( 400 ) . json ( { message : 'Email, text, and environment are required.' } ) ;
6482 }
6583
6684 if ( ! isEmail ( email ) ) {
@@ -71,6 +89,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
7189 return res . status ( 400 ) . json ( { error : 'Missing inquiry' } ) ;
7290 }
7391
92+ if ( environment !== 'production' && environment !== 'testnet' ) {
93+ return res . status ( 400 ) . json ( { message : 'Invalid environment value.' } ) ;
94+ }
95+
96+ const plainConfig = getPlainConfig ( environment ) ;
97+
7498 const upsertCustomerVariables = {
7599 input : {
76100 identifier : {
@@ -87,7 +111,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
87111 } ,
88112 } ;
89113
90- const customerRes = await makeGraphQLRequest ( UPSERT_CUSTOMER_MUTATION , upsertCustomerVariables ) ;
114+ const customerRes = await makeGraphQLRequest (
115+ UPSERT_CUSTOMER_MUTATION ,
116+ upsertCustomerVariables ,
117+ plainConfig
118+ ) ;
91119
92120 if ( customerRes . errors ) {
93121 console . error ( 'GraphQL errors:' , customerRes . errors ) ;
@@ -157,7 +185,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
157185 ] ,
158186 } ,
159187 } ;
160- const result = await makeGraphQLRequest ( CREATE_THREAD_MUTATION , createThreadVariables ) ;
188+ const result = await makeGraphQLRequest (
189+ CREATE_THREAD_MUTATION ,
190+ createThreadVariables ,
191+ plainConfig
192+ ) ;
161193
162194 if ( result . errors ) {
163195 console . error ( 'GraphQL errors in createThread:' , result . errors ) ;
0 commit comments