import {BaseSerializer, ObjectSerializer, SerialPayload} from '../../support/bus' import {AuthenticationEvent} from '../event/AuthenticationEvent' import {ErrorWithContext, JSONState} from '../../util' import {Authenticatable} from '../types' import {StaticInstantiable} from '../../di' import {SecurityContext} from '../context/SecurityContext' import {UserAuthenticatedEvent} from '../event/UserAuthenticatedEvent' import {UserAuthenticationResumedEvent} from '../event/UserAuthenticationResumedEvent' import {UserFlushedEvent} from '../event/UserFlushedEvent' export interface AuthenticationEventSerialPayload extends JSONState { user: SerialPayload eventName: string } @ObjectSerializer() export class AuthenticationEventSerializer extends BaseSerializer { protected async decodeSerial(serial: AuthenticationEventSerialPayload): Promise { const user = await this.getSerialization().decode(serial.user) const context = await this.getRequest().make(SecurityContext) const EventClass = this.getEventClass(serial.eventName) return new EventClass(user, context) } protected async encodeActual(actual: AuthenticationEvent): Promise { return { eventName: actual.eventName, user: await this.getSerialization().encode(actual.user), } } protected getName(): string { return '@extollo/lib:AuthenticationEventSerializer' } matchActual(some: AuthenticationEvent): boolean { return some instanceof AuthenticationEvent } protected getEventClass(name: string): StaticInstantiable { if ( name === '@extollo/lib:UserAuthenticatedEvent' ) { return UserAuthenticatedEvent } else if ( name === '@extollo/lib:UserAuthenticationResumedEvent' ) { return UserAuthenticationResumedEvent } else if ( name === '@extollo/lib:UserFlushedEvent' ) { return UserFlushedEvent } throw new ErrorWithContext('Unable to map event name to AuthenticationEvent implementation', { eventName: name, }) } }