feat: auto-set billFwdStatus to pending when all bills have attachments
Enables automatic email notification trigger when the last bill receives an attachment under "when-attached" forwarding strategy. This eliminates manual intervention for marking locations ready for tenant notification. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -72,6 +72,45 @@ const FormSchema = (t: IntlTemplateFn) => z.object({
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if billFwdStatus should be updated to "pending" based on attachment conditions
|
||||||
|
* @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
|
||||||
|
* @returns true if billFwdStatus should be set to "pending"
|
||||||
|
*/
|
||||||
|
const shouldUpdateBillFwdStatus = (
|
||||||
|
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 billFwdStatus is null or "failed"
|
||||||
|
if (location.billFwdStatus !== null && location.billFwdStatus !== "failed") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if ALL other bills have attachments
|
||||||
|
const otherBills = location.bills.filter(bill => bill._id !== currentBillId);
|
||||||
|
const allOtherBillsHaveAttachments = otherBills.every(bill => bill.attachment !== null);
|
||||||
|
|
||||||
|
return allOtherBillsHaveAttachments;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* converts the file to a format stored in the database
|
* converts the file to a format stored in the database
|
||||||
* @param billAttachment
|
* @param billAttachment
|
||||||
@@ -177,6 +216,19 @@ export const updateOrAddBill = withUser(async (user: AuthenticatedUser, location
|
|||||||
|
|
||||||
const billAttachment = await serializeAttachment(attachmentFile);
|
const billAttachment = await serializeAttachment(attachmentFile);
|
||||||
|
|
||||||
|
// Fetch the location to check billFwdStatus conditions
|
||||||
|
const location = await dbClient.collection<BillingLocation>("lokacije").findOne({
|
||||||
|
_id: locationId,
|
||||||
|
userId
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!location) {
|
||||||
|
return { success: false, error: 'Location not found' };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if we should update billFwdStatus to "pending"
|
||||||
|
const shouldSetFwdPending = shouldUpdateBillFwdStatus(location, billId, billAttachment !== null);
|
||||||
|
|
||||||
if (billId) {
|
if (billId) {
|
||||||
|
|
||||||
// if there is an attachment, update the attachment field
|
// if there is an attachment, update the attachment field
|
||||||
@@ -199,6 +251,11 @@ export const updateOrAddBill = withUser(async (user: AuthenticatedUser, location
|
|||||||
"bills.$[elem].hub3aText": hub3aText,
|
"bills.$[elem].hub3aText": hub3aText,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Add billFwdStatus if needed
|
||||||
|
if (shouldSetFwdPending) {
|
||||||
|
(mongoDbSet as any).billFwdStatus = "pending";
|
||||||
|
}
|
||||||
|
|
||||||
// update bill in given location with the given locationID
|
// update bill in given location with the given locationID
|
||||||
await dbClient.collection<BillingLocation>("lokacije").updateOne(
|
await dbClient.collection<BillingLocation>("lokacije").updateOne(
|
||||||
{
|
{
|
||||||
@@ -225,17 +282,27 @@ export const updateOrAddBill = withUser(async (user: AuthenticatedUser, location
|
|||||||
hub3aText,
|
hub3aText,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Build update operation
|
||||||
|
const updateOp: any = {
|
||||||
|
$push: {
|
||||||
|
bills: newBill
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add billFwdStatus update if needed
|
||||||
|
if (shouldSetFwdPending) {
|
||||||
|
updateOp.$set = {
|
||||||
|
billFwdStatus: "pending"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Add to current location
|
// Add to current location
|
||||||
await dbClient.collection<BillingLocation>("lokacije").updateOne(
|
await dbClient.collection<BillingLocation>("lokacije").updateOne(
|
||||||
{
|
{
|
||||||
_id: locationId, // find a location with the given locationID
|
_id: locationId, // find a location with the given locationID
|
||||||
userId // make sure that the location belongs to the user
|
userId // make sure that the location belongs to the user
|
||||||
},
|
},
|
||||||
{
|
updateOp);
|
||||||
$push: {
|
|
||||||
bills: newBill
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// If addToSubsequentMonths is enabled, add to subsequent months
|
// If addToSubsequentMonths is enabled, add to subsequent months
|
||||||
if (addToSubsequentMonths && billYear && billMonth) {
|
if (addToSubsequentMonths && billYear && billMonth) {
|
||||||
|
|||||||
Reference in New Issue
Block a user