Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backend/frontend] Fix team creation #1402

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.openbas.rest.exception;

public class AlreadyExistingException extends RuntimeException{

public AlreadyExistingException() {
super();
}

Check warning on line 7 in openbas-api/src/main/java/io/openbas/rest/exception/AlreadyExistingException.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/exception/AlreadyExistingException.java#L6-L7

Added lines #L6 - L7 were not covered by tests

public AlreadyExistingException(String errorMessage) {
super(errorMessage);
}

Check warning on line 11 in openbas-api/src/main/java/io/openbas/rest/exception/AlreadyExistingException.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/exception/AlreadyExistingException.java#L10-L11

Added lines #L10 - L11 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@
return new ResponseEntity<>(message, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(AlreadyExistingException.class)
public ResponseEntity<ErrorMessage> handleAlreadyExistingException(AlreadyExistingException ex) {
ErrorMessage message = new ErrorMessage(ex.getMessage());
log.warning("AlreadyExistingException: " + ex.getMessage());
return new ResponseEntity<>(message, HttpStatus.BAD_REQUEST);

Check warning on line 143 in openbas-api/src/main/java/io/openbas/rest/helper/RestBehavior.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/helper/RestBehavior.java#L141-L143

Added lines #L141 - L143 were not covered by tests
}

// --- Open channel access
public User impersonateUser(UserRepository userRepository, Optional<String> userId) {
if (currentUser().getId().equals(ANONYMOUS)) {
Expand Down
33 changes: 26 additions & 7 deletions openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.openbas.database.raw.RawPaginationTeam;
import io.openbas.database.raw.RawTeam;
import io.openbas.database.repository.*;
import io.openbas.rest.exception.AlreadyExistingException;
import io.openbas.rest.exception.ElementNotFoundException;
import io.openbas.rest.helper.RestBehavior;
import io.openbas.rest.helper.TeamHelper;
Expand All @@ -16,6 +17,7 @@
import io.openbas.rest.team.form.UpdateUsersTeamInput;
import io.openbas.utils.pagination.SearchPaginationInput;
import jakarta.validation.Valid;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -40,6 +42,7 @@
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
import static java.time.Instant.now;
import static org.springframework.util.StringUtils.hasText;

@RestController
@Secured(ROLE_USER)
Expand Down Expand Up @@ -169,13 +172,7 @@
@PreAuthorize("isPlanner()")
@Transactional(rollbackFor = Exception.class)
public Team createTeam(@Valid @RequestBody TeamCreateInput input) {
if (TRUE.equals(input.getContextual()) && input.getExerciseIds().toArray().length > 1) {
throw new UnsupportedOperationException("Contextual team can only be associated to one exercise");
}
Optional<Team> existingTeam = teamRepository.findByName(input.getName());
if (existingTeam.isPresent() && FALSE.equals(input.getContextual())) {
throw new UnsupportedOperationException("Global teams (non contextual) cannot have the same name (already exists)");
}
isTeamAlreadyExists(input);

Check warning on line 175 in openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java#L175

Added line #L175 was not covered by tests
Team team = new Team();
team.setUpdateAttributes(input);
team.setOrganization(updateRelation(input.getOrganizationId(), team.getOrganization(), organizationRepository));
Expand Down Expand Up @@ -236,4 +233,26 @@
team.setUsers(fromIterable(teamUsers));
return teamRepository.save(team);
}

// -- PRIVATE --

private void isTeamAlreadyExists(@NotNull final TeamCreateInput input) {
List<Team> teams = this.teamRepository.findAllByNameIgnoreCase(input.getName());

Check warning on line 240 in openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java#L240

Added line #L240 was not covered by tests
if (teams.isEmpty()) return;

if (FALSE.equals(input.getContextual()) && teams.stream().anyMatch(t -> FALSE.equals(t.getContextual()))) {
throw new AlreadyExistingException("Global teams (non contextual) cannot have the same name (already exists)");

Check warning on line 244 in openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java#L244

Added line #L244 was not covered by tests
}
if (TRUE.equals(input.getContextual())) {
String exerciseId = input.getExerciseIds().stream().findFirst().orElse(null);

Check warning on line 247 in openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java#L247

Added line #L247 was not covered by tests
if (hasText(exerciseId) && teams.stream().anyMatch(t -> TRUE.equals(t.getContextual()) && t.getExercises().stream().anyMatch((e) -> exerciseId.equals(e.getId())))) {
throw new AlreadyExistingException("A contextual team with the same name already exists on this simulation");

Check warning on line 249 in openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java#L249

Added line #L249 was not covered by tests
}
String scenarioId = input.getScenarioIds().stream().findFirst().orElse(null);

Check warning on line 251 in openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java#L251

Added line #L251 was not covered by tests
if (hasText(scenarioId) && teams.stream().anyMatch(t -> TRUE.equals(t.getContextual()) && t.getScenarios().stream().anyMatch((e) -> scenarioId.equals(e.getId())))) {
throw new AlreadyExistingException("A contextual team with the same name already exists on this scenario");

Check warning on line 253 in openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java#L253

Added line #L253 was not covered by tests
}
RomuDeuxfois marked this conversation as resolved.
Show resolved Hide resolved
}

}

Check warning on line 257 in openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java#L257

Added line #L257 was not covered by tests
}
3 changes: 3 additions & 0 deletions openbas-front/src/utils/Action.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ export const postReferential = (schema, uri, data) => (dispatch) => {
if (error.status === 500) {
MESSAGING$.notifyError('Internal error');
}
if (error.status === 400) {
MESSAGING$.notifyError(error.message);
}
return buildError(error);
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public interface TeamRepository extends CrudRepository<Team, String>,
@NotNull
Optional<Team> findByName(@NotNull final String name);

@NotNull
List<Team> findAllByNameIgnoreCase(@NotNull final String name);

RomuDeuxfois marked this conversation as resolved.
Show resolved Hide resolved
@Query("SELECT team FROM Team team where lower(team.name) = lower(:name) and team.contextual = false")
List<Team> findByNameIgnoreCaseAndNotContextual(@NotNull final String name);

Expand Down