diff --git a/backend/src/routes/apolloApiKey.ts b/backend/src/routes/apolloApiKey.ts index bf7b080..3cd8c97 100644 --- a/backend/src/routes/apolloApiKey.ts +++ b/backend/src/routes/apolloApiKey.ts @@ -1,80 +1,91 @@ -import express, {Response, Request} from 'express'; +import express, {Request, Response} from 'express'; +import {SessionRequest} from 'supertokens-node/framework/express'; +import {ApolloApiKey} from '../models/apolloApiKey'; import {DI} from '../server'; import {logger} from '../utilities/logger'; -import {ApolloApiKey} from '../models/apolloApiKey'; -import {SessionRequest} from 'supertokens-node/framework/express'; -import config from '../config/config'; const router = express.Router(); -const handleRequest = async (req: Request | SessionRequest, defaultUserId = 'anonymous') => { +const handleRequest = async ( + req: Request | SessionRequest, + defaultUserId = 'anonymous' +) => { const userId = (req as SessionRequest).session?.getUserId() || defaultUserId; return userId; }; -router.get('/apollo-api-key', async (req: Request | SessionRequest, res: Response) => { - try { - const userId = await handleRequest(req); - const apiKey = await DI.apolloApiKeys.findOne({userId}); - - if (apiKey) { - const decryptedKey = apiKey.getDecryptedKey(); - const obfuscatedKey = `${decryptedKey.slice(0, 4)}****${decryptedKey.slice(-4)}`; - res.json({key: obfuscatedKey}); - } else { - res.json({key: null}); +router.get( + '/apollo-api-key', + async (req: Request | SessionRequest, res: Response) => { + try { + const userId = await handleRequest(req); + const apiKey = await DI.apolloApiKeys.findOne({userId}); + + if (apiKey) { + const decryptedKey = apiKey.getDecryptedKey(); + const obfuscatedKey = `${decryptedKey.slice(0, 4)}****${decryptedKey.slice(-4)}`; + res.json({key: obfuscatedKey}); + } else { + res.json({key: null}); + } + } catch (error) { + logger.error('Failed to fetch API key', {error}); + res.status(500).json({error: 'Failed to fetch API key'}); } - } catch (error) { - logger.error('Failed to fetch API key', {error}); - res.status(500).json({error: 'Failed to fetch API key'}); } -}); +); -router.post('/apollo-api-key', async (req: Request | SessionRequest, res: Response) => { - try { - const userId = await handleRequest(req); - const {key} = req.body; - - if (!key) { - return res.status(400).json({error: 'API key is required'}); - } +router.post( + '/apollo-api-key', + async (req: Request | SessionRequest, res: Response) => { + try { + const userId = await handleRequest(req); + const {key} = req.body; - let apiKey = await DI.apolloApiKeys.findOne({userId}); - if (apiKey) { - const newApiKey = new ApolloApiKey(key, userId); - apiKey.encryptedKey = newApiKey.encryptedKey; - apiKey.iv = newApiKey.iv; - apiKey.tag = newApiKey.tag; - } else { - apiKey = new ApolloApiKey(key, userId); - DI.em.persist(apiKey); - } + if (!key) { + return res.status(400).json({error: 'API key is required'}); + } - await DI.em.flush(); - await DI.apolloClient.updateApiKey(key, userId); - res.status(200).json({message: 'API key saved successfully'}); - } catch (error) { - logger.error('Failed to save API key', {error}); - res.status(500).json({error: 'Failed to save API key'}); - } -}); + let apiKey = await DI.apolloApiKeys.findOne({userId}); + if (apiKey) { + const newApiKey = new ApolloApiKey(key, userId); + apiKey.encryptedKey = newApiKey.encryptedKey; + apiKey.iv = newApiKey.iv; + apiKey.tag = newApiKey.tag; + } else { + apiKey = new ApolloApiKey(key, userId); + DI.em.persist(apiKey); + } -router.delete('/apollo-api-key', async (req: Request | SessionRequest, res: Response) => { - try { - const userId = await handleRequest(req); - const apiKey = await DI.apolloApiKeys.findOne({userId}); - - if (!apiKey) { - return res.status(404).json({error: 'API key not found'}); + await DI.em.flush(); + await DI.apolloClient.updateApiKey(key, userId); + res.status(200).json({message: 'API key saved successfully'}); + } catch (error) { + logger.error('Failed to save API key', {error}); + res.status(500).json({error: 'Failed to save API key'}); } + } +); - await DI.em.remove(apiKey).flush(); - await DI.apolloClient.updateApiKey('', userId); - res.status(200).json({message: 'API key deleted successfully'}); - } catch (error) { - logger.error('Failed to delete API key', {error}); - res.status(500).json({error: 'Failed to delete API key'}); +router.delete( + '/apollo-api-key', + async (req: Request | SessionRequest, res: Response) => { + try { + const userId = await handleRequest(req); + const apiKey = await DI.apolloApiKeys.findOne({userId}); + + if (!apiKey) { + return res.status(404).json({error: 'API key not found'}); + } + + await DI.em.remove(apiKey).flush(); + await DI.apolloClient.updateApiKey('', userId); + res.status(200).json({message: 'API key deleted successfully'}); + } catch (error) { + logger.error('Failed to delete API key', {error}); + res.status(500).json({error: 'Failed to delete API key'}); + } } -}); +); export default router;