Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ FAMILY_API_KEY=
FAMILY_API_URL=
COINGECKO_API_KEY=
PLAIN_API_KEY=
PLAIN_TEST_API_KEY=
50 changes: 41 additions & 9 deletions pages/api/support-create-ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,35 @@ import type { NextApiRequest, NextApiResponse } from 'next';

import { CREATE_THREAD_MUTATION, UPSERT_CUSTOMER_MUTATION } from './plain-mutations';

const apiKey = process.env.PLAIN_API_KEY;
if (!apiKey) throw new Error('PLAIN_API_KEY env variable is missing');
const apiKeyProd = process.env.PLAIN_API_KEY;
const apiKeyTest = process.env.PLAIN_TEST_API_KEY;
if (!apiKeyProd) throw new Error('PLAIN_API_KEY env variable is missing');
if (!apiKeyTest) throw new Error('PLAIN_TEST_API_KEY env variable is missing');

const isEmail = (v: string) => /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(v.trim());

const makeGraphQLRequest = async (query: string, variables: Record<string, unknown>) => {
const getPlainConfig = (env: 'testnet' | 'production'): string => {
const apiKey = env === 'testnet' ? apiKeyTest : apiKeyProd;

if (!apiKey) {
throw new Error(
`Missing Plain credentials for ${env} environment. Check PLAIN_API_KEY[_TEST].`
);
}

return apiKey;
};

const makeGraphQLRequest = async (
query: string,
variables: Record<string, unknown>,
plainConfig: string
) => {
const response = await fetch('https://core-api.uk.plain.com/graphql/v1', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
Authorization: `Bearer ${plainConfig}`,
},
body: JSON.stringify({
query,
Expand Down Expand Up @@ -57,10 +75,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}

try {
const { email, text } = req.body;
const { email, text, environment } = req.body;

if (!email || !text) {
return res.status(400).json({ message: 'Email and text are required.' });
if (!email || !text || !environment) {
return res.status(400).json({ message: 'Email, text, and environment are required.' });
}

if (!isEmail(email)) {
Expand All @@ -71,6 +89,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return res.status(400).json({ error: 'Missing inquiry' });
}

if (environment !== 'production' && environment !== 'testnet') {
return res.status(400).json({ message: 'Invalid environment value.' });
}

const plainConfig = getPlainConfig(environment);

const upsertCustomerVariables = {
input: {
identifier: {
Expand All @@ -87,7 +111,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
},
};

const customerRes = await makeGraphQLRequest(UPSERT_CUSTOMER_MUTATION, upsertCustomerVariables);
const customerRes = await makeGraphQLRequest(
UPSERT_CUSTOMER_MUTATION,
upsertCustomerVariables,
plainConfig
);

if (customerRes.errors) {
console.error('GraphQL errors:', customerRes.errors);
Expand Down Expand Up @@ -157,7 +185,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
],
},
};
const result = await makeGraphQLRequest(CREATE_THREAD_MUTATION, createThreadVariables);
const result = await makeGraphQLRequest(
CREATE_THREAD_MUTATION,
createThreadVariables,
plainConfig
);

if (result.errors) {
console.error('GraphQL errors in createThread:', result.errors);
Expand Down
3 changes: 3 additions & 0 deletions src/layouts/SupportModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export const SupportModal = () => {
const [feedbackDialogOpen, setFeedbackOpen] = useRootStore(
useShallow((state) => [state.feedbackDialogOpen, state.setFeedbackOpen])
);
const currentNetworkConfig = useRootStore((state) => state.currentNetworkConfig);
const isTestnet = currentNetworkConfig.isTestnet ?? false;

const [value, setValue] = useState('');
const [isLoading, setIsLoading] = useState(false);
Expand Down Expand Up @@ -60,6 +62,7 @@ export const SupportModal = () => {
const payload = {
text: value,
email: email,
environment: isTestnet ? 'testnet' : 'production',
};

try {
Expand Down
Loading