Skip to content

Open source personal finance application

fix(convex): clear stale fields on trn type change to prevent data loss

Ilya Komichev6115a351d521
diff --git a/convex/trns.ts b/convex/trns.ts
index 76d4469e..a84da7d8 100644
--- a/convex/trns.ts
+++ b/convex/trns.ts
@@ -162,7 +162,26 @@ export const update = mutation({
const user = await requireAuthUser(ctx)
const trn = await getOwnEntity(ctx, id, user._id)
await validateTrnFields(ctx, { ...trn, ...args }, user._id)
- await ctx.db.patch(id, { ...args, updatedAt: Date.now() })
+
+ const patch: Record<string, any> = { ...args, updatedAt: Date.now() }
+
+ // Clear stale fields when type changes to prevent orphaned index entries
+ if (args.type !== undefined && args.type !== trn.type) {
+ if (args.type === TrnType.Transfer) {
+ patch.walletId = undefined
+ patch.amount = undefined
+ if (!args.categoryId)
+ patch.categoryId = 'transfer'
+ }
+ else {
+ patch.expenseWalletId = undefined
+ patch.incomeWalletId = undefined
+ patch.expenseAmount = undefined
+ patch.incomeAmount = undefined
+ }
+ }
+
+ await ctx.db.patch(id, patch)
},
})