refactor: extract bill forwarding helpers and add to multi-bill-edit
Created shared billForwardingHelpers module to avoid code duplication and implemented automatic bill forwarding trigger in the multi-bill-edit feature. Changes: - Extract shouldUpdateBillFwdStatusWhenAttached and shouldUpdateBillFwdStatusWhenPayed to new billForwardingHelpers.ts module - Update billActions.ts to import from shared module instead of local definitions - Add forwarding logic to monthActions.updateMonth to set billFwdStatus to "pending" when all tenant bills are marked as paid This ensures consistent bill forwarding behavior whether updating bills individually or in bulk via the multi-bill-edit page. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
97
web-app/app/lib/billForwardingHelpers.ts
Normal file
97
web-app/app/lib/billForwardingHelpers.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import { BillingLocation, BilledTo } from '@evidencija-rezija/shared-code';
|
||||
|
||||
/**
|
||||
* Checks if billFwdStatus should be updated to "pending" based on attachment status
|
||||
* @param location - The billing location containing the bill
|
||||
* @param currentBillId - The ID of the bill being updated (to exclude from check)
|
||||
* @param hasNewAttachment - Whether a new attachment is being added to the current bill
|
||||
* @returns true if billFwdStatus should be set to "pending"
|
||||
*/
|
||||
export const shouldUpdateBillFwdStatusWhenAttached = (
|
||||
location: BillingLocation,
|
||||
currentBillId: string | undefined,
|
||||
hasNewAttachment: boolean
|
||||
): boolean => {
|
||||
// Only proceed if a new attachment is being added
|
||||
if (!hasNewAttachment) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check billFwdEnabled is true
|
||||
if (location.billFwdEnabled !== true) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check billFwdStrategy is "when-attached"
|
||||
if (location.billFwdStrategy !== "when-attached") {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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);
|
||||
// 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;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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"
|
||||
*/
|
||||
export 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 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);
|
||||
// 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;
|
||||
};
|
||||
Reference in New Issue
Block a user