@posiwise/user-module 0.0.160 → 0.0.161

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -54,6 +54,7 @@ import * as i12 from '@posiwise/utils';
54
54
  import { ValidateForm, clearQueryParams, appendQueryParam } from '@posiwise/utils';
55
55
  import { StatusCodes } from 'http-status-codes';
56
56
  import { map, from, forkJoin, interval } from 'rxjs';
57
+ import { getDomain } from 'tldts';
57
58
  import * as i8$1 from '@jsverse/transloco';
58
59
  import cloneDeep from 'lodash/cloneDeep';
59
60
  import sortBy from 'lodash/sortBy';
@@ -110,6 +111,10 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.19", ngImpo
110
111
  args: ['menuItems', { static: true }]
111
112
  }] } });
112
113
 
114
+ /** Environment segments that may already sit in front of the company domain. */
115
+ const MCP_ENV_SEGMENTS = ['dev', 'uat', 'staging', 'go'];
116
+ /** Segment assumed when the host carries none — PROD is served from the apex too. */
117
+ const MCP_PROD_SEGMENT = 'go';
113
118
  class AccountDetailsComponent extends AppBaseComponent {
114
119
  constructor(fb, modalService, authService, geoService, profileService, injector, cdr, confirmDialog) {
115
120
  super(injector);
@@ -521,16 +526,40 @@ class AccountDetailsComponent extends AppBaseComponent {
521
526
  return this.user?.api_keys?.api_token_scope ?? 'read_only';
522
527
  }
523
528
  /**
524
- * Tenant-dynamic MCP endpoint URL. Swaps the frontend subdomain for the
525
- * brain-api-mcp host while keeping the company domain, so it follows the URL
526
- * the user is on (www.dev.posiwise.com -> brain-api-mcp.dev.posiwise.com).
529
+ * Tenant-dynamic MCP endpoint URL, resolved from the host the user is on.
530
+ * The MCP host is always `brain-api-mcp.{env}.{company-domain}`, so the
531
+ * environment segment has to survive the swap:
532
+ * www.dev.posiwise.com -> brain-api-mcp.dev.posiwise.com
533
+ * www.go.posiwise.com -> brain-api-mcp.go.posiwise.com
534
+ * www.posiwise.com -> brain-api-mcp.go.posiwise.com (apex = PROD)
535
+ * Stripping the first label unconditionally (the previous behaviour) broke
536
+ * PROD, where the app is also served from the apex: it produced
537
+ * `brain-api-mcp.posiwise.com`, which does not exist in DNS.
527
538
  */
528
539
  get mcpEndpointUrl() {
529
- const base = window.location.hostname.replace(/^[^.]+\./, '');
530
- const url = `https://brain-api-mcp.${base}/mcp`;
540
+ const url = `https://brain-api-mcp.${this.mcpHostBase(window.location.hostname)}/mcp`;
531
541
  const shard = this.user?.user_shard;
532
542
  return shard ? `${url}?user_shard=${encodeURIComponent(shard)}` : url;
533
543
  }
544
+ /**
545
+ * Company domain prefixed by its environment segment. Resolves the company
546
+ * domain with the same `getDomain` the AppConfigService uses to identify the
547
+ * tenant, so multi-label TLDs (acme.co.uk) and arbitrary subdomains both
548
+ * reduce correctly. Only a recognised environment segment is carried over —
549
+ * anything else (`www`, a custom subdomain) falls back to PROD rather than
550
+ * being prefixed onto the host, which would misroute.
551
+ */
552
+ mcpHostBase(hostname) {
553
+ const companyDomain = getDomain(hostname, { allowPrivateDomains: true });
554
+ if (!companyDomain) {
555
+ // Non-public hosts (localhost, bare IPs) carry no company domain.
556
+ return hostname;
557
+ }
558
+ const prefix = hostname.slice(0, -companyDomain.length).split('.').filter(Boolean);
559
+ const segment = prefix[prefix.length - 1];
560
+ const env = MCP_ENV_SEGMENTS.includes(segment) ? segment : MCP_PROD_SEGMENT;
561
+ return `${env}.${companyDomain}`;
562
+ }
534
563
  /** Tenant company name (domain-metadata driven), falling back to PosiWise. */
535
564
  get mcpCompanyName() {
536
565
  return this.appConfig?.company?.name || 'PosiWise';