Skip to content

Commit

Permalink
organization domain and url, date field fixes, displayName fix on update
Browse files Browse the repository at this point in the history
  • Loading branch information
epipav committed Jul 24, 2023
1 parent ad1c9ef commit 3d4276a
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 20 deletions.
2 changes: 1 addition & 1 deletion services/apps/data_sink_worker/src/repo/member.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function getSelectMemberColumnSet(instance: DbInstance): DbColumnSet {
if (getMemberColumnSet) return getMemberColumnSet

getMemberColumnSet = new instance.helpers.ColumnSet(
['id', 'emails', 'score', 'joinedAt', 'reach', 'attributes', 'weakIdentities'],
['id', 'emails', 'score', 'joinedAt', 'reach', 'attributes', 'weakIdentities', 'displayName'],
{
table: {
table: 'members',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export function getUpdateOrganizationColumnSet(instance: DbInstance): DbColumnSe
'industry',
'founded',
'updatedAt',
'attributes',
],
{
table: {
Expand Down
3 changes: 2 additions & 1 deletion services/apps/data_sink_worker/src/service/member.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export default class MemberService extends LoggerBase {
weakIdentities: member.weakIdentities || undefined,
identities: member.identities,
organizations: member.organizations,
displayName: member.displayName || undefined,
},
dbMember,
false,
Expand Down Expand Up @@ -368,7 +369,7 @@ export default class MemberService extends LoggerBase {
attributes,
weakIdentities,
identities,
displayName: member.displayName,
displayName: member.displayName || dbMember.displayName,
}
}
}
14 changes: 11 additions & 3 deletions services/apps/data_sink_worker/src/service/organization.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import mergeWith from 'lodash.mergewith'
import isEqual from 'lodash.isequal'
import IntegrationRepository from '@/repo/integration.repo'
import { IDbInsertOrganizationData } from '@/repo/organization.data'
import { OrganizationRepository } from '@/repo/organization.repo'
Expand Down Expand Up @@ -82,9 +84,15 @@ export class OrganizationService extends LoggerBase {
const existing = await this.repo.findByName(tenantId, segmentId, data.name)

const displayName = existing.displayName ? existing.displayName : data.name
const attributes = existing.attributes
? { ...existing.attributes, ...data.attributes }
: data.attributes

let attributes = existing.attributes

if (data.attributes) {
const temp = mergeWith({}, existing.attributes, data.attributes)
if (!isEqual(temp, existing.attributes)) {
attributes = temp
}
}

if (existing) {
// if it does exists update it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { IGenerateStreamsContext, IProcessStreamContext } from '@/types'
import axios, { AxiosRequestConfig } from 'axios'
import { getNangoToken } from './../../../nango'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { IGenerateStreamsContext, IProcessStreamContext } from '@/types'
import axios, { AxiosRequestConfig } from 'axios'
import { getNangoToken } from './../../../nango'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { IGenerateStreamsContext, IProcessStreamContext } from '@/types'
import axios, { AxiosRequestConfig } from 'axios'
import { getNangoToken } from './../../../nango'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { IGenerateStreamsContext, IProcessStreamContext } from '@/types'
import axios, { AxiosRequestConfig } from 'axios'
import { getNangoToken } from './../../../nango'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { IGenerateStreamsContext, IProcessStreamContext } from '@/types'
import { HubspotAssociationType, HubspotEndpoint, IHubspotContact, IHubspotObject } from '../types'
import { IHubspotContact, IHubspotObject } from '../types'
import axios, { AxiosRequestConfig } from 'axios'
import { HubspotFieldMapper } from '../field-mapper/hubspotFieldMapper'
import { getNangoToken } from './../../../nango'
import { PlatformType } from '@crowd/types'
import { IPaginatedResponse } from './types'
import { RequestThrottler } from '@crowd/common'
import { HubspotMemberFieldMapper } from '../field-mapper/memberFieldMapper'
import { HubspotOrganizationFieldMapper } from '../field-mapper/organizationFieldMapper'
import { getContactAssociations } from './contactAssociations'
import { getCompanyById } from './companyById'
import { HUBSPOT_API_PAGE_SIZE } from './common'

export const getCompanies = async (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { IGenerateStreamsContext, IProcessStreamContext } from '@/types'
import { HubspotAssociationType, HubspotEndpoint, IHubspotContact, IHubspotObject } from '../types'
import axios, { AxiosRequestConfig } from 'axios'
import { HubspotFieldMapper } from '../field-mapper/hubspotFieldMapper'
import { getNangoToken } from './../../../nango'
import { PlatformType } from '@crowd/types'
import { IPaginatedResponse } from './types'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import {
IMemberAttribute,
IMemberData,
IMemberIdentity,
MemberAttributeName,
MemberAttributeType,
PlatformType,
ITagOpensearch,
} from '@crowd/types'
import { HubspotPropertyType, IFieldProperty, IHubspotContact } from '../types'
import { HubspotFieldMapper } from './hubspotFieldMapper'
import { HubspotOrganizationFieldMapper } from './organizationFieldMapper'
import { serializeArray, serializeDate } from './utils/serialization'
import { ITagOpensearch } from '../../../../../../types/dist/tags'

export class HubspotMemberFieldMapper extends HubspotFieldMapper {
protected fieldProperties: Record<string, IFieldProperty> = {
Expand Down Expand Up @@ -167,11 +169,11 @@ export class HubspotMemberFieldMapper extends HubspotFieldMapper {
const crowdKey = this.getCrowdFieldName(hubspotPropertyName)

// discard readonly fields, readonly fields will be only used when pushing data back to hubspot
if (!this.fieldProperties[crowdKey].readonly) {
if (crowdKey && !this.fieldProperties[crowdKey].readonly) {
// For incoming integrations, we already get the member email from hubspot defined field `email`
// if user mapped crowd field `emails` to some other field
// this will be saved to the mapped field when sending the member back to hubspot
if (crowdKey && crowdKey !== 'emails' && contactProperties[hubspotPropertyName] !== null) {
if (crowdKey !== 'emails' && contactProperties[hubspotPropertyName] !== null) {
if (crowdKey.startsWith('attributes')) {
const crowdAttributeName = crowdKey.split('.')[1] || null

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { IOrganization, MemberAttributeName, PlatformType } from '@crowd/types'
/* eslint-disable @typescript-eslint/no-explicit-any */

import { IOrganization, OrganizationAttributeName, PlatformType } from '@crowd/types'
import { HubspotPropertyType, IFieldProperty, IHubspotObject } from '../types'
import { HubspotFieldMapper } from './hubspotFieldMapper'
import { serializeArray } from './utils/serialization'
Expand Down Expand Up @@ -46,7 +48,7 @@ export class HubspotOrganizationFieldMapper extends HubspotFieldMapper {
hubspotType: HubspotPropertyType.STRING,
readonly: true,
serialize: (linkedin: any) => {
return linkedin.handle
return linkedin?.handle ? linkedin.handle : undefined
},
},
crunchbase: {
Expand Down Expand Up @@ -111,6 +113,10 @@ export class HubspotOrganizationFieldMapper extends HubspotFieldMapper {
override getEntity(hubspotOrganization: IHubspotObject): IOrganization {
this.ensureFieldMapExists()

if (!this.hubspotId) {
throw new Error('Hubspot Id should be set before parsing the organization!')
}

const organizationProperties = hubspotOrganization.properties as any

if (!organizationProperties.name) {
Expand All @@ -122,9 +128,15 @@ export class HubspotOrganizationFieldMapper extends HubspotFieldMapper {
const organization: IOrganization = {
name: organizationProperties.name,
attributes: {
[MemberAttributeName.SOURCE_ID]: {
[OrganizationAttributeName.SOURCE_ID]: {
[PlatformType.HUBSPOT]: hubspotOrganization.id,
},
[OrganizationAttributeName.URL]: {
[PlatformType.HUBSPOT]: `https://app.hubspot.com/contacts/${this.hubspotId}/company/${hubspotOrganization.id}`,
},
[OrganizationAttributeName.DOMAIN]: {
[PlatformType.HUBSPOT]: organizationProperties.domain,
},
},
}

Expand All @@ -133,8 +145,8 @@ export class HubspotOrganizationFieldMapper extends HubspotFieldMapper {
const crowdKey = this.getCrowdFieldName(hubspotPropertyName)

// discard readonly fields, readonly fields will be only used when pushing data back to hubspot
if (!this.fieldProperties[crowdKey].readonly) {
if (crowdKey && organizationProperties[hubspotPropertyName] !== null) {
if (crowdKey && !this.fieldProperties[crowdKey].readonly) {
if (organizationProperties[hubspotPropertyName] !== null) {
organization[crowdKey] = organizationProperties[hubspotPropertyName]

// fix for linkedin social, it comes as a full url
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export const serializeArray = (array: string[]): string => {
if (!array || array.length === 0) {
return undefined
}
return array.toString()
}

Expand All @@ -8,7 +11,7 @@ export const deserializeArray = (string: string): string[] => {

export const serializeDate = (date: string): number => {
const dateObj = new Date(date)
if (isNaN(dateObj.getTime())) {
if (isNaN(dateObj.getTime()) || dateObj.getTime() === 0) {
return undefined
}
dateObj.setUTCHours(0, 0, 0, 0)
Expand Down
6 changes: 6 additions & 0 deletions services/libs/types/src/enums/organizations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum OrganizationAttributeName {
SOURCE_ID = 'sourceId',
URL = 'url',
SYNC_REMOTE = 'syncRemote',
DOMAIN = 'domain',
}
3 changes: 3 additions & 0 deletions services/libs/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from './members'
export * from './enums/activities'
export * from './activities'

export * from './enums/organizations'
export * from './organizations'

export * from './enums/edition'
Expand All @@ -34,3 +35,5 @@ export * from './opensearch'
export * from './enums/webhooks'

export * from './enums/entities'

export * from './tags'

0 comments on commit 3d4276a

Please sign in to comment.