// Copyright (c) Cratis. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. import { fromContractsGuid } from '../connection/Guid.js'; import { Identity } from '../identity/Identity.js'; import type { EventContext } from './EventContext.js'; import { EventType } from './EventType.js'; import { EventTypeGeneration } from './EventTypeGeneration.js'; import { EventTypeId } from './EventTypeId.js'; import { Tag } from './Tag.js'; import { NamedTag } from './NamedTag.js'; import type { WireEventContext } from './WireEventContext.js'; import { DecoratorType } from '../types/DecoratorType.js'; import { TypeDiscoverer } from '../types/TypeDiscoverer.js'; import { getCompensatedTypeFor } from './compensationForDecorator.js'; /** Converts kernel metadata identically for reads, reactors, and reducers. */ export function toClientEventContext(context: WireEventContext): EventContext { return { eventStore: context.EventStore, namespace: context.Namespace, sequenceNumber: context.SequenceNumber, eventSourceId: context.EventSourceId, eventSourceType: context.EventSourceType, eventStreamType: context.EventStreamType, eventStreamId: context.EventStreamId, eventSource: context.EventSource ? context.EventSource : undefined, subject: context.Subject, hash: context.Hash, observationState: context.ObservationState, causedBy: toClientIdentity(context.CausedBy), eventType: new EventType( new EventTypeId(context.EventType?.Id ?? ''), new EventTypeGeneration(context.EventType?.Generation ?? EventTypeGeneration.firstValue), context.EventType?.Tombstone ?? false ), occurred: new Date(context.Occurred?.Value ?? ''), correlationId: fromContractsGuid(context.CorrelationId).toString(), causation: (context.Causation ?? []).map(causation => ({ type: causation.Type, occurred: causation.Occurred ? new Date(causation.Occurred.Value) : undefined, properties: { ...causation.Properties } })), compensationFor: toClientCompensationFor(context), tags: (context.Tags ?? []).map(value => new Tag(value)), namedTags: (context.NamedTags ?? []).map(tag => new NamedTag(tag.Name, tag.Value)) }; } function toClientIdentity(identity: WireEventContext['CausedBy']): Identity | undefined { return identity ? new Identity(identity.Subject, identity.Name, identity.UserName, toClientIdentity(identity.OnBehalfOf)) : undefined; } /** * The wire format carries an unset compensation as zero, which is also a valid sequence number. Zero is therefore * only a compensation when the event type is a registered compensation event type. */ function toClientCompensationFor(context: WireEventContext): bigint | undefined { const compensationFor = context.CompensationFor; if (compensationFor === undefined) { return undefined; } if (compensationFor !== 0n) { return compensationFor; } const eventTypeId = context.EventType?.Id; const type = eventTypeId ? TypeDiscoverer.default.getTypeByDecoratorTypeAndName(DecoratorType.EventType, eventTypeId) : undefined; return type && getCompensatedTypeFor(type) ? 0n : undefined; }