feat: add billFwdStatus auto-trigger for when-payed strategy

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 <noreply@anthropic.com>
This commit is contained in:
Knee Cola
2025-12-30 13:03:01 +01:00
parent e26a478577
commit aa6ae91db8

View File

@@ -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) {