MCPcopy Create free account
hub / github.com/adcontextprotocol/adcp / resolveFromDatabase

Function resolveFromDatabase

server/src/db/domain-resolution-db.ts:144–207  ·  view source on GitHub ↗

* Database-only resolution: exact match, brand alias, sub-brand hierarchy.

(normalized: string)

Source from the content-addressed store, hash-verified

142 * Database-only resolution: exact match, brand alias, sub-brand hierarchy.
143 */
144async function resolveFromDatabase(normalized: string): Promise<DomainResolution | null> {
145 const pool = getPool();
146
147 // 1. Exact match: organization_domains or organizations.email_domain
148 const exact = await pool.query<{ workos_organization_id: string; domain: string }>(
149 `SELECT o.workos_organization_id, COALESCE(od.domain, o.email_domain) AS domain
150 FROM organizations o
151 LEFT JOIN organization_domains od
152 ON o.workos_organization_id = od.workos_organization_id AND od.domain = $1
153 WHERE o.email_domain = $1 OR od.domain = $1
154 LIMIT 1`,
155 [normalized]
156 );
157
158 if (exact.rows.length > 0) {
159 return {
160 orgId: exact.rows[0].workos_organization_id,
161 matchedDomain: exact.rows[0].domain,
162 method: 'exact',
163 };
164 }
165
166 // 2. Brand alias: domain is a known alias → resolve to canonical brand → find org
167 // e.g. omc.com → omnicomgroup.com → Omnicom org
168 const brandAlias = await pool.query<{ workos_organization_id: string; brand_domain: string }>(
169 `SELECT o.workos_organization_id, bda.brand_domain
170 FROM brand_domain_aliases bda
171 JOIN organizations o ON o.email_domain = bda.brand_domain
172 WHERE bda.alias_domain = $1
173 LIMIT 1`,
174 [normalized]
175 );
176
177 if (brandAlias.rows.length > 0) {
178 return {
179 orgId: brandAlias.rows[0].workos_organization_id,
180 matchedDomain: brandAlias.rows[0].brand_domain,
181 method: 'brand_alias',
182 };
183 }
184
185 // 3. Sub-brand: domain is a discovered brand whose house_domain maps to an org
186 // e.g. instagram.com → house_domain=meta.com → Meta org
187 const subBrand = await pool.query<{ workos_organization_id: string; house_domain: string }>(
188 `SELECT o.workos_organization_id, db.house_domain
189 FROM brands db
190 JOIN organizations o ON o.email_domain = db.house_domain
191 WHERE db.domain = $1
192 AND db.house_domain IS NOT NULL
193 AND db.house_domain != $1
194 LIMIT 1`,
195 [normalized]
196 );
197
198 if (subBrand.rows.length > 0) {
199 return {
200 orgId: subBrand.rows[0].workos_organization_id,
201 matchedDomain: subBrand.rows[0].house_domain,

Callers 1

resolveOrgByDomainFunction · 0.85

Calls 1

getPoolFunction · 0.85

Tested by

no test coverage detected