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:
@@ -8,6 +8,7 @@ import { withUser } from '../auth';
|
||||
import { unstable_noStore as noStore, unstable_noStore, revalidatePath } from 'next/cache';
|
||||
import { getLocale } from 'next-intl/server';
|
||||
import { gotoHomeWithMessage } from './navigationActions';
|
||||
import { shouldUpdateBillFwdStatusWhenPayed } from '../billForwardingHelpers';
|
||||
|
||||
/**
|
||||
* Server-side action which adds a new month to the database
|
||||
@@ -206,6 +207,46 @@ export const updateMonth = withUser(async (
|
||||
|
||||
await Promise.all(updatePromises);
|
||||
|
||||
// Check if bill forwarding should be triggered for any locations
|
||||
const forwardingCheckPromises = Object.entries(updatesByLocation).map(
|
||||
async ([locationId, locationUpdates]) => {
|
||||
// Check if any bills were marked as paid
|
||||
const hasPaidUpdate = locationUpdates.some(update => update.paid === true);
|
||||
|
||||
if (!hasPaidUpdate) {
|
||||
return; // Skip if no bills were marked as paid
|
||||
}
|
||||
|
||||
// Fetch the full location data to check forwarding conditions
|
||||
const location = await dbClient.collection<BillingLocation>("lokacije").findOne({
|
||||
_id: locationId,
|
||||
userId,
|
||||
yearMonth: {
|
||||
year: yearMonth.year,
|
||||
month: yearMonth.month,
|
||||
},
|
||||
});
|
||||
|
||||
if (!location) {
|
||||
return; // Location not found
|
||||
}
|
||||
|
||||
// Check each bill update to see if it triggers forwarding
|
||||
for (const update of locationUpdates) {
|
||||
if (shouldUpdateBillFwdStatusWhenPayed(location, update.billId, update.paid)) {
|
||||
// Update billFwdStatus to "pending"
|
||||
await dbClient.collection<BillingLocation>("lokacije").updateOne(
|
||||
{ _id: locationId },
|
||||
{ $set: { billFwdStatus: "pending" } }
|
||||
);
|
||||
break; // Only need to set once per location
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
await Promise.all(forwardingCheckPromises);
|
||||
|
||||
// Revalidate the home page and multi-edit page to show fresh data
|
||||
revalidatePath('/home');
|
||||
revalidatePath(`/home/multi-bill-edit/${yearMonth.year}/${yearMonth.month}`);
|
||||
|
||||
Reference in New Issue
Block a user