Files
Flowise/packages/server/src/controllers/chatflows/index.ts
T
Henry Heng 19e14c4798 Bugfix/Undefined Flowdata (#2116)
add fix for undefined flowdata
2024-04-07 12:01:57 +01:00

174 lines
6.2 KiB
TypeScript

import { Request, Response, NextFunction } from 'express'
import chatflowsService from '../../services/chatflows'
import { ChatFlow } from '../../database/entities/ChatFlow'
import { createRateLimiter } from '../../utils/rateLimit'
import { getApiKey } from '../../utils/apiKey'
const checkIfChatflowIsValidForStreaming = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params.id === 'undefined' || req.params.id === '') {
throw new Error(`Error: chatflowsRouter.checkIfChatflowIsValidForStreaming - id not provided!`)
}
const apiResponse = await chatflowsService.checkIfChatflowIsValidForStreaming(req.params.id)
if (apiResponse.executionError) {
return res.status(apiResponse.status).send(apiResponse.msg)
}
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
const checkIfChatflowIsValidForUploads = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params.id === 'undefined' || req.params.id === '') {
throw new Error(`Error: chatflowsRouter.checkIfChatflowIsValidForUploads - id not provided!`)
}
const apiResponse = await chatflowsService.checkIfChatflowIsValidForUploads(req.params.id)
if (apiResponse.executionError) {
return res.status(apiResponse.status).send(apiResponse.msg)
}
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
const deleteChatflow = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params.id === 'undefined' || req.params.id === '') {
throw new Error(`Error: chatflowsRouter.deleteChatflow - id not provided!`)
}
const apiResponse = await chatflowsService.deleteChatflow(req.params.id)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
const getAllChatflows = async (req: Request, res: Response, next: NextFunction) => {
try {
const apiResponse = await chatflowsService.getAllChatflows()
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
// Get specific chatflow via api key
const getChatflowByApiKey = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params.apiKey === 'undefined' || req.params.apiKey === '') {
throw new Error(`Error: chatflowsRouter.getChatflowById - apiKey not provided!`)
}
const apiKey = await getApiKey(req.params.apiKey)
if (!apiKey) {
return res.status(401).send('Unauthorized')
}
const apiResponse = await chatflowsService.getChatflowByApiKey(apiKey.id)
if (apiResponse.executionError) {
return res.status(apiResponse.status).send(apiResponse.msg)
}
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
const getChatflowById = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params.id === 'undefined' || req.params.id === '') {
throw new Error(`Error: chatflowsRouter.getChatflowById - id not provided!`)
}
const apiResponse = await chatflowsService.getChatflowById(req.params.id)
if (apiResponse.executionError) {
return res.status(apiResponse.status).send(apiResponse.msg)
}
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
const saveChatflow = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.body === 'undefined' || req.body === '') {
throw new Error(`Error: chatflowsRouter.saveChatflow - body not provided!`)
}
const body = req.body
const newChatFlow = new ChatFlow()
Object.assign(newChatFlow, body)
const apiResponse = await chatflowsService.saveChatflow(newChatFlow)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
const updateChatflow = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params.id === 'undefined' || req.params.id === '') {
throw new Error(`Error: chatflowsRouter.updateChatflow - id not provided!`)
}
const chatflow = await chatflowsService.getChatflowById(req.params.id)
if (!chatflow) {
return res.status(404).send(`Chatflow ${req.params.id} not found`)
}
const body = req.body
const updateChatFlow = new ChatFlow()
Object.assign(updateChatFlow, body)
updateChatFlow.id = chatflow.id
createRateLimiter(updateChatFlow)
const apiResponse = await chatflowsService.updateChatflow(chatflow, updateChatFlow)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
const getSinglePublicChatflow = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params.id === 'undefined' || req.params.id === '') {
throw new Error(`Error: chatflowsRouter.updateChatflow - id not provided!`)
}
const apiResponse = await chatflowsService.getSinglePublicChatflow(req.params.id)
if (apiResponse.executionError) {
return res.status(apiResponse.status).send(apiResponse.msg)
}
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
const getSinglePublicChatbotConfig = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params.id === 'undefined' || req.params.id === '') {
throw new Error(`Error: chatflowsRouter.getSinglePublicChatbotConfig - id not provided!`)
}
const apiResponse = await chatflowsService.getSinglePublicChatbotConfig(req.params.id)
if (apiResponse.executionError) {
return res.status(apiResponse.status).send(apiResponse.msg)
}
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
export default {
checkIfChatflowIsValidForStreaming,
checkIfChatflowIsValidForUploads,
deleteChatflow,
getAllChatflows,
getChatflowByApiKey,
getChatflowById,
saveChatflow,
updateChatflow,
getSinglePublicChatflow,
getSinglePublicChatbotConfig
}