add update scope options for location editing

- Added radio button group to LocationEditForm with three update modes:
  1. Current month only (default) - updates specific location
  2. Current and all future months - updates current and subsequent months
  3. All months - updates all locations with same name across all time periods
- Enhanced updateOrAddLocation action with smart update logic based on scope
- Uses name-based matching to find related locations across months
- Added compact radio button styling with reduced spacing and indentation
- Added translations for update scope options in Croatian and English
- Maintains backward compatibility with existing single-location updates

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-08-11 11:42:48 +02:00
parent 80e30deace
commit a6d0cc77ac
4 changed files with 94 additions and 12 deletions

View File

@@ -28,6 +28,7 @@ const FormSchema = (t:IntlTemplateFn) => z.object({
locationName: z.coerce.string().min(1, t("location-name-required")),
locationNotes: z.string(),
addToSubsequentMonths: z.boolean().optional(),
updateScope: z.enum(["current", "subsequent", "all"]).optional(),
})
// dont include the _id field in the response
.omit({ _id: true });
@@ -49,6 +50,7 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
locationName: formData.get('locationName'),
locationNotes: formData.get('locationNotes'),
addToSubsequentMonths: formData.get('addToSubsequentMonths') === 'on',
updateScope: formData.get('updateScope') as "current" | "subsequent" | "all" | undefined,
});
// If form validation fails, return errors early. Otherwise, continue...
@@ -63,6 +65,7 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
locationName,
locationNotes,
addToSubsequentMonths,
updateScope,
} = validatedFields.data;
// update the bill in the mongodb
@@ -71,17 +74,68 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
const { id: userId, email: userEmail } = user;
if(locationId) {
await dbClient.collection<BillingLocation>("lokacije").updateOne(
{
_id: locationId, // find a location with the given locationID
userId // make sure the location belongs to the user
},
{
$set: {
name: locationName,
notes: locationNotes,
// Get the current location first to find its name
const currentLocation = await dbClient.collection<BillingLocation>("lokacije")
.findOne({ _id: locationId, userId });
if (!currentLocation) {
return {
message: "Location not found",
errors: undefined,
};
}
// Handle different update scopes
if (updateScope === "current" || !updateScope) {
// Update only the current location (default behavior)
await dbClient.collection<BillingLocation>("lokacije").updateOne(
{
_id: locationId,
userId
},
{
$set: {
name: locationName,
notes: locationNotes,
}
}
});
);
} else if (updateScope === "subsequent") {
// Update current and all subsequent months
await dbClient.collection<BillingLocation>("lokacije").updateMany(
{
userId,
name: currentLocation.name,
$or: [
{ "yearMonth.year": { $gt: currentLocation.yearMonth.year } },
{
"yearMonth.year": currentLocation.yearMonth.year,
"yearMonth.month": { $gte: currentLocation.yearMonth.month }
}
]
},
{
$set: {
name: locationName,
notes: locationNotes,
}
}
);
} else if (updateScope === "all") {
// Update all locations with the same name across all months
await dbClient.collection<BillingLocation>("lokacije").updateMany(
{
userId,
name: currentLocation.name
},
{
$set: {
name: locationName,
notes: locationNotes,
}
}
);
}
} else if(yearMonth) {
// Always add location to the specified month
await dbClient.collection<BillingLocation>("lokacije").insertOne({