This commit is contained in:
2025-04-18 11:39:03 +02:00
parent 62f1689c1a
commit 76f1685c71
23 changed files with 1286 additions and 157 deletions
+22
View File
@@ -1,6 +1,7 @@
import type { SplitType } from "@/lib/store/expense-store";
import type { z } from "zod";
import { expenseSplitSchema } from "../validations/expense";
import type { ExpenseSplit } from "@/server/db/schema";
const debtSchema = expenseSplitSchema.pick({
owedFromId: true,
@@ -118,3 +119,24 @@ export function calculateDepts(
return debts;
}
export const calculateTotalValue: (
userId: string,
splits: Array<ExpenseSplit>
) => {
owed: number;
owes: number;
} = (userId, splits) => {
let owed = 0;
let owes = 0;
splits.forEach((split) => {
if (split.owedToId === userId) {
owes += Number(split.amount); // You are the debitor
} else if (split.owedFromId === userId) {
owed += Number(split.amount); // You are the creditor
}
});
return { owed, owes };
};