Something went wrong. Try again.
A social knowledge tool for research trails, built on ATProto semble.so
research-tool social-network tools-for-thought atproto
Something went wrong. Try again.
4.6 kB · 126 lines
TypeScript
at development
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127import { IDomainEvent } from '../../domain/events/IDomainEvent';import { CardAddedToLibraryEvent } from '../../../modules/cards/domain/events/CardAddedToLibraryEvent';import { CardAddedToCollectionEvent } from '../../../modules/cards/domain/events/CardAddedToCollectionEvent';import { CollectionCreatedEvent } from '../../../modules/cards/domain/events/CollectionCreatedEvent';import { CardId } from '../../../modules/cards/domain/value-objects/CardId';import { CollectionId } from '../../../modules/cards/domain/value-objects/CollectionId';import { CuratorId } from '../../../modules/cards/domain/value-objects/CuratorId';import { EventNames } from './EventConfig';
export interface SerializedEvent { eventType: string; aggregateId: string; dateTimeOccurred: string;}
export interface SerializedCardAddedToLibraryEvent extends SerializedEvent { eventType: typeof EventNames.CARD_ADDED_TO_LIBRARY; cardId: string; curatorId: string;}
export interface SerializedCardAddedToCollectionEvent extends SerializedEvent { eventType: typeof EventNames.CARD_ADDED_TO_COLLECTION; cardId: string; collectionId: string; addedBy: string;}
export interface SerializedCollectionCreatedEvent extends SerializedEvent { eventType: typeof EventNames.COLLECTION_CREATED; collectionId: string; authorId: string; collectionName: string;}
export type SerializedEventUnion = | SerializedCardAddedToLibraryEvent | SerializedCardAddedToCollectionEvent | SerializedCollectionCreatedEvent;
export class EventMapper { static toSerialized(event: IDomainEvent): SerializedEventUnion { if (event instanceof CardAddedToLibraryEvent) { return { eventType: EventNames.CARD_ADDED_TO_LIBRARY, aggregateId: event.getAggregateId().toString(), dateTimeOccurred: event.dateTimeOccurred.toISOString(), cardId: event.cardId.getValue().toString(), curatorId: event.curatorId.value, }; }
if (event instanceof CardAddedToCollectionEvent) { return { eventType: EventNames.CARD_ADDED_TO_COLLECTION, aggregateId: event.getAggregateId().toString(), dateTimeOccurred: event.dateTimeOccurred.toISOString(), cardId: event.cardId.getValue().toString(), collectionId: event.collectionId.getValue().toString(), addedBy: event.addedBy.value, }; }
if (event instanceof CollectionCreatedEvent) { return { eventType: EventNames.COLLECTION_CREATED, aggregateId: event.getAggregateId().toString(), dateTimeOccurred: event.dateTimeOccurred.toISOString(), collectionId: event.collectionId.getValue().toString(), authorId: event.authorId.value, collectionName: event.collectionName, }; }
throw new Error( `Unknown event type for serialization: ${event.constructor.name}`, ); }
static fromSerialized(eventData: SerializedEventUnion): IDomainEvent { switch (eventData.eventType) { case EventNames.CARD_ADDED_TO_LIBRARY: { const cardId = CardId.createFromString(eventData.cardId).unwrap(); const curatorId = CuratorId.create(eventData.curatorId).unwrap(); const dateTimeOccurred = new Date(eventData.dateTimeOccurred);
return CardAddedToLibraryEvent.reconstruct( cardId, curatorId, dateTimeOccurred, ).unwrap(); } case EventNames.CARD_ADDED_TO_COLLECTION: { const cardId = CardId.createFromString(eventData.cardId).unwrap(); const collectionId = CollectionId.createFromString( eventData.collectionId, ).unwrap(); const addedBy = CuratorId.create(eventData.addedBy).unwrap(); const dateTimeOccurred = new Date(eventData.dateTimeOccurred);
return CardAddedToCollectionEvent.reconstruct( cardId, collectionId, addedBy, dateTimeOccurred, ).unwrap(); } case EventNames.COLLECTION_CREATED: { const collectionId = CollectionId.createFromString( eventData.collectionId, ).unwrap(); const authorId = CuratorId.create(eventData.authorId).unwrap(); const dateTimeOccurred = new Date(eventData.dateTimeOccurred);
return CollectionCreatedEvent.reconstruct( collectionId, authorId, eventData.collectionName, dateTimeOccurred, ).unwrap(); } default: throw new Error(`Unknown event type for deserialization: ${eventData}`); } }}