From 8e143b45d5ef9326b2ce4c871784a1c8b1496399 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 4 Feb 2025 15:04:42 -0800 Subject: [PATCH] preserver user selected order --- Server/db/mongo/modules/statusPageModule.js | 31 +++++++++++++-------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/Server/db/mongo/modules/statusPageModule.js b/Server/db/mongo/modules/statusPageModule.js index 3e647190b..61a1877d7 100644 --- a/Server/db/mongo/modules/statusPageModule.js +++ b/Server/db/mongo/modules/statusPageModule.js @@ -29,19 +29,13 @@ const createStatusPage = async (statusPageData, image) => { const getStatusPage = async () => { try { - // const statusPage = await StatusPage.findOne(); - // if (statusPage === null || statusPage === undefined) { - // const error = new Error(errorMessages.STATUS_PAGE_NOT_FOUND); - // error.status = 404; - - // throw error; - // } - // const ids = statusPage.monitors; - // const monitors = await Monitor.find({ _id: { $in: ids } }).lean(); - // return { statusPage, monitors }; - const statusPageQuery = await StatusPage.aggregate([ { $limit: 1 }, + { + $set: { + originalMonitors: "$monitors", + }, + }, { $lookup: { from: "monitors", @@ -69,6 +63,13 @@ const getStatusPage = async () => { as: "monitors.checks", }, }, + { + $addFields: { + "monitors.orderIndex": { + $indexOfArray: ["$originalMonitors", "$monitors._id"], + }, + }, + }, { $group: { _id: "$_id", @@ -79,7 +80,12 @@ const getStatusPage = async () => { { $project: { statusPage: 1, - monitors: 1, + monitors: { + $sortArray: { + input: "$monitors", + sortBy: { orderIndex: 1 }, + }, + }, }, }, ]); @@ -90,6 +96,7 @@ const getStatusPage = async () => { } const { statusPage, monitors } = statusPageQuery[0]; + const normalizedMonitors = monitors.map((monitor) => { return { ...monitor,