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:
1 parent
d5384c6ac0
commit
66e2e488d9
6 files changed
+269
-1
No files matched your search
+5
@@ -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}";
|
||||
|
||||
+54
@@ -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());
|
||||
}
|
||||
}
|
||||
+65
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user