diff --git a/openspec/changes/add-admin-audit-log/tasks.md b/openspec/changes/add-admin-audit-log/tasks.md index 569754b..9fa8e8b 100644 --- a/openspec/changes/add-admin-audit-log/tasks.md +++ b/openspec/changes/add-admin-audit-log/tasks.md @@ -13,7 +13,7 @@ Design: `design.md`. All tasks run serially: each builds on the one before. - Apply them to the local database and emulator. This is local only. - Scope: `local/**`, `openspec/changes/add-admin-audit-log/sql/**` - Verify: `select` shows the table and the module row. -- [ ] 1.2 core-libs `AuditEvent` (design decision 1). +- [x] 1.2 core-libs `AuditEvent` (design decision 1). - Tests first: JSON round trip through the same `PubSubUtil` used for `ResponseTime`. - Scope: `solvway-core-libs/src/**` - Verify: `mvn -T 1C -B -q -pl solvway-core-libs verify` diff --git a/solvway-core-libs/pom.xml b/solvway-core-libs/pom.xml index 48d4eb6..b3fd531 100644 --- a/solvway-core-libs/pom.xml +++ b/solvway-core-libs/pom.xml @@ -125,6 +125,11 @@ org.projectlombok lombok + + org.springframework.boot + spring-boot-starter-test + test + com.github.jknack handlebars diff --git a/solvway-core-libs/src/main/java/com/solvway/core/model/AuditEvent.java b/solvway-core-libs/src/main/java/com/solvway/core/model/AuditEvent.java new file mode 100644 index 0000000..970e18b --- /dev/null +++ b/solvway-core-libs/src/main/java/com/solvway/core/model/AuditEvent.java @@ -0,0 +1,30 @@ +package com.solvway.core.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalDateTime; + +/** One admin audit event: published by mobile-api on audit-event-topic, stored by services-log. */ +@Setter +@Getter +@JsonIgnoreProperties(ignoreUnknown = true) +public class AuditEvent { + public static final String OUTCOME_SUCCESS = "SUCCESS"; + public static final String OUTCOME_FAILURE = "FAILURE"; + + private Long companyId; + private LocalDateTime occurredAt; + private String actor; + private String module; + private String action; + private String httpMethod; + private String pathTemplate; + private String targetId; + private String outcome; + private Integer httpStatus; + private String clientIp; + /** JSON: {field: {before, after}} for allow-listed fields, or {fields: [..]} for lead edits. */ + private String changes; +} diff --git a/solvway-core-libs/src/main/resources/application.yaml b/solvway-core-libs/src/main/resources/application.yaml index 55a9f6e..de026de 100644 --- a/solvway-core-libs/src/main/resources/application.yaml +++ b/solvway-core-libs/src/main/resources/application.yaml @@ -47,6 +47,9 @@ pubsub: activate-member: subscription: activate-member-subscription topic: activate-member-topic + audit-event: + subscription: audit-event-subscription + topic: audit-event-topic backoffice-message-source-refresh: subscription: backoffice-message-source-refresh-subscription topic: backoffice-message-source-refresh-topic diff --git a/solvway-core-libs/src/test/java/com/solvway/core/model/AuditEventTest.java b/solvway-core-libs/src/test/java/com/solvway/core/model/AuditEventTest.java new file mode 100644 index 0000000..aa4c8db --- /dev/null +++ b/solvway-core-libs/src/test/java/com/solvway/core/model/AuditEventTest.java @@ -0,0 +1,46 @@ +package com.solvway.core.model; + +import com.solvway.core.utils.PubSubUtil; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.time.LocalDateTime; + +import static org.assertj.core.api.Assertions.assertThat; + +class AuditEventTest { + + private final PubSubUtil util = new PubSubUtil(); + + @Test + void roundTripsEveryFieldThroughPubSubUtil() { + AuditEvent e = new AuditEvent(); + e.setCompanyId(7L); + e.setOccurredAt(LocalDateTime.of(2026, 11, 30, 15, 4, 5, 123_000_000)); + e.setActor("admin"); + e.setModule("BO_USERS"); + e.setAction("USER_UPDATE"); + e.setHttpMethod("PUT"); + e.setPathTemplate("/api/admin/users/{id}"); + e.setTargetId("42"); + e.setOutcome(AuditEvent.OUTCOME_SUCCESS); + e.setHttpStatus(200); + e.setClientIp("203.0.113.9"); + e.setChanges("{\"enabled\":{\"before\":true,\"after\":false}}"); + + String json = util.convertToString(e); + assertThat(json).contains("\"occurredAt\":\"2026-11-30T15:04:05.123\""); + + AuditEvent back = util.convertToOriginalObject(json.getBytes(StandardCharsets.UTF_8), AuditEvent.class); + assertThat(back).usingRecursiveComparison().isEqualTo(e); + assertThat(AuditEvent.OUTCOME_FAILURE).isEqualTo("FAILURE"); + } + + @Test + void ignoresUnknownFieldsSoTheContractCanGrow() { + String json = "{\"actor\":\"admin\",\"action\":\"USER_UPDATE\",\"outcome\":\"SUCCESS\",\"addedLater\":1}"; + AuditEvent back = util.convertToOriginalObject(json.getBytes(StandardCharsets.UTF_8), AuditEvent.class); + assertThat(back).isNotNull(); + assertThat(back.getActor()).isEqualTo("admin"); + } +}