251123:2300 Update T1

This commit is contained in:
2025-11-24 08:15:15 +07:00
parent 23006898d9
commit 9360d78ea6
81 changed files with 4232 additions and 347 deletions
@@ -0,0 +1,35 @@
// File: src/common/services/request-context.service.ts
// บันทึกการแก้ไข: เก็บ Context ระหว่าง Request (User, TraceID) (T1.1)
import { Injectable, Scope } from '@nestjs/common';
import { AsyncLocalStorage } from 'async_hooks';
@Injectable({ scope: Scope.DEFAULT })
export class RequestContextService {
private static readonly cls = new AsyncLocalStorage<Map<string, any>>();
static run(fn: () => void) {
this.cls.run(new Map(), fn);
}
static set(key: string, value: any) {
const store = this.cls.getStore();
if (store) {
store.set(key, value);
}
}
static get<T>(key: string): T | undefined {
const store = this.cls.getStore();
return store?.get(key);
}
// Helper methods
static get currentUserId(): number | null {
return this.get('user_id') || null;
}
static get requestId(): string | null {
return this.get('request_id') || null;
}
}