import { HostComponentInfo, ContextId, ContextIdFactory, ContextIdStrategy, ContextIdResolver } from '@nestjs/core' import { Request } from 'express' import { TenantPayload } from '@api/core' const tenants = new Map() export class TestTenantStrategy implements ContextIdStrategy { static apply() { ContextIdFactory.apply(new TestTenantStrategy()) } protected constructor() {} attach(contextId: ContextId, request: Request): ContextIdResolver { request.headers ??= {} request.headers.origin = 'http://test' request.headers['x-real-ip'] = '127.0.0.1' request.cookies ??= {} const payload = new TenantPayload(request) let tenantSubTreeId: ContextId if (tenants.has(payload.id)) { tenantSubTreeId = tenants.get(payload.id)! } else { tenantSubTreeId = ContextIdFactory.create() tenants.set(payload.id, tenantSubTreeId) } return { resolve: (info: HostComponentInfo) => (info.isTreeDurable ? tenantSubTreeId : contextId), payload, } } }