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:
1 parent
a482a3a6d0
commit
7ff5c77b78
5 files changed
+355
No files matched your search
+181
@@ -0,0 +1,181 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.core.model.Role;
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.useraccess.AdminCompanyScope;
|
||||
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.ArgumentCaptor;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.HashMap;
|
||||
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.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
class AdminDashboardServiceTest {
|
||||
|
||||
private static final String LEADS = "/counter/dashboard/leads";
|
||||
private static final String USER = "/user/findById/{id}";
|
||||
|
||||
private RestTemplateCustomUtil rest;
|
||||
private AdminAuthenticationService auth;
|
||||
private AdminDashboardService service;
|
||||
private MockedStatic<HttpReqRespUtils> http;
|
||||
private Map<String, Object> answer;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
rest = mock(RestTemplateCustomUtil.class);
|
||||
auth = mock(AdminAuthenticationService.class);
|
||||
service = new AdminDashboardService(rest, auth, new AdminCompanyScope(), Clock.fixed(Instant.parse("2026-11-30T17:00:00Z"), ZoneOffset.UTC));
|
||||
when(rest.getURLforApiService(any())).thenAnswer(i -> i.getArgument(0));
|
||||
http = mockStatic(HttpReqRespUtils.class);
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn("tok");
|
||||
answer = new HashMap<>();
|
||||
answer.put("captures", 10);
|
||||
answer.put("salespeople", List.of());
|
||||
when(rest.postForEntity(eq(LEADS), any(ParameterizedTypeReference.class), any())).thenAnswer(i -> ResponseEntity.ok(answer));
|
||||
caller(null, true);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void close() { http.close(); }
|
||||
|
||||
private void caller(Long companyId, boolean superAdmin) {
|
||||
User u = new User();
|
||||
u.setId(21L);
|
||||
u.setUsername("boss");
|
||||
u.setCompanyId(companyId);
|
||||
if (superAdmin) {
|
||||
Role r = new Role("Super");
|
||||
r.setId(1L);
|
||||
u.setRoles(List.of(r));
|
||||
}
|
||||
when(auth.loadUserByToken("tok")).thenReturn(u);
|
||||
}
|
||||
|
||||
private Map<String, Object> sentBody() {
|
||||
ArgumentCaptor<Object> c = ArgumentCaptor.forClass(Object.class);
|
||||
verify(rest).postForEntity(eq(LEADS), any(ParameterizedTypeReference.class), c.capture());
|
||||
return (Map<String, Object>) c.getValue();
|
||||
}
|
||||
|
||||
private void assertInvalidPeriod(String from, String to) {
|
||||
assertThatThrownBy(() -> service.leads(from, to, null)).isInstanceOfSatisfying(CodedResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
||||
assertThat(e.getCode()).isEqualTo("INVALID_PERIOD");
|
||||
});
|
||||
verify(rest, never()).postForEntity(eq(LEADS), any(ParameterizedTypeReference.class), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultsToMalaysianMonthToToday() {
|
||||
Map<String, Object> r = service.leads(null, null, null);
|
||||
assertThat(r).containsEntry("from", "2026-12-01").containsEntry("to", "2026-12-01").containsEntry("bucket", "DAY");
|
||||
assertThat(sentBody()).containsEntry("from", "2026-12-01").containsEntry("to", "2026-12-01");
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultsTheMissingEnd() {
|
||||
assertThat(service.leads("2026-11-20", null, null)).containsEntry("from", "2026-11-20").containsEntry("to", "2026-12-01");
|
||||
}
|
||||
|
||||
@Test
|
||||
void bucketIsDayUpTo62DaysThenMonth() {
|
||||
assertThat(service.leads("2026-01-01", "2026-03-03", null)).containsEntry("bucket", "DAY");
|
||||
assertThat(service.leads("2026-01-01", "2026-03-04", null)).containsEntry("bucket", "MONTH");
|
||||
}
|
||||
|
||||
@Test
|
||||
void badDateIsInvalidPeriod() {
|
||||
assertInvalidPeriod("11/01/2026", "2026-11-30");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromAfterToIsInvalidPeriod() {
|
||||
assertInvalidPeriod("2026-11-30", "2026-11-01");
|
||||
}
|
||||
|
||||
@Test
|
||||
void staffIsPinnedToOwnCompany() {
|
||||
caller(5L, false);
|
||||
service.leads("2026-11-01", "2026-11-30", 9L);
|
||||
assertThat(sentBody()).containsEntry("companyId", 5L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void superAdminPassesRequestedCompanyOrNull() {
|
||||
service.leads("2026-11-01", "2026-11-30", 2L);
|
||||
assertThat(sentBody()).containsEntry("companyId", 2L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void superAdminWithoutCompanySendsNull() {
|
||||
service.leads("2026-11-01", "2026-11-30", null);
|
||||
assertThat(sentBody()).containsEntry("companyId", null).containsEntry("bucket", "DAY").containsEntry("to", "2026-11-30");
|
||||
}
|
||||
|
||||
@Test
|
||||
void salespeopleGetNamesOneLookupPerUser() {
|
||||
answer.put("salespeople", List.of(
|
||||
Map.of("userId", 7, "assigned", 4), Map.of("userId", 7, "assigned", 1), Map.of("userId", 8, "assigned", 2),
|
||||
new HashMap<>(Map.of("assigned", 3))));
|
||||
when(rest.getForEntity(eq(USER), any(ParameterizedTypeReference.class), eq(7L))).thenReturn(ResponseEntity.ok(Map.of("name", "Aisyah")));
|
||||
when(rest.getForEntity(eq(USER), any(ParameterizedTypeReference.class), eq(8L))).thenReturn(ResponseEntity.ok(null));
|
||||
|
||||
List<Map<String, Object>> rows = (List<Map<String, Object>>) service.leads(null, null, null).get("salespeople");
|
||||
|
||||
assertThat(rows.get(0)).containsEntry("name", "Aisyah").containsEntry("assigned", 4);
|
||||
assertThat(rows.get(1)).containsEntry("name", "Aisyah");
|
||||
assertThat(rows.get(2)).containsEntry("name", null);
|
||||
assertThat(rows.get(3)).containsEntry("name", null);
|
||||
verify(rest, times(1)).getForEntity(eq(USER), any(ParameterizedTypeReference.class), eq(7L));
|
||||
verify(rest, times(2)).getForEntity(eq(USER), any(ParameterizedTypeReference.class), any(Long.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void periodOf731DaysIsAccepted() {
|
||||
assertThat(service.leads("2025-01-01", "2027-01-01", null)).containsEntry("bucket", "MONTH");
|
||||
}
|
||||
|
||||
@Test
|
||||
void periodOf732DaysIsRefused() {
|
||||
assertInvalidPeriod("2025-01-01", "2027-01-02");
|
||||
}
|
||||
|
||||
@Test
|
||||
void extendedYearDateIsInvalidPeriod() {
|
||||
assertInvalidPeriod("2026-11-01", "+999999999-12-31");
|
||||
assertInvalidPeriod("-999999999-01-01", "2026-11-01");
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingUserIsLookedUpOnce() {
|
||||
answer.put("salespeople", List.of(Map.of("userId", 8, "assigned", 2), Map.of("userId", 8, "assigned", 1)));
|
||||
when(rest.getForEntity(eq(USER), any(ParameterizedTypeReference.class), eq(8L))).thenReturn(ResponseEntity.ok(null));
|
||||
service.leads(null, null, null);
|
||||
verify(rest, times(1)).getForEntity(eq(USER), any(ParameterizedTypeReference.class), eq(8L));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user