feat(services-counter): store each event's team of salespeople

GET /counter/eventSalesperson/findByEventUuid/{eventUuid} and
POST /counter/eventSalesperson/replace (bulk delete then insert, one transaction).

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-11-26 23:57:50 +08:00
1 parent 814c51da3d
commit 70e0651a75
8 files changed
+429 -1

No files matched your search

+1 -1
View File
@@ -9,7 +9,7 @@ Babyboo source: `~/Projects/babyboo` (read only). Tastefully backoffice rules: `
## 2. services-counter event team
- [ ] 2.1 Add `EventSalesperson` entity and repository, and `EventSalespersonController` with `GET /counter/eventSalesperson/findByEventUuid/{eventUuid}` and `POST /counter/eventSalesperson/replace` (`{eventUuid, companyId, userIds, doneBy}`; delete the event's rows and insert the set in one transaction; duplicate ids collapse to one row). Tests first: replace then find returns the new set; replacing with an empty list clears it; a failure midway leaves the old set. Verify: `mvn -T 1C -B -q -pl solvway-services-counter -am verify`.
- [x] 2.1 Add `EventSalesperson` entity and repository, and `EventSalespersonController` with `GET /counter/eventSalesperson/findByEventUuid/{eventUuid}` and `POST /counter/eventSalesperson/replace` (`{eventUuid, companyId, userIds, doneBy}`; delete the event's rows and insert the set in one transaction; duplicate ids collapse to one row). Tests first: replace then find returns the new set; replacing with an empty list clears it; a failure midway leaves the old set. Verify: `mvn -T 1C -B -q -pl solvway-services-counter -am verify`.
- Scope: `solvway-services-counter/src/**`
## 3. mobile-api events
@@ -0,0 +1,28 @@
package com.solvway.services.counter.controller;
import com.solvway.services.counter.dto.EventSalespersonReplaceRequest;
import com.solvway.services.counter.model.EventSalesperson;
import com.solvway.services.counter.service.EventSalespersonService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/counter/eventSalesperson")
@AllArgsConstructor
@Slf4j
public class EventSalespersonController {
private final EventSalespersonService eventSalespersonService;
@GetMapping("/findByEventUuid/{eventUuid}")
public List<EventSalesperson> findByEventUuid(@PathVariable("eventUuid") long eventUuid) {
return eventSalespersonService.findByEventUuid(eventUuid);
}
@PostMapping("/replace")
public List<EventSalesperson> replace(@RequestBody EventSalespersonReplaceRequest body) {
return eventSalespersonService.replace(body.eventUuid(), body.companyId(), body.userIds(), body.doneBy());
}
}
@@ -0,0 +1,8 @@
package com.solvway.services.counter.dto;
public record EventSalespersonReplaceRequest(
long eventUuid,
long companyId,
java.util.List<Long> userIds,
String doneBy
) {}
@@ -0,0 +1,24 @@
package com.solvway.services.counter.model;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDateTime;
@Getter
@Setter
@Entity
@Table(name = "event_salesperson", uniqueConstraints = @UniqueConstraint(columnNames = {"event_uuid", "user_id"}))
public class EventSalesperson {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private long companyId;
private long eventUuid;
private long userId;
@Column(updatable = false)
private String createBy;
@Column(updatable = false)
private LocalDateTime createDateTime = LocalDateTime.now();
}
@@ -0,0 +1,19 @@
package com.solvway.services.counter.repo;
import com.solvway.services.counter.model.EventSalesperson;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface EventSalespersonRepository extends JpaRepository<EventSalesperson, Long> {
List<EventSalesperson> findByEventUuid(long eventUuid);
@Modifying
@Query("delete from EventSalesperson e where e.eventUuid = :eventUuid")
void deleteByEventUuid(@Param("eventUuid") long eventUuid);
}
@@ -0,0 +1,51 @@
package com.solvway.services.counter.service;
import com.solvway.services.counter.model.EventSalesperson;
import com.solvway.services.counter.repo.EventSalespersonRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@Service
@Slf4j
@AllArgsConstructor
public class EventSalespersonService {
private final EventSalespersonRepository eventSalespersonRepository;
public List<EventSalesperson> findByEventUuid(long eventUuid) {
return eventSalespersonRepository.findByEventUuid(eventUuid);
}
@Transactional
public List<EventSalesperson> replace(long eventUuid, long companyId, List<Long> userIds, String doneBy) {
eventSalespersonRepository.deleteByEventUuid(eventUuid);
List<Long> effective = userIds == null ? List.of() : userIds;
List<Long> distinct = effective.stream()
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());
List<EventSalesperson> rows = distinct.stream()
.map(userId -> {
EventSalesperson e = new EventSalesperson();
e.setCompanyId(companyId);
e.setEventUuid(eventUuid);
e.setUserId(userId);
e.setCreateBy(doneBy);
return e;
})
.collect(Collectors.toList());
if (rows.isEmpty()) {
return List.of();
}
return eventSalespersonRepository.saveAll(rows);
}
}
@@ -0,0 +1,75 @@
package com.solvway.services.counter.controller;
import com.solvway.services.counter.dto.EventSalespersonReplaceRequest;
import com.solvway.services.counter.model.EventSalesperson;
import com.solvway.services.counter.service.EventSalespersonService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.time.LocalDateTime;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* EventSalespersonController delegates to service and returns results.
*/
@ExtendWith(MockitoExtension.class)
class EventSalespersonControllerTest {
private static final long EVENT_UUID = 100L;
private static final long COMPANY_ID = 42L;
private static final String DONE_BY = "admin";
@Mock private EventSalespersonService eventSalespersonService;
private EventSalespersonController controller;
@BeforeEach
void setUp() {
controller = new EventSalespersonController(eventSalespersonService);
}
@Test
void findByEventUuidDelegatesToService() {
EventSalesperson row = makeSalesperson(1L, EVENT_UUID, COMPANY_ID, 10L);
when(eventSalespersonService.findByEventUuid(EVENT_UUID)).thenReturn(List.of(row));
List<EventSalesperson> result = controller.findByEventUuid(EVENT_UUID);
assertThat(result).hasSize(1).first().isSameAs(row);
verify(eventSalespersonService).findByEventUuid(EVENT_UUID);
}
@Test
void replacePassesBodyFieldsAndReturnsServiceResult() {
EventSalesperson row = makeSalesperson(1L, EVENT_UUID, COMPANY_ID, 10L);
EventSalespersonReplaceRequest body = new EventSalespersonReplaceRequest(
EVENT_UUID, COMPANY_ID, List.of(10L), DONE_BY);
when(eventSalespersonService.replace(eq(EVENT_UUID), eq(COMPANY_ID), eq(List.of(10L)), eq(DONE_BY)))
.thenReturn(List.of(row));
List<EventSalesperson> result = controller.replace(body);
assertThat(result).hasSize(1).first().isSameAs(row);
verify(eventSalespersonService).replace(EVENT_UUID, COMPANY_ID, List.of(10L), DONE_BY);
}
private static EventSalesperson makeSalesperson(Long id, long eventUuid, long companyId, long userId) {
EventSalesperson e = new EventSalesperson();
e.setId(id);
e.setEventUuid(eventUuid);
e.setCompanyId(companyId);
e.setUserId(userId);
e.setCreateBy("admin");
e.setCreateDateTime(LocalDateTime.now());
return e;
}
}
@@ -0,0 +1,223 @@
package com.solvway.services.counter.service;
import com.solvway.services.counter.model.EventSalesperson;
import com.solvway.services.counter.repo.EventSalespersonRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* EventSalespersonService.replace() — delete-old, save-distinct, collapse-duplicates.
*/
@ExtendWith(MockitoExtension.class)
class EventSalespersonServiceReplaceTest {
private static final long EVENT_UUID = 100L;
private static final long COMPANY_ID = 42L;
private static final String DONE_BY = "admin";
@Mock private EventSalespersonRepository repository;
private EventSalespersonService service;
@BeforeEach
void setUp() throws Exception {
service = new EventSalespersonService(repository);
}
// --- Test 1: replace deletes existing rows before saving (InOrder) ---
@Test
void replaceDeletesBeforeSaving() {
EventSalesperson row = makeSalesperson(1L, EVENT_UUID, COMPANY_ID, 10L, "admin");
when(repository.saveAll(any())).thenReturn(List.of(row));
service.replace(EVENT_UUID, COMPANY_ID, List.of(10L), DONE_BY);
org.mockito.InOrder inOrder = inOrder(repository);
inOrder.verify(repository).deleteByEventUuid(EVENT_UUID);
inOrder.verify(repository).saveAll(any());
}
// --- Test 2: saves one row per distinct user id ---
@Test
void replaceSavesOneRowPerDistinctId() {
when(repository.saveAll(any()))
.thenAnswer(inv -> {
List<EventSalesperson> args = inv.getArgument(0);
return args.stream()
.map(e -> {
EventSalesperson copy = new EventSalesperson();
copy.setId(e.getUserId());
copy.setEventUuid(EVENT_UUID);
copy.setCompanyId(COMPANY_ID);
copy.setUserId(e.getUserId());
copy.setCreateBy(DONE_BY);
copy.setCreateDateTime(e.getCreateDateTime());
return copy;
})
.toList();
});
List<EventSalesperson> result = service.replace(EVENT_UUID, COMPANY_ID, List.of(10L, 20L), DONE_BY);
ArgumentCaptor<List<EventSalesperson>> captor = ArgumentCaptor.forClass(List.class);
verify(repository).saveAll(captor.capture());
assertThat(captor.getValue()).hasSize(2);
assertThat(result).hasSize(2);
}
// --- Test 3: collapses duplicate user ids ---
@Test
void replaceCollapsesDuplicateIds() {
when(repository.saveAll(any()))
.thenAnswer(inv -> {
List<EventSalesperson> args = inv.getArgument(0);
return args.stream()
.map(e -> {
EventSalesperson copy = new EventSalesperson();
copy.setId(1L);
copy.setEventUuid(EVENT_UUID);
copy.setCompanyId(COMPANY_ID);
copy.setUserId(e.getUserId());
copy.setCreateBy(DONE_BY);
copy.setCreateDateTime(e.getCreateDateTime());
return copy;
})
.toList();
});
List<EventSalesperson> result = service.replace(EVENT_UUID, COMPANY_ID, List.of(10L, 10L, 10L), DONE_BY);
ArgumentCaptor<List<EventSalesperson>> captor = ArgumentCaptor.forClass(List.class);
verify(repository).saveAll(captor.capture());
assertThat(captor.getValue()).hasSize(1);
assertThat(result).hasSize(1);
}
// --- Test 4: null or empty list deletes and saves nothing ---
@Test
void replaceWithEmptyListDeletesAndSavesNothing() {
service.replace(EVENT_UUID, COMPANY_ID, Collections.emptyList(), DONE_BY);
verify(repository).deleteByEventUuid(EVENT_UUID);
verify(repository, never()).saveAll(any());
}
// --- Test 5: replace method carries @Transactional ---
@Test
void replaceMethodHasTransactionalAnnotation() throws Exception {
Method replaceMethod = EventSalespersonService.class.getDeclaredMethod(
"replace", long.class, long.class, List.class, String.class);
assertThat(replaceMethod.isAnnotationPresent(org.springframework.transaction.annotation.Transactional.class))
.isTrue();
}
// --- Test 6: findByEventUuid returns repository rows ---
@Test
void findByEventUuidReturnsRepositoryRows() {
EventSalesperson row = makeSalesperson(1L, EVENT_UUID, COMPANY_ID, 10L, "admin");
when(repository.findByEventUuid(EVENT_UUID)).thenReturn(List.of(row));
List<EventSalesperson> result = service.findByEventUuid(EVENT_UUID);
assertThat(result).hasSize(1).first().isSameAs(row);
}
// --- Test 7: deleteByEventUuid carries a @Query whose value starts with "delete" ---
@Test
void deleteByEventUuidHasDeleteQuery() throws Exception {
Method deleteMethod = EventSalespersonRepository.class
.getDeclaredMethod("deleteByEventUuid", long.class);
assertThat(deleteMethod.isAnnotationPresent(Modifying.class)).isTrue();
Query queryAnn = deleteMethod.getAnnotation(Query.class);
assertThat(queryAnn)
.as("deleteByEventUuid must carry @Query to avoid Hibernate derived-delete flush-order issue")
.isNotNull();
assertThat(queryAnn.value())
.as("@Query value must start with 'delete' so Hibernate runs it immediately, not after inserts")
.startsWith("delete");
}
// --- Test 8: replace with null userIds deletes existing rows and saves nothing ---
@Test
void replaceWithNullUserIds() {
service.replace(EVENT_UUID, COMPANY_ID, null, DONE_BY);
verify(repository).deleteByEventUuid(EVENT_UUID);
verify(repository, never()).saveAll(any());
}
// --- Test 9: replace with [5, null, 5, 6] saves exactly users 5 and 6 ---
@Test
void replaceWithNullEntriesInList() {
when(repository.saveAll(any()))
.thenAnswer(inv -> {
List<EventSalesperson> args = inv.getArgument(0);
return args.stream()
.map(e -> {
EventSalesperson copy = new EventSalesperson();
copy.setId(1L);
copy.setEventUuid(EVENT_UUID);
copy.setCompanyId(COMPANY_ID);
copy.setUserId(e.getUserId());
copy.setCreateBy(DONE_BY);
copy.setCreateDateTime(e.getCreateDateTime());
return copy;
})
.toList();
});
List<EventSalesperson> result = service.replace(EVENT_UUID, COMPANY_ID, Arrays.asList(5L, null, 5L, 6L), DONE_BY);
ArgumentCaptor<List<EventSalesperson>> captor = ArgumentCaptor.forClass(List.class);
verify(repository).saveAll(captor.capture());
List<EventSalesperson> saved = captor.getValue();
assertThat(saved).hasSize(2);
assertThat(saved.get(0).getUserId()).isEqualTo(5L);
assertThat(saved.get(1).getUserId()).isEqualTo(6L);
assertThat(result).hasSize(2);
}
// --- Test 10: createBy column has updatable = false ---
@Test
void createByHasUpdatableFalse() throws Exception {
jakarta.persistence.Column colAnn = EventSalesperson.class
.getDeclaredField("createBy")
.getAnnotation(jakarta.persistence.Column.class);
assertThat(colAnn)
.as("createBy must have @Column(updatable = false) to prevent accidental overwrites")
.isNotNull();
assertThat(colAnn.updatable()).isFalse();
}
private static EventSalesperson makeSalesperson(Long id, long eventUuid, long companyId, long userId, String createBy) {
EventSalesperson e = new EventSalesperson();
e.setId(id);
e.setEventUuid(eventUuid);
e.setCompanyId(companyId);
e.setUserId(userId);
e.setCreateBy(createBy);
e.setCreateDateTime(LocalDateTime.now());
return e;
}
}