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/employment/employment.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
Student,
} from '@oyster/types';

import { parseLinkedInCompanySlug } from '@/modules/employment/use-cases/fetch-company-from-linkedin';
import { parseLinkedInCompanySlug } from '@/modules/employment/employment.utils';
import { ListSearchParams } from '@/shared/types';

// Enums
Expand Down
25 changes: 25 additions & 0 deletions packages/core/src/modules/employment/employment.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Normalizes user input into a LinkedIn company slug.
*
* Accepts either a full LinkedIn company URL or a bare slug, so forms can be
* lenient about what they accept.
*
* This lives apart from the use cases that fetch from LinkedIn because it is
* also needed by `employment.types.ts`, which is bundled for the browser. Any
* import of a use case from there pulls Redis and the Sentry Node SDK into the
* client build.
*
* @example parseLinkedInCompanySlug('https://www.linkedin.com/company/google') // 'google'
* @example parseLinkedInCompanySlug('@google') // 'google'
*/
export function parseLinkedInCompanySlug(input: string) {
const trimmed = input.trim();

const match = trimmed.match(/linkedin\.com\/company\/([^/?#]+)/i);

if (match?.[1]) {
return match[1];
}

return trimmed.replace(/^@/, '');
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,46 @@ import { z } from 'zod';

import { withCache } from '@/infrastructure/redis';
import { runActor } from '@/modules/apify';
import { parseLinkedInCompanySlug } from '@/modules/employment/employment.utils';
import { ColorStackError } from '@/shared/errors';

const LinkedInCompany = z.object({
const LinkedInLogo = z.object({
height: z.number().optional(),
url: z.string().url(),
width: z.number().optional(),
});

const LinkedInCompanyResponse = z.object({
description: z.string().nullish(),
id: z.string(),
logo: z.string().url().optional(),
logos: LinkedInLogo.array().optional(),
name: z.string(),
universalName: z.string(),
website: z.string().url().nullish(),
});

export type LinkedInCompany = z.infer<typeof LinkedInCompany>;

export function parseLinkedInCompanySlug(input: string) {
const trimmed = input.trim();
const LinkedInCompany = LinkedInCompanyResponse.transform((company) => {
return {
description: company.description,
id: company.id,
logo: company.logo ?? getBestLogoUrl(company.logos),
name: company.name,
universalName: company.universalName,
website: company.website,
};
});

const match = trimmed.match(/linkedin\.com\/company\/([^/?#]+)/i);
export type LinkedInCompany = z.infer<typeof LinkedInCompany>;

if (match?.[1]) {
return match[1];
function getBestLogoUrl(logos: z.infer<typeof LinkedInLogo>[] | undefined) {
if (!logos?.length) {
return undefined;
}

return trimmed.replace(/^@/, '');
return logos.reduce((best, current) => {
return (current.width ?? 0) > (best.width ?? 0) ? current : best;
}).url;
}

export async function fetchCompanyFromLinkedIn(
Expand Down
Loading