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
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default function EditCompanyPage() {
)}
<CompanyResumeBooksField
defaultValue={resumeBookIds}
error={errors.resumeBookIds || error}
error={errors.resumeBookIds}
resumeBooks={resumeBooks}
/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ export async function updateCompany({
}
}

const conflictingCompany = await findCompanyWithSameLinkedIn({
id,
linkedinId,
linkedinSlug: resolvedLinkedInSlug ?? linkedinSlug,
});

if (conflictingCompany) {
return fail({
code: 409,
error: `${conflictingCompany.name} is already linked to that LinkedIn company.`,
});
}

await db
.updateTable('companies')
.set({
Expand All @@ -74,6 +87,37 @@ export async function updateCompany({
return success({});
}

/**
* Finds another company that already holds the LinkedIn ID or slug we're about
* to save. Both columns are unique, so writing them without this check fails
* with a constraint violation instead of an error we can show to the admin.
*/
async function findCompanyWithSameLinkedIn({
id,
linkedinId,
linkedinSlug,
}: {
id: string;
linkedinId?: string | null;
linkedinSlug?: string | null;
}) {
if (!linkedinId && !linkedinSlug) {
return undefined;
}

return db
.selectFrom('companies')
.select(['id', 'name'])
.where('id', '!=', id)
.where((eb) => {
return eb.or([
...(linkedinId ? [eb('linkedinId', '=', linkedinId)] : []),
...(linkedinSlug ? [eb('linkedinSlug', '=', linkedinSlug)] : []),
]);
})
.executeTakeFirst();
}

export async function syncCompanyFromLinkedIn(id: string) {
const company = await db
.selectFrom('companies')
Expand Down Expand Up @@ -106,6 +150,19 @@ export async function syncCompanyFromLinkedIn(id: string) {
});
}

const conflictingCompany = await findCompanyWithSameLinkedIn({
id,
linkedinId: companyFromLinkedIn.id,
linkedinSlug: companyFromLinkedIn.universalName,
});

if (conflictingCompany) {
return fail({
code: 409,
error: `${conflictingCompany.name} is already linked to that LinkedIn company.`,
});
}

await db
.updateTable('companies')
.set({
Expand Down
Loading