feat(notifications): admin bell API in mobile-api

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 19:46:06 +08:00
1 parent d5384c6ac0
commit 66e2e488d9
6 files changed
+269 -1

No files matched your search

@@ -7,6 +7,11 @@ public class RestApiSystemServiceConstants {
public static final String API_REST_SYSTEM_USER_FIND_BY_USERNAME = "/user/findByUsername/{username}";
public static final String API_REST_SYSTEM_USER_FIND_BY_ID = "/user/findById/{id}";
public static final String API_REST_SYSTEM_USER_ACTIVE_ADMIN_OF = "/user/{id}/activeAdminOf/{companyId}";
// add-lead-notifications: the staff bell (receiver = the caller's user id)
public static final String API_REST_SYSTEM_NOTIFICATION_INBOX = "/notification/inbox/";
public static final String API_REST_SYSTEM_NOTIFICATION_UNREAD_COUNT = "/notification/unreadCount/";
public static final String API_REST_SYSTEM_NOTIFICATION_MARK_READ = "/notification/markRead/";
public static final String API_REST_SYSTEM_NOTIFICATION_MARK_ALL_READ = "/notification/markAllRead/";
public static final String API_REST_SYSTEM_USER_ACTIVE_WITH_MODULE = "/user/activeWithModule?companyId={companyId}&module={module}";
public static final String API_REST_SYSTEM_USER_FIND_BY_EMAIL = "/user/findByEmail/{email}";
public static final String API_REST_SYSTEM_USER_FIND_BY_EMAIL_AND_ID_NOT = "/user/findByEmailAndIdNot/{email}/{id}";
@@ -0,0 +1,54 @@
package com.solvway.mobile.api.controller.admin;
import com.solvway.core.annotation.AppAuth;
import com.solvway.mobile.api.service.AdminNotificationService;
import com.solvway.mobile.api.useraccess.AdminModule;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/** add-lead-notifications: the signed-in user's own bell. Must stay under {@code controller..} for AppAccessAspect. */
@RestController
@RequestMapping("/api/admin")
public class AdminNotificationController {
private final AdminNotificationService service;
public AdminNotificationController(AdminNotificationService service) {
this.service = service;
}
@AppAuth
@AdminModule(AdminModule.ANY_ADMIN)
@GetMapping("/notifications")
public ResponseEntity<Map<String, Object>> list(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "20") int size) {
return ResponseEntity.ok(service.list(page, size));
}
@AppAuth
@AdminModule(AdminModule.ANY_ADMIN)
@GetMapping("/notifications/unread-count")
public ResponseEntity<Map<String, Object>> unreadCount() {
return ResponseEntity.ok(service.unreadCount());
}
@AppAuth
@AdminModule(AdminModule.ANY_ADMIN)
@PostMapping("/notifications/{id}/read")
public ResponseEntity<Void> markRead(@PathVariable long id) {
service.markRead(id);
return ResponseEntity.noContent().build();
}
@AppAuth
@AdminModule(AdminModule.ANY_ADMIN)
@PostMapping("/notifications/read-all")
public ResponseEntity<Map<String, Object>> markAllRead() {
return ResponseEntity.ok(service.markAllRead());
}
}
@@ -0,0 +1,65 @@
package com.solvway.mobile.api.service;
import com.solvway.core.model.User;
import com.solvway.core.utils.HttpReqRespUtils;
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.web.client.HttpClientErrorException;
import java.util.Map;
/**
* add-lead-notifications: the staff bell. Every call acts for the signed-in user, whose id comes from the token,
* never from the request; service-system only reads and marks that user's rows.
*/
@Service
public class AdminNotificationService {
private static final ParameterizedTypeReference<Map<String, Object>> MAP = new ParameterizedTypeReference<>() {};
private static final int MAX_PAGE = 50;
private final RestTemplateCustomUtil rest;
private final AdminAuthenticationService adminAuthenticationService;
public AdminNotificationService(RestTemplateCustomUtil rest, AdminAuthenticationService adminAuthenticationService) {
this.rest = rest;
this.adminAuthenticationService = adminAuthenticationService;
}
private long caller() {
User u = adminAuthenticationService.loadUserByToken(HttpReqRespUtils.getJwtToken());
if (u == null || u.getId() == null) throw new CodedResponseStatusException(HttpStatus.UNAUTHORIZED, "Not signed in", "UNAUTHORIZED");
return u.getId();
}
private String url(String api, Object... parts) {
StringBuilder b = new StringBuilder(rest.getURLforApiService(api));
for (int i = 0; i < parts.length; i++) b.append(i == 0 ? "" : "/").append(parts[i]);
return b.toString();
}
public Map<String, Object> list(int page, int size) {
int p = Math.max(page, 0);
int s = Math.min(Math.max(size, 1), MAX_PAGE);
return rest.getForEntity(url(RestApiSystemServiceConstants.API_REST_SYSTEM_NOTIFICATION_INBOX, caller(), p, s), MAP).getBody();
}
public Map<String, Object> unreadCount() {
return rest.getForEntity(url(RestApiSystemServiceConstants.API_REST_SYSTEM_NOTIFICATION_UNREAD_COUNT, caller()), MAP).getBody();
}
public void markRead(long id) {
try {
rest.postForEntity(url(RestApiSystemServiceConstants.API_REST_SYSTEM_NOTIFICATION_MARK_READ, caller(), id), Void.class, Map.of());
} catch (HttpClientErrorException.NotFound e) {
throw new CodedResponseStatusException(HttpStatus.NOT_FOUND, "Notification not found", "NOTIFICATION_NOT_FOUND");
}
}
public Map<String, Object> markAllRead() {
return rest.postForEntity(url(RestApiSystemServiceConstants.API_REST_SYSTEM_NOTIFICATION_MARK_ALL_READ, caller()), MAP, Map.of()).getBody();
}
}
@@ -0,0 +1,41 @@
package com.solvway.mobile.api.controller.admin;
import com.solvway.core.annotation.AppAuth;
import com.solvway.mobile.api.service.AdminNotificationService;
import com.solvway.mobile.api.useraccess.AdminModule;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
// add-lead-notifications: every signed-in staff user has the bell; no module, and no user id from the request.
class AdminNotificationControllerTest {
@Test
void everyEndpointNeedsASignedInStaffUserOnly() {
for (Method m : AdminNotificationController.class.getDeclaredMethods()) {
if (!java.lang.reflect.Modifier.isPublic(m.getModifiers())) continue;
assertThat(m.isAnnotationPresent(AppAuth.class)).as(m.getName()).isTrue();
assertThat(m.getAnnotation(AdminModule.class).value()).as(m.getName()).containsExactly(AdminModule.ANY_ADMIN);
}
}
@Test
void passesThrough() {
AdminNotificationService service = mock(AdminNotificationService.class);
AdminNotificationController c = new AdminNotificationController(service);
when(service.list(2, 20)).thenReturn(Map.of("total", 0));
when(service.unreadCount()).thenReturn(Map.of("count", 1));
assertThat(c.list(2, 20).getBody()).containsEntry("total", 0);
assertThat(c.unreadCount().getBody()).containsEntry("count", 1);
assertThat(c.markRead(5L).getStatusCode().value()).isEqualTo(204);
verify(service).markRead(5L);
c.markAllRead();
verify(service).markAllRead();
}
}
@@ -0,0 +1,103 @@
package com.solvway.mobile.api.service;
import com.solvway.core.model.User;
import com.solvway.core.utils.HttpReqRespUtils;
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.server.ResponseStatusException;
import java.util.List;
import java.util.Map;
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.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
// add-lead-notifications: the bell's admin API always acts for the signed-in user, never one named in the request.
@SuppressWarnings({"unchecked", "rawtypes"})
class AdminNotificationServiceTest {
private RestTemplateCustomUtil rest;
private AdminAuthenticationService auth;
private AdminNotificationService service;
private MockedStatic<HttpReqRespUtils> http;
@BeforeEach
void setUp() {
rest = mock(RestTemplateCustomUtil.class);
auth = mock(AdminAuthenticationService.class);
service = new AdminNotificationService(rest, auth);
when(rest.getURLforApiService(any())).thenAnswer(i -> "http://gw" + i.getArgument(0));
http = mockStatic(HttpReqRespUtils.class);
http.when(HttpReqRespUtils::getJwtToken).thenReturn("tok");
User u = new User();
u.setId(11L);
u.setCompanyId(2L);
when(auth.loadUserByToken("tok")).thenReturn(u);
}
@AfterEach
void close() {
http.close();
}
@Test
void listsTheCallersOwnNotifications() {
Map<String, Object> page = Map.of("items", List.of(), "total", 0);
when(rest.getForEntity(eq("http://gw/notification/inbox/11/1/20"), any(ParameterizedTypeReference.class)))
.thenReturn(ResponseEntity.ok(page));
assertThat(service.list(1, 20)).isEqualTo(page);
}
@Test
void capsThePageSize() {
when(rest.getForEntity(eq("http://gw/notification/inbox/11/0/50"), any(ParameterizedTypeReference.class)))
.thenReturn(ResponseEntity.ok(Map.of("items", List.of(), "total", 0)));
service.list(-3, 500);
verify(rest).getForEntity(eq("http://gw/notification/inbox/11/0/50"), any(ParameterizedTypeReference.class));
}
@Test
void countsTheCallersUnread() {
when(rest.getForEntity(eq("http://gw/notification/unreadCount/11"), any(ParameterizedTypeReference.class)))
.thenReturn(ResponseEntity.ok(Map.of("count", 3)));
assertThat(service.unreadCount()).containsEntry("count", 3);
}
@Test
void marksReadForTheCaller() {
service.markRead(5L);
verify(rest).postForEntity(eq("http://gw/notification/markRead/11/5"), eq(Void.class), any());
when(rest.postForEntity(eq("http://gw/notification/markAllRead/11"), any(ParameterizedTypeReference.class), any()))
.thenReturn(ResponseEntity.ok(Map.of("count", 2)));
assertThat(service.markAllRead()).containsEntry("count", 2);
verify(rest).postForEntity(eq("http://gw/notification/markAllRead/11"), any(ParameterizedTypeReference.class), any());
}
@Test
void anotherUsersNotificationIsNotFound() {
when(rest.postForEntity(eq("http://gw/notification/markRead/11/6"), eq(Void.class), any()))
.thenThrow(HttpClientErrorException.create(HttpStatus.NOT_FOUND, "nf", null, null, null));
assertThatThrownBy(() -> service.markRead(6L))
.isInstanceOfSatisfying(ResponseStatusException.class, e -> assertThat(e.getStatusCode().value()).isEqualTo(404));
}
@Test
void notSignedInIsRefused() {
when(auth.loadUserByToken("tok")).thenReturn(null);
assertThatThrownBy(() -> service.unreadCount())
.isInstanceOfSatisfying(ResponseStatusException.class, e -> assertThat(e.getStatusCode().value()).isEqualTo(401));
}
}