Skip to content

Commit c6dca10

Browse files
committed
feat: added hook on testnet for plain support test space
1 parent 0eb43a5 commit c6dca10

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ FAMILY_API_KEY=
4444
FAMILY_API_URL=
4545
COINGECKO_API_KEY=
4646
PLAIN_API_KEY=
47+
PLAIN_TEST_API_KEY=

pages/api/support-create-ticket.ts

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,35 @@ import type { NextApiRequest, NextApiResponse } from 'next';
22

33
import { 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

810
const 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);

src/layouts/SupportModal.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export const SupportModal = () => {
1212
const [feedbackDialogOpen, setFeedbackOpen] = useRootStore(
1313
useShallow((state) => [state.feedbackDialogOpen, state.setFeedbackOpen])
1414
);
15+
const currentNetworkConfig = useRootStore((state) => state.currentNetworkConfig);
16+
const isTestnet = currentNetworkConfig.isTestnet ?? false;
1517

1618
const [value, setValue] = useState('');
1719
const [isLoading, setIsLoading] = useState(false);
@@ -60,6 +62,7 @@ export const SupportModal = () => {
6062
const payload = {
6163
text: value,
6264
email: email,
65+
environment: isTestnet ? 'testnet' : 'production',
6366
};
6467

6568
try {

0 commit comments

Comments
 (0)