add bulk bill deletion across subsequent months
Implement ability to delete bills from all subsequent months with toggle option and warning message similar to location deletion. Includes centered warning box and efficient bulk operations. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -352,27 +352,86 @@ export const fetchBillById = async (locationID:string, billID:string, includeAtt
|
||||
return([billLocation, bill] as [BillingLocation, Bill]);
|
||||
};
|
||||
|
||||
export const deleteBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string, year:number, month:number) => {
|
||||
export const deleteBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string, year:number, month:number, _prevState:any, formData?: FormData) => {
|
||||
|
||||
const { id: userId } = user;
|
||||
|
||||
const dbClient = await getDbClient();
|
||||
|
||||
const deleteInSubsequentMonths = formData?.get('deleteInSubsequentMonths') === 'on';
|
||||
|
||||
// find a location with the given locationID
|
||||
const post = await dbClient.collection<BillingLocation>("lokacije").updateOne(
|
||||
{
|
||||
_id: locationID, // find a location with the given locationID
|
||||
userId // make sure that the location belongs to the user
|
||||
},
|
||||
{
|
||||
// remove the bill with the given billID
|
||||
$pull: {
|
||||
bills: {
|
||||
_id: billID
|
||||
if (deleteInSubsequentMonths) {
|
||||
// Get the current location and bill to find the bill name and location name
|
||||
const location = await dbClient.collection<BillingLocation>("lokacije")
|
||||
.findOne({ _id: locationID, userId });
|
||||
|
||||
if (location) {
|
||||
const bill = location.bills.find(b => b._id === billID);
|
||||
|
||||
if (bill) {
|
||||
// Find all subsequent locations with the same name that have the same bill
|
||||
const subsequentLocations = await dbClient.collection<BillingLocation>("lokacije")
|
||||
.find({
|
||||
userId,
|
||||
name: location.name,
|
||||
$or: [
|
||||
{ "yearMonth.year": { $gt: year } },
|
||||
{
|
||||
"yearMonth.year": year,
|
||||
"yearMonth.month": { $gt: month }
|
||||
}
|
||||
],
|
||||
"bills.name": bill.name
|
||||
}, { projection: { bills: 0 } })
|
||||
.toArray();
|
||||
|
||||
// Delete the bill from all subsequent locations (by name)
|
||||
const updateOperations = subsequentLocations.map(loc => ({
|
||||
updateOne: {
|
||||
filter: { _id: loc._id, userId },
|
||||
update: {
|
||||
$pull: {
|
||||
bills: { name: bill.name }
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
// Also delete from current location (by ID for precision)
|
||||
updateOperations.push({
|
||||
updateOne: {
|
||||
filter: { _id: locationID, userId },
|
||||
update: {
|
||||
$pull: {
|
||||
bills: { _id: billID }
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Execute all delete operations
|
||||
if (updateOperations.length > 0) {
|
||||
await dbClient.collection<BillingLocation>("lokacije").bulkWrite(updateOperations);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Delete only from current location (original behavior)
|
||||
await dbClient.collection<BillingLocation>("lokacije").updateOne(
|
||||
{
|
||||
_id: locationID, // find a location with the given locationID
|
||||
userId // make sure that the location belongs to the user
|
||||
},
|
||||
{
|
||||
// remove the bill with the given billID
|
||||
$pull: {
|
||||
bills: {
|
||||
_id: billID
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
await gotoHome({year, month});
|
||||
return(post.modifiedCount);
|
||||
return { message: null };
|
||||
});
|
||||
Reference in New Issue
Block a user