Fix event sorting on /cospend dashboard to match payments view

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 <noreply@anthropic.com>
This commit is contained in:
2025-09-10 12:28:17 +02:00
parent c53300d5a7
commit 701434d532

View File

@@ -55,11 +55,20 @@ export const GET: RequestHandler = async ({ locals, url }) => {
// Calculate net balance: negative = you are owed money, positive = you owe money // Calculate net balance: negative = you are owed money, positive = you owe money
const netBalance = userSplits.reduce((sum, split) => sum + split.amount, 0); const netBalance = userSplits.reduce((sum, split) => sum + split.amount, 0);
const recentSplits = await PaymentSplit.find({ username }) const recentSplits = await PaymentSplit.aggregate([
.populate('paymentId') { $match: { username } },
.sort({ createdAt: -1 }) {
.limit(10) $lookup: {
.lean(); 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 settlements, fetch the other user's split info
for (const split of recentSplits) { for (const split of recentSplits) {