Skip to content

Commit b4ed7fb

Browse files
authored
Merge pull request #115 from ithsjava25/feat/admin-incident-audithlog-search
feat: add audith logs in admin panel and search functionality for inc…
2 parents aff3713 + 0d94e4a commit b4ed7fb

5 files changed

Lines changed: 297 additions & 97 deletions

File tree

src/main/java/org/example/team6backend/admin/AdminController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public ResponseEntity<UserResponse> updateUserRole(@PathVariable String userId,
102102
log.info("PATCH /api/admin/users/{}/role - Admin {} changing role to {}", userId, admin.getGithubLogin(),
103103
request.role());
104104

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

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

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

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

src/main/java/org/example/team6backend/incident/controller/IncidentController.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.example.team6backend.incident.dto.UpdateIncidentStatusRequest;
1010
import org.example.team6backend.incident.entity.Incident;
1111
import org.example.team6backend.incident.entity.IncidentCategory;
12+
import org.example.team6backend.incident.entity.IncidentStatus;
1213
import org.example.team6backend.incident.service.IncidentService;
1314
import org.example.team6backend.notification.service.NotificationService;
1415
import org.example.team6backend.security.CustomUserDetails;
@@ -112,9 +113,20 @@ public ResponseEntity<Page<IncidentResponse>> getAssignedIncidents(
112113

113114
@PreAuthorize("hasRole('ADMIN')")
114115
@GetMapping("/all")
115-
public ResponseEntity<Page<IncidentResponse>> getAllIncidents(Pageable pageable) {
116-
log.info("GET /api/incidents/all - Fetching all incidents");
117-
return ResponseEntity.ok(incidentService.findAll(pageable).map(IncidentResponse::fromEntityBasic));
116+
public ResponseEntity<Page<IncidentResponse>> getAllIncidents(@RequestParam(required = false) String search,
117+
@RequestParam(required = false) IncidentStatus status, Pageable pageable) {
118+
log.info("GET /api/incidents/all - Fetching all incidents with search={}, status={}", search, status);
119+
120+
Page<Incident> incidents;
121+
if (search != null && !search.trim().isEmpty()) {
122+
incidents = incidentService.searchIncidents(search.trim(), pageable);
123+
} else if (status != null) {
124+
incidents = incidentService.findByStatus(status, pageable);
125+
} else {
126+
incidents = incidentService.findAll(pageable);
127+
}
128+
129+
return ResponseEntity.ok(incidents.map(IncidentResponse::fromEntityBasic));
118130
}
119131

120132
@PreAuthorize("hasAnyRole('RESIDENT', 'HANDLER', 'ADMIN')")

src/main/java/org/example/team6backend/incident/repository/IncidentRepository.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

26+
@EntityGraph(attributePaths = {"documents", "createdBy", "assignedTo"})
27+
Page<Incident> findByIncidentStatus(IncidentStatus status, Pageable pageable);
28+
2529
@Query("SELECT i FROM Incident i LEFT JOIN FETCH i.documents WHERE i.id = :id")
2630
Optional<Incident> findByIdWithDocuments(@Param("id") Long id);
31+
32+
@Query("SELECT i FROM Incident i WHERE " + "LOWER(i.subject) LIKE LOWER(CONCAT('%', :search, '%')) OR "
33+
+ "LOWER(COALESCE(i.description, '')) LIKE LOWER(CONCAT('%', :search, '%'))")
34+
Page<Incident> searchIncidents(@Param("search") String search, Pageable pageable);
2735
}

src/main/java/org/example/team6backend/incident/service/IncidentService.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,25 @@ public Page<Incident> findByAssignedTo(AppUser user, Pageable pageable) {
141141
return incidentRepository.findByAssignedTo(user, withDefaultSort(pageable));
142142
}
143143

144+
/**
145+
* Find incidents by status (Admin)
146+
*/
147+
public Page<Incident> findByStatus(IncidentStatus status, Pageable pageable) {
148+
log.info("Finding incidents by status: {}", status);
149+
return incidentRepository.findByIncidentStatus(status, withDefaultSort(pageable));
150+
}
151+
152+
/**
153+
* Search incidents by subject or description (Admin)
154+
*/
155+
public Page<Incident> searchIncidents(String search, Pageable pageable) {
156+
log.info("Searching incidents with term: {}", search);
157+
if (search == null || search.trim().isEmpty()) {
158+
return incidentRepository.findAll(withDefaultSort(pageable));
159+
}
160+
return incidentRepository.searchIncidents(search.trim(), withDefaultSort(pageable));
161+
}
162+
144163
public Incident getById(Long id, AppUser user) {
145164
Incident incident = incidentRepository.findByIdWithDocuments(id)
146165
.orElseThrow(() -> new ResourceNotFoundException("Not found"));

0 commit comments

Comments
 (0)