feat(mobile-api): admin translations and languages APIs
Locales list, create and update; public enabled list; message catalog, version, search, detail, update, export and import. Refusals from services-system pass through as 409 with their reason code. 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
45bd81cdbb
commit
27e0ec7bff
8 files changed
+617
-2
No files matched your search
+9
@@ -60,6 +60,15 @@ public class RestApiSystemServiceConstants {
|
||||
public static final String API_REST_SYSTEM_ADMIN_ACTIVE_PASSWORD_RULES = "/passwordRule/adminActive";
|
||||
|
||||
//Message
|
||||
public static final String API_REST_SYSTEM_MESSAGE_SEARCH = "/message/search";
|
||||
public static final String API_REST_SYSTEM_MESSAGE_FIND_BY_ID = "/message/findById/{id}";
|
||||
public static final String API_REST_SYSTEM_MESSAGE_SAVE = "/message/save";
|
||||
public static final String API_REST_SYSTEM_MESSAGE_ALL_BY_AREA = "/message/getAllMessages/{area}";
|
||||
public static final String API_REST_SYSTEM_MESSAGE_CATALOG_VERSION = "/message/catalogVersion";
|
||||
public static final String API_REST_SYSTEM_LOCALE_FIND_ALL = "/locale/findAll";
|
||||
public static final String API_REST_SYSTEM_LOCALE_FIND_ALL_ENABLED = "/locale/findAllEnabled";
|
||||
public static final String API_REST_SYSTEM_LOCALE_SAVE = "/locale/save";
|
||||
public static final String API_REST_SYSTEM_LOCALE_UPDATE = "/locale/update";
|
||||
public static final String API_REST_SYSTEM_MESSAGE_JSON_BY_AREA_AND_LOCALE = "/message/getMessageJsonByAreaAndLocale/{area}/{locale}";
|
||||
|
||||
//Nationality
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.solvway.mobile.api.controller;
|
||||
|
||||
import com.solvway.core.annotation.AppAuth;
|
||||
import com.solvway.mobile.api.service.AdminTranslationService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/** Enabled languages for the sign-in page switcher: API key only, no session. */
|
||||
@RestController
|
||||
@RequestMapping("/api/public")
|
||||
@RequiredArgsConstructor
|
||||
public class PublicLocaleController {
|
||||
private final AdminTranslationService translations;
|
||||
|
||||
@AppAuth(skipValidate = true)
|
||||
@GetMapping("/locales")
|
||||
public ResponseEntity<List<Map<String, Object>>> enabled() {
|
||||
return ResponseEntity.ok(translations.locales(true));
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.solvway.mobile.api.controller.admin;
|
||||
|
||||
import com.solvway.core.annotation.AppAuth;
|
||||
import com.solvway.mobile.api.service.AdminTranslationService;
|
||||
import com.solvway.mobile.api.useraccess.AdminModule;
|
||||
import com.solvway.mobile.api.useraccess.Audited;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/** Languages, ported from babyboo. Any signed-in admin reads the list for the switcher; writes need BO_LOCALES. */
|
||||
@RestController
|
||||
@RequestMapping("/api/admin")
|
||||
@RequiredArgsConstructor
|
||||
public class AdminLocaleController {
|
||||
private final AdminTranslationService translations;
|
||||
|
||||
@AppAuth
|
||||
@AdminModule(AdminModule.ANY_ADMIN)
|
||||
@GetMapping("/locales")
|
||||
public ResponseEntity<List<Map<String, Object>>> list() {
|
||||
return ResponseEntity.ok(translations.locales(false));
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@AdminModule("BO_LOCALES")
|
||||
@Audited(action = "LOCALE_CREATE")
|
||||
@PostMapping("/locales")
|
||||
public ResponseEntity<Map<String, Object>> create(@RequestBody Map<String, Object> body) {
|
||||
return ResponseEntity.ok(translations.createLocale(body));
|
||||
}
|
||||
|
||||
/** The path id wins over any id in the body. */
|
||||
@AppAuth
|
||||
@AdminModule("BO_LOCALES")
|
||||
@Audited(action = "LOCALE_UPDATE")
|
||||
@PutMapping("/locales/{id}")
|
||||
public ResponseEntity<Map<String, Object>> update(@PathVariable Long id, @RequestBody Map<String, Object> body) {
|
||||
return ResponseEntity.ok(translations.updateLocale(id, body));
|
||||
}
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
package com.solvway.mobile.api.controller.admin;
|
||||
|
||||
import com.solvway.core.annotation.AppAuth;
|
||||
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.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Back-office labels (area A), ported from babyboo and trimmed: no area filter, no create, no delete.
|
||||
* The catalog and its version need only the API key, because the sign-in page shows labels before a session exists.
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/admin")
|
||||
@RequiredArgsConstructor
|
||||
public class AdminMessageController {
|
||||
private final AdminTranslationService translations;
|
||||
private final SystemService systemService;
|
||||
|
||||
@AppAuth(skipValidate = true)
|
||||
@GetMapping("/messages/catalog/{locale}")
|
||||
public ResponseEntity<Map<String, String>> catalog(@PathVariable String locale) {
|
||||
return ResponseEntity.ok(systemService.findMessageCatalog("A", locale));
|
||||
}
|
||||
|
||||
@AppAuth(skipValidate = true)
|
||||
@GetMapping("/messages/catalog-version")
|
||||
public ResponseEntity<Map<String, Object>> catalogVersion() {
|
||||
return ResponseEntity.ok(translations.catalogVersion());
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@AdminModule("BO_MESSAGES")
|
||||
@GetMapping("/messages")
|
||||
public ResponseEntity<Map<String, Object>> list(@RequestParam(required = false) String searchKey,
|
||||
@RequestParam(required = false) String missingLocale,
|
||||
@RequestParam(defaultValue = "0") int page,
|
||||
@RequestParam(defaultValue = "20") int size) {
|
||||
return ResponseEntity.ok(translations.search(searchKey, missingLocale, page, size));
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@AdminModule("BO_MESSAGES")
|
||||
@Audited(action = "MESSAGES_EXPORT")
|
||||
@GetMapping("/messages/export")
|
||||
public ResponseEntity<List<Map<String, Object>>> export() {
|
||||
return ResponseEntity.ok(translations.export());
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@AdminModule("BO_MESSAGES")
|
||||
@GetMapping("/messages/{id}")
|
||||
public ResponseEntity<Map<String, Object>> detail(@PathVariable Long id) {
|
||||
return ResponseEntity.ok(translations.message(id));
|
||||
}
|
||||
|
||||
/** The path id wins; only the content changes. */
|
||||
@AppAuth
|
||||
@AdminModule("BO_MESSAGES")
|
||||
@Audited(action = "MESSAGE_UPDATE")
|
||||
@PutMapping("/messages/{id}")
|
||||
public ResponseEntity<Void> update(@PathVariable Long id, @RequestBody Map<String, Object> body) {
|
||||
translations.updateMessage(id, body);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@AdminModule("BO_MESSAGES")
|
||||
@Audited(action = "MESSAGES_IMPORT")
|
||||
@PostMapping("/messages/import")
|
||||
public ResponseEntity<Map<String, Object>> importMessages(@RequestBody List<Map<String, Object>> rows) {
|
||||
return ResponseEntity.ok(translations.importMessages(rows));
|
||||
}
|
||||
}
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.solvway.mobile.api.constant.api.RestApiSystemServiceConstants;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Translations (area A labels) and languages for the admin portal, ported from babyboo
|
||||
* AdminMessageService and AdminLocaleService, trimmed to area A with no create or delete of labels.
|
||||
*/
|
||||
@Service
|
||||
public class AdminTranslationService {
|
||||
|
||||
static final String AREA = "A";
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
private static final ParameterizedTypeReference<Map<String, Object>> MAP = new ParameterizedTypeReference<>() {};
|
||||
private static final ParameterizedTypeReference<List<Map<String, Object>>> MAP_LIST = new ParameterizedTypeReference<>() {};
|
||||
|
||||
private final RestTemplateCustomUtil rest;
|
||||
|
||||
public AdminTranslationService(RestTemplateCustomUtil rest) {
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
// ---- languages
|
||||
|
||||
public List<Map<String, Object>> locales(boolean enabledOnly) {
|
||||
String api = enabledOnly ? RestApiSystemServiceConstants.API_REST_SYSTEM_LOCALE_FIND_ALL_ENABLED
|
||||
: RestApiSystemServiceConstants.API_REST_SYSTEM_LOCALE_FIND_ALL;
|
||||
List<Map<String, Object>> body = rest.getForEntity(url(api), MAP_LIST).getBody();
|
||||
return body == null ? List.of() : body;
|
||||
}
|
||||
|
||||
public Map<String, Object> createLocale(Map<String, Object> body) {
|
||||
Map<String, Object> sent = new LinkedHashMap<>(body);
|
||||
sent.remove("id");
|
||||
return relay(() -> rest.postForEntity(url(RestApiSystemServiceConstants.API_REST_SYSTEM_LOCALE_SAVE), MAP, sent).getBody());
|
||||
}
|
||||
|
||||
public Map<String, Object> updateLocale(Long id, Map<String, Object> body) {
|
||||
Map<String, Object> sent = new LinkedHashMap<>(body);
|
||||
sent.put("id", id);
|
||||
return relay(() -> rest.postForEntity(url(RestApiSystemServiceConstants.API_REST_SYSTEM_LOCALE_UPDATE), MAP, sent).getBody());
|
||||
}
|
||||
|
||||
// ---- labels
|
||||
|
||||
public Map<String, Object> catalogVersion() {
|
||||
Map<String, Object> body = rest.getForEntity(url(RestApiSystemServiceConstants.API_REST_SYSTEM_MESSAGE_CATALOG_VERSION), MAP).getBody();
|
||||
return body == null ? Map.of("version", 0) : body;
|
||||
}
|
||||
|
||||
public Map<String, Object> search(String searchKey, String missingLocale, int page, int size) {
|
||||
Map<String, Object> req = new HashMap<>();
|
||||
req.put("searchKey", searchKey);
|
||||
req.put("missingLocale", missingLocale);
|
||||
req.put("area", AREA);
|
||||
req.put("page", page);
|
||||
req.put("size", size);
|
||||
return rest.postForEntity(url(RestApiSystemServiceConstants.API_REST_SYSTEM_MESSAGE_SEARCH), MAP, req).getBody();
|
||||
}
|
||||
|
||||
/** Area A only: other areas belong to portals Co-Lite does not run, so they answer 404. */
|
||||
public Map<String, Object> message(Long id) {
|
||||
Map<String, Object> row = rest.getForEntity(url(RestApiSystemServiceConstants.API_REST_SYSTEM_MESSAGE_FIND_BY_ID), MAP, id).getBody();
|
||||
if (row == null || !AREA.equals(row.get("area"))) {
|
||||
throw new CodedResponseStatusException(HttpStatus.NOT_FOUND, "Translation not found", "MsgsNotFound");
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
/** The path id wins; code and area stay as stored, only content changes. */
|
||||
public void updateMessage(Long id, Map<String, Object> body) {
|
||||
Map<String, Object> stored = message(id);
|
||||
save(row(id, (String) stored.get("code"), body.get("content"), stored.get("description")));
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> export() {
|
||||
List<Map<String, Object>> body = rest.getForEntity(url(RestApiSystemServiceConstants.API_REST_SYSTEM_MESSAGE_ALL_BY_AREA), MAP_LIST, AREA).getBody();
|
||||
return body == null ? List.of() : body;
|
||||
}
|
||||
|
||||
/** Matches rows by code ignoring case, never by the file's id; one failed row does not stop the rest. */
|
||||
public Map<String, Object> importMessages(List<Map<String, Object>> rows) {
|
||||
Map<String, Map<String, Object>> byCode = new HashMap<>();
|
||||
for (Map<String, Object> m : export()) byCode.put(lower(m.get("code")), m);
|
||||
int created = 0;
|
||||
int updated = 0;
|
||||
List<Map<String, Object>> errors = new ArrayList<>();
|
||||
for (int i = 0; i < rows.size(); i++) {
|
||||
Map<String, Object> r = rows.get(i);
|
||||
Object code = r.get("code");
|
||||
if (!(code instanceof String c) || !StringUtils.hasText(c)) {
|
||||
errors.add(Map.of("index", i, "code", "", "error", "MsgsCodeRequired"));
|
||||
continue;
|
||||
}
|
||||
Map<String, Object> existing = byCode.get(lower(c));
|
||||
try {
|
||||
if (existing == null) {
|
||||
save(row(0L, c.trim(), r.get("content"), r.get("description")));
|
||||
created++;
|
||||
} else {
|
||||
Object description = r.containsKey("description") ? r.get("description") : existing.get("description");
|
||||
save(row(((Number) existing.get("id")).longValue(), (String) existing.get("code"), r.get("content"), description));
|
||||
updated++;
|
||||
}
|
||||
} catch (CodedResponseStatusException e) {
|
||||
errors.add(Map.of("index", i, "code", c, "error", e.getCode()));
|
||||
}
|
||||
}
|
||||
Map<String, Object> summary = new LinkedHashMap<>();
|
||||
summary.put("created", created);
|
||||
summary.put("updated", updated);
|
||||
summary.put("errors", errors);
|
||||
return summary;
|
||||
}
|
||||
|
||||
private void save(Map<String, Object> row) {
|
||||
relay(() -> rest.postForEntity(url(RestApiSystemServiceConstants.API_REST_SYSTEM_MESSAGE_SAVE), Void.class, row));
|
||||
}
|
||||
|
||||
private static Map<String, Object> row(long id, String code, Object content, Object description) {
|
||||
Map<String, Object> m = new LinkedHashMap<>();
|
||||
m.put("id", id);
|
||||
m.put("code", code);
|
||||
m.put("area", AREA);
|
||||
m.put("content", content);
|
||||
m.put("description", description);
|
||||
return m;
|
||||
}
|
||||
|
||||
/** services-system refuses with a ProblemDetail whose detail is the reason code; pass it through as a 409. */
|
||||
private static <T> T relay(java.util.function.Supplier<T> call) {
|
||||
try {
|
||||
return call.get();
|
||||
} catch (HttpClientErrorException.Conflict | HttpClientErrorException.NotFound e) {
|
||||
String reason = detail(e);
|
||||
throw new CodedResponseStatusException(e.getStatusCode(), reason, reason);
|
||||
}
|
||||
}
|
||||
|
||||
private static String detail(HttpClientErrorException e) {
|
||||
try {
|
||||
Object d = JSON.readValue(e.getResponseBodyAsString(), Map.class).get("detail");
|
||||
if (d != null) return String.valueOf(d);
|
||||
} catch (JsonProcessingException ignored) {
|
||||
// fall through to the generic reason
|
||||
}
|
||||
return e.getStatusCode().value() == 404 ? "NotFound" : "Conflict";
|
||||
}
|
||||
|
||||
private static String lower(Object code) {
|
||||
return code == null ? "" : code.toString().toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
private String url(String api) {
|
||||
return rest.getURLforApiService(api);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user