Files
Edu/services/core-edu/src/shared/outbox/event-builder.ts
SpecialX 58c0ba1bd9 feat(core-edu): 完整实现 core-edu 教学核心服务
包含 classes/exams/homework/grades/attendance/scheduling 域、outbox、iam-consumer、redis 配置等完整实现
2026-07-10 19:08:56 +08:00

62 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { randomUUID } from "node:crypto";
/**
* Outbox 事件构建器
*
* 所有事件 payload 必须包含events.proto 仲裁 §2.1
* - event_id: UUID幂等消费
* - occurred_at: 业务时间戳ms epoch
* - schema_version: 默认 "v1"
* - metadata: { traceId, userId }
*/
export interface EventMetadata {
schema_version: string;
trace_id: string;
user_id: string;
}
export interface OutboxEvent<T = Record<string, unknown>> {
event_id: string;
aggregate_id: string;
event_type: string;
occurred_at: number;
payload: T;
metadata: EventMetadata;
}
export interface BuildEventInput {
aggregateId: string;
eventType: string;
payload: Record<string, unknown>;
userId?: string;
traceId?: string;
schemaVersion?: string;
}
/**
* 构建标准 Outbox 事件(含 event_id / occurred_at / schema_version / metadata
* 返回的对象可直接 JSON.stringify 作为 outbox.payload 存储。
*/
export function buildEvent(input: BuildEventInput): OutboxEvent {
return {
event_id: randomUUID(),
aggregate_id: input.aggregateId,
event_type: input.eventType,
occurred_at: Date.now(),
payload: input.payload,
metadata: {
schema_version: input.schemaVersion ?? "v1",
trace_id: input.traceId ?? "unknown",
user_id: input.userId ?? "system",
},
};
}
/**
* 将 OutboxEvent 序列化为 JSON 字符串(用于 outbox.payload 列)。
*/
export function serializeEvent(event: OutboxEvent): string {
return JSON.stringify(event);
}