From aa6ae91db835d9173d0484c7f37b46cfecd4b868 Mon Sep 17 00:00:00 2001 From: Knee Cola Date: Tue, 30 Dec 2025 13:03:01 +0100 Subject: [PATCH] feat: add billFwdStatus auto-trigger for when-payed strategy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Enables automatic email notification trigger when all bills are marked as paid under "when-payed" forwarding strategy. Complements the existing "when-attached" strategy to support both notification workflows. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- web-app/app/lib/actions/billActions.ts | 45 ++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/web-app/app/lib/actions/billActions.ts b/web-app/app/lib/actions/billActions.ts index 66c7338..3fc6646 100644 --- a/web-app/app/lib/actions/billActions.ts +++ b/web-app/app/lib/actions/billActions.ts @@ -79,7 +79,7 @@ const FormSchema = (t: IntlTemplateFn) => z.object({ * @param hasNewAttachment - Whether a new attachment is being added * @returns true if billFwdStatus should be set to "pending" */ -const shouldUpdateBillFwdStatus = ( +const shouldUpdateBillFwdStatusWhenAttached = ( location: BillingLocation, currentBillId: string | undefined, hasNewAttachment: boolean @@ -111,6 +111,45 @@ const shouldUpdateBillFwdStatus = ( return allOtherBillsHaveAttachments; }; +/** + * Checks if billFwdStatus should be updated to "pending" based on paid status + * @param location - The billing location containing the bill + * @param currentBillId - The ID of the bill being updated (to exclude from check) + * @param isPaid - Whether the current bill is being marked as paid + * @returns true if billFwdStatus should be set to "pending" + */ +const shouldUpdateBillFwdStatusWhenPayed = ( + location: BillingLocation, + currentBillId: string | undefined, + isPaid: boolean +): boolean => { + // Only proceed if the bill is being marked as paid + if (!isPaid) { + return false; + } + + // Check billFwdEnabled is true + if (location.billFwdEnabled !== true) { + return false; + } + + // Check billFwdStrategy is "when-payed" + if (location.billFwdStrategy !== "when-payed") { + return false; + } + + // Check billFwdStatus is null or "failed" + if (location.billFwdStatus !== null && location.billFwdStatus !== "failed") { + 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); + + return allOtherBillsPaid; +}; + /** * converts the file to a format stored in the database * @param billAttachment @@ -227,7 +266,9 @@ export const updateOrAddBill = withUser(async (user: AuthenticatedUser, location } // Check if we should update billFwdStatus to "pending" - const shouldSetFwdPending = shouldUpdateBillFwdStatus(location, billId, billAttachment !== null); + const shouldSetFwdPendingWhenAttached = shouldUpdateBillFwdStatusWhenAttached(location, billId, billAttachment !== null); + const shouldSetFwdPendingWhenPayed = shouldUpdateBillFwdStatusWhenPayed(location, billId, billPaid); + const shouldSetFwdPending = shouldSetFwdPendingWhenAttached || shouldSetFwdPendingWhenPayed; if (billId) {