import { BillingLocation, BilledTo, BillsNotificationStatus, BillsNotificationStrategy } from '@evidencija-rezija/shared-code'; /** * Checks if billsNotificationStatus should be updated to `Scheduled` 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 billsNotificationStatus should be set to `Scheduled` */ export const shouldUpdateBillsNotificationStatusWhenAttached = ( location: BillingLocation, currentBillId: string | undefined, hasNewAttachment: boolean ): boolean => { // Only proceed if a new attachment is being added if (!hasNewAttachment) { return false; } // Check billsNotificationEnabled is true if (location.billsNotificationEnabled !== true) { return false; } // Check billsNotificationStrategy is `WhenAttached` if (location.billsNotificationStrategy !== BillsNotificationStrategy.WhenAttached) { return false; } // Check bills have already been sent or are scheduled -> don't send them again if (location.billsNotificationStatus === BillsNotificationStatus.Scheduled || location.billsNotificationStatus === BillsNotificationStatus.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 billsNotificationStatus should be updated to "scheduled" 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 billsNotificationStatus should be set to "scheduled" */ export const shouldUpdateBillsNotificationStatusWhenPayed = ( location: BillingLocation, currentBillId: string | undefined, isPaid: boolean ): boolean => { // Only proceed if the bill is being marked as paid if (!isPaid) { return false; } // Check billsNotificationEnabled is true if (location.billsNotificationEnabled !== true) { return false; } // Check billsNotificationStrategy is `WhenPayed` if (location.billsNotificationStrategy !== BillsNotificationStrategy.WhenPayed) { return false; } // Check bills have already been sent or are scheduled -> don't sent them again if (location.billsNotificationStatus === BillsNotificationStatus.Scheduled || location.billsNotificationStatus === BillsNotificationStatus.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; };