diff --git a/backend/core/src/main/java/org/opencdmp/service/plan/PlanServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/plan/PlanServiceImpl.java index ad7bd6a79..6d499320c 100644 --- a/backend/core/src/main/java/org/opencdmp/service/plan/PlanServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/plan/PlanServiceImpl.java @@ -18,6 +18,7 @@ import gr.cite.tools.validation.ValidationFailure; import gr.cite.tools.validation.ValidatorFactory; import jakarta.xml.bind.JAXBException; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.opencdmp.authorization.AuthorizationFlags; import org.opencdmp.authorization.Permission; import org.opencdmp.authorization.authorizationcontentresolver.AuthorizationContentResolver; @@ -734,19 +735,21 @@ public class PlanServiceImpl implements PlanService { .collect(); UUID currentUserId = this.userScope.getUserId(); - boolean currentUserIsInPlan = planUsers.stream().anyMatch(u -> u.getUserId() == currentUserId); - if (!currentUserIsInPlan) { - PlanUserEntity newUser = new PlanUserEntity(); - newUser.setId(UUID.randomUUID()); - newUser.setPlanId(newPlan.getId()); - newUser.setUserId(currentUserId); - newUser.setRole(PlanUserRole.Owner); - newUser.setCreatedAt(Instant.now()); - newUser.setUpdatedAt(Instant.now()); - newUser.setIsActive(IsActive.Active); - this.entityManager.persist(newUser); + boolean isCurrentUserInPlan = planUsers.stream().anyMatch(u -> u.getUserId().equals(currentUserId)); + + if (!isCurrentUserInPlan) { + this.addOwner(newPlan); + } else { + PlanUserEntity currentPlanUser = planUsers.stream().filter(u -> u.getUserId().equals(currentUserId)).toList().getFirst(); + + boolean isCurrentUserOwner = PlanUserRole.Owner.equals(currentPlanUser.getRole()); + if (!isCurrentUserOwner) { + planUsers.remove(currentPlanUser); + this.addOwner(newPlan); + } } + for (PlanUserEntity planUser : planUsers) { PlanUserEntity newUser = new PlanUserEntity(); newUser.setId(UUID.randomUUID());