feat(expenses): add walletId to expense model and implement wallet adjustments in expense operations
This commit is contained in:
@@ -259,6 +259,7 @@ export async function createNonPeriodicExpense(
|
||||
year,
|
||||
month,
|
||||
status: PaymentStatus.PAYED,
|
||||
walletId: data.walletId,
|
||||
};
|
||||
|
||||
if (isUSDC && data.usdcConversionRate) {
|
||||
@@ -325,6 +326,7 @@ export async function payExpense(
|
||||
status: PaymentStatus.PAYED,
|
||||
amountPayed: data.amountPayed,
|
||||
paymentDate,
|
||||
walletId: data.walletId,
|
||||
};
|
||||
|
||||
if (isUSDC && data.usdcConversionRate) {
|
||||
@@ -372,12 +374,29 @@ export async function updateExpense(
|
||||
month,
|
||||
};
|
||||
|
||||
let walletAdjustment: {
|
||||
walletId: number;
|
||||
amount: number;
|
||||
description: string;
|
||||
} | null = null;
|
||||
|
||||
if (data.status === "PENDING") {
|
||||
updateData.status = PaymentStatus.PENDING;
|
||||
updateData.amountPayed = null;
|
||||
updateData.paymentDate = null;
|
||||
updateData.beloPrice = null;
|
||||
updateData.usdcEquivalent = null;
|
||||
|
||||
if (existing.status === PaymentStatus.PAYED && existing.walletId) {
|
||||
const refundAmount = existing.amountPayed
|
||||
? Number(existing.amountPayed)
|
||||
: Number(existing.amount);
|
||||
walletAdjustment = {
|
||||
walletId: existing.walletId,
|
||||
amount: refundAmount,
|
||||
description: "Anulación de gasto",
|
||||
};
|
||||
}
|
||||
} else if (existing.status === PaymentStatus.PAYED) {
|
||||
if (data.amountPayed !== undefined) {
|
||||
updateData.amountPayed = data.amountPayed;
|
||||
@@ -400,6 +419,51 @@ export async function updateExpense(
|
||||
updateData.usdcEquivalent = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (existing.walletId) {
|
||||
const oldPayed = existing.amountPayed
|
||||
? Number(existing.amountPayed)
|
||||
: Number(existing.amount);
|
||||
const newPayed = data.amountPayed !== undefined
|
||||
? Number(data.amountPayed)
|
||||
: Number(data.amount);
|
||||
|
||||
if (newPayed !== oldPayed) {
|
||||
const difference = newPayed - oldPayed;
|
||||
walletAdjustment = {
|
||||
walletId: existing.walletId,
|
||||
amount: -difference,
|
||||
description: "Ajuste por edición de gasto",
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (walletAdjustment) {
|
||||
const wallet = await prisma.wallet.findUnique({
|
||||
where: { id: walletAdjustment.walletId },
|
||||
});
|
||||
if (!wallet) {
|
||||
throw new Error("Billetera no encontrada");
|
||||
}
|
||||
|
||||
const [result] = await prisma.$transaction([
|
||||
prisma.expense.update({ where: { id }, data: updateData }),
|
||||
prisma.wallet.update({
|
||||
where: { id: walletAdjustment.walletId },
|
||||
data: { balance: { increment: walletAdjustment.amount } },
|
||||
}),
|
||||
prisma.walletMovement.create({
|
||||
data: {
|
||||
walletId: walletAdjustment.walletId,
|
||||
type: walletAdjustment.amount > 0 ? "DEPOSIT" : "WITHDRAWAL",
|
||||
amount: walletAdjustment.amount,
|
||||
description: walletAdjustment.description,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
cache.invalidateByPrefix("expenses:");
|
||||
return result;
|
||||
}
|
||||
|
||||
const result = await prisma.expense.update({
|
||||
|
||||
Reference in New Issue
Block a user