Files
Flowise/packages/server/src/controllers/feedback/index.ts
T
Mehdi 54d1b5e3bb feat(feedback): add validation for feedback creation and update (#4260)
* feat(feedback): add validation for feedback creation and update

Introduced validation functions for feedback creation and update in the new validation service.
Updated feedback controller to utilize these validation functions before processing requests.

* refactor(feedback): update validation to return feedback object

- Modified validateFeedbackExists to return the ChatMessageFeedback object instead of a boolean.
- Updated validateFeedbackForUpdate to utilize the returned feedback object for setting default values.
2025-04-15 02:38:02 +08:00

70 lines
2.8 KiB
TypeScript

import { Request, Response, NextFunction } from 'express'
import feedbackService from '../../services/feedback'
import { validateFeedbackForCreation, validateFeedbackForUpdate } from '../../services/feedback/validation'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { StatusCodes } from 'http-status-codes'
const getAllChatMessageFeedback = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params === 'undefined' || !req.params.id) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: feedbackController.getAllChatMessageFeedback - id not provided!`
)
}
const chatflowid = req.params.id
const chatId = req.query?.chatId as string | undefined
const sortOrder = req.query?.order as string | undefined
const startDate = req.query?.startDate as string | undefined
const endDate = req.query?.endDate as string | undefined
const apiResponse = await feedbackService.getAllChatMessageFeedback(chatflowid, chatId, sortOrder, startDate, endDate)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
const createChatMessageFeedbackForChatflow = async (req: Request, res: Response, next: NextFunction) => {
try {
if (!req.body) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: feedbackController.createChatMessageFeedbackForChatflow - body not provided!`
)
}
await validateFeedbackForCreation(req.body)
const apiResponse = await feedbackService.createChatMessageFeedbackForChatflow(req.body)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
const updateChatMessageFeedbackForChatflow = async (req: Request, res: Response, next: NextFunction) => {
try {
if (!req.body) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: feedbackController.updateChatMessageFeedbackForChatflow - body not provided!`
)
}
if (typeof req.params === 'undefined' || !req.params.id) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: feedbackController.updateChatMessageFeedbackForChatflow - id not provided!`
)
}
await validateFeedbackForUpdate(req.params.id, req.body)
const apiResponse = await feedbackService.updateChatMessageFeedbackForChatflow(req.params.id, req.body)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
export default {
getAllChatMessageFeedback,
createChatMessageFeedbackForChatflow,
updateChatMessageFeedbackForChatflow
}