diff --git a/app/lib/actions/locationActions.ts b/app/lib/actions/locationActions.ts index 88f21e4..baed3c9 100644 --- a/app/lib/actions/locationActions.ts +++ b/app/lib/actions/locationActions.ts @@ -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("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("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("lokacije").updateOne( + { + _id: locationId, + userId + }, + { + $set: { + name: locationName, + notes: locationNotes, + } } - }); + ); + } else if (updateScope === "subsequent") { + // Update current and all subsequent months + await dbClient.collection("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("lokacije").updateMany( + { + userId, + name: currentLocation.name + }, + { + $set: { + name: locationName, + notes: locationNotes, + } + } + ); + } } else if(yearMonth) { // Always add location to the specified month await dbClient.collection("lokacije").insertOne({ diff --git a/app/ui/LocationEditForm.tsx b/app/ui/LocationEditForm.tsx index b3020f9..cf95c0c 100644 --- a/app/ui/LocationEditForm.tsx +++ b/app/ui/LocationEditForm.tsx @@ -60,14 +60,34 @@ export const LocationEditForm:FC = ({ location, yearMonth ))} - {/* Show toggle only when adding a new location (not editing) */} - {!location && ( + {/* Show different options for add vs edit operations */} + {!location ? (
+ ) : ( +
+
+ {t("update-scope")} +
+
+ + + +
+
)}
diff --git a/messages/en.json b/messages/en.json index b5566b3..b783ce6 100644 --- a/messages/en.json +++ b/messages/en.json @@ -102,6 +102,10 @@ "cancel-button": "Cancel", "delete-tooltip": "Delete realestate", "add-to-subsequent-months": "Add to all subsequent months", + "update-scope": "Update scope:", + "update-current-month": "current month only", + "update-subsequent-months": "current and all future months", + "update-all-months": "all months", "validation": { "location-name-required": "Relaestate name is required" } diff --git a/messages/hr.json b/messages/hr.json index 1cba01e..05cf605 100644 --- a/messages/hr.json +++ b/messages/hr.json @@ -101,6 +101,10 @@ "cancel-button": "Odbaci", "delete-tooltip": "Brisanje nekretnine", "add-to-subsequent-months": "Dodaj u sve mjesece koji slijede", + "update-scope": "Opseg ažuriranja:", + "update-current-month": "samo trenutni mjesec", + "update-subsequent-months": "trenutni i svi budući mjeseci", + "update-all-months": "svi mjeseci", "validation": { "location-name-required": "Ime nekretnine je obavezno" }