feat: soft delete for interests and roles
A super admin can delete an interest or a role. Deleted rows leave every list, picker and filter, keep their names on existing leads, and keep their names reserved. A role held by any user, and the protected roles, cannot be deleted. Deletes are audited. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
0f83e9cc08
commit
0b201a51ca
25 files changed
+765
-9
No files matched your search
+1
@@ -50,6 +50,7 @@ public class RestApiCounterServiceConstants {
|
||||
public static final String API_REST_COUNTER_EVENT_SALESPERSON_REPLACE="/counter/eventSalesperson/replace";
|
||||
public static final String API_REST_COUNTER_INTEREST_FIND_BY_COMPANY="/counter/interest/findByCompany/{companyId}";
|
||||
public static final String API_REST_COUNTER_INTEREST_SAVE="/counter/interest/save";
|
||||
public static final String API_REST_COUNTER_INTEREST_DELETE="/counter/interest/delete";
|
||||
public static final String API_REST_COUNTER_INTEREST_FIND_BY_IDS="/counter/interest/findByIds";
|
||||
public static final String API_REST_COUNTER_LEAD_FIND_MATCHES="/counter/lead/findMatches";
|
||||
public static final String API_REST_COUNTER_LEAD_SEARCH="/counter/lead/search";
|
||||
|
||||
+1
@@ -14,6 +14,7 @@ public class RestApiSystemServiceConstants {
|
||||
public static final String API_REST_SYSTEM_USER_FIND_BY_PROPERTY_AND_PAGEABLE = "/user/findByPropertyAndPageable";
|
||||
public static final String API_REST_SYSTEM_ROLE_FIND_ALL = "/role/findAll";
|
||||
public static final String API_REST_SYSTEM_ROLE_SAVE = "/role/save";
|
||||
public static final String API_REST_SYSTEM_ROLE_DELETE = "/role/delete";
|
||||
public static final String API_REST_SYSTEM_MODULE_FIND_ALL_BY_AREA = "/module/findAllByArea/{area}";
|
||||
public static final String API_REST_SYSTEM_MODULE_ADD_MODULE_ROLE = "/module/addModuleRole/{moduleId}/{roleId}";
|
||||
public static final String API_REST_SYSTEM_MODULE_DELETE_MODULE_ROLE = "/module/deleteModuleRole/{moduleId}/{roleId}";
|
||||
|
||||
+9
@@ -6,6 +6,7 @@ import com.solvway.mobile.api.service.AdminInterestService;
|
||||
import com.solvway.mobile.api.useraccess.AdminModule;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -44,4 +45,12 @@ public class AdminInterestController {
|
||||
public ResponseEntity<Map<String, Object>> update(@PathVariable long id, @RequestBody AdminInterestRequest request) {
|
||||
return ResponseEntity.ok(adminInterestService.update(id, request));
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@AdminModule("BO_INTERESTS")
|
||||
@DeleteMapping("/interests/{id}")
|
||||
public ResponseEntity<Void> delete(@PathVariable long id) {
|
||||
adminInterestService.delete(id);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
}
|
||||
+7
@@ -49,6 +49,13 @@ public class AdminRoleController {
|
||||
}
|
||||
@AppAuth
|
||||
@AdminModule("BO_ROLES")
|
||||
@DeleteMapping("/roles/{id}")
|
||||
public ResponseEntity<Void> delete(@PathVariable Long id) {
|
||||
adminRoleService.delete(id);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
@AppAuth
|
||||
@AdminModule("BO_ROLES")
|
||||
@GetMapping("/roles/{id}/modules")
|
||||
public ResponseEntity<List<AdminModuleGrant>> getModules(@PathVariable Long id) {
|
||||
return ResponseEntity.ok(adminAclService.getGrantTree(id));
|
||||
|
||||
+1
-1
@@ -386,7 +386,7 @@ public class AdminEventLeadService {
|
||||
Map<Long, Map<String, Object>> out = new LinkedHashMap<>();
|
||||
if (rows != null) for (Map<String, Object> r : rows) {
|
||||
long id = ((Number) r.get("id")).longValue();
|
||||
out.put(id, Map.of("id", id, "name", String.valueOf(r.get("name")), "active", Boolean.TRUE.equals(r.get("active"))));
|
||||
out.put(id, Map.of("id", id, "name", String.valueOf(r.get("name")), "active", Boolean.TRUE.equals(r.get("active")) && r.get("deletedAt") == null)); // a deleted interest keeps its name but is never offered or accepted
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
+14
-2
@@ -32,7 +32,7 @@ public class AdminInterestService {
|
||||
|
||||
public List<Map<String, Object>> list() {
|
||||
caller();
|
||||
return load().stream().map(AdminInterestService::row).toList();
|
||||
return load().stream().filter(m -> m.get("deletedAt") == null).map(AdminInterestService::row).toList();
|
||||
}
|
||||
|
||||
public Map<String, Object> create(AdminInterestRequest r) {
|
||||
@@ -47,7 +47,7 @@ public class AdminInterestService {
|
||||
public Map<String, Object> update(long id, AdminInterestRequest r) {
|
||||
scope.requireSuper(caller());
|
||||
Map<String, Object> stored = load().stream()
|
||||
.filter(m -> m.get("id") instanceof Number n && n.longValue() == id).findFirst()
|
||||
.filter(m -> m.get("deletedAt") == null && m.get("id") instanceof Number n && n.longValue() == id).findFirst()
|
||||
.orElseThrow(() -> error(HttpStatus.NOT_FOUND, "Interest not found", "INTEREST_NOT_FOUND", null));
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("id", id);
|
||||
@@ -61,10 +61,22 @@ public class AdminInterestService {
|
||||
try {
|
||||
return row(restTemplateUtil.postForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_INTEREST_SAVE), MAP, body).getBody());
|
||||
} catch (HttpClientErrorException.Conflict e) {
|
||||
if (e.getResponseBodyAsString().contains("INTEREST_NAME_DELETED")) {
|
||||
throw error(HttpStatus.CONFLICT, "This name belongs to a deleted interest", "INTEREST_NAME_DELETED", "name");
|
||||
}
|
||||
throw error(HttpStatus.CONFLICT, "An interest with this name already exists", "DUPLICATE_NAME", "name");
|
||||
}
|
||||
}
|
||||
|
||||
public void delete(long id) {
|
||||
scope.requireSuper(caller());
|
||||
try {
|
||||
restTemplateUtil.postForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_INTEREST_DELETE), Void.class, id);
|
||||
} catch (HttpClientErrorException.NotFound e) {
|
||||
throw error(HttpStatus.NOT_FOUND, "Interest not found", "INTEREST_NOT_FOUND", null);
|
||||
}
|
||||
}
|
||||
|
||||
// ponytail: the counter ignores the company id (one shared list); 0 is a placeholder for its path variable.
|
||||
private List<Map<String, Object>> load() {
|
||||
List<Map<String, Object>> rows = restTemplateUtil.getForEntity(
|
||||
|
||||
+32
-1
@@ -1,5 +1,7 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.constant.api.RestApiSystemServiceConstants;
|
||||
@@ -13,6 +15,7 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -30,6 +33,7 @@ import java.util.Map;
|
||||
public class AdminRoleService {
|
||||
private static final ParameterizedTypeReference<Map<String, Object>> MAP = new ParameterizedTypeReference<>() {};
|
||||
private static final ParameterizedTypeReference<List<Map<String, Object>>> MAP_LIST = new ParameterizedTypeReference<>() {};
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
private static final long SUPER_ADMIN_ROLE_ID = 1L;
|
||||
private static final List<String> HIDDEN = List.of("ROLE_MEMBER", "ROLE_ADMIN");
|
||||
private static final List<String> PROTECTED = List.of("ROLE_SUPER_ADMIN", "ROLE_ADMIN", "ROLE_MEMBER");
|
||||
@@ -95,6 +99,28 @@ public class AdminRoleService {
|
||||
return clean(reread);
|
||||
}
|
||||
|
||||
public void delete(Long id) {
|
||||
scope.requireSuper(caller());
|
||||
if (isProtected(find(id))) throw error(HttpStatus.CONFLICT, "ROLE_PROTECTED", "ROLE_PROTECTED");
|
||||
try {
|
||||
restTemplateUtil.postForEntity(url(RestApiSystemServiceConstants.API_REST_SYSTEM_ROLE_DELETE), Void.class, id);
|
||||
} catch (HttpClientErrorException.NotFound e) {
|
||||
throw notFound();
|
||||
} catch (HttpClientErrorException.Conflict e) {
|
||||
// services-system reports ROLE_IN_USE / ROLE_PROTECTED in the ProblemDetail detail; relay it with the user count
|
||||
Map<?, ?> body;
|
||||
try {
|
||||
body = JSON.readValue(e.getResponseBodyAsString(), Map.class);
|
||||
} catch (JsonProcessingException x) {
|
||||
body = Map.of();
|
||||
}
|
||||
String reason = body.get("detail") == null ? "ROLE_DELETE_REFUSED" : String.valueOf(body.get("detail"));
|
||||
CodedResponseStatusException out = error(HttpStatus.CONFLICT, reason, reason);
|
||||
if (body.get("users") != null) out.getBody().setProperty("users", body.get("users"));
|
||||
throw out;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isProtected(Map<String, Object> role) {
|
||||
return PROTECTED.contains(role.get("name"));
|
||||
}
|
||||
@@ -127,7 +153,12 @@ public class AdminRoleService {
|
||||
}
|
||||
|
||||
private Map<String, Object> save(Map<String, Object> role) {
|
||||
ResponseEntity<Map<String, Object>> ent = restTemplateUtil.postForEntity(url(RestApiSystemServiceConstants.API_REST_SYSTEM_ROLE_SAVE), MAP, role);
|
||||
ResponseEntity<Map<String, Object>> ent;
|
||||
try {
|
||||
ent = restTemplateUtil.postForEntity(url(RestApiSystemServiceConstants.API_REST_SYSTEM_ROLE_SAVE), MAP, role);
|
||||
} catch (HttpClientErrorException.Conflict e) {
|
||||
throw error(HttpStatus.CONFLICT, "This name belongs to a deleted role", "ROLE_NAME_DELETED");
|
||||
}
|
||||
// services-system swallows save errors and returns the unsaved entity, so a missing id means it failed.
|
||||
if (ent.getBody() == null || ent.getBody().get("id") == null) {
|
||||
throw error(HttpStatus.INTERNAL_SERVER_ERROR, "Role could not be saved", "SAVE_FAILED");
|
||||
|
||||
Reference in new issue
Block a user