@@ -19,6 +19,7 @@ import { type CurrencyCode, isCurrencyCode } from '~/lib/currency';
1919import { SplitType } from '@prisma/client' ;
2020import { DEFAULT_CATEGORY } from '~/lib/category' ;
2121import { getUserMap } from './user' ;
22+ import { FriendBalance } from '~/components/Friend/FriendBalance' ;
2223
2324export const expenseRouter = createTRPCRouter ( {
2425 getCumulatedBalances : protectedProcedure . query ( async ( { ctx } ) => {
@@ -76,6 +77,10 @@ export const expenseRouter = createTRPCRouter({
7677 } ) ,
7778 ) ;
7879
80+ // Group by friendId and fetch user details
81+ const friendIds = [ ...new Set ( [ ...processedBalances ] . map ( ( b ) => b . friendId ) ) ] ;
82+ const userMap = await getUserMap ( friendIds ) ;
83+
7984 // Aggregate by (friendId, currency) since same pair can appear in multiple groups
8085 const aggregated = processedBalances
8186 . filter ( ( b ) => 0n !== b . amount )
@@ -93,10 +98,6 @@ export const expenseRouter = createTRPCRouter({
9398 new Map ( ) ,
9499 ) ;
95100
96- // Group by friendId and fetch user details
97- const friendIds = [ ...new Set ( [ ...aggregated . values ( ) ] . map ( ( b ) => b . friendId ) ) ] ;
98- const userMap = await getUserMap ( friendIds ) ;
99-
100101 const balancesByFriend = [ ...aggregated . values ( ) ] . reduce <
101102 Record < number , { currency : string ; amount : bigint } [ ] >
102103 > ( ( acc , { friendId, currency, amount } ) => {
@@ -107,6 +108,12 @@ export const expenseRouter = createTRPCRouter({
107108 return acc ;
108109 } , { } ) ;
109110
111+ friendIds . forEach ( ( friendId ) => {
112+ if ( ! balancesByFriend [ friendId ] ) {
113+ balancesByFriend [ friendId ] = [ ] ;
114+ }
115+ } ) ;
116+
110117 const balances = Object . entries ( balancesByFriend )
111118 . map ( ( [ friendId , currencies ] ) => ( {
112119 friendId : Number ( friendId ) ,
0 commit comments