Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/modules/apify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
36 changes: 36 additions & 0 deletions packages/core/src/modules/employment/employment.utils.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
15 changes: 15 additions & 0 deletions packages/core/src/modules/employment/employment.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,58 @@ function getBestLogoUrl(logos: z.infer<typeof LinkedInLogo>[] | 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<LinkedInCompany | null> {
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<LinkedInCompany | null> {
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<LinkedInCompany | null> {
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 || '';
Expand All @@ -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<DB>,
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/opportunities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading