A social knowledge tool for researchers built on ATProto
1import { IDomainEvent } from '../../domain/events/IDomainEvent';
2import { CardAddedToLibraryEvent } from '../../../modules/cards/domain/events/CardAddedToLibraryEvent';
3import { CardAddedToCollectionEvent } from '../../../modules/cards/domain/events/CardAddedToCollectionEvent';
4import { CollectionCreatedEvent } from '../../../modules/cards/domain/events/CollectionCreatedEvent';
5import { CardId } from '../../../modules/cards/domain/value-objects/CardId';
6import { CollectionId } from '../../../modules/cards/domain/value-objects/CollectionId';
7import { CuratorId } from '../../../modules/cards/domain/value-objects/CuratorId';
8import { EventNames } from './EventConfig';
9
10export interface SerializedEvent {
11 eventType: string;
12 aggregateId: string;
13 dateTimeOccurred: string;
14}
15
16export interface SerializedCardAddedToLibraryEvent extends SerializedEvent {
17 eventType: typeof EventNames.CARD_ADDED_TO_LIBRARY;
18 cardId: string;
19 curatorId: string;
20}
21
22export interface SerializedCardAddedToCollectionEvent extends SerializedEvent {
23 eventType: typeof EventNames.CARD_ADDED_TO_COLLECTION;
24 cardId: string;
25 collectionId: string;
26 addedBy: string;
27}
28
29export interface SerializedCollectionCreatedEvent extends SerializedEvent {
30 eventType: typeof EventNames.COLLECTION_CREATED;
31 collectionId: string;
32 authorId: string;
33 collectionName: string;
34}
35
36export type SerializedEventUnion =
37 | SerializedCardAddedToLibraryEvent
38 | SerializedCardAddedToCollectionEvent
39 | SerializedCollectionCreatedEvent;
40
41export class EventMapper {
42 static toSerialized(event: IDomainEvent): SerializedEventUnion {
43 if (event instanceof CardAddedToLibraryEvent) {
44 return {
45 eventType: EventNames.CARD_ADDED_TO_LIBRARY,
46 aggregateId: event.getAggregateId().toString(),
47 dateTimeOccurred: event.dateTimeOccurred.toISOString(),
48 cardId: event.cardId.getValue().toString(),
49 curatorId: event.curatorId.value,
50 };
51 }
52
53 if (event instanceof CardAddedToCollectionEvent) {
54 return {
55 eventType: EventNames.CARD_ADDED_TO_COLLECTION,
56 aggregateId: event.getAggregateId().toString(),
57 dateTimeOccurred: event.dateTimeOccurred.toISOString(),
58 cardId: event.cardId.getValue().toString(),
59 collectionId: event.collectionId.getValue().toString(),
60 addedBy: event.addedBy.value,
61 };
62 }
63
64 if (event instanceof CollectionCreatedEvent) {
65 return {
66 eventType: EventNames.COLLECTION_CREATED,
67 aggregateId: event.getAggregateId().toString(),
68 dateTimeOccurred: event.dateTimeOccurred.toISOString(),
69 collectionId: event.collectionId.getValue().toString(),
70 authorId: event.authorId.value,
71 collectionName: event.collectionName,
72 };
73 }
74
75 throw new Error(
76 `Unknown event type for serialization: ${event.constructor.name}`,
77 );
78 }
79
80 static fromSerialized(eventData: SerializedEventUnion): IDomainEvent {
81 switch (eventData.eventType) {
82 case EventNames.CARD_ADDED_TO_LIBRARY: {
83 const cardId = CardId.createFromString(eventData.cardId).unwrap();
84 const curatorId = CuratorId.create(eventData.curatorId).unwrap();
85 const dateTimeOccurred = new Date(eventData.dateTimeOccurred);
86
87 return CardAddedToLibraryEvent.reconstruct(
88 cardId,
89 curatorId,
90 dateTimeOccurred,
91 ).unwrap();
92 }
93 case EventNames.CARD_ADDED_TO_COLLECTION: {
94 const cardId = CardId.createFromString(eventData.cardId).unwrap();
95 const collectionId = CollectionId.createFromString(
96 eventData.collectionId,
97 ).unwrap();
98 const addedBy = CuratorId.create(eventData.addedBy).unwrap();
99 const dateTimeOccurred = new Date(eventData.dateTimeOccurred);
100
101 return CardAddedToCollectionEvent.reconstruct(
102 cardId,
103 collectionId,
104 addedBy,
105 dateTimeOccurred,
106 ).unwrap();
107 }
108 case EventNames.COLLECTION_CREATED: {
109 const collectionId = CollectionId.createFromString(
110 eventData.collectionId,
111 ).unwrap();
112 const authorId = CuratorId.create(eventData.authorId).unwrap();
113 const dateTimeOccurred = new Date(eventData.dateTimeOccurred);
114
115 return CollectionCreatedEvent.reconstruct(
116 collectionId,
117 authorId,
118 eventData.collectionName,
119 dateTimeOccurred,
120 ).unwrap();
121 }
122 default:
123 throw new Error(`Unknown event type for deserialization: ${eventData}`);
124 }
125 }
126}