Skip to content

The open alternative to Salesforce, designed for AI.

1.8 KB
Raw Blame
import { buildPersonNameParam } from 'src/logic-functions/utils/build-person-name-param';
import { toText } from 'src/logic-functions/utils/to-text';
import { type MinLikelihoods } from 'src/types/min-likelihoods';
import { type PdlPersonEnrichParams } from 'src/types/pdl-person-enrich-params';
import { type PersonNode } from 'src/types/person-node';
import { isDefined } from 'src/utils/is-defined';
import { pruneUndefined } from 'src/utils/prune-undefined';

export const extractPersonMatchParams = ({
  node,
  minLikelihoods,
}: {
  node: PersonNode;
  minLikelihoods: MinLikelihoods;
}): PdlPersonEnrichParams | undefined => {
  const existingPdlId = toText(node.pdlId);
  if (isDefined(existingPdlId)) {
    return {
      pdlId: existingPdlId,
      minLikelihood: minLikelihoods.strongIdentifierMinLikelihood,
    };
  }

  const linkedinProfileUrl = toText(node.linkedinLink?.primaryLinkUrl);
  const primaryEmail = toText(node.emails?.primaryEmail);
  const fullName = buildPersonNameParam({
    firstName: node.name?.firstName,
    lastName: node.name?.lastName,
  });
  const companyName = toText(node.company?.name);
  const hasStrongIdentifier =
    isDefined(linkedinProfileUrl) || isDefined(primaryEmail);

  const nameIsUsableAsMatchSignal =
    isDefined(fullName) && (hasStrongIdentifier || isDefined(companyName));

  const personMatchParams = pruneUndefined({
    profile: linkedinProfileUrl,
    email: primaryEmail,
    name: nameIsUsableAsMatchSignal ? fullName : undefined,
    company: nameIsUsableAsMatchSignal ? companyName : undefined,
  });

  if (Object.keys(personMatchParams).length === 0) {
    return undefined;
  }

  return {
    ...personMatchParams,
    minLikelihood: hasStrongIdentifier
      ? minLikelihoods.strongIdentifierMinLikelihood
      : minLikelihoods.weakIdentifierMinLikelihood,
  };
};