diff --git a/src/get-user/get-user.service.ts b/src/get-user/get-user.service.ts index 1a7d51a..848dabe 100644 --- a/src/get-user/get-user.service.ts +++ b/src/get-user/get-user.service.ts @@ -17,7 +17,7 @@ export class GetUserService { where: { isActive: true }, }); - const tenant = tenants.find((item) => this.matchesHost(item, host)); + const tenant = this.findTenantForHost(tenants, host); if (!tenant) { throw new NotFoundException('tenant not found for domain'); } @@ -181,6 +181,46 @@ export class GetUserService { return out; } + /** + * اول دامنهٔ اصلی (Tenant.domain)، بعد aliases داخل domains[]. + * اگر چند نسخه یک دامنه را داشته باشند، نسخهٔ primary برنده است. + */ + private findTenantForHost< + T extends { + domain: string; + domains: unknown; + }, + >(tenants: T[], host: string): T | undefined { + const normalizedHost = this.normalizeHost(host); + const hostNoWww = normalizedHost.replace(/^www\./, ''); + + const primary = tenants.find((item) => { + const d = this.normalizeHost(item.domain); + return d === normalizedHost || d.replace(/^www\./, '') === hostNoWww; + }); + if (primary) return primary; + + return tenants.find((item) => this.matchesAliasHost(item, normalizedHost)); + } + + private matchesAliasHost( + tenant: { + domain: string; + domains: unknown; + }, + host: string, + ) { + const hostNoWww = host.replace(/^www\./, ''); + for (const value of this.readDomains(tenant.domains)) { + const normalized = this.normalizeHost(value); + if (!normalized) continue; + if (normalized === host || normalized.replace(/^www\./, '') === hostNoWww) { + return true; + } + } + return false; + } + private matchesHost( tenant: { domain: string; @@ -188,15 +228,7 @@ export class GetUserService { }, host: string, ) { - const candidates = new Set(); - for (const value of [tenant.domain, ...this.readDomains(tenant.domains)]) { - const normalized = this.normalizeHost(value); - if (!normalized) continue; - candidates.add(normalized); - candidates.add(normalized.replace(/^www\./, '')); - candidates.add(`www.${normalized.replace(/^www\./, '')}`); - } - return candidates.has(host) || candidates.has(host.replace(/^www\./, '')); + return Boolean(this.findTenantForHost([tenant], host)); } private readDomains(raw: unknown): string[] { diff --git a/src/get-version/get-version.service.ts b/src/get-version/get-version.service.ts index 5ffedf3..a4b5f4e 100644 --- a/src/get-version/get-version.service.ts +++ b/src/get-version/get-version.service.ts @@ -20,7 +20,7 @@ export class GetVersionService { }, }); - const tenant = tenants.find((item) => this.matchesHost(item, host)); + const tenant = this.findTenantForHost(tenants, host); if (!tenant) { throw new NotFoundException('tenant not found for domain'); } @@ -28,22 +28,35 @@ export class GetVersionService { return { version: tenant.appVersion || '1' }; } - private matchesHost( - tenant: { + /** هم‌راستا با GetUserService: اول Tenant.domain، بعد aliases */ + private findTenantForHost< + T extends { domain: string; domains: unknown; }, - host: string, - ) { - const candidates = new Set(); - for (const value of [tenant.domain, ...this.readDomains(tenant.domains)]) { - const normalized = this.normalizeHost(value); - if (!normalized) continue; - candidates.add(normalized); - candidates.add(normalized.replace(/^www\./, '')); - candidates.add(`www.${normalized.replace(/^www\./, '')}`); - } - return candidates.has(host) || candidates.has(host.replace(/^www\./, '')); + >(tenants: T[], host: string): T | undefined { + const normalizedHost = this.normalizeHost(host); + const hostNoWww = normalizedHost.replace(/^www\./, ''); + + const primary = tenants.find((item) => { + const d = this.normalizeHost(item.domain); + return d === normalizedHost || d.replace(/^www\./, '') === hostNoWww; + }); + if (primary) return primary; + + return tenants.find((item) => { + for (const value of this.readDomains(item.domains)) { + const normalized = this.normalizeHost(value); + if (!normalized) continue; + if ( + normalized === normalizedHost || + normalized.replace(/^www\./, '') === hostNoWww + ) { + return true; + } + } + return false; + }); } private readDomains(raw: unknown): string[] {