feat(system): message search, guarded saves and the catalog version read
Ported from babyboo message-admin-api: search over the code, English and Chinese text; a missing-Chinese filter; a save that refuses a code change or a duplicate code (409, no longer swallowed) and bumps the catalog version. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
1 parent
4622a5219d
commit
ebfee582c2
5 files changed
+267
-8
No files matched your search
@@ -15,7 +15,7 @@ Verify commands:
|
|||||||
|
|
||||||
- [x] 2.1 Language registry: entity, repository, service and `/locale/*` controller, with the duplicate, code-locked, English-locked and last-enabled refusals, and the catalog version bump on save and update.
|
- [x] 2.1 Language registry: entity, repository, service and `/locale/*` controller, with the duplicate, code-locked, English-locked and last-enabled refusals, and the catalog version bump on save and update.
|
||||||
- Verify: `mvn -T 1C -B -q -pl solvway-services-system -am verify`
|
- Verify: `mvn -T 1C -B -q -pl solvway-services-system -am verify`
|
||||||
- [ ] 2.2 Messages:
|
- [x] 2.2 Messages:
|
||||||
- `catalog_meta` with `/message/catalogVersion`;
|
- `catalog_meta` with `/message/catalogVersion`;
|
||||||
- `/message/search` (searchKey over code, `en_US`, `zh_CN`; missingLocale; area; paging);
|
- `/message/search` (searchKey over code, `en_US`, `zh_CN`; missingLocale; area; paging);
|
||||||
- save refusals (`MsgsCodeAreaLocked`, `MsgsDuplicateCode`);
|
- save refusals (`MsgsCodeAreaLocked`, `MsgsDuplicateCode`);
|
||||||
|
|||||||
+17
-6
@@ -3,6 +3,8 @@ package com.solvway.services.system.controller;
|
|||||||
import com.solvway.core.utils.StackTraceUtil;
|
import com.solvway.core.utils.StackTraceUtil;
|
||||||
import com.solvway.services.system.constant.SystemConstants.Area;
|
import com.solvway.services.system.constant.SystemConstants.Area;
|
||||||
import com.solvway.services.system.model.Message;
|
import com.solvway.services.system.model.Message;
|
||||||
|
import com.solvway.services.system.model.MessageSearchRequest;
|
||||||
|
import com.solvway.services.system.service.CatalogVersionService;
|
||||||
import com.solvway.services.system.service.MessageService;
|
import com.solvway.services.system.service.MessageService;
|
||||||
import com.solvway.services.system.util.paging.PageableRest;
|
import com.solvway.services.system.util.paging.PageableRest;
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
@@ -33,6 +35,9 @@ public class MessageController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private MessageService messageService;
|
private MessageService messageService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CatalogVersionService catalogVersion;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
ModelMapper modelMapper;
|
ModelMapper modelMapper;
|
||||||
|
|
||||||
@@ -69,15 +74,21 @@ public class MessageController {
|
|||||||
return messageService.findByCodeContaining(searchKey, pageable);
|
return messageService.findByCodeContaining(searchKey, pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Refusals (MsgsCodeAreaLocked, MsgsDuplicateCode) reach the caller as 409s; nothing is swallowed. */
|
||||||
@RequestMapping("/save")
|
@RequestMapping("/save")
|
||||||
public void save(@RequestBody Message message) {
|
public void save(@RequestBody Message message) {
|
||||||
try {
|
messageService.save(message);
|
||||||
messageService.save(message);
|
}
|
||||||
}catch(Exception ignore){
|
|
||||||
//return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//return true;
|
@RequestMapping("/search")
|
||||||
|
public Page<Message> search(@RequestBody MessageSearchRequest req) {
|
||||||
|
return messageService.search(req);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The catalog version the portals key their cache on; a read, never a bump. */
|
||||||
|
@RequestMapping("/catalogVersion")
|
||||||
|
public Map<String, Long> catalogVersion() {
|
||||||
|
return Map.of("version", catalogVersion.current());
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/findById/{id}")
|
@RequestMapping("/findById/{id}")
|
||||||
|
|||||||
+16
@@ -0,0 +1,16 @@
|
|||||||
|
package com.solvway.services.system.model;
|
||||||
|
|
||||||
|
import com.solvway.services.system.constant.SystemConstants.Area;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
/** Translations search: searchKey over the code, English and Chinese text; missingLocale lists labels lacking that language. */
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class MessageSearchRequest {
|
||||||
|
private String searchKey;
|
||||||
|
private Area area;
|
||||||
|
private String missingLocale;
|
||||||
|
private int page;
|
||||||
|
private int size;
|
||||||
|
}
|
||||||
+79
-1
@@ -3,16 +3,24 @@ package com.solvway.services.system.service;
|
|||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.solvway.services.system.constant.SystemConstants.Area;
|
import com.solvway.services.system.constant.SystemConstants.Area;
|
||||||
import com.solvway.services.system.model.Message;
|
import com.solvway.services.system.model.Message;
|
||||||
|
import com.solvway.services.system.model.MessageSearchRequest;
|
||||||
import com.solvway.services.system.repo.MessageRepository;
|
import com.solvway.services.system.repo.MessageRepository;
|
||||||
import com.solvway.services.system.util.paging.PageableRest;
|
import com.solvway.services.system.util.paging.PageableRest;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageImpl;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -48,9 +56,79 @@ public class MessageService {
|
|||||||
return messageRepository.findByCodeContainingOrderByIdAsc(searchKey, PageRequest.of(pageable.getNumber(), pageable.getSize(), pageable.getSort()));
|
return messageRepository.findByCodeContainingOrderByIdAsc(searchKey, PageRequest.of(pageable.getNumber(), pageable.getSize(), pageable.getSort()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CatalogVersionService catalogVersion;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upsert of one label. Code and area are a label's identity, so an update may not change them and a create may
|
||||||
|
* not reuse a code (ignoring case) in its area; both are 409s with a message code. Every accepted save bumps the
|
||||||
|
* catalog version in the same transaction, so the portals reload on their next request.
|
||||||
|
*/
|
||||||
@Transactional
|
@Transactional
|
||||||
public void save(Message message) {
|
public void save(Message message) {
|
||||||
messageRepository.save(message);
|
Message stored = message.getId() > 0 ? messageRepository.findById(message.getId()).orElse(null) : null;
|
||||||
|
if (stored != null) {
|
||||||
|
if (!Objects.equals(stored.getCode(), message.getCode()) || stored.getArea() != message.getArea()) {
|
||||||
|
throw new ResponseStatusException(HttpStatus.CONFLICT, "MsgsCodeAreaLocked");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Message existing = messageRepository.findByCodeAndArea(message.getCode(), message.getArea());
|
||||||
|
if (existing != null && existing.getCode() != null && existing.getCode().equalsIgnoreCase(message.getCode())) {
|
||||||
|
throw new ResponseStatusException(HttpStatus.CONFLICT, "MsgsDuplicateCode");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
messageRepository.save(message);
|
||||||
|
catalogVersion.bump();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Filters in memory, sorted by code. ponytail: fine for the ~3.5k area-A rows; move to SQL past ~50k. */
|
||||||
|
public Page<Message> search(MessageSearchRequest req) {
|
||||||
|
List<Message> rows = req.getArea() == null ? messageRepository.findAll() : messageRepository.findAllByArea(req.getArea());
|
||||||
|
String key = StringUtils.isEmpty(req.getSearchKey()) || req.getSearchKey().isBlank() ? null : lower(req.getSearchKey().trim());
|
||||||
|
String missing = StringUtils.isEmpty(req.getMissingLocale()) ? null : req.getMissingLocale();
|
||||||
|
List<Message> hits = new ArrayList<>();
|
||||||
|
for (Message m : rows) {
|
||||||
|
Map<String, String> text = parseContent(m.getContent());
|
||||||
|
if (key != null && !(lower(m.getCode()).contains(key) || lower(text.get("en_US")).contains(key) || lower(text.get("zh_CN")).contains(key))) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (missing != null && !isMissing(text, missing)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
hits.add(m);
|
||||||
|
}
|
||||||
|
hits.sort(Comparator.comparing((Message m) -> lower(m.getCode())));
|
||||||
|
int size = req.getSize() >= 1 ? req.getSize() : 20;
|
||||||
|
int number = Math.max(req.getPage(), 0);
|
||||||
|
int from = (int) Math.min((long) number * size, hits.size());
|
||||||
|
int to = Math.min(from + size, hits.size());
|
||||||
|
return new PageImpl<>(new ArrayList<>(hits.subList(from, to)), PageRequest.of(number, size), hits.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, String> parseContent(String json) {
|
||||||
|
if (StringUtils.isEmpty(json)) {
|
||||||
|
return Map.of();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Map<?, ?> raw = gson.fromJson(json, Map.class);
|
||||||
|
Map<String, String> out = new HashMap<>();
|
||||||
|
if (raw != null) {
|
||||||
|
raw.forEach((k, v) -> out.put(String.valueOf(k), v == null ? null : String.valueOf(v)));
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
return Map.of();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String lower(String s) {
|
||||||
|
return s == null ? "" : s.toLowerCase(Locale.ROOT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Absent, blank, or the English copied over. */
|
||||||
|
private static boolean isMissing(Map<String, String> text, String locale) {
|
||||||
|
String v = text.get(locale);
|
||||||
|
return v == null || v.isBlank() || (!"en_US".equals(locale) && v.equals(text.get("en_US")));
|
||||||
}
|
}
|
||||||
public Optional<Message> findById(Long id) {
|
public Optional<Message> findById(Long id) {
|
||||||
return messageRepository.findById(id);
|
return messageRepository.findById(id);
|
||||||
|
|||||||
+154
@@ -0,0 +1,154 @@
|
|||||||
|
package com.solvway.services.system.service;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.solvway.services.system.constant.SystemConstants.Area;
|
||||||
|
import com.solvway.services.system.model.Message;
|
||||||
|
import com.solvway.services.system.model.MessageSearchRequest;
|
||||||
|
import com.solvway.services.system.repo.MessageRepository;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/** Translations rules ported from babyboo message-admin-api: search, missing Chinese, save refusals, the version bump. */
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class MessageServiceTranslationsTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private MessageRepository repository;
|
||||||
|
@Mock
|
||||||
|
private CatalogVersionService catalogVersion;
|
||||||
|
|
||||||
|
private MessageService service;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
service = new MessageService();
|
||||||
|
ReflectionTestUtils.setField(service, "messageRepository", repository);
|
||||||
|
ReflectionTestUtils.setField(service, "gson", new Gson());
|
||||||
|
ReflectionTestUtils.setField(service, "catalogVersion", catalogVersion);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void searchMatchesCodeEnglishOrChineseIgnoringCase() {
|
||||||
|
when(repository.findAllByArea(Area.A)).thenReturn(List.of(
|
||||||
|
msg(1, "Leads", "Leads", "线索"),
|
||||||
|
msg(2, "Users", "Users", "用户"),
|
||||||
|
msg(3, "EventLeads", "Event leads", "活动线索")));
|
||||||
|
|
||||||
|
assertThat(codes(service.search(req("线索", null, 0, 10)))).containsExactly("EventLeads", "Leads");
|
||||||
|
assertThat(codes(service.search(req("users", null, 0, 10)))).containsExactly("Users");
|
||||||
|
assertThat(codes(service.search(req("EVENT", null, 0, 10)))).containsExactly("EventLeads");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void searchNeverMatchesLanguageKeyNames() {
|
||||||
|
when(repository.findAllByArea(Area.A)).thenReturn(List.of(msg(1, "Leads", "Leads", "线索")));
|
||||||
|
|
||||||
|
assertThat(service.search(req("en_US", null, 0, 10)).getTotalElements()).isZero();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void missingChineseMeansAbsentBlankOrCopiedEnglish() {
|
||||||
|
when(repository.findAllByArea(Area.A)).thenReturn(List.of(
|
||||||
|
msg(1, "Leads", "Leads", "线索"),
|
||||||
|
msg(2, "WhatsApp", "WhatsApp", "WhatsApp"),
|
||||||
|
msg(3, "Users", "Users", null),
|
||||||
|
msg(4, "Roles", "Roles", " ")));
|
||||||
|
|
||||||
|
assertThat(codes(service.search(req(null, "zh_CN", 0, 10)))).containsExactly("Roles", "Users", "WhatsApp");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void searchPagesAndCountsOnlyMatches() {
|
||||||
|
when(repository.findAllByArea(Area.A)).thenReturn(List.of(
|
||||||
|
msg(1, "A1", "a", "甲"), msg(2, "A2", "a", "甲"), msg(3, "A3", "a", "甲"), msg(4, "B1", "b", "乙")));
|
||||||
|
|
||||||
|
Page<Message> page = service.search(req("a", null, 1, 2));
|
||||||
|
|
||||||
|
assertThat(page.getTotalElements()).isEqualTo(3);
|
||||||
|
assertThat(codes(page)).containsExactly("A3");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void savingAnExistingLabelBumpsTheVersion() {
|
||||||
|
Message stored = msg(7, "Leads", "Leads", "线索");
|
||||||
|
when(repository.findById(7L)).thenReturn(Optional.of(stored));
|
||||||
|
Message edit = msg(7, "Leads", "Leads", "潜在客户");
|
||||||
|
|
||||||
|
service.save(edit);
|
||||||
|
|
||||||
|
verify(repository).save(edit);
|
||||||
|
verify(catalogVersion).bump();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void changingALabelsCodeIsRefused() {
|
||||||
|
when(repository.findById(7L)).thenReturn(Optional.of(msg(7, "Leads", "Leads", "线索")));
|
||||||
|
|
||||||
|
assertRefused(() -> service.save(msg(7, "Contacts", "Leads", "线索")), "MsgsCodeAreaLocked");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void creatingADuplicateCodeIgnoringCaseIsRefused() {
|
||||||
|
when(repository.findByCodeAndArea("leads", Area.A)).thenReturn(msg(7, "Leads", "Leads", "线索"));
|
||||||
|
|
||||||
|
assertRefused(() -> service.save(msg(0, "leads", "Leads", null)), "MsgsDuplicateCode");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void creatingANewCodeSavesAndBumps() {
|
||||||
|
when(repository.findByCodeAndArea("NewLabel", Area.A)).thenReturn(null);
|
||||||
|
Message created = msg(0, "NewLabel", "New label", "新标签");
|
||||||
|
|
||||||
|
service.save(created);
|
||||||
|
|
||||||
|
verify(repository).save(created);
|
||||||
|
verify(catalogVersion).bump();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertRefused(Runnable call, String reason) {
|
||||||
|
assertThatThrownBy(call::run).isInstanceOfSatisfying(ResponseStatusException.class, e -> {
|
||||||
|
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.CONFLICT);
|
||||||
|
assertThat(e.getReason()).isEqualTo(reason);
|
||||||
|
});
|
||||||
|
verify(repository, never()).save(any());
|
||||||
|
verify(catalogVersion, never()).bump();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MessageSearchRequest req(String key, String missing, int page, int size) {
|
||||||
|
MessageSearchRequest r = new MessageSearchRequest();
|
||||||
|
r.setSearchKey(key);
|
||||||
|
r.setMissingLocale(missing);
|
||||||
|
r.setArea(Area.A);
|
||||||
|
r.setPage(page);
|
||||||
|
r.setSize(size);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<String> codes(Page<Message> page) {
|
||||||
|
return page.getContent().stream().map(Message::getCode).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Message msg(long id, String code, String en, String zh) {
|
||||||
|
String content = zh == null ? "{\"en_US\":\"" + en + "\"}" : "{\"en_US\":\"" + en + "\",\"zh_CN\":\"" + zh + "\"}";
|
||||||
|
Message m = new Message(code, content, Area.A, null);
|
||||||
|
m.setId(id);
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user