@@ -6,7 +6,7 @@ import { simplifyDebts } from '~/lib/simplify';
66import { createTRPCRouter , groupProcedure , protectedProcedure } from '~/server/api/trpc' ;
77
88import { recalculateGroupBalances } from '../services/splitService' ;
9- import { getGroupBalances } from '../services/balanceService' ;
9+ import { assertBalancesMatch , getGroupBalances } from '../services/balanceService' ;
1010
1111export const groupRouter = createTRPCRouter ( {
1212 create : protectedProcedure
@@ -66,6 +66,10 @@ export const groupRouter = createTRPCRouter({
6666 groupBalances : {
6767 where : { userId : ctx . session . user . id } ,
6868 } ,
69+ groupBalanceViews : {
70+ where : { userId : ctx . session . user . id , NOT : { groupId : null } } ,
71+ } ,
72+ // We can sort by group balance view instead
6973 expenses : {
7074 orderBy : {
7175 createdAt : 'desc' ,
@@ -77,6 +81,12 @@ export const groupRouter = createTRPCRouter({
7781 } ,
7882 } ) ;
7983
84+ assertBalancesMatch (
85+ groups . flatMap ( ( g ) => g . group . groupBalances ) ,
86+ groups . flatMap ( ( g ) => g . group . groupBalanceViews ) ,
87+ 'getAllGroupsWithBalances' ,
88+ ) ;
89+
8090 const sortedGroupsByLatestExpense = groups . sort ( ( a , b ) => {
8191 const aDate = a . group . expenses [ 0 ] ?. createdAt ?? new Date ( 0 ) ;
8292 const bDate = b . group . expenses [ 0 ] ?. createdAt ?? new Date ( 0 ) ;
@@ -134,9 +144,16 @@ export const groupRouter = createTRPCRouter({
134144 } ,
135145 } ,
136146 groupBalances : true ,
147+ groupBalanceViews : true ,
137148 } ,
138149 } ) ;
139150
151+ assertBalancesMatch (
152+ group ?. groupBalances || [ ] ,
153+ group ?. groupBalanceViews || [ ] ,
154+ 'getGroupDetails' ,
155+ ) ;
156+
140157 if ( group ?. simplifyDebts ) {
141158 group . groupBalances = simplifyDebts ( group . groupBalances ) ;
142159 }
@@ -259,6 +276,7 @@ export const groupRouter = createTRPCRouter({
259276
260277 const groupBalances = await getGroupBalances ( input . groupId ) ;
261278
279+ // @ts -ignore This will be resolved once we move away from balance tables
262280 const finalGroupBalances = group . simplifyDebts ? simplifyDebts ( groupBalances ) : groupBalances ;
263281
264282 if ( finalGroupBalances . some ( ( b ) => b . userId === userId && 0n !== b . amount ) ) {
@@ -318,9 +336,16 @@ export const groupRouter = createTRPCRouter({
318336 } ,
319337 include : {
320338 groupBalances : true ,
339+ groupBalanceViews : true ,
321340 } ,
322341 } ) ;
323342
343+ assertBalancesMatch (
344+ group ?. groupBalances || [ ] ,
345+ group ?. groupBalanceViews || [ ] ,
346+ 'toggleArchive' ,
347+ ) ;
348+
324349 if ( ! group ) {
325350 throw new TRPCError ( { code : 'NOT_FOUND' , message : 'Group not found' } ) ;
326351 }
@@ -380,9 +405,16 @@ export const groupRouter = createTRPCRouter({
380405 } ,
381406 include : {
382407 groupBalances : true ,
408+ groupBalanceViews : true ,
383409 } ,
384410 } ) ;
385411
412+ assertBalancesMatch (
413+ group ?. groupBalances || [ ] ,
414+ group ?. groupBalanceViews || [ ] ,
415+ 'deleteGroup' ,
416+ ) ;
417+
386418 if ( group ?. userId !== ctx . session . user . id ) {
387419 throw new TRPCError ( { code : 'UNAUTHORIZED' , message : 'Only creator can delete the group' } ) ;
388420 }
0 commit comments