fix: fixing prompt endpoint
This commit is contained in:
@ -1,11 +1,11 @@
|
|||||||
const Prompt = require('../../models/Prompt');
|
const Prompt = require('../../models/Prompt');
|
||||||
const ErrorLog = require('../../models/Error');
|
const ErrorLog = require('../../models/Error');
|
||||||
|
|
||||||
async function fetchPrompts(limit = 100) {
|
async function getLatestPrompts(limit = 100) {
|
||||||
return Prompt.getLatest(limit);
|
return Prompt.getPrompts({ limit });
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchErrors(limit = 100) {
|
async function getLatestErrors(limit = 100) {
|
||||||
return ErrorLog.getLatest(limit);
|
return ErrorLog.getLatest(limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,9 +25,9 @@ async function deleteAllErrors() {
|
|||||||
return ErrorLog.destroy({ where: {}, truncate: true });
|
return ErrorLog.destroy({ where: {}, truncate: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
fetchPrompts,
|
getLatestPrompts,
|
||||||
fetchErrors,
|
getLatestErrors,
|
||||||
deletePromptById,
|
deletePromptById,
|
||||||
deleteErrorById,
|
deleteErrorById,
|
||||||
deleteAllPrompts,
|
deleteAllPrompts,
|
||||||
|
@ -1,71 +1,117 @@
|
|||||||
const {
|
const {
|
||||||
fetchPrompts,
|
getLatestPrompts,
|
||||||
fetchErrors,
|
getLatestErrors,
|
||||||
deletePromptById,
|
deletePromptById,
|
||||||
deleteErrorById,
|
deleteErrorById,
|
||||||
deleteAllPrompts,
|
deleteAllPrompts,
|
||||||
deleteAllErrors
|
deleteAllErrors
|
||||||
} = require('../controller/logs');
|
} = require('../controller/logs');
|
||||||
|
|
||||||
async function getLatestPrompts(req, res) {
|
async function getPrompts(req, res) {
|
||||||
try {
|
try {
|
||||||
const limit = parseInt(req.query.limit) || 100;
|
const limit = parseInt(req.query.limit) || 100;
|
||||||
const prompts = await fetchPrompts(limit);
|
const { rows: prompts, count } = await getLatestPrompts(limit);
|
||||||
res.json({ success: true, data: prompts });
|
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
data: prompts,
|
||||||
|
total: count,
|
||||||
|
limit
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ success: false, error: 'Internal Server Error', message: error.message });
|
console.error('Error fetching prompts:', error);
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Failed to fetch prompts',
|
||||||
|
message: error.message
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getLatestErrors(req, res) {
|
async function getErrors(req, res) {
|
||||||
try {
|
try {
|
||||||
const limit = parseInt(req.query.limit) || 100;
|
const limit = parseInt(req.query.limit) || 100;
|
||||||
const errors = await fetchErrors(limit);
|
const { rows: errors, count } = await getLatestErrors(limit);
|
||||||
res.json({ success: true, data: errors });
|
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
data: errors,
|
||||||
|
total: count,
|
||||||
|
limit
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ success: false, error: 'Internal Server Error', message: error.message });
|
console.error('Error fetching errors:', error);
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Failed to fetch errors',
|
||||||
|
message: error.message
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deletePrompt(req, res) {
|
async function deletePrompt(req, res) {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|
||||||
if (id === 'all') {
|
if (id === 'all') {
|
||||||
await deleteAllPrompts();
|
await deleteAllPrompts();
|
||||||
res.json({ success: true, message: 'All prompts deleted successfully' });
|
res.json({ success: true, message: 'All prompts deleted successfully' });
|
||||||
} else {
|
return;
|
||||||
const deleted = await deletePromptById(id);
|
|
||||||
if (!deleted) {
|
|
||||||
return res.status(404).json({ success: false, error: 'Not Found', message: 'Prompt not found' });
|
|
||||||
}
|
|
||||||
res.json({ success: true, message: 'Prompt deleted successfully' });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const deleted = await deletePromptById(id);
|
||||||
|
if (!deleted) {
|
||||||
|
res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Prompt not found'
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json({ success: true, message: 'Prompt deleted successfully' });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ success: false, error: 'Internal Server Error', message: error.message });
|
console.error('Error deleting prompt:', error);
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Failed to delete prompt',
|
||||||
|
message: error.message
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteError(req, res) {
|
async function deleteError(req, res) {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|
||||||
if (id === 'all') {
|
if (id === 'all') {
|
||||||
await deleteAllErrors();
|
await deleteAllErrors();
|
||||||
res.json({ success: true, message: 'All errors deleted successfully' });
|
res.json({ success: true, message: 'All errors deleted successfully' });
|
||||||
} else {
|
return;
|
||||||
const deleted = await deleteErrorById(id);
|
|
||||||
if (!deleted) {
|
|
||||||
return res.status(404).json({ success: false, error: 'Not Found', message: 'Error log not found' });
|
|
||||||
}
|
|
||||||
res.json({ success: true, message: 'Error log deleted successfully' });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const deleted = await deleteErrorById(id);
|
||||||
|
if (!deleted) {
|
||||||
|
res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Error log not found'
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json({ success: true, message: 'Error log deleted successfully' });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ success: false, error: 'Internal Server Error', message: error.message });
|
console.error('Error deleting error log:', error);
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Failed to delete error log',
|
||||||
|
message: error.message
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getLatestPrompts,
|
getPrompts,
|
||||||
getLatestErrors,
|
getErrors,
|
||||||
deletePrompt,
|
deletePrompt,
|
||||||
deleteError
|
deleteError
|
||||||
};
|
};
|
@ -1,9 +1,9 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const { validateApiKey } = require('../middleware/auth');
|
const { validateApiKey } = require('../middleware/auth');
|
||||||
const {
|
const {
|
||||||
getLatestPrompts,
|
getPrompts,
|
||||||
getLatestErrors,
|
getErrors,
|
||||||
deletePrompt,
|
deletePrompt,
|
||||||
deleteError
|
deleteError
|
||||||
} = require('../api/network/logs');
|
} = require('../api/network/logs');
|
||||||
@ -11,16 +11,16 @@ const {
|
|||||||
// Apply API key validation to all routes
|
// Apply API key validation to all routes
|
||||||
router.use(validateApiKey);
|
router.use(validateApiKey);
|
||||||
|
|
||||||
// Get latest prompts
|
// Get latest prompts with optional limit
|
||||||
router.get('/prompts', getLatestPrompts);
|
router.get('/prompts', getPrompts);
|
||||||
|
|
||||||
// Get latest error logs
|
// Get latest errors with optional limit
|
||||||
router.get('/errors', getLatestErrors);
|
router.get('/errors', getErrors);
|
||||||
|
|
||||||
// Delete a specific prompt or all prompts
|
// Delete a specific prompt or all prompts
|
||||||
router.delete('/prompts/:id', deletePrompt);
|
router.delete('/prompts/:id', deletePrompt);
|
||||||
|
|
||||||
// Delete a specific error log or all error logs
|
// Delete a specific error or all errors
|
||||||
router.delete('/errors/:id', deleteError);
|
router.delete('/errors/:id', deleteError);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
Reference in New Issue
Block a user