preserver user selected order

This commit is contained in:
Alex Holliday
2025-02-04 15:04:42 -08:00
parent 1bc27789f3
commit 8e143b45d5

View File

@@ -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,