Skip to content

Commit 2adf349

Browse files
committed
Sort all emails by date in descending order
1 parent 2feb44e commit 2adf349

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

mailserver/emailRoutes.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,35 @@ router.get("/all-emails", async (req, res) => {
8080
try {
8181
const db = getDB();
8282
const collections = await db.listCollections().toArray();
83-
const allEmails = [];
83+
let allEmails = [];
8484

8585
for (const collection of collections) {
8686
const emails = await db
8787
.collection(collection.name)
88-
.find({}, { projection: { "from.text": 1, subject: 1, "to.value.address": 1, "from.value.address": 1, date: 1, readStatus: 1 } })
89-
.sort({ date: -1 }) // Sort by date in descending order
88+
.find(
89+
{},
90+
{
91+
projection: {
92+
"from.text": 1,
93+
subject: 1,
94+
"to.value.address": 1,
95+
"from.value.address": 1,
96+
date: 1,
97+
readStatus: 1,
98+
},
99+
}
100+
)
90101
.toArray();
91102
allEmails.push(...emails);
92103
}
93104

105+
// Sort all emails by date in descending order
106+
allEmails.sort((a, b) => {
107+
const dateA = a.date ? new Date(a.date) : new Date(0);
108+
const dateB = b.date ? new Date(b.date) : new Date(0);
109+
return dateB - dateA;
110+
});
111+
94112
if (allEmails.length === 0) {
95113
return res.status(404).json({ message: "No emails found in the database" });
96114
}

0 commit comments

Comments
 (0)