rwadurian/backend/services/presence-service/src/domain/value-objects/event-name.vo.ts

49 lines
1.4 KiB
TypeScript

import { DomainException } from '../../shared/exceptions/domain.exception';
export class EventName {
// 预定义的事件名
static readonly APP_SESSION_START = new EventName('app_session_start');
static readonly PRESENCE_HEARTBEAT = new EventName('presence_heartbeat');
static readonly APP_SESSION_END = new EventName('app_session_end');
private readonly _value: string;
private constructor(value: string) {
this._value = value;
}
get value(): string {
return this._value;
}
static fromString(value: string): EventName {
if (!value || value.trim() === '') {
throw new DomainException('EventName cannot be empty');
}
const trimmed = value.trim().toLowerCase();
if (trimmed.length > 64) {
throw new DomainException('EventName cannot exceed 64 characters');
}
// 验证格式:字母、数字、下划线
if (!/^[a-z][a-z0-9_]*$/.test(trimmed)) {
throw new DomainException('EventName must start with letter and contain only lowercase letters, numbers, and underscores');
}
return new EventName(trimmed);
}
/**
* 是否为DAU统计事件
*/
isDauEvent(): boolean {
return this._value === EventName.APP_SESSION_START.value;
}
equals(other: EventName): boolean {
return this._value === other._value;
}
toString(): string {
return this._value;
}
}