Skip to content

Commit

Permalink
Email based organization assign in new integration fw (#1368)
Browse files Browse the repository at this point in the history
  • Loading branch information
skwowet authored Aug 21, 2023
1 parent 8de242b commit 9947682
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
55 changes: 55 additions & 0 deletions services/apps/data_sink_worker/src/repo/organization.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,61 @@ export class OrganizationRepository extends RepositoryBase<OrganizationRepositor
return result
}

public async findByDomain(
tenantId: string,
segmentId: string,
domain: string,
): Promise<IDbOrganization> {
const result = await this.db().oneOrNone(
`
SELECT
o.id,
o.name,
o.url,
o.description,
o.emails,
o.logo,
o.tags,
o.github,
o.twitter,
o.linkedin,
o.crunchbase,
o.employees,
o.location,
o.website,
o.type,
o.size,
o.headline,
o.industry,
o.founded,
o.attributes
FROM
organizations o
WHERE
o."tenantId" = $(tenantId) AND
(
o.website ILIKE $(protocolDomain) OR
o.website ILIKE $(domainWithWww) OR
o.website ILIKE $(domain)
) AND
o.id IN (
SELECT os."organizationId"
FROM "organizationSegments" os
WHERE os."segmentId" = $(segmentId)
)
`,
{
tenantId,
protocolDomain: `%://${domain}`,
domainWithWww: `%://www.${domain}`,
domain,
segmentId,
},
)

return result
}

public async insert(tenantId: string, data: IDbInsertOrganizationData): Promise<string> {
const id = generateUUIDv1()
const ts = new Date()
Expand Down
48 changes: 48 additions & 0 deletions services/apps/data_sink_worker/src/service/member.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ export default class MemberService extends LoggerBase {
}
}

if (data.emails) {
const orgIds = await this.assignOrganizationByEmailDomain(
tenantId,
segmentId,
data.emails,
)
if (orgIds.length > 0) {
organizationIds.push(...orgIds)
}
}

return {
id,
organizationIds,
Expand Down Expand Up @@ -179,6 +190,17 @@ export default class MemberService extends LoggerBase {
}
}

if (data.emails) {
const orgIds = await this.assignOrganizationByEmailDomain(
tenantId,
segmentId,
data.emails,
)
if (orgIds.length > 0) {
organizationIds.push(...orgIds)
}
}

return { updated, organizationIds }
})

Expand All @@ -198,6 +220,32 @@ export default class MemberService extends LoggerBase {
}
}

public async assignOrganizationByEmailDomain(
tenantId: string,
segmentId: string,
emails: string[],
): Promise<string[]> {
const orgService = new OrganizationService(this.store, this.log)
const organizationIds: string[] = []
const emailDomains = new Set<string>()

// Collect unique domains
for (const email of emails) {
const domain = email.split('@')[1]
emailDomains.add(domain)
}

// Assign member to organization based on email domain
for (const domain of emailDomains) {
const org = await orgService.findByDomain(tenantId, segmentId, domain as string)
if (org) {
organizationIds.push(org.id)
}
}

return organizationIds
}

public async processMemberEnrich(
tenantId: string,
integrationId: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ export class OrganizationService extends LoggerBase {
await this.repo.addToMember(memberId, orgIds)
}

public async findByDomain(
tenantId: string,
segmentId: string,
domain: string,
): Promise<IOrganization> {
return await this.repo.findByDomain(tenantId, segmentId, domain)
}

public async processOrganizationEnrich(
tenantId: string,
integrationId: string,
Expand Down

0 comments on commit 9947682

Please sign in to comment.