1 package com.reallifedeveloper.common.application.notification;
2
3 import java.time.ZonedDateTime;
4
5 import org.checkerframework.checker.nullness.qual.Nullable;
6
7 import com.reallifedeveloper.common.domain.ErrorHandling;
8 import com.reallifedeveloper.common.domain.event.DomainEvent;
9
10 /**
11 * A notification is information about a domain event that has occurred, and that has been stored in an
12 * {@link com.reallifedeveloper.common.application.eventstore.EventStore}. The notification can be sent to external systems that need to be
13 * informed about the event.
14 *
15 * @param eventType the type of the event
16 * @param storedEventId the stored event ID, must not be {@code null}
17 * @param occurredOn the timestamp the event occurred
18 * @param event the domain event that occurred, must not be {@code null}
19 *
20 * @author RealLifeDeveloper
21 */
22 public record Notification(@Nullable String eventType, Long storedEventId, @Nullable ZonedDateTime occurredOn, DomainEvent event) {
23
24 /**
25 * Creates a new {@code Notification}.
26 */
27 public Notification {
28 ErrorHandling.checkNull("Arguments must not be null: event=%s, storedEventId=%s", event, storedEventId);
29 }
30
31 /**
32 * A factory method that sets the {@code eventType} to the name of the event class and {@code occurredOn} to the date and time when the
33 * event occurred.
34 *
35 * @param event the event to store and use to set {@code eventType} and {@code occurredOn}
36 * @param storedEventId the stored event ID to store
37 *
38 * @return a new {@code Notification}
39 *
40 * @throws IllegalArgumentException if {@code event} or {@code storedEventId} is {@code null}
41 */
42 public static Notification create(DomainEvent event, Long storedEventId) {
43 ErrorHandling.checkNull("Arguments must not be null: event=%s, storedEventId=%s", event, storedEventId);
44 return new Notification(event.getClass().getName(), storedEventId, event.eventOccurredOn(), event);
45 }
46
47 }