From 701434d532d058c4a84a560b829bbc3ad7912415 Mon Sep 17 00:00:00 2001 From: Alexander Bocken Date: Wed, 10 Sep 2025 12:28:17 +0200 Subject: [PATCH] Fix event sorting on /cospend dashboard to match payments view MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sort recent activity by payment date instead of creation date using MongoDB aggregation pipeline to properly handle populated fields. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/routes/api/cospend/balance/+server.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/routes/api/cospend/balance/+server.ts b/src/routes/api/cospend/balance/+server.ts index 3a41ed0..db558ec 100644 --- a/src/routes/api/cospend/balance/+server.ts +++ b/src/routes/api/cospend/balance/+server.ts @@ -55,11 +55,20 @@ export const GET: RequestHandler = async ({ locals, url }) => { // Calculate net balance: negative = you are owed money, positive = you owe money const netBalance = userSplits.reduce((sum, split) => sum + split.amount, 0); - const recentSplits = await PaymentSplit.find({ username }) - .populate('paymentId') - .sort({ createdAt: -1 }) - .limit(10) - .lean(); + const recentSplits = await PaymentSplit.aggregate([ + { $match: { username } }, + { + $lookup: { + from: 'payments', + localField: 'paymentId', + foreignField: '_id', + as: 'paymentId' + } + }, + { $unwind: '$paymentId' }, + { $sort: { 'paymentId.date': -1 } }, + { $limit: 10 } + ]); // For settlements, fetch the other user's split info for (const split of recentSplits) {