fix: fixing prompt endpoint

This commit is contained in:
Carlos
2025-05-24 16:18:49 -04:00
parent 583f6b2c9d
commit f001c55da7
3 changed files with 88 additions and 42 deletions

View File

@ -1,11 +1,11 @@
const Prompt = require('../../models/Prompt');
const ErrorLog = require('../../models/Error');
async function fetchPrompts(limit = 100) {
return Prompt.getLatest(limit);
async function getLatestPrompts(limit = 100) {
return Prompt.getPrompts({ limit });
}
async function fetchErrors(limit = 100) {
async function getLatestErrors(limit = 100) {
return ErrorLog.getLatest(limit);
}
@ -25,9 +25,9 @@ async function deleteAllErrors() {
return ErrorLog.destroy({ where: {}, truncate: true });
}
module.exports = {
fetchPrompts,
fetchErrors,
module.exports = {
getLatestPrompts,
getLatestErrors,
deletePromptById,
deleteErrorById,
deleteAllPrompts,

View File

@ -1,71 +1,117 @@
const {
fetchPrompts,
fetchErrors,
const {
getLatestPrompts,
getLatestErrors,
deletePromptById,
deleteErrorById,
deleteAllPrompts,
deleteAllErrors
} = require('../controller/logs');
async function getLatestPrompts(req, res) {
async function getPrompts(req, res) {
try {
const limit = parseInt(req.query.limit) || 100;
const prompts = await fetchPrompts(limit);
res.json({ success: true, data: prompts });
const { rows: prompts, count } = await getLatestPrompts(limit);
res.json({
success: true,
data: prompts,
total: count,
limit
});
} 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 {
const limit = parseInt(req.query.limit) || 100;
const errors = await fetchErrors(limit);
res.json({ success: true, data: errors });
const { rows: errors, count } = await getLatestErrors(limit);
res.json({
success: true,
data: errors,
total: count,
limit
});
} 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) {
try {
const { id } = req.params;
if (id === 'all') {
await deleteAllPrompts();
res.json({ success: true, message: 'All prompts deleted successfully' });
} else {
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' });
return;
}
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) {
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) {
try {
const { id } = req.params;
if (id === 'all') {
await deleteAllErrors();
res.json({ success: true, message: 'All errors deleted successfully' });
} else {
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' });
return;
}
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) {
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 = {
getLatestPrompts,
getLatestErrors,
module.exports = {
getPrompts,
getErrors,
deletePrompt,
deleteError
};

View File

@ -1,9 +1,9 @@
const express = require('express');
const router = express.Router();
const { validateApiKey } = require('../middleware/auth');
const {
getLatestPrompts,
getLatestErrors,
const {
getPrompts,
getErrors,
deletePrompt,
deleteError
} = require('../api/network/logs');
@ -11,16 +11,16 @@ const {
// Apply API key validation to all routes
router.use(validateApiKey);
// Get latest prompts
router.get('/prompts', getLatestPrompts);
// Get latest prompts with optional limit
router.get('/prompts', getPrompts);
// Get latest error logs
router.get('/errors', getLatestErrors);
// Get latest errors with optional limit
router.get('/errors', getErrors);
// Delete a specific prompt or all prompts
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);
module.exports = router;