diff --git a/web-app/app/lib/actions/billActions.ts b/web-app/app/lib/actions/billActions.ts index fd405bc..ef7ee0b 100644 --- a/web-app/app/lib/actions/billActions.ts +++ b/web-app/app/lib/actions/billActions.ts @@ -99,14 +99,23 @@ const shouldUpdateBillFwdStatusWhenAttached = ( return false; } - // Check billFwdStatus is null or "failed" - if (location.billFwdStatus !== null && location.billFwdStatus !== "failed") { + // Check bills have already been sent or are pending -> don't sent them again + if (location.billFwdStatus === "pending" || location.billFwdStatus === "sent") { return false; } // Check if ALL other bills have attachments const otherBills = location.bills.filter(bill => bill._id !== currentBillId); - const allOtherBillsHaveAttachments = otherBills.every(bill => bill.attachment !== null); + // filter only bills billed to tenant + // because bills billed to landlord should not trigger forwarding + const billsPayedByTenant = otherBills.filter(bill => bill.billedTo === BilledTo.Tenant); + + if(billsPayedByTenant.length === 0) { + // No other bills billed to tenant exist, so do not trigger forwarding + return false; + } + + const allOtherBillsHaveAttachments = billsPayedByTenant.every(bill => bill.attachment !== null); return allOtherBillsHaveAttachments; }; @@ -138,14 +147,23 @@ const shouldUpdateBillFwdStatusWhenPayed = ( return false; } - // Check billFwdStatus is null or "failed" - if (location.billFwdStatus !== null && location.billFwdStatus !== "failed") { + // Check bills have already been sent or are pending -> don't sent them again + if (location.billFwdStatus === "pending" || location.billFwdStatus === "sent") { return false; } // Check if ALL other bills are paid const otherBills = location.bills.filter(bill => bill._id !== currentBillId); - const allOtherBillsPaid = otherBills.every(bill => bill.paid === true); + // filter only bills billed to tenant + // because bills billed to landlord should not trigger forwarding + const billsPayedByTenant = otherBills.filter(bill => bill.billedTo === BilledTo.Tenant); + + if(billsPayedByTenant.length === 0) { + // No other bills billed to tenant exist, so do not trigger forwarding + return false; + } + + const allOtherBillsPaid = billsPayedByTenant.every(bill => bill.paid === true); return allOtherBillsPaid; };