Skip to content

Commit 29ada25

Browse files
chore: ⚡ Improved Convex functions
1 parent b1a3843 commit 29ada25

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

convex/budgets.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export const getAllBudgets = query({
4141
)
4242
.reduce((sum, tx) => sum + tx.amount, 0);
4343

44+
// Calculate the actual remaining amount based on the budget amount and transactions
45+
const remaining = budget.amount - totalSpend + totalIncome;
46+
4447
return {
4548
...budget,
4649
category: {
@@ -49,6 +52,7 @@ export const getAllBudgets = query({
4952
},
5053
spend: totalSpend,
5154
income: totalIncome,
55+
remaining: remaining,
5256
};
5357
});
5458
return budgetWithDetails;
@@ -97,11 +101,25 @@ export const updateBudget = mutation({
97101
throw new ConvexError("Budget not found");
98102
}
99103

100-
// Calculate the spent amount (existingAmount - remaining)
101-
const spent = existingBudget.amount - existingBudget.remaining;
104+
// Get all transactions for this category
105+
const transactions = await ctx.db
106+
.query("transactions")
107+
.withIndex("by_category", (q) =>
108+
q.eq("categoryId", existingBudget.categoryId),
109+
)
110+
.collect();
111+
112+
// Calculate total spend and income
113+
const totalSpend = transactions
114+
.filter((tx) => tx.type === "expense")
115+
.reduce((sum, tx) => sum + tx.amount, 0);
116+
117+
const totalIncome = transactions
118+
.filter((tx) => tx.type === "income")
119+
.reduce((sum, tx) => sum + tx.amount, 0);
102120

103121
// Calculate the new remaining amount
104-
const newRemaining = args.amount - spent;
122+
const newRemaining = args.amount - totalSpend + totalIncome;
105123

106124
await ctx.db.patch(args.id, {
107125
categoryId: args.categoryId,

convex/transactions.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -272,27 +272,28 @@ export const deleteTransaction = mutation({
272272
);
273273
}
274274

275-
const budget = await ctx.db
276-
.query("budgets")
277-
.withIndex("by_category", (q) =>
278-
q.eq("categoryId", existingTransaction.categoryId),
279-
)
280-
.first();
281-
282-
if (!budget) {
283-
throw new ConvexError("No budget found for the selected category");
284-
}
285-
// Adjust the remaining based on the transaction type
286-
let remaining = budget.remaining;
287-
if (existingTransaction.type === "expense") {
288-
remaining += existingTransaction.amount; // Revert the expense
289-
} else if (existingTransaction.type === "income") {
290-
remaining -= existingTransaction.amount; // Revert the income
275+
// If there's a categoryId, try to update the associated budget
276+
if (existingTransaction.categoryId) {
277+
const budget = await ctx.db
278+
.query("budgets")
279+
.withIndex("by_category", (q) =>
280+
q.eq("categoryId", existingTransaction.categoryId),
281+
)
282+
.first();
283+
// Only update budget if one exists
284+
if (budget) {
285+
let remaining = budget.remaining;
286+
if (existingTransaction.type === "expense") {
287+
remaining += existingTransaction.amount; // Revert the expense
288+
} else if (existingTransaction.type === "income") {
289+
remaining -= existingTransaction.amount; // Revert the income
290+
}
291+
await ctx.db.patch(budget._id, { remaining: remaining });
292+
}
291293
}
292294

293295
//Delete the transaction and patch the budget
294296
await ctx.db.delete(args.id);
295-
await ctx.db.patch(budget._id, { remaining: remaining });
296297
},
297298
});
298299

0 commit comments

Comments
 (0)