Skip to content

Commit 2034bb7

Browse files
committed
fix: normalize getData errors and fix per-project log aggregation
1 parent dcaf5c6 commit 2034bb7

1 file changed

Lines changed: 34 additions & 15 deletions

File tree

apps/dashboard-api/src/controllers/project.controller.js

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -332,16 +332,34 @@ module.exports.getAllProject = async (req, res) => {
332332

333333
const projectIds = projects.map(p => p._id);
334334
const recentLogs = await Log.aggregate([
335-
{ $match: { projectId: { $in: projectIds } } },
336-
{ $sort: { timestamp: -1 } },
337-
{ $limit: 100 },
338-
{ $group: {
339-
_id: "$projectId",
340-
errorCount: { $sum: { $cond: [{ $gte: ["$status", 400] }, 1, 0] } },
341-
successCount: { $sum: { $cond: [{ $lt: ["$status", 400] }, 1, 0] } }
335+
{ $match: { projectId: { $in: projectIds } } },
336+
{ $sort: { timestamp: -1 } },
337+
{
338+
$group: {
339+
_id: "$projectId",
340+
logs: { $push: { status: "$status" } }
341+
}
342+
},
343+
{
344+
$project: {
345+
logs: { $slice: ["$logs", 100] }
346+
}
347+
},
348+
{
349+
$project: {
350+
errorCount: {
351+
$size: {
352+
$filter: { input: "$logs", as: "l", cond: { $gte: ["$$l.status", 400] } }
353+
}
354+
},
355+
successCount: {
356+
$size: {
357+
$filter: { input: "$logs", as: "l", cond: { $lt: ["$$l.status", 400] } }
342358
}
343359
}
344-
]);
360+
}
361+
}
362+
]);
345363

346364
const logsMap = recentLogs.reduce((acc, log) => {
347365
acc[log._id.toString()] = log;
@@ -763,15 +781,13 @@ module.exports.getData = async (req, res) => {
763781
try {
764782
const { projectId, collectionName } = req.params;
765783
const project = await Project.findOne({ _id: projectId, owner: req.user._id });
766-
if (!project) return res.status(404).json({ error: "Project not found." });
784+
if (!project) return res.status(404).json({ success: false, data: {}, message: "Project not found." });
785+
767786

768787
const collectionConfig = project.collections.find(c => c.name === collectionName);
769788
if (!collectionConfig) {
770-
return res.status(404).json({
771-
error: "Collection not found",
772-
collection: collectionName
773-
});
774-
}
789+
return res.status(404).json({ success: false, data: {}, message: `Collection ${collectionName} not found.` });
790+
}
775791

776792
const connection = await getConnection(projectId);
777793
const model = getCompiledModel(
@@ -850,8 +866,11 @@ module.exports.getData = async (req, res) => {
850866
message: "Data fetched successfully.",
851867
});
852868
} catch (err) {
853-
res.status(500).json({ error: err.message });
869+
if (err && (err.statusCode === 400 || err.name === 'QueryFilterError')) {
870+
return res.status(400).json({ success: false, data: {}, message: err.message || "Invalid query filter." });
854871
}
872+
return res.status(500).json({ success: false, data: {}, message: "Failed to fetch data." });
873+
}
855874
};
856875

857876
module.exports.deleteCollection = async (req, res) => {

0 commit comments

Comments
 (0)