feat(admin-api): label search with an optional code list

POST /api/admin/messages/search takes the list filters plus up to 5,000
codes. The no-create guard test now allows this read-only POST by name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-12-02 14:50:28 +08:00
1 parent 89662d0eb2
commit d2cbc3c227
5 files changed
+74 -2

No files matched your search

@@ -1,11 +1,13 @@
package com.solvway.mobile.api.controller.admin;
import com.solvway.core.annotation.AppAuth;
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
import com.solvway.mobile.api.service.AdminTranslationService;
import com.solvway.mobile.api.service.SystemService;
import com.solvway.mobile.api.useraccess.AdminModule;
import com.solvway.mobile.api.useraccess.Audited;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -27,6 +29,7 @@ import java.util.Map;
@RequestMapping("/api/admin")
@RequiredArgsConstructor
public class AdminMessageController {
private static final int MAX_CODES = 5000;
private final AdminTranslationService translations;
private final SystemService systemService;
@@ -52,6 +55,29 @@ public class AdminMessageController {
return ResponseEntity.ok(translations.search(searchKey, missingLocale, page, size));
}
/** Same as the list, plus an optional code list; a POST because the portal sends its ~700 label codes. */
@AppAuth
@AdminModule("BO_MESSAGES")
@PostMapping("/messages/search")
public ResponseEntity<Map<String, Object>> search(@RequestBody Map<String, Object> body) {
Object codes = body.get("codes");
if (codes != null && !(codes instanceof List<?> list && list.size() <= MAX_CODES && list.stream().allMatch(String.class::isInstance))) {
throw new CodedResponseStatusException(HttpStatus.BAD_REQUEST, "codes must be a list of at most " + MAX_CODES + " strings", "MsgsInvalidCodes");
}
@SuppressWarnings("unchecked")
List<String> codeList = (List<String>) codes;
return ResponseEntity.ok(translations.search(text(body.get("searchKey")), text(body.get("missingLocale")),
number(body.get("page"), 0), number(body.get("size"), 20), codeList));
}
private static String text(Object v) {
return v instanceof String s ? s : null;
}
private static int number(Object v, int fallback) {
return v instanceof Number n ? n.intValue() : fallback;
}
@AppAuth
@AdminModule("BO_MESSAGES")
@Audited(action = "MESSAGES_EXPORT")
@@ -65,7 +65,13 @@ public class AdminTranslationService {
}
public Map<String, Object> search(String searchKey, String missingLocale, int page, int size) {
return search(searchKey, missingLocale, page, size, null);
}
/** codes, when set, limits the list to those codes (ignoring case); null lists every area-A label. */
public Map<String, Object> search(String searchKey, String missingLocale, int page, int size, List<String> codes) {
Map<String, Object> req = new HashMap<>();
req.put("codes", codes);
req.put("searchKey", searchKey);
req.put("missingLocale", missingLocale);
req.put("area", AREA);
@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -23,8 +24,10 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -59,7 +62,7 @@ class AdminTranslationControllersTest {
void labelsCannotBeAddedOrDeletedAndExportIsAudited() throws Exception {
assertThat(Arrays.stream(AdminMessageController.class.getDeclaredMethods())
.filter(m -> m.isAnnotationPresent(DeleteMapping.class)
|| (m.isAnnotationPresent(PostMapping.class) && !m.getName().equals("importMessages")))).isEmpty();
|| (m.isAnnotationPresent(PostMapping.class) && !List.of("importMessages", "search").contains(m.getName())))).isEmpty();
assertThat(AdminMessageController.class.getMethod("export").getAnnotation(Audited.class)).isNotNull();
}
@@ -69,6 +72,31 @@ class AdminTranslationControllersTest {
mvc.perform(get("/api/admin/messages").param("missingLocale", "zh_CN")).andExpect(status().isOk()).andExpect(jsonPath("$.totalElements").value(0));
}
@Test
void searchPassesTheCodesAndFilters() throws Exception {
when(translations.search("lead", "zh_CN", 1, 50, List.of("Leads", "Users"))).thenReturn(Map.of("content", List.of(), "totalElements", 2));
mvc.perform(post("/api/admin/messages/search").contentType(MediaType.APPLICATION_JSON)
.content("{\"searchKey\":\"lead\",\"missingLocale\":\"zh_CN\",\"page\":1,\"size\":50,\"codes\":[\"Leads\",\"Users\"]}"))
.andExpect(status().isOk()).andExpect(jsonPath("$.totalElements").value(2));
}
@Test
void searchWithoutCodesListsEveryLabel() throws Exception {
when(translations.search(null, null, 0, 20, null)).thenReturn(Map.of("content", List.of(), "totalElements", 9));
mvc.perform(post("/api/admin/messages/search").contentType(MediaType.APPLICATION_JSON).content("{}"))
.andExpect(status().isOk()).andExpect(jsonPath("$.totalElements").value(9));
}
@Test
void searchRefusesABadCodeList() throws Exception {
String tooMany = "[" + String.join(",", Collections.nCopies(5001, "\"A\"")) + "]";
for (String codes : List.of(tooMany, "\"Leads\"", "[1,2]", "[null]")) {
mvc.perform(post("/api/admin/messages/search").contentType(MediaType.APPLICATION_JSON).content("{\"codes\":" + codes + "}"))
.andExpect(status().isBadRequest());
}
verifyNoInteractions(translations);
}
@Test
void updateUsesThePathId() throws Exception {
mvc.perform(put("/api/admin/messages/7").contentType(MediaType.APPLICATION_JSON).content("{\"id\":9,\"content\":\"{}\"}"))
@@ -91,6 +91,18 @@ class AdminTranslationServiceTest {
.containsEntry("area", "A").containsEntry("page", 2).containsEntry("size", 20);
}
@Test
void searchForwardsTheCodes() {
when(rest.postForEntity(eq("/message/search"), any(ParameterizedTypeReference.class), any()))
.thenReturn(ResponseEntity.ok(Map.of("content", List.of(), "totalElements", 0)));
service.search(null, null, 0, 20, List.of("Leads"));
ArgumentCaptor<Object> body = ArgumentCaptor.forClass(Object.class);
verify(rest).postForEntity(eq("/message/search"), any(ParameterizedTypeReference.class), body.capture());
assertThat((Map<String, Object>) body.getValue()).containsEntry("codes", List.of("Leads")).containsEntry("area", "A");
}
@Test
void importMatchesByCodeIgnoringCaseNeverByIdAndKeepsGoingAfterAFailure() {
when(rest.getForEntity(eq("/message/getAllMessages/{area}"), any(ParameterizedTypeReference.class), eq("A")))