Skip to content
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
Expand Up @@ -102,7 +102,7 @@ public ResponseEntity<UserResponse> updateUserRole(@PathVariable String userId,
log.info("PATCH /api/admin/users/{}/role - Admin {} changing role to {}", userId, admin.getGithubLogin(),
request.role());

if (admin.getId().equals(userId)) { // ← FÖRENKLAT kollen
if (admin.getId().equals(userId)) {
log.warn("User {} attempted to change their own role", userId);
throw new IllegalStateException("You cannot change your own role");
}
Expand Down Expand Up @@ -156,13 +156,13 @@ public ResponseEntity<Void> deleteUser(@PathVariable String userId,

log.info("DELETE /api/admin/users/{} - Admin {} attempting to delete user", userId, admin.getGithubLogin());

if (admin.getId().equals(userId)) { // ← FÖRENKLAT
if (admin.getId().equals(userId)) {
log.warn("User {} attempted to delete their own account", userId);
throw new IllegalStateException("You cannot delete your own account");
}

var userToDelete = userService.getUserById(userId);
userService.deleteUser(userId, admin); // ← SKICKA MED ADMIN
userService.deleteUser(userId, admin);
log.info("User {} ({}) deleted by admin {}", userToDelete.getGithubLogin(), userToDelete.getRole(),
admin.getGithubLogin());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.example.team6backend.incident.dto.UpdateIncidentStatusRequest;
import org.example.team6backend.incident.entity.Incident;
import org.example.team6backend.incident.entity.IncidentCategory;
import org.example.team6backend.incident.entity.IncidentStatus;
import org.example.team6backend.incident.service.IncidentService;
import org.example.team6backend.notification.service.NotificationService;
import org.example.team6backend.security.CustomUserDetails;
Expand Down Expand Up @@ -112,9 +113,20 @@ public ResponseEntity<Page<IncidentResponse>> getAssignedIncidents(

@PreAuthorize("hasRole('ADMIN')")
@GetMapping("/all")
public ResponseEntity<Page<IncidentResponse>> getAllIncidents(Pageable pageable) {
log.info("GET /api/incidents/all - Fetching all incidents");
return ResponseEntity.ok(incidentService.findAll(pageable).map(IncidentResponse::fromEntityBasic));
public ResponseEntity<Page<IncidentResponse>> getAllIncidents(@RequestParam(required = false) String search,
@RequestParam(required = false) IncidentStatus status, Pageable pageable) {
log.info("GET /api/incidents/all - Fetching all incidents with search={}, status={}", search, status);

Page<Incident> incidents;
if (search != null && !search.trim().isEmpty()) {
incidents = incidentService.searchIncidents(search.trim(), pageable);
} else if (status != null) {
incidents = incidentService.findByStatus(status, pageable);
} else {
incidents = incidentService.findAll(pageable);
}

return ResponseEntity.ok(incidents.map(IncidentResponse::fromEntityBasic));
}

@PreAuthorize("hasAnyRole('RESIDENT', 'HANDLER', 'ADMIN')")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.example.team6backend.user.entity.AppUser;
import org.example.team6backend.incident.entity.Incident;
import org.example.team6backend.incident.entity.IncidentStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
Expand All @@ -22,6 +23,13 @@ public interface IncidentRepository extends JpaRepository<Incident, Long> {
@EntityGraph(attributePaths = {"documents", "createdBy", "assignedTo"})
Page<Incident> findByAssignedTo(AppUser user, Pageable pageable);

@EntityGraph(attributePaths = {"documents", "createdBy", "assignedTo"})
Page<Incident> findByIncidentStatus(IncidentStatus status, Pageable pageable);

@Query("SELECT i FROM Incident i LEFT JOIN FETCH i.documents WHERE i.id = :id")
Optional<Incident> findByIdWithDocuments(@Param("id") Long id);

@Query("SELECT i FROM Incident i WHERE " + "LOWER(i.subject) LIKE LOWER(CONCAT('%', :search, '%')) OR "
+ "LOWER(COALESCE(i.description, '')) LIKE LOWER(CONCAT('%', :search, '%'))")
Page<Incident> searchIncidents(@Param("search") String search, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@ public Page<Incident> findByAssignedTo(AppUser user, Pageable pageable) {
return incidentRepository.findByAssignedTo(user, withDefaultSort(pageable));
}

/**
* Find incidents by status (Admin)
*/
public Page<Incident> findByStatus(IncidentStatus status, Pageable pageable) {
log.info("Finding incidents by status: {}", status);
return incidentRepository.findByIncidentStatus(status, withDefaultSort(pageable));
}

/**
* Search incidents by subject or description (Admin)
*/
public Page<Incident> searchIncidents(String search, Pageable pageable) {
log.info("Searching incidents with term: {}", search);
if (search == null || search.trim().isEmpty()) {
return incidentRepository.findAll(withDefaultSort(pageable));
}
return incidentRepository.searchIncidents(search.trim(), withDefaultSort(pageable));
}

public Incident getById(Long id, AppUser user) {
Incident incident = incidentRepository.findByIdWithDocuments(id)
.orElseThrow(() -> new ResourceNotFoundException("Not found"));
Expand Down
Loading
Loading