@@ -4,8 +4,7 @@ import { createExpense, deleteExpense, editExpense } from '~/server/api/services
44import { dummyData } from '~/dummies' ;
55import { calculateParticipantSplit } from '~/store/addStore' ;
66import assert from 'node:assert' ;
7- import { BigMath } from '~/utils/numbers' ;
8- import { DEFAULT_CATEGORY } from '~/lib/category' ;
7+ import { settleBalances } from './seedSettlement' ;
98
109const prisma = new PrismaClient ( ) ;
1110
@@ -57,6 +56,15 @@ async function createExpenses() {
5756 expense . addedBy ,
5857 ) ;
5958
59+ await prisma . expense . update ( {
60+ where : {
61+ id : res ! . id ,
62+ } ,
63+ data : {
64+ createdAt : expense . createdAt ,
65+ } ,
66+ } ) ;
67+
6068 idLookup . set ( idx , res ! . id ) ;
6169 } ) ,
6270 ) ;
@@ -103,52 +111,6 @@ async function deleteExpenses() {
103111 return prisma . expense . findMany ( { include : { expenseParticipants : true } } ) ;
104112}
105113
106- async function settleBalances ( ) {
107- const groupBalances = await prisma . balanceView . findMany ( ) ;
108- const groupBalanceMap = Object . fromEntries (
109- groupBalances . map ( ( gb ) => [ `${ gb . userId } -${ gb . friendId } -${ gb . groupId } -${ gb . currency } ` , gb ] ) ,
110- ) ;
111-
112- await Promise . all (
113- dummyData . balancesToSettle . map ( async ( [ userId , friendId , groupId , currency ] ) => {
114- const groupBalance = groupBalanceMap [ `${ userId } -${ friendId } -${ groupId } -${ currency } ` ] ;
115-
116- if ( ! groupBalance || groupBalance . amount === 0n ) {
117- console . warn ( `No balance to settle for ${ userId } , ${ friendId } , ${ groupId } , ${ currency } ` ) ;
118- return ;
119- }
120-
121- const sender = 0n > groupBalance . amount ? friendId : userId ;
122- const receiver = 0n > groupBalance . amount ? userId : friendId ;
123-
124- await createExpense (
125- {
126- name : 'Settle up' ,
127- amount : BigMath . abs ( groupBalance . amount ) ,
128- currency,
129- splitType : SplitType . SETTLEMENT ,
130- groupId,
131- participants : [
132- {
133- userId : sender ,
134- amount : groupBalance . amount ,
135- } ,
136- {
137- userId : receiver ,
138- amount : - groupBalance . amount ,
139- } ,
140- ] ,
141- paidBy : sender ,
142- category : DEFAULT_CATEGORY ,
143- } ,
144- sender ,
145- ) ;
146- } ) ,
147- ) ;
148-
149- console . log ( 'Finished settling balances' ) ;
150- }
151-
152114async function main ( ) {
153115 // await prisma.user.deleteMany();
154116 // await prisma.expense.deleteMany();
@@ -161,7 +123,7 @@ async function main() {
161123 await createExpenses ( ) ;
162124 await editExpenses ( ) ;
163125 await deleteExpenses ( ) ;
164- await settleBalances ( ) ;
126+ await settleBalances ( prisma , dummyData . balancesToSettle ) ;
165127}
166128
167129main ( )
0 commit comments