diff --git a/packages/core/src/modules/apify.ts b/packages/core/src/modules/apify.ts index 541e1d36e..c2c6e6812 100644 --- a/packages/core/src/modules/apify.ts +++ b/packages/core/src/modules/apify.ts @@ -27,7 +27,7 @@ const rateLimiter = new RateLimiter('apify:connections', { type RunActorInput = | { actorId: 'harvestapi~linkedin-company'; - body: { companies: string[] }; + body: { companies: string[] } | { searches: string[] }; } | { actorId: 'harvestapi~linkedin-profile-scraper'; diff --git a/packages/core/src/modules/employment/employment.utils.test.ts b/packages/core/src/modules/employment/employment.utils.test.ts new file mode 100644 index 000000000..0e1817708 --- /dev/null +++ b/packages/core/src/modules/employment/employment.utils.test.ts @@ -0,0 +1,36 @@ +import { describe, expect, test } from 'bun:test'; + +import { + isLinkedInCompanyReference, + parseLinkedInCompanySlug, +} from './employment.utils'; + +describe(isLinkedInCompanyReference.name, () => { + test('Should return true for a numeric LinkedIn ID.', () => { + expect(isLinkedInCompanyReference('1441')).toBe(true); + }); + + test('Should return true for a LinkedIn company URL.', () => { + expect( + isLinkedInCompanyReference('https://www.linkedin.com/company/google') + ).toBe(true); + }); + + test('Should return false for a company name.', () => { + expect(isLinkedInCompanyReference('Capital One')).toBe(false); + expect(isLinkedInCompanyReference('JPMorgan Chase & Co.')).toBe(false); + expect(isLinkedInCompanyReference('Google')).toBe(false); + }); +}); + +describe(parseLinkedInCompanySlug.name, () => { + test('Should extract the slug from a LinkedIn company URL.', () => { + expect( + parseLinkedInCompanySlug('https://www.linkedin.com/company/capital-one/') + ).toBe('capital-one'); + }); + + test('Should strip a leading "@" from a slug.', () => { + expect(parseLinkedInCompanySlug('@google')).toBe('google'); + }); +}); diff --git a/packages/core/src/modules/employment/employment.utils.ts b/packages/core/src/modules/employment/employment.utils.ts index d6584f14a..05089d3cf 100644 --- a/packages/core/src/modules/employment/employment.utils.ts +++ b/packages/core/src/modules/employment/employment.utils.ts @@ -23,3 +23,18 @@ export function parseLinkedInCompanySlug(input: string) { return trimmed.replace(/^@/, ''); } + +/** + * Returns `true` if the input points to a specific LinkedIn company (ie: a + * numeric LinkedIn ID or a LinkedIn company URL), and `false` if it is a + * free-text company name that we need to search for. + * + * @example isLinkedInCompanyReference('1441') // true + * @example isLinkedInCompanyReference('https://www.linkedin.com/company/google') // true + * @example isLinkedInCompanyReference('Capital One') // false + */ +export function isLinkedInCompanyReference(input: string) { + const trimmed = input.trim(); + + return /^\d+$/.test(trimmed) || /linkedin\.com\/company\//i.test(trimmed); +} diff --git a/packages/core/src/modules/employment/use-cases/fetch-company-from-linkedin.ts b/packages/core/src/modules/employment/use-cases/fetch-company-from-linkedin.ts index 555380ed3..ce466d42d 100644 --- a/packages/core/src/modules/employment/use-cases/fetch-company-from-linkedin.ts +++ b/packages/core/src/modules/employment/use-cases/fetch-company-from-linkedin.ts @@ -44,23 +44,58 @@ function getBestLogoUrl(logos: z.infer[] | undefined) { }).url; } +/** + * Fetches a company from LinkedIn by its slug, numeric ID or company URL. + * + * @param companyNameOrLinkedInSlug - LinkedIn slug, numeric ID or company URL. + */ export async function fetchCompanyFromLinkedIn( companyNameOrLinkedInSlug: string ): Promise { const linkedInSlug = parseLinkedInCompanySlug(companyNameOrLinkedInSlug); - const apifyResult = await withCache( + return runLinkedInCompanyActor( `harvestapi~linkedin-company:v2:${linkedInSlug}`, - 60 * 60 * 24 * 30, - async () => { - return runActor({ - actorId: 'harvestapi~linkedin-company', - body: { - companies: [`https://www.linkedin.com/company/${linkedInSlug}`], - }, - }); - } + { companies: [`https://www.linkedin.com/company/${linkedInSlug}`] } ); +} + +/** + * Searches LinkedIn for a company by its name (ie: "Capital One") and returns + * the top result. Use this when we don't know the company's LinkedIn slug, + * such as when the name was extracted from a job posting. + * + * @param companyName - Name of the company to search for. + */ +export async function searchCompanyOnLinkedIn( + companyName: string +): Promise { + const name = companyName.trim(); + + return runLinkedInCompanyActor( + `harvestapi~linkedin-company:search:v1:${name.toLowerCase()}`, + { searches: [name] } + ); +} + +async function runLinkedInCompanyActor( + cacheKey: string, + body: { companies: string[] } | { searches: string[] } +): Promise { + const apifyResult = await withCache(cacheKey, 60 * 60 * 24 * 30, async () => { + const result = await runActor({ + actorId: 'harvestapi~linkedin-company', + body, + }); + + // We don't want to cache an empty result, otherwise a company that + // wasn't found once would never be looked up again for 30 days. + return Array.isArray(result) && !result.length ? null : result; + }); + + if (!apifyResult) { + return null; + } const parseResult = z.array(LinkedInCompany).safeParse(apifyResult); diff --git a/packages/core/src/modules/employment/use-cases/save-company-if-necessary.ts b/packages/core/src/modules/employment/use-cases/save-company-if-necessary.ts index 73f1053ab..69931764a 100644 --- a/packages/core/src/modules/employment/use-cases/save-company-if-necessary.ts +++ b/packages/core/src/modules/employment/use-cases/save-company-if-necessary.ts @@ -9,9 +9,11 @@ import { R2_PUBLIC_BUCKET_NAME, R2_PUBLIC_BUCKET_URL, } from '@/infrastructure/s3'; +import { isLinkedInCompanyReference } from '@/modules/employment/employment.utils'; import { fetchCompanyFromLinkedIn, getDomainFromCompanyWebsite, + searchCompanyOnLinkedIn, } from '@/modules/employment/use-cases/fetch-company-from-linkedin'; const R2_ACCESS_KEY_ID = process.env.R2_ACCESS_KEY_ID || ''; @@ -23,11 +25,12 @@ const R2_ACCESS_KEY_ID = process.env.R2_ACCESS_KEY_ID || ''; * - If the company is found in our database, it will return the ID of the * existing company. * - If the company is not found in our database, we will scrape the company - * from LinkedIn and save the company in our database. Will throw an error if - * the company is not found in LinkedIn. + * from LinkedIn and save the company in our database. A LinkedIn ID or URL + * is looked up directly, while a company name is searched for. Returns + * `null` if the company is not found on LinkedIn. * * @param trx - Database transaction to use for the operation. - * @param companyName - Name of the company. + * @param companyNameOrLinkedInId - Name, LinkedIn ID or LinkedIn URL. */ export async function saveCompanyIfNecessary( trx: Transaction, @@ -52,9 +55,11 @@ export async function saveCompanyIfNecessary( return existingCompany.id; } - const companyFromLinkedIn = await fetchCompanyFromLinkedIn( + const companyFromLinkedIn = isLinkedInCompanyReference( companyNameOrLinkedInId - ); + ) + ? await fetchCompanyFromLinkedIn(companyNameOrLinkedInId) + : await searchCompanyOnLinkedIn(companyNameOrLinkedInId); if (!companyFromLinkedIn) { return null; diff --git a/packages/core/src/modules/opportunities.ts b/packages/core/src/modules/opportunities.ts index fac58355b..ad8737589 100644 --- a/packages/core/src/modules/opportunities.ts +++ b/packages/core/src/modules/opportunities.ts @@ -833,7 +833,7 @@ export async function refineOpportunity( .set({ ...(data.description && { description: data.description }), ...(data.title && { title: data.title }), - companyId, + ...(companyId && { companyId }), expiresAt, }) .where('id', '=', input.opportunityId)