API Keys: Displaying the names of the chatflows associated with the keys and Warning the user before deletion.

This commit is contained in:
vinodkiran
2023-11-18 16:47:15 +05:30
parent 34702a9ba2
commit 28f5d94c13
2 changed files with 104 additions and 64 deletions
+26 -4
View File
@@ -1135,28 +1135,50 @@ export class App {
// API Keys // API Keys
// ---------------------------------------- // ----------------------------------------
const addChatflowsCount = async (keys: any, res: Response) => {
if (keys) {
const updatedKeys: any[] = []
//iterate through keys and get chatflows
for (const key of keys) {
const chatflows = await this.AppDataSource.getRepository(ChatFlow)
.createQueryBuilder('cf')
.where('cf.apikeyid = :apikeyid', { apikeyid: key.id })
.getMany()
const linkedChatFlows: any[] = []
chatflows.map((cf) => {
linkedChatFlows.push({
flowName: cf.name
})
})
key.chatFlows = linkedChatFlows
updatedKeys.push(key)
}
return res.json(updatedKeys)
}
return res.json(keys)
}
// Get api keys // Get api keys
this.app.get('/api/v1/apikey', async (req: Request, res: Response) => { this.app.get('/api/v1/apikey', async (req: Request, res: Response) => {
const keys = await getAPIKeys() const keys = await getAPIKeys()
return res.json(keys) return addChatflowsCount(keys, res)
}) })
// Add new api key // Add new api key
this.app.post('/api/v1/apikey', async (req: Request, res: Response) => { this.app.post('/api/v1/apikey', async (req: Request, res: Response) => {
const keys = await addAPIKey(req.body.keyName) const keys = await addAPIKey(req.body.keyName)
return res.json(keys) return addChatflowsCount(keys, res)
}) })
// Update api key // Update api key
this.app.put('/api/v1/apikey/:id', async (req: Request, res: Response) => { this.app.put('/api/v1/apikey/:id', async (req: Request, res: Response) => {
const keys = await updateAPIKey(req.params.id, req.body.keyName) const keys = await updateAPIKey(req.params.id, req.body.keyName)
return res.json(keys) return addChatflowsCount(keys, res)
}) })
// Delete new api key // Delete new api key
this.app.delete('/api/v1/apikey/:id', async (req: Request, res: Response) => { this.app.delete('/api/v1/apikey/:id', async (req: Request, res: Response) => {
const keys = await deleteAPIKey(req.params.id) const keys = await deleteAPIKey(req.params.id)
return res.json(keys) return addChatflowsCount(keys, res)
}) })
// Verify api key // Verify api key
+20 -2
View File
@@ -6,6 +6,7 @@ import { enqueueSnackbar as enqueueSnackbarAction, closeSnackbar as closeSnackba
import { import {
Button, Button,
Box, Box,
Chip,
Stack, Stack,
Table, Table,
TableBody, TableBody,
@@ -37,7 +38,7 @@ import useConfirm from 'hooks/useConfirm'
import useNotifier from 'utils/useNotifier' import useNotifier from 'utils/useNotifier'
// Icons // Icons
import { IconTrash, IconEdit, IconCopy, IconX, IconPlus, IconEye, IconEyeOff } from '@tabler/icons' import { IconTrash, IconEdit, IconCopy, IconCornerDownRight, IconX, IconPlus, IconEye, IconEyeOff } from '@tabler/icons'
import APIEmptySVG from 'assets/images/api_empty.svg' import APIEmptySVG from 'assets/images/api_empty.svg'
// ==============================|| APIKey ||============================== // // ==============================|| APIKey ||============================== //
@@ -106,7 +107,10 @@ const APIKey = () => {
const deleteKey = async (key) => { const deleteKey = async (key) => {
const confirmPayload = { const confirmPayload = {
title: `Delete`, title: `Delete`,
description: `Delete key ${key.keyName}?`, description:
key.chatFlows.length === 0
? `Delete key [${key.keyName}] ? `
: `Delete key [${key.keyName}] ?\n There are ${key.chatFlows.length} chatflows using this key.`,
confirmButtonName: 'Delete', confirmButtonName: 'Delete',
cancelButtonName: 'Cancel' cancelButtonName: 'Cancel'
} }
@@ -193,6 +197,7 @@ const APIKey = () => {
<TableRow> <TableRow>
<TableCell>Key Name</TableCell> <TableCell>Key Name</TableCell>
<TableCell>API Key</TableCell> <TableCell>API Key</TableCell>
<TableCell>Usage</TableCell>
<TableCell>Created</TableCell> <TableCell>Created</TableCell>
<TableCell> </TableCell> <TableCell> </TableCell>
<TableCell> </TableCell> <TableCell> </TableCell>
@@ -200,6 +205,7 @@ const APIKey = () => {
</TableHead> </TableHead>
<TableBody> <TableBody>
{apiKeys.map((key, index) => ( {apiKeys.map((key, index) => (
<>
<TableRow key={index} sx={{ '&:last-child td, &:last-child th': { border: 0 } }}> <TableRow key={index} sx={{ '&:last-child td, &:last-child th': { border: 0 } }}>
<TableCell component='th' scope='row'> <TableCell component='th' scope='row'>
{key.keyName} {key.keyName}
@@ -247,6 +253,7 @@ const APIKey = () => {
</Typography> </Typography>
</Popover> </Popover>
</TableCell> </TableCell>
<TableCell>{key.chatFlows.length}</TableCell>
<TableCell>{key.createdAt}</TableCell> <TableCell>{key.createdAt}</TableCell>
<TableCell> <TableCell>
<IconButton title='Edit' color='primary' onClick={() => edit(key)}> <IconButton title='Edit' color='primary' onClick={() => edit(key)}>
@@ -259,6 +266,17 @@ const APIKey = () => {
</IconButton> </IconButton>
</TableCell> </TableCell>
</TableRow> </TableRow>
{key.chatFlows.length > 0 && (
<TableRow sx={{ verticalAlign: 'middle' }}>
<TableCell colSpan={6}>
<IconCornerDownRight />{' '}
{key.chatFlows.map((flow, index) => (
<Chip key={index} label={flow.flowName} />
))}
</TableCell>
</TableRow>
)}
</>
))} ))}
</TableBody> </TableBody>
</Table> </Table>