feat(mobile-api): GET /api/admin/dashboard/leads

BO_DASHBOARD-gated lead dashboard: Malaysian-month default period, at
most 731 days, DAY or MONTH buckets, company scope, salesperson names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-12-01 10:22:35 +08:00
1 parent a482a3a6d0
commit 7ff5c77b78
5 files changed
+355

No files matched your search

@@ -44,6 +44,7 @@ public class RestApiCounterServiceConstants {
public static final String API_REST_COUNTER_LEAD_SEARCH="/counter/lead/search";
public static final String API_REST_COUNTER_LEAD_DETAIL="/counter/lead/detail/{leadUuid}";
public static final String API_REST_COUNTER_LEAD_UPDATE="/counter/lead/update";
public static final String API_REST_COUNTER_DASHBOARD_LEADS="/counter/dashboard/leads";
public static final String API_REST_COUNTER_EVENT_LEAD_CAPTURE="/counter/eventLead/capture";
public static final String API_REST_COUNTER_EVENT_LEAD_FIND_BY_UUID="/counter/eventLead/findByUuid/{eventLeadUuid}";
public static final String API_REST_COUNTER_EVENT_LEAD_UPDATE="/counter/eventLead/update";
@@ -0,0 +1,30 @@
package com.solvway.mobile.api.controller.admin;
import com.solvway.core.annotation.AppAuth;
import com.solvway.mobile.api.service.AdminDashboardService;
import com.solvway.mobile.api.useraccess.AdminModule;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/** Lead dashboard. Must stay under {@code controller..} for AppAccessAspect. */
@RestController
@RequestMapping("/api/admin/dashboard")
@RequiredArgsConstructor
public class AdminDashboardController {
private final AdminDashboardService adminDashboardService;
@AppAuth
@AdminModule("BO_DASHBOARD")
@GetMapping("/leads")
public ResponseEntity<Map<String, Object>> leads(@RequestParam(required = false) String from,
@RequestParam(required = false) String to,
@RequestParam(required = false) Long companyId) {
return ResponseEntity.ok(adminDashboardService.leads(from, to, companyId));
}
}
@@ -0,0 +1,106 @@
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.RestApiCounterServiceConstants;
import com.solvway.mobile.api.constant.api.RestApiSystemServiceConstants;
import com.solvway.mobile.api.useraccess.AdminCompanyScope;
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import java.time.Clock;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static com.solvway.mobile.api.service.AdminEventLeadService.error;
/** Lead dashboard for the admin portal: validates the period and company scope here; services-counter aggregates. */
@Service
@SuppressWarnings("unchecked")
public class AdminDashboardService {
private static final ParameterizedTypeReference<Map<String, Object>> MAP = new ParameterizedTypeReference<>() {};
private static final ZoneId KL = ZoneId.of("Asia/Kuala_Lumpur");
private final RestTemplateCustomUtil restTemplateUtil;
private final AdminAuthenticationService adminAuthenticationService;
private final AdminCompanyScope scope;
private final Clock clock;
public AdminDashboardService(RestTemplateCustomUtil restTemplateUtil, AdminAuthenticationService adminAuthenticationService, AdminCompanyScope scope) {
this(restTemplateUtil, adminAuthenticationService, scope, Clock.systemUTC());
}
AdminDashboardService(RestTemplateCustomUtil restTemplateUtil, AdminAuthenticationService adminAuthenticationService, AdminCompanyScope scope, Clock clock) {
this.restTemplateUtil = restTemplateUtil;
this.adminAuthenticationService = adminAuthenticationService;
this.scope = scope;
this.clock = clock;
}
public Map<String, Object> leads(String fromParam, String toParam, Long companyId) {
Long company = scope.scope(caller(), companyId);
LocalDate today = LocalDate.now(clock.withZone(KL));
LocalDate from = date(fromParam, today.withDayOfMonth(1));
LocalDate to = date(toParam, today);
if (from.isAfter(to)) throw invalid("Start date must be on or before end date.");
long days = ChronoUnit.DAYS.between(from, to) + 1;
if (days > 731) throw invalid("The period can be at most 731 days.");
String bucket = days <= 62 ? "DAY" : "MONTH";
Map<String, Object> body = new LinkedHashMap<>();
body.put("companyId", company);
body.put("from", from.toString());
body.put("to", to.toString());
body.put("bucket", bucket);
Map<String, Object> res = restTemplateUtil.postForEntity(restTemplateUtil.getURLforApiService(RestApiCounterServiceConstants.API_REST_COUNTER_DASHBOARD_LEADS), MAP, body).getBody();
Map<String, Object> out = new LinkedHashMap<>(res == null ? Map.of() : res);
out.put("from", from.toString());
out.put("to", to.toString());
out.put("bucket", bucket);
Map<Long, Map<String, Object>> users = new HashMap<>();
List<Map<String, Object>> rows = out.get("salespeople") == null ? List.of() : (List<Map<String, Object>>) out.get("salespeople");
out.put("salespeople", rows.stream().map(r -> {
Map<String, Object> m = new LinkedHashMap<>(r);
m.put("name", r.get("userId") instanceof Number n ? name(n.longValue(), users) : null);
return m;
}).toList());
return out;
}
/** User name, or null when the user no longer exists; one lookup per distinct id per request. */
private String name(long id, Map<Long, Map<String, Object>> users) {
if (!users.containsKey(id)) { // computeIfAbsent would not cache a missing user
users.put(id, restTemplateUtil.getForEntity(restTemplateUtil.getURLforApiService(RestApiSystemServiceConstants.API_REST_SYSTEM_USER_FIND_BY_ID), MAP, id).getBody());
}
Map<String, Object> u = users.get(id);
return u == null || u.get("name") == null ? null : String.valueOf(u.get("name"));
}
private static LocalDate date(String s, LocalDate dflt) {
if (s == null || s.isBlank()) return dflt;
try {
return LocalDate.parse(s.trim());
} catch (DateTimeParseException e) {
throw invalid("Dates must be yyyy-MM-dd.");
}
}
private static RuntimeException invalid(String message) {
return error(HttpStatus.BAD_REQUEST, message, "INVALID_PERIOD", null);
}
private User caller() {
User caller = adminAuthenticationService.loadUserByToken(HttpReqRespUtils.getJwtToken());
if (caller == null) throw error(HttpStatus.UNAUTHORIZED, "Not signed in", "UNAUTHORIZED", null);
return caller;
}
}