feat: company on emails and audit search
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
a6996841ac
commit
118c57c0ea
6 files changed
+147
-4
No files matched your search
@@ -22,6 +22,8 @@ public class Email implements Cloneable{
|
|||||||
private String sender = EmailConstant.EMAIL_SENDER;
|
private String sender = EmailConstant.EMAIL_SENDER;
|
||||||
private String subject;
|
private String subject;
|
||||||
private String body;
|
private String body;
|
||||||
|
/** Null for system emails with no company (visible to super admins only). */
|
||||||
|
private Long companyId;
|
||||||
@Column(updatable = false)
|
@Column(updatable = false)
|
||||||
private LocalDateTime datetime = LocalDateTime.now();
|
private LocalDateTime datetime = LocalDateTime.now();
|
||||||
|
|
||||||
|
|||||||
+4
@@ -59,6 +59,10 @@ public class EmailRepositoryImpl implements JpaRepositoryCustom<Email>{
|
|||||||
predicates.add(cb.equal(root.get(k), Long.parseLong(value)));
|
predicates.add(cb.equal(root.get(k), Long.parseLong(value)));
|
||||||
predicatesCount.add(cb.equal(rootCount.get(k), Long.parseLong(value)));
|
predicatesCount.add(cb.equal(rootCount.get(k), Long.parseLong(value)));
|
||||||
}
|
}
|
||||||
|
case "companyId" -> {
|
||||||
|
predicates.add(cb.equal(root.get(k), Long.parseLong(value)));
|
||||||
|
predicatesCount.add(cb.equal(rootCount.get(k), Long.parseLong(value)));
|
||||||
|
}
|
||||||
case "sender" -> {
|
case "sender" -> {
|
||||||
predicates.add(cb.like(root.get(k), "%" + value + "%"));
|
predicates.add(cb.like(root.get(k), "%" + value + "%"));
|
||||||
predicatesCount.add(cb.like(rootCount.get(k), "%" + value + "%"));
|
predicatesCount.add(cb.like(rootCount.get(k), "%" + value + "%"));
|
||||||
|
|||||||
+67
@@ -0,0 +1,67 @@
|
|||||||
|
package com.solvway.services.email.repo;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.solvway.services.email.model.Email;
|
||||||
|
import com.solvway.services.email.util.paging.PageableRest;
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||||
|
class EmailCompanyScopingTest {
|
||||||
|
|
||||||
|
private final EntityManager em = mock(EntityManager.class, RETURNS_DEEP_STUBS);
|
||||||
|
private final CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
|
private final EmailRepositoryImpl repo = new EmailRepositoryImpl();
|
||||||
|
|
||||||
|
EmailCompanyScopingTest() {
|
||||||
|
ReflectionTestUtils.setField(repo, "em", em);
|
||||||
|
jakarta.persistence.TypedQuery query = mock(jakarta.persistence.TypedQuery.class, org.mockito.Mockito.RETURNS_SELF);
|
||||||
|
org.mockito.Mockito.doReturn(java.util.List.of()).when(query).getResultList();
|
||||||
|
org.mockito.Mockito.doReturn(0L).when(query).getSingleResult();
|
||||||
|
org.mockito.Mockito.doReturn(query).when(em).createQuery(any(jakarta.persistence.criteria.CriteriaQuery.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
private PageableRest pageable() {
|
||||||
|
PageableRest p = new PageableRest();
|
||||||
|
p.setNumber(0);
|
||||||
|
p.setSize(10);
|
||||||
|
PageableRest.SortRest sr = new PageableRest.SortRest();
|
||||||
|
sr.setDirection("DESC");
|
||||||
|
sr.setProperty("id");
|
||||||
|
p.setSorts(new java.util.ArrayList<>(java.util.List.of(sr)));
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void emailKeepsCompanyFromPayload() throws Exception {
|
||||||
|
Email e = new ObjectMapper().readValue("{\"subject\":\"s\",\"companyId\":2}", Email.class);
|
||||||
|
assertThat(e.getCompanyId()).isEqualTo(2L);
|
||||||
|
assertThat(new ObjectMapper().readValue("{\"subject\":\"s\"}", Email.class).getCompanyId()).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void searchFiltersByCompanyWhenGiven() {
|
||||||
|
HashMap<String, String> property = new HashMap<>();
|
||||||
|
property.put("companyId", "2");
|
||||||
|
repo.findByPropertyAndPageable(property, pageable());
|
||||||
|
// once for the page query, once for the count query
|
||||||
|
verify(cb, org.mockito.Mockito.times(2)).equal(any(), org.mockito.ArgumentMatchers.eq(2L));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void searchDoesNotFilterByCompanyWhenAbsent() {
|
||||||
|
repo.findByPropertyAndPageable(new HashMap<>(), pageable());
|
||||||
|
verify(cb, never()).equal(any(), any(Object.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
-4
@@ -24,7 +24,12 @@ import java.util.Map;
|
|||||||
public class AuditEventController {
|
public class AuditEventController {
|
||||||
|
|
||||||
public record SearchRequest(String actor, String module, String action, String outcome,
|
public record SearchRequest(String actor, String module, String action, String outcome,
|
||||||
String from, String to, Integer page, Integer size) {
|
String from, String to, Integer page, Integer size, Long companyId) {
|
||||||
|
/** Keeps the pre-company body shape: no companyId means all companies. */
|
||||||
|
public SearchRequest(String actor, String module, String action, String outcome,
|
||||||
|
String from, String to, Integer page, Integer size) {
|
||||||
|
this(actor, module, action, outcome, from, to, page, size, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final AuditEventLogRepository repository;
|
private final AuditEventLogRepository repository;
|
||||||
@@ -39,9 +44,12 @@ public class AuditEventController {
|
|||||||
int s = r.size() == null || r.size() < 1 ? 20 : Math.min(r.size(), 100);
|
int s = r.size() == null || r.size() < 1 ? 20 : Math.min(r.size(), 100);
|
||||||
LocalDateTime from = day(r.from(), 0);
|
LocalDateTime from = day(r.from(), 0);
|
||||||
LocalDateTime to = day(r.to(), 1);
|
LocalDateTime to = day(r.to(), 1);
|
||||||
Page<AuditEventLog> page = repository.search(blankToNull(r.actor()), blankToNull(r.module()),
|
PageRequest paging = PageRequest.of(p, s, Sort.by(Sort.Direction.DESC, "occurredAt", "id"));
|
||||||
blankToNull(r.action()), blankToNull(r.outcome()), from, to,
|
Page<AuditEventLog> page = r.companyId() == null
|
||||||
PageRequest.of(p, s, Sort.by(Sort.Direction.DESC, "occurredAt", "id")));
|
? repository.search(blankToNull(r.actor()), blankToNull(r.module()),
|
||||||
|
blankToNull(r.action()), blankToNull(r.outcome()), from, to, paging)
|
||||||
|
: repository.searchInCompany(r.companyId(), blankToNull(r.actor()), blankToNull(r.module()),
|
||||||
|
blankToNull(r.action()), blankToNull(r.outcome()), from, to, paging);
|
||||||
return Map.of("content", page.getContent(), "totalElements", page.getTotalElements(), "number", p, "size", s);
|
return Map.of("content", page.getContent(), "totalElements", page.getTotalElements(), "number", p, "size", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+10
@@ -23,6 +23,16 @@ public interface AuditEventLogRepository extends JpaRepository<AuditEventLog, Lo
|
|||||||
@Param("action") String action, @Param("outcome") String outcome,
|
@Param("action") String action, @Param("outcome") String outcome,
|
||||||
@Param("from") LocalDateTime from, @Param("to") LocalDateTime to, Pageable pageable);
|
@Param("from") LocalDateTime from, @Param("to") LocalDateTime to, Pageable pageable);
|
||||||
|
|
||||||
|
/** Same as {@link #search} but only rows of one company; rows with no company never match. */
|
||||||
|
@Query("select e from AuditEventLog e where e.companyId = :companyId and (:actor is null or e.actor = :actor)"
|
||||||
|
+ " and (:module is null or e.module = :module) and (:action is null or e.action = :action)"
|
||||||
|
+ " and (:outcome is null or e.outcome = :outcome)"
|
||||||
|
+ " and (:from is null or e.occurredAt >= :from)"
|
||||||
|
+ " and (:to is null or e.occurredAt < :to)")
|
||||||
|
Page<AuditEventLog> searchInCompany(@Param("companyId") Long companyId, @Param("actor") String actor,
|
||||||
|
@Param("module") String module, @Param("action") String action, @Param("outcome") String outcome,
|
||||||
|
@Param("from") LocalDateTime from, @Param("to") LocalDateTime to, Pageable pageable);
|
||||||
|
|
||||||
/** Deletes up to {@code limit} old rows; each call is its own transaction. */
|
/** Deletes up to {@code limit} old rows; each call is its own transaction. */
|
||||||
@Transactional
|
@Transactional
|
||||||
@Modifying
|
@Modifying
|
||||||
|
|||||||
+52
@@ -0,0 +1,52 @@
|
|||||||
|
package com.solvway.services.log.controller;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.solvway.services.log.model.AuditEventLog;
|
||||||
|
import com.solvway.services.log.repo.AuditEventLogRepository;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.data.domain.PageImpl;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.ArgumentMatchers.isNull;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
class AuditEventControllerCompanyTest {
|
||||||
|
|
||||||
|
private final AuditEventLogRepository repository = mock(AuditEventLogRepository.class);
|
||||||
|
private final AuditEventController controller = new AuditEventController(repository);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void companyIdGivenSearchesOnlyThatCompany() {
|
||||||
|
when(repository.searchInCompany(eq(2L), any(), any(), any(), any(), any(), any(), any()))
|
||||||
|
.thenReturn(new PageImpl<>(List.of(new AuditEventLog())));
|
||||||
|
controller.search(new AuditEventController.SearchRequest(
|
||||||
|
null, null, null, null, null, null, null, null, 2L));
|
||||||
|
verify(repository).searchInCompany(eq(2L), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), any());
|
||||||
|
verify(repository, never()).search(any(), any(), any(), any(), any(), any(), any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void companyIdAbsentSearchesAllRows() {
|
||||||
|
when(repository.search(any(), any(), any(), any(), any(), any(), any()))
|
||||||
|
.thenReturn(new PageImpl<>(List.of(new AuditEventLog())));
|
||||||
|
controller.search(new AuditEventController.SearchRequest(
|
||||||
|
null, null, null, null, null, null, null, null, null));
|
||||||
|
verify(repository).search(any(), any(), any(), any(), any(), any(), any());
|
||||||
|
verify(repository, never()).searchInCompany(any(), any(), any(), any(), any(), any(), any(), any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void companyIdIsReadFromJsonBodyAndOldBodiesStillWork() throws Exception {
|
||||||
|
var m = new ObjectMapper();
|
||||||
|
assertThat(m.readValue("{\"companyId\":3,\"actor\":\"a\"}", AuditEventController.SearchRequest.class)
|
||||||
|
.companyId()).isEqualTo(3L);
|
||||||
|
assertThat(m.readValue("{\"actor\":\"a\"}", AuditEventController.SearchRequest.class).companyId()).isNull();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user