Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

refactor: consolidate entity resolution behind shared canonical resolver

Khuram Niazcd2ade1462ef
diff --git a/__tests__/ats-runner.spec.ts b/__tests__/ats-runner.spec.ts
index aced445..742061d 100644
--- a/__tests__/ats-runner.spec.ts
+++ b/__tests__/ats-runner.spec.ts
@@ -13,10 +13,10 @@ vi.mock("@prisma/client", () => {
resume: { findUnique: vi.fn() },
userSettings: { findUnique: vi.fn() },
job: { findMany: vi.fn(), create: vi.fn() },
- jobTitle: { findFirst: vi.fn(), create: vi.fn() },
- location: { findFirst: vi.fn(), create: vi.fn() },
- company: { findFirst: vi.fn(), create: vi.fn() },
- jobSource: { findFirst: vi.fn(), create: vi.fn() },
+ jobTitle: { findUnique: vi.fn(), create: vi.fn() },
+ location: { findUnique: vi.fn(), create: vi.fn() },
+ company: { findUnique: vi.fn(), create: vi.fn() },
+ jobSource: { findUnique: vi.fn(), create: vi.fn() },
jobStatus: { findFirst: vi.fn(), create: vi.fn() },
};
return {
@@ -117,10 +117,10 @@ describe("runAutomation (lever)", () => {
});
(prisma.job.findMany as any).mockResolvedValue([]);
(prisma.job.create as any).mockResolvedValue({});
- (prisma.jobTitle.findFirst as any).mockResolvedValue({ id: "jt" });
- (prisma.location.findFirst as any).mockResolvedValue({ id: "loc" });
- (prisma.company.findFirst as any).mockResolvedValue({ id: "co" });
- (prisma.jobSource.findFirst as any).mockResolvedValue({ id: "src" });
+ (prisma.jobTitle.findUnique as any).mockResolvedValue({ id: "jt" });
+ (prisma.location.findUnique as any).mockResolvedValue({ id: "loc" });
+ (prisma.company.findUnique as any).mockResolvedValue({ id: "co" });
+ (prisma.jobSource.findUnique as any).mockResolvedValue({ id: "src" });
(prisma.jobStatus.findFirst as any).mockResolvedValue({ id: "st" });
(generateText as any).mockResolvedValue({
diff --git a/__tests__/canonicalize.spec.ts b/__tests__/canonicalize.spec.ts
new file mode 100644
index 0000000..96a7c4b
--- /dev/null
+++ b/__tests__/canonicalize.spec.ts
@@ -0,0 +1,65 @@
+import { canonicalizeEntityValue } from "@/lib/jobs/canonicalize";
+
+describe("canonicalizeEntityValue", () => {
+ it("lowercases and trims", () => {
+ expect(canonicalizeEntityValue(" Figma ")).toBe("figma");
+ });
+
+ it("collapses internal whitespace to a single space", () => {
+ expect(canonicalizeEntityValue("Full Stack Developer")).toBe(
+ "full stack developer",
+ );
+ });
+
+ it("treats commas as separators", () => {
+ expect(canonicalizeEntityValue("San Francisco, CA")).toBe(
+ "san francisco ca",
+ );
+ });
+
+ it("folds diacritics", () => {
+ expect(canonicalizeEntityValue("Zürich")).toBe("zurich");
+ expect(canonicalizeEntityValue("México City")).toBe("mexico city");
+ });
+
+ it("keeps distinguishing punctuation so C++ and C# never collapse", () => {
+ expect(canonicalizeEntityValue("C++ Developer")).toBe("c++ developer");
+ expect(canonicalizeEntityValue("C# Developer")).toBe("c# developer");
+ expect(canonicalizeEntityValue("C++ Developer")).not.toBe(
+ canonicalizeEntityValue("C# Developer"),
+ );
+ });
+
+ it("does not fold seniority (Senior vs Junior stay distinct)", () => {
+ expect(canonicalizeEntityValue("Senior Engineer")).not.toBe(
+ canonicalizeEntityValue("Junior Engineer"),
+ );
+ });
+
+ describe("legal-suffix stripping (companies only)", () => {
+ const strip = { stripLegalSuffix: true };
+
+ it("collapses Inc / LLC / Ltd variants of the same company", () => {
+ const canonical = canonicalizeEntityValue("Google", strip);
+ expect(canonicalizeEntityValue("Google Inc", strip)).toBe(canonical);
+ expect(canonicalizeEntityValue("Google, Inc.", strip)).toBe(canonical);
+ expect(canonicalizeEntityValue("Google LLC", strip)).toBe(canonical);
+ });
+
+ it("unwinds stacked suffixes", () => {
+ expect(canonicalizeEntityValue("Foo Corp Company", strip)).toBe("foo");
+ });
+
+ it("does not strip when stripLegalSuffix is off", () => {
+ expect(canonicalizeEntityValue("Google Inc")).toBe("google inc");
+ });
+
+ it("does not over-strip a suffix embedded in a real name", () => {
+ expect(canonicalizeEntityValue("Costco", strip)).toBe("costco");
+ });
+
+ it("falls back to the base form when the label is only a suffix", () => {
+ expect(canonicalizeEntityValue("Inc", strip)).toBe("inc");
+ });
+ });
+});
diff --git a/__tests__/company.actions.spec.ts b/__tests__/company.actions.spec.ts
index 629a5d8..9cd65bf 100644
--- a/__tests__/company.actions.spec.ts
+++ b/__tests__/company.actions.spec.ts
@@ -203,7 +203,7 @@ describe("Company Actions", () => {
describe("addCompany", () => {
const validData = {
- company: "New Company",
+ company: "New Employer",
logoUrl: "http://example.com/logo.png",
};
@@ -212,8 +212,8 @@ describe("Company Actions", () => {
(prisma.company.findFirst as any).mockResolvedValue(null);
const mockCompany = {
id: "company-id",
- label: "New Company",
- value: "new company",
+ label: "New Employer",
+ value: "new employer",
logoUrl: "http://example.com/logo.png",
createdBy: mockUser.id,
};
@@ -225,13 +225,13 @@ describe("Company Actions", () => {
expect(result).toEqual({ success: true, data: mockCompany });
expect(prisma.company.findFirst).toHaveBeenCalledWith({
- where: { value: "new company", createdBy: mockUser.id },
+ where: { value: "new employer", createdBy: mockUser.id },
});
expect(prisma.company.create).toHaveBeenCalledWith({
data: {
createdBy: mockUser.id,
- value: "new company",
- label: "New Company",
+ value: "new employer",
+ label: "New Employer",
logoUrl: "http://example.com/logo.png",
},
});
@@ -253,7 +253,7 @@ describe("Company Actions", () => {
const mockExistingCompany = {
id: "existing-company-id",
...validData,
- value: "new company",
+ value: "new employer",
createdBy: mockUser.id,
};
(prisma.company.findFirst as any).mockResolvedValue(mockExistingCompany);
@@ -265,7 +265,7 @@ describe("Company Actions", () => {
message: "Company already exists!",
});
expect(prisma.company.findFirst).toHaveBeenCalledWith({
- where: { value: "new company", createdBy: mockUser.id },
+ where: { value: "new employer", createdBy: mockUser.id },
});
expect(prisma.company.create).not.toHaveBeenCalled();
});
@@ -280,7 +280,7 @@ describe("Company Actions", () => {
expect(result).toEqual({ success: false, message: "Unexpected error" });
expect(prisma.company.findFirst).toHaveBeenCalledWith({
- where: { value: "new company", createdBy: mockUser.id },
+ where: { value: "new employer", createdBy: mockUser.id },
});
expect(prisma.company.create).not.toHaveBeenCalled();
});
@@ -289,7 +289,7 @@ describe("Company Actions", () => {
(getCurrentUser as any).mockResolvedValue(mockUser);
const invalidData = {
- company: "New Company",
+ company: "New Employer",
logoUrl: "javascript:alert('xss')",
};
@@ -308,7 +308,7 @@ describe("Company Actions", () => {
(getCurrentUser as any).mockResolvedValue(mockUser);
const invalidData = {
- company: "New Company",
+ company: "New Employer",
logoUrl: "data:image/png;base64,iVBORw0KGgo=",
};
@@ -327,8 +327,8 @@ describe("Company Actions", () => {
(prisma.company.findFirst as any).mockResolvedValue(null);
const mockCompany = {
id: "company-id",
- label: "New Company",
- value: "new company",
+ label: "New Employer",
+ value: "new employer",
logoUrl: "",
createdBy: mockUser.id,
};
@@ -336,7 +336,7 @@ describe("Company Actions", () => {
(revalidatePath as any).mockResolvedValue(undefined);
const result = await addCompany({
- company: "New Company",
+ company: "New Employer",
logoUrl: "",
});
@@ -349,8 +349,8 @@ describe("Company Actions", () => {
(prisma.company.findFirst as any).mockResolvedValue(null);
const mockCompany = {
id: "company-id",
- label: "New Company",
- value: "new company",
+ label: "New Employer",
+ value: "new employer",
logoUrl: "https://example.com/logo.png",
createdBy: mockUser.id,
};
@@ -358,19 +358,47 @@ describe("Company Actions", () => {
(revalidatePath as any).mockResolvedValue(undefined);
const result = await addCompany({
- company: "New Company",
+ company: "New Employer",
logoUrl: "https://example.com/logo.png",
});
expect(result).toEqual({ success: true, data: mockCompany });
expect(prisma.company.create).toHaveBeenCalled();
});
+
+ it("strips a legal suffix so 'Acme Inc.' matches an existing 'Acme'", async () => {
+ // Regression guard: value computation now passes
+ // { stripLegalSuffix: true }, so a legal-suffix variant of an existing
+ // company is treated as a duplicate instead of creating a second row.
+ (getCurrentUser as any).mockResolvedValue(mockUser);
+ const mockExistingCompany = {
+ id: "existing-company-id",
+ label: "Acme",
+ value: "acme",
+ createdBy: mockUser.id,
+ };
+ (prisma.company.findFirst as any).mockResolvedValue(mockExistingCompany);
+
+ const result = await addCompany({
+ company: "Acme Inc.",
+ logoUrl: "",
+ });
+
+ expect(prisma.company.findFirst).toHaveBeenCalledWith({
+ where: { value: "acme", createdBy: mockUser.id },
+ });
+ expect(result).toEqual({
+ success: false,
+ message: "Company already exists!",
+ });
+ expect(prisma.company.create).not.toHaveBeenCalled();
+ });
});
describe("updateCompany", () => {
const validData = {
id: "company-id",
- company: "Updated Company",
+ company: "Updated Employer",
logoUrl: "http://example.com/logo.png",
createdBy: "user-id",
};
@@ -382,7 +410,7 @@ describe("Company Actions", () => {
const mockUpdatedCompany = {
id: "company-id",
- value: "updated company",
+ value: "updated employer",
};
(prisma.company.update as any).mockResolvedValue(mockUpdatedCompany);
@@ -392,19 +420,44 @@ describe("Company Actions", () => {
expect(result).toEqual({ success: true, data: mockUpdatedCompany });
expect(prisma.company.findFirst).toHaveBeenCalledWith({
- where: { value: "updated company", createdBy: mockUser.id },
+ where: { value: "updated employer", createdBy: mockUser.id },
});
expect(prisma.company.update).toHaveBeenCalledWith({
where: { id: "company-id", createdBy: "user-id" },
data: {
- value: "updated company",
- label: "Updated Company",
+ value: "updated employer",
+ label: "Updated Employer",
logoUrl: "http://example.com/logo.png",
},
});
});
+ it("strips a legal suffix so 'Acme Inc.' matches an existing 'Acme'", async () => {
+ (getCurrentUser as any).mockResolvedValue(mockUser);
+ const mockExistingCompany = {
+ id: "other-company-id",
+ label: "Acme",
+ value: "acme",
+ createdBy: mockUser.id,
+ };
+ (prisma.company.findFirst as any).mockResolvedValue(mockExistingCompany);
+
+ const result = await updateCompany({
+ ...validData,
+ company: "Acme Inc.",
+ });
+
+ expect(prisma.company.findFirst).toHaveBeenCalledWith({
+ where: { value: "acme", createdBy: mockUser.id },
+ });
+ expect(result).toEqual({
+ success: false,
+ message: "Company already exists!",
+ });
+ expect(prisma.company.update).not.toHaveBeenCalled();
+ });
+
it("should return error if user is not authenticated", async () => {
(getCurrentUser as any).mockResolvedValue(null);
@@ -493,7 +546,7 @@ describe("Company Actions", () => {
const mockUpdatedCompany = {
id: "company-id",
- value: "updated company",
+ value: "updated employer",
};
(prisma.company.update as any).mockResolvedValue(mockUpdatedCompany);
diff --git a/__tests__/createJobFromNames.spec.ts b/__tests__/createJobFromNames.spec.ts
index 1f4a0aa..c15eec5 100644
--- a/__tests__/createJobFromNames.spec.ts
+++ b/__tests__/createJobFromNames.spec.ts
@@ -16,7 +16,7 @@ const prisma = new PrismaClient();
vi.mock("@prisma/client", () => {
const mPrismaClient = {
- job: { findFirst: vi.fn() },
+ job: { findFirst: vi.fn(), findMany: vi.fn() },
};
return { PrismaClient: vi.fn(function () { return mPrismaClient; }) };
});
@@ -56,6 +56,7 @@ describe("createJobFromNames", () => {
(resolveJobStatus as any).mockResolvedValue("status-1");
(resolveTags as any).mockResolvedValue({ resolved: [], dropped: [] });
(prisma.job.findFirst as any).mockResolvedValue(null);
+ (prisma.job.findMany as any).mockResolvedValue([]);
(createJobRecord as any).mockResolvedValue({ id: "job-1" });
});
@@ -115,12 +116,15 @@ describe("createJobFromNames", () => {
expect(createJobRecord).toHaveBeenCalled();
});
- it("detects a duplicate by jobUrl and skips creation", async () => {
- (prisma.job.findFirst as any).mockResolvedValueOnce({
- id: "existing-job",
- JobTitle: { label: "Engineer" },
- Company: { label: "Acme" },
- });
+ it("detects a duplicate by jobUrl (aggressive key catches www/case/tracking variants)", async () => {
+ (prisma.job.findMany as any).mockResolvedValueOnce([
+ {
+ id: "existing-job",
+ jobUrl: "https://www.Example.com/job/1?utm_source=x",
+ JobTitle: { label: "Engineer" },
+ Company: { label: "Acme" },
+ },
+ ]);
const result = await createJobFromNames(
{ ...baseInput, jobUrl: "https://example.com/job/1" },
diff --git a/__tests__/greenhouse-runner.spec.ts b/__tests__/greenhouse-runner.spec.ts
index 707ab0f..9d4d322 100644
--- a/__tests__/greenhouse-runner.spec.ts
+++ b/__tests__/greenhouse-runner.spec.ts
@@ -13,10 +13,10 @@ vi.mock("@prisma/client", () => {
resume: { findUnique: vi.fn() },
userSettings: { findUnique: vi.fn() },
job: { findMany: vi.fn(), create: vi.fn() },
- jobTitle: { findFirst: vi.fn(), create: vi.fn() },
- location: { findFirst: vi.fn(), create: vi.fn() },
- company: { findFirst: vi.fn(), create: vi.fn() },
- jobSource: { findFirst: vi.fn(), create: vi.fn() },
+ jobTitle: { findUnique: vi.fn(), create: vi.fn() },
+ location: { findUnique: vi.fn(), create: vi.fn() },
+ company: { findUnique: vi.fn(), create: vi.fn() },
+ jobSource: { findUnique: vi.fn(), create: vi.fn() },
jobStatus: { findFirst: vi.fn(), create: vi.fn() },
};
return {
@@ -139,10 +139,10 @@ describe("runAutomation (greenhouse)", () => {
});
(prisma.job.findMany as any).mockResolvedValue([]); // no existing urls
(prisma.job.create as any).mockResolvedValue({});
- (prisma.jobTitle.findFirst as any).mockResolvedValue({ id: "jt" });
- (prisma.location.findFirst as any).mockResolvedValue({ id: "loc" });
- (prisma.company.findFirst as any).mockResolvedValue({ id: "co" });
- (prisma.jobSource.findFirst as any).mockResolvedValue({ id: "src" });
+ (prisma.jobTitle.findUnique as any).mockResolvedValue({ id: "jt" });
+ (prisma.location.findUnique as any).mockResolvedValue({ id: "loc" });
+ (prisma.company.findUnique as any).mockResolvedValue({ id: "co" });
+ (prisma.jobSource.findUnique as any).mockResolvedValue({ id: "src" });
(prisma.jobStatus.findFirst as any).mockResolvedValue({ id: "st" });
(generateText as any).mockResolvedValue({
@@ -202,7 +202,7 @@ describe("runAutomation (greenhouse)", () => {
expect((generateText as any).mock.calls.length).toBe(0);
});
- // Exercises getExistingJobKeys' wiring of real DB row shape (JobTitle/
+ // Exercises getExistingJobDedupeMap's wiring of real DB row shape (JobTitle/
// Company/Location relations) into jobDedupeKey, not just the pure
// dedupeJobs unit tested in scraper-utils.spec.ts.
it("dedupes against an existing DB job whose URL differs only by www/trailing-slash/tracking-param", async () => {
@@ -469,10 +469,10 @@ describe("runAutomation (jsearch) concurrency", () => {
});
(prisma.job.findMany as any).mockResolvedValue([]); // no existing urls
(prisma.job.create as any).mockResolvedValue({});
- (prisma.jobTitle.findFirst as any).mockResolvedValue({ id: "jt" });
- (prisma.location.findFirst as any).mockResolvedValue({ id: "loc" });
- (prisma.company.findFirst as any).mockResolvedValue({ id: "co" });
- (prisma.jobSource.findFirst as any).mockResolvedValue({ id: "src" });
+ (prisma.jobTitle.findUnique as any).mockResolvedValue({ id: "jt" });
+ (prisma.location.findUnique as any).mockResolvedValue({ id: "loc" });
+ (prisma.company.findUnique as any).mockResolvedValue({ id: "co" });
+ (prisma.jobSource.findUnique as any).mockResolvedValue({ id: "src" });
(prisma.jobStatus.findFirst as any).mockResolvedValue({ id: "st" });
// Hosted provider -> concurrency 3 (Ollama would force 1).
diff --git a/__tests__/job.actions.spec.ts b/__tests__/job.actions.spec.ts
index b4c4d38..e1ce9eb 100644
--- a/__tests__/job.actions.spec.ts
+++ b/__tests__/job.actions.spec.ts
@@ -1,6 +1,7 @@
import {
addJob,
createLocation,
+ createJobSource,
deleteJobById,
getJobDetails,
getJobsList,
@@ -26,6 +27,8 @@ vi.mock("@prisma/client", () => {
},
jobSource: {
findMany: vi.fn(),
+ findFirst: vi.fn(),
+ create: vi.fn(),
},
job: {
findMany: vi.fn(),
@@ -798,6 +801,85 @@ describe("jobActions", () => {
message: "Unexpected error",
});
});
+ it("should canonicalize the value (not just lowercase) for matching", async () => {
+ // Regression guard: value computation was switched from
+ // label.trim().toLowerCase() to canonicalizeEntityValue(), which also
+ // folds diacritics and comma separators. A plain-ASCII label wouldn't
+ // catch a revert to the old behavior.
+ (getCurrentUser as any).mockResolvedValue(mockUser);
+ (prisma.location.findFirst as any).mockResolvedValue(null);
+ (prisma.location.create as any).mockImplementation(({ data }: any) =>
+ Promise.resolve(data),
+ );
+
+ const result = await createLocation("São Paulo, Brazil");
+
+ expect(prisma.location.findFirst).toHaveBeenCalledWith({
+ where: { value: "sao paulo brazil", createdBy: mockUser.id },
+ });
+ expect(result.data.value).toBe("sao paulo brazil");
+ });
+ });
+ describe("createJobSource", () => {
+ it("should throw error when user is not authenticated", async () => {
+ (getCurrentUser as any).mockResolvedValue(null);
+
+ await expect(createJobSource("source-name")).resolves.toStrictEqual({
+ success: false,
+ message: "Not authenticated",
+ });
+ });
+ it("should throw error when job source name is not provided or empty", async () => {
+ (getCurrentUser as any).mockResolvedValue(mockUser);
+ await expect(createJobSource(" ")).resolves.toStrictEqual({
+ success: false,
+ message: "Please provide job source name",
+ });
+ });
+ it("should create with valid input", async () => {
+ const label = "New Source";
+ const mockJobSource = {
+ label: "New Source",
+ value: "new source",
+ createdBy: mockUser.id,
+ };
+ (getCurrentUser as any).mockResolvedValue(mockUser);
+ (prisma.jobSource.findFirst as any).mockResolvedValue(null);
+ (prisma.jobSource.create as any).mockResolvedValue(mockJobSource);
+
+ const result = await createJobSource(label);
+
+ expect(prisma.jobSource.create).toHaveBeenCalledTimes(1);
+ expect(prisma.jobSource.create).toHaveBeenCalledWith({
+ data: mockJobSource,
+ });
+ expect(result).toStrictEqual({
+ data: mockJobSource,
+ success: true,
+ });
+ });
+ it("should reuse an existing job source instead of creating a duplicate", async () => {
+ (getCurrentUser as any).mockResolvedValue(mockUser);
+ const existing = { label: "Indeed", value: "indeed", createdBy: mockUser.id };
+ (prisma.jobSource.findFirst as any).mockResolvedValue(existing);
+
+ const result = await createJobSource("Indeed");
+
+ expect(prisma.jobSource.create).not.toHaveBeenCalled();
+ expect(result).toStrictEqual({ data: existing, success: true });
+ });
+ it("should handle unexpected errors", async () => {
+ (getCurrentUser as any).mockResolvedValue(mockUser);
+ (prisma.jobSource.findFirst as any).mockResolvedValue(null);
+ (prisma.jobSource.create as any).mockRejectedValue(
+ new Error("Unexpected error"),
+ );
+
+ await expect(createJobSource("source-name")).resolves.toStrictEqual({
+ success: false,
+ message: "Unexpected error",
+ });
+ });
});
describe("addJob", () => {
it("should create a new job successfully", async () => {
diff --git a/__tests__/jobDedupe.spec.ts b/__tests__/jobDedupe.spec.ts
new file mode 100644
index 0000000..5fb4fa1
--- /dev/null
+++ b/__tests__/jobDedupe.spec.ts
@@ -0,0 +1,200 @@
+import { getExistingJobDedupeMap, findExistingJobByUrl } from "@/lib/jobs/jobDedupe";
+import { jobDedupeKey } from "@/lib/scraper/utils";
+import { PrismaClient } from "@prisma/client";
+
+const prisma = new PrismaClient();
+
+vi.mock("@prisma/client", () => {
+ const m = { job: { findMany: vi.fn() } };
+ return { PrismaClient: vi.fn(function () { return m; }) };
+});
+
+// Shape a DB row the way getExistingJobDedupeMap selects it.
+function row(over: Partial<{
+ id: string;
+ jobUrl: string | null;
+ title: string | null;
+ company: string | null;
+ location: string | null;
+}> = {}) {
+ return {
+ id: over.id ?? "job-1",
+ jobUrl: over.jobUrl ?? null,
+ JobTitle: over.title === null ? null : { label: over.title ?? "Frontend Engineer" },
+ Company: over.company === null ? null : { label: over.company ?? "Acme" },
+ Location: over.location === null ? null : { label: over.location ?? "Remote" },
+ };
+}
+
+function mockJobs(rows: ReturnType<typeof row>[]) {
+ (prisma.job.findMany as any).mockResolvedValue(rows);
+}
+
+// Simulates the add_job Tier 1 lookup: seed the user's jobs, build the map,
+// then probe it with a candidate's canonical key.
+async function lookupByUrl(rows: ReturnType<typeof row>[], url: string) {
+ mockJobs(rows);
+ const map = await getExistingJobDedupeMap("user-1");
+ return map.get(jobDedupeKey({ url }));
+}
+
+describe("getExistingJobDedupeMap", () => {
+ beforeEach(() => (prisma.job.findMany as any).mockReset());
+
+ it("returns an empty map when the user has no jobs", async () => {
+ mockJobs([]);
+ const map = await getExistingJobDedupeMap("user-1");
+ expect(map.size).toBe(0);
+ });
+
+ it("scopes the query to the given user", async () => {
+ mockJobs([]);
+ await getExistingJobDedupeMap("user-42");
+ expect((prisma.job.findMany as any).mock.calls[0][0].where).toEqual({
+ userId: "user-42",
+ });
+ });
+
+ it("keys URL-bearing jobs by URL and exposes their identity", async () => {
+ mockJobs([row({ id: "j1", jobUrl: "https://ex.com/jobs/1", title: "Engineer", company: "Acme" })]);
+ const hit = await lookupByUrl(
+ [row({ id: "j1", jobUrl: "https://ex.com/jobs/1", title: "Engineer", company: "Acme" })],
+ "https://ex.com/jobs/1",
+ );
+ expect(hit).toEqual({ id: "j1", title: "Engineer", company: "Acme" });
+ });
+
+ it("keeps the first job when two rows collapse to the same key", async () => {
+ mockJobs([
+ row({ id: "first", jobUrl: "https://ex.com/jobs/1" }),
+ row({ id: "second", jobUrl: "https://www.ex.com/jobs/1/?utm_source=x" }),
+ ]);
+ const map = await getExistingJobDedupeMap("user-1");
+ expect(map.size).toBe(1);
+ expect(map.get(jobDedupeKey({ url: "https://ex.com/jobs/1" }))?.id).toBe("first");
+ });
+
+ it("does not crash on null title/company/location", async () => {
+ mockJobs([row({ id: "j1", jobUrl: null, title: null, company: null, location: null })]);
+ const map = await getExistingJobDedupeMap("user-1");
+ expect(map.size).toBe(1);
+ const [ref] = [...map.values()];
+ expect(ref).toEqual({ id: "j1", title: "", company: "" });
+ });
+});
+
+describe("add_job Tier 1 URL matching via the dedupe map", () => {
+ beforeEach(() => (prisma.job.findMany as any).mockReset());
+
+ const existing = [row({ id: "existing", jobUrl: "https://job-boards.greenhouse.io/acme/jobs/1" })];
+
+ it.each([
+ ["exact", "https://job-boards.greenhouse.io/acme/jobs/1"],
+ ["trailing slash", "https://job-boards.greenhouse.io/acme/jobs/1/"],
+ ["www prefix", "https://www.job-boards.greenhouse.io/acme/jobs/1"],
+ ["host case", "https://Job-Boards.Greenhouse.io/acme/jobs/1"],
+ ["tracking param", "https://job-boards.greenhouse.io/acme/jobs/1?gh_src=abc&utm_source=li"],
+ ["fragment", "https://job-boards.greenhouse.io/acme/jobs/1#apply"],
+ ])("flags a duplicate for a %s variant", async (_label, url) => {
+ const hit = await lookupByUrl(existing, url);
+ expect(hit?.id).toBe("existing");
+ });
+
+ it.each([
+ ["different path", "https://job-boards.greenhouse.io/acme/jobs/2"],
+ ["meaningful query differs", "https://job-boards.greenhouse.io/acme/jobs/1?jid=99"],
+ ["different host", "https://boards.lever.co/acme/jobs/1"],
+ ])("does NOT flag %s as a duplicate", async (_label, url) => {
+ const hit = await lookupByUrl(existing, url);
+ expect(hit).toBeUndefined();
+ });
+
+ it("preserves an order-independent query as the same job", async () => {
+ const withQuery = [row({ id: "q", jobUrl: "https://ex.com/j/1?a=1&b=2" })];
+ const hit = await lookupByUrl(withQuery, "https://ex.com/j/1?b=2&a=1");
+ expect(hit?.id).toBe("q");
+ });
+
+ it("falls back to raw string for an unparseable URL (no false positive)", async () => {
+ mockJobs([row({ id: "j1", jobUrl: "https://ex.com/j/1" })]);
+ const hit = await lookupByUrl([row({ id: "j1", jobUrl: "https://ex.com/j/1" })], "not a url");
+ expect(hit).toBeUndefined();
+ });
+});
+
+describe("findExistingJobByUrl", () => {
+ beforeEach(() => (prisma.job.findMany as any).mockReset());
+
+ it("returns null when the user has no jobs", async () => {
+ mockJobs([]);
+ const hit = await findExistingJobByUrl("user-1", "https://ex.com/j/1");
+ expect(hit).toBeNull();
+ });
+
+ it("scopes the query to the given user and excludes null jobUrls", async () => {
+ mockJobs([]);
+ await findExistingJobByUrl("user-42", "https://ex.com/j/1");
+ expect((prisma.job.findMany as any).mock.calls[0][0].where).toEqual({
+ userId: "user-42",
+ jobUrl: { not: null },
+ });
+ });
+
+ it("finds a match and reports its identity", async () => {
+ mockJobs([
+ row({ id: "j1", jobUrl: "https://ex.com/j/1", title: "Engineer", company: "Acme" }),
+ ]);
+
+ const hit = await findExistingJobByUrl("user-1", "https://ex.com/j/1");
+
+ expect(hit).toEqual({ id: "j1", title: "Engineer", company: "Acme" });
+ });
+
+ it("matches a normalized URL variant (www + tracking params)", async () => {
+ mockJobs([row({ id: "j1", jobUrl: "https://ex.com/j/1" })]);
+
+ const hit = await findExistingJobByUrl(
+ "user-1",
+ "https://www.ex.com/j/1?utm_source=li",
+ );
+
+ expect(hit?.id).toBe("j1");
+ });
+
+ it("returns null when no job matches the URL", async () => {
+ mockJobs([row({ id: "j1", jobUrl: "https://ex.com/j/1" })]);
+
+ const hit = await findExistingJobByUrl("user-1", "https://ex.com/j/2");
+
+ expect(hit).toBeNull();
+ });
+});
+
+describe("meta-key fallback (jobs without a URL)", () => {
+ beforeEach(() => (prisma.job.findMany as any).mockReset());
+
+ it("matches a URL-less job by title|company|location signature", async () => {
+ mockJobs([row({ id: "m1", jobUrl: null, title: "Staff Engineer", company: "Acme", location: "Remote" })]);
+ const map = await getExistingJobDedupeMap("user-1");
+ const hit = map.get(
+ jobDedupeKey({ title: "staff engineer", company: "ACME", location: "remote" }),
+ );
+ expect(hit?.id).toBe("m1");
+ });
+
+ it("treats a different location as a different job", async () => {
+ mockJobs([row({ id: "m1", jobUrl: null, title: "Staff Engineer", company: "Acme", location: "Remote" })]);
+ const map = await getExistingJobDedupeMap("user-1");
+ const hit = map.get(
+ jobDedupeKey({ title: "Staff Engineer", company: "Acme", location: "New York" }),
+ );
+ expect(hit).toBeUndefined();
+ });
+
+ it("does not cross-match a URL job against a meta key", async () => {
+ mockJobs([row({ id: "u1", jobUrl: "https://ex.com/j/1", title: "Staff Engineer", company: "Acme", location: "Remote" })]);
+ const map = await getExistingJobDedupeMap("user-1");
+ const hit = map.get(jobDedupeKey({ title: "Staff Engineer", company: "Acme", location: "Remote" }));
+ expect(hit).toBeUndefined();
+ });
+});
diff --git a/__tests__/jobtitle.actions.spec.ts b/__tests__/jobtitle.actions.spec.ts
index 63a8931..8a8896f 100644
--- a/__tests__/jobtitle.actions.spec.ts
+++ b/__tests__/jobtitle.actions.spec.ts
@@ -218,6 +218,34 @@ describe("Job Title Actions", () => {
expect(result).toEqual({ success: false, message: "Upsert failed" });
});
+ it("should canonicalize the value (not just lowercase) for matching", async () => {
+ // Regression guard: value computation was switched from
+ // label.trim().toLowerCase() to canonicalizeEntityValue(), which also
+ // folds diacritics and comma separators. A plain-ASCII label wouldn't
+ // catch a revert to the old behavior.
+ (getCurrentUser as any).mockResolvedValue(mockUser);
+ (prisma.jobTitle.upsert as any).mockImplementation(({ create }: any) =>
+ Promise.resolve(create),
+ );
+
+ const result = await createJobTitle("Ingénieur Logiciel");
+
+ expect(prisma.jobTitle.upsert).toHaveBeenCalledWith({
+ where: {
+ value_createdBy: {
+ value: "ingenieur logiciel",
+ createdBy: mockUser.id,
+ },
+ },
+ update: { label: "Ingénieur Logiciel" },
+ create: {
+ label: "Ingénieur Logiciel",
+ value: "ingenieur logiciel",
+ createdBy: mockUser.id,
+ },
+ });
+ expect(result.value).toBe("ingenieur logiciel");
+ });
});
describe("deleteJobTitleById", () => {
diff --git a/__tests__/lib-utils.spec.ts b/__tests__/lib-utils.spec.ts
index cc027e8..0ef6896 100644
--- a/__tests__/lib-utils.spec.ts
+++ b/__tests__/lib-utils.spec.ts
@@ -5,8 +5,35 @@ import {
calculatePercentageDifference,
getLast7Days,
getTimestampedFileName,
+ capitalize,
} from "@/lib/utils";
+describe("capitalize", () => {
+ it("uppercases the first character", () => {
+ expect(capitalize("greenhouse")).toBe("Greenhouse");
+ });
+
+ it("leaves an already-capitalized string unchanged", () => {
+ expect(capitalize("Lever")).toBe("Lever");
+ });
+
+ it("only capitalizes the first word, not each word", () => {
+ expect(capitalize("job board api")).toBe("Job board api");
+ });
+
+ it("handles a single character", () => {
+ expect(capitalize("x")).toBe("X");
+ });
+
+ it("returns an empty string unchanged", () => {
+ expect(capitalize("")).toBe("");
+ });
+
+ it("leaves a non-letter first character unchanged", () => {
+ expect(capitalize("1source")).toBe("1source");
+ });
+});
+
describe("formatUrl", () => {
it("prepends https:// to bare domain", () => {
expect(formatUrl("example.com")).toBe("https://example.com");
diff --git a/__tests__/resolveCompany.spec.ts b/__tests__/resolveCompany.spec.ts
new file mode 100644
index 0000000..8bd8812
--- /dev/null
+++ b/__tests__/resolveCompany.spec.ts
@@ -0,0 +1,213 @@
+import {
+ resolveCompany,
+ resolveJobTitle,
+ resolveLocation,
+ resolveJobSource,
+} from "@/lib/jobs/resolve";
+import { PrismaClient } from "@prisma/client";
+
+const prisma = new PrismaClient();
+
+vi.mock("@prisma/client", () => {
+ const mPrismaClient = {
+ company: { findUnique: vi.fn(), create: vi.fn() },
+ jobTitle: { findUnique: vi.fn(), create: vi.fn() },
+ location: { findUnique: vi.fn(), create: vi.fn() },
+ jobSource: { findUnique: vi.fn(), create: vi.fn() },
+ };
+ return { PrismaClient: vi.fn(function () { return mPrismaClient; }) };
+});
+
+describe("resolveCompany", () => {
+ const userId = "user-1";
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ (prisma.company.findUnique as any).mockResolvedValue(null);
+ (prisma.company.create as any).mockImplementation(({ data }: any) =>
+ Promise.resolve({ id: `company-${data.value}`, label: data.label }),
+ );
+ });
+
+ it("creates a new company keyed by its legal-suffix-stripped value", async () => {
+ const result = await resolveCompany("Acme Inc.", userId);
+
+ expect(prisma.company.findUnique).toHaveBeenCalledWith({
+ where: { value_createdBy: { value: "acme", createdBy: userId } },
+ });
+ expect(result).toEqual({ id: "company-acme", label: "Acme Inc.", created: true });
+ });
+
+ it("reuses an existing company instead of creating a duplicate", async () => {
+ (prisma.company.findUnique as any).mockResolvedValue({
+ id: "existing-company",
+ label: "Acme",
+ });
+
+ const result = await resolveCompany("Acme Inc.", userId);
+
+ expect(prisma.company.create).not.toHaveBeenCalled();
+ expect(result).toEqual({ id: "existing-company", label: "Acme", created: false });
+ });
+
+ it("recovers from a concurrent-create race instead of throwing", async () => {
+ (prisma.company.findUnique as any)
+ .mockResolvedValueOnce(null) // this call's own findUnique: nothing yet
+ .mockResolvedValueOnce({ id: "winner-company", label: "Acme" }); // re-fetch after losing the race
+ const p2002 = Object.assign(new Error("Unique constraint failed"), {
+ code: "P2002",
+ });
+ (prisma.company.create as any).mockRejectedValue(p2002);
+
+ const result = await resolveCompany("Acme", userId);
+
+ expect(result).toEqual({ id: "winner-company", label: "Acme", created: false });
+ });
+
+ it("rethrows non-P2002 errors from create", async () => {
+ (prisma.company.create as any).mockRejectedValue(new Error("connection lost"));
+
+ await expect(resolveCompany("Acme", userId)).rejects.toThrow("connection lost");
+ });
+});
+
+describe("resolveJobTitle", () => {
+ const userId = "user-1";
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ (prisma.jobTitle.findUnique as any).mockResolvedValue(null);
+ (prisma.jobTitle.create as any).mockImplementation(({ data }: any) =>
+ Promise.resolve({ id: `title-${data.value}`, label: data.label }),
+ );
+ });
+
+ it("does not strip a trailing word that looks like a legal suffix", async () => {
+ // Job titles don't get stripLegalSuffix, so "... Co" style titles keep
+ // their full canonical value (only Company resolution strips suffixes).
+ await resolveJobTitle("Head of Co", userId);
+
+ expect(prisma.jobTitle.findUnique).toHaveBeenCalledWith({
+ where: { value_createdBy: { value: "head of co", createdBy: userId } },
+ });
+ });
+});
+
+describe("resolveLocation", () => {
+ const userId = "user-1";
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ (prisma.location.findUnique as any).mockResolvedValue(null);
+ (prisma.location.create as any).mockImplementation(({ data }: any) =>
+ Promise.resolve({ id: `location-${data.value}`, label: data.label }),
+ );
+ });
+
+ it("creates a new location keyed by its canonical value", async () => {
+ const result = await resolveLocation("San Francisco, CA", userId);
+
+ expect(prisma.location.findUnique).toHaveBeenCalledWith({
+ where: { value_createdBy: { value: "san francisco ca", createdBy: userId } },
+ });
+ expect(result).toEqual({
+ id: "location-san francisco ca",
+ label: "San Francisco, CA",
+ created: true,
+ });
+ });
+
+ it("reuses an existing location instead of creating a duplicate", async () => {
+ (prisma.location.findUnique as any).mockResolvedValue({
+ id: "existing-location",
+ label: "Remote",
+ });
+
+ const result = await resolveLocation("Remote", userId);
+
+ expect(prisma.location.create).not.toHaveBeenCalled();
+ expect(result).toEqual({ id: "existing-location", label: "Remote", created: false });
+ });
+});
+
+describe("resolveJobSource", () => {
+ const userId = "user-1";
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ (prisma.jobSource.findUnique as any).mockResolvedValue(null);
+ (prisma.jobSource.create as any).mockImplementation(({ data }: any) =>
+ Promise.resolve({ id: `source-${data.value}`, label: data.label }),
+ );
+ });
+
+ it("creates a new job source keyed by its canonical value", async () => {
+ const result = await resolveJobSource("Greenhouse", userId);
+
+ expect(prisma.jobSource.findUnique).toHaveBeenCalledWith({
+ where: { value_createdBy: { value: "greenhouse", createdBy: userId } },
+ });
+ expect(result).toEqual({ id: "source-greenhouse", label: "Greenhouse", created: true });
+ });
+
+ it("reuses an existing job source instead of creating a duplicate", async () => {
+ (prisma.jobSource.findUnique as any).mockResolvedValue({
+ id: "existing-source",
+ label: "Lever",
+ });
+
+ const result = await resolveJobSource("Lever", userId);
+
+ expect(prisma.jobSource.create).not.toHaveBeenCalled();
+ expect(result).toEqual({ id: "existing-source", label: "Lever", created: false });
+ });
+});
+
+describe("resolveEntity — empty label guard", () => {
+ const userId = "user-1";
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ // Regression guard: consolidating Company/JobTitle/Location/JobSource
+ // resolution into the shared resolveEntity() dropped the `if (!value)
+ // throw` guard the standalone createLocation/createJobSource/createJobTitle
+ // actions used to have — without it, a whitespace-only label silently
+ // created a blank-labeled record instead of failing loudly.
+ it("throws instead of creating a blank company for a whitespace-only label", async () => {
+ await expect(resolveCompany(" ", userId)).rejects.toThrow(
+ "A non-empty label is required",
+ );
+ expect(prisma.company.findUnique).not.toHaveBeenCalled();
+ expect(prisma.company.create).not.toHaveBeenCalled();
+ });
+
+ it("throws instead of creating a blank job title for an empty label", async () => {
+ await expect(resolveJobTitle("", userId)).rejects.toThrow(
+ "A non-empty label is required",
+ );
+ expect(prisma.jobTitle.create).not.toHaveBeenCalled();
+ });
+
+ it("throws instead of creating a blank location for a whitespace-only label", async () => {
+ await expect(resolveLocation(" ", userId)).rejects.toThrow(
+ "A non-empty label is required",
+ );
+ expect(prisma.location.create).not.toHaveBeenCalled();
+ });
+
+ it("throws instead of creating a blank job source for a whitespace-only label", async () => {
+ await expect(resolveJobSource(" ", userId)).rejects.toThrow(
+ "A non-empty label is required",
+ );
+ expect(prisma.jobSource.create).not.toHaveBeenCalled();
+ });
+
+ it("throws for a label that canonicalizes to empty (e.g. a bare comma)", async () => {
+ await expect(resolveLocation(",", userId)).rejects.toThrow(
+ "A non-empty label is required",
+ );
+ expect(prisma.location.create).not.toHaveBeenCalled();
+ });
+});
diff --git a/__tests__/resolveJobStatus.spec.ts b/__tests__/resolveJobStatus.spec.ts
new file mode 100644
index 0000000..7fb4aea
--- /dev/null
+++ b/__tests__/resolveJobStatus.spec.ts
@@ -0,0 +1,52 @@
+import { resolveJobStatus } from "@/lib/jobs/resolve";
+import { PrismaClient } from "@prisma/client";
+
+const prisma = new PrismaClient();
+
+vi.mock("@prisma/client", () => {
+ const mPrismaClient = {
+ jobStatus: { findUnique: vi.fn(), findMany: vi.fn() },
+ };
+ return { PrismaClient: vi.fn(function () { return mPrismaClient; }) };
+});
+
+describe("resolveJobStatus", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it("defaults to draft when no value is given", async () => {
+ (prisma.jobStatus.findUnique as any).mockResolvedValue({ id: "status-draft" });
+
+ const result = await resolveJobStatus(undefined);
+
+ expect(prisma.jobStatus.findUnique).toHaveBeenCalledWith({
+ where: { value: "draft" },
+ });
+ expect(result).toBe("status-draft");
+ });
+
+ it("resolves a known status case-insensitively", async () => {
+ (prisma.jobStatus.findUnique as any).mockResolvedValue({ id: "status-applied" });
+
+ const result = await resolveJobStatus("APPLIED");
+
+ expect(prisma.jobStatus.findUnique).toHaveBeenCalledWith({
+ where: { value: "applied" },
+ });
+ expect(result).toBe("status-applied");
+ });
+
+ it("throws listing valid values when the status is unknown", async () => {
+ (prisma.jobStatus.findUnique as any).mockResolvedValue(null);
+ (prisma.jobStatus.findMany as any).mockResolvedValue([
+ { value: "draft" },
+ { value: "applied" },
+ { value: "interviewing" },
+ ]);
+
+ await expect(resolveJobStatus("bogus")).rejects.toThrow(
+ 'Invalid status "bogus". Valid values: draft, applied, interviewing',
+ );
+ });
+});
diff --git a/__tests__/resolveJobType.spec.ts b/__tests__/resolveJobType.spec.ts
new file mode 100644
index 0000000..7647919
--- /dev/null
+++ b/__tests__/resolveJobType.spec.ts
@@ -0,0 +1,26 @@
+import { resolveJobType } from "@/lib/jobs/resolve";
+
+describe("resolveJobType", () => {
+ it("resolves a valid key", () => {
+ expect(resolveJobType("FT")).toBe("FT");
+ });
+
+ it("resolves a valid label", () => {
+ expect(resolveJobType("Contract")).toBe("C");
+ });
+
+ it("resolves case-insensitively", () => {
+ expect(resolveJobType("part-time")).toBe("PT");
+ });
+
+ it("defaults to the first entry for empty input", () => {
+ expect(resolveJobType(undefined)).toBe("FT");
+ expect(resolveJobType("")).toBe("FT");
+ });
+
+ it("throws a helpful message for an invalid value", () => {
+ expect(() => resolveJobType("Internship")).toThrow(
+ /Invalid jobType "Internship"\. Valid values: Full-time, Part-time, Contract/,
+ );
+ });
+});
diff --git a/__tests__/resolveTags.spec.ts b/__tests__/resolveTags.spec.ts
index 8dcbeda..657f815 100644
--- a/__tests__/resolveTags.spec.ts
+++ b/__tests__/resolveTags.spec.ts
@@ -85,4 +85,42 @@ describe("resolveTags", () => {
expect(result.dropped).toEqual([]);
expect(prisma.tag.findUnique).not.toHaveBeenCalled();
});
+
+ it("dedupes diacritic variants, matching entity resolution's canonicalization", async () => {
+ const result = await resolveTags(["São Paulo", "Sao Paulo"], userId, 10);
+
+ expect(prisma.tag.findUnique).toHaveBeenCalledTimes(1);
+ expect(result.resolved).toHaveLength(1);
+ });
+
+ it("recovers from a concurrent-create race instead of throwing", async () => {
+ (prisma.tag.findUnique as any)
+ .mockResolvedValueOnce(null)
+ .mockResolvedValueOnce({
+ id: "winner-tag",
+ label: "React",
+ value: "react",
+ createdBy: userId,
+ });
+ const p2002 = Object.assign(new Error("Unique constraint failed"), {
+ code: "P2002",
+ });
+ (prisma.tag.create as any).mockRejectedValue(p2002);
+
+ const result = await resolveTags(["React"], userId, 10);
+
+ expect(result.resolved[0]).toEqual({
+ id: "winner-tag",
+ label: "React",
+ value: "react",
+ createdBy: userId,
+ created: false,
+ });
+ });
+
+ it("rethrows non-P2002 errors from create", async () => {
+ (prisma.tag.create as any).mockRejectedValue(new Error("connection lost"));
+
+ await expect(resolveTags(["React"], userId, 10)).rejects.toThrow("connection lost");
+ });
});
diff --git a/__tests__/resumeImport.actions.spec.ts b/__tests__/resumeImport.actions.spec.ts
index fba7260..ce77dd5 100644
--- a/__tests__/resumeImport.actions.spec.ts
+++ b/__tests__/resumeImport.actions.spec.ts
@@ -1,7 +1,5 @@
import { resolveImportCard } from "@/actions/resumeImport.actions";
import { getCurrentUser } from "@/utils/user.utils";
-import { createJobTitle } from "@/actions/jobtitle.actions";
-import { createLocation } from "@/actions/job.actions";
import { PrismaClient } from "@prisma/client";
import { APP_CONSTANTS } from "@/lib/constants";
@@ -12,7 +10,9 @@ vi.mock("@prisma/client", () => {
resume: { findUnique: vi.fn(), update: vi.fn() },
resumeSection: { create: vi.fn(), findFirst: vi.fn(), update: vi.fn() },
summary: { update: vi.fn() },
- company: { findFirst: vi.fn(), create: vi.fn() },
+ company: { findUnique: vi.fn(), create: vi.fn() },
+ jobTitle: { findUnique: vi.fn(), create: vi.fn() },
+ location: { findUnique: vi.fn(), create: vi.fn() },
skill: { createMany: vi.fn() },
tag: { findUnique: vi.fn(), create: vi.fn() },
};
@@ -24,8 +24,6 @@ vi.mock("@prisma/client", () => {
});
vi.mock("@/utils/user.utils", () => ({ getCurrentUser: vi.fn() }));
-vi.mock("@/actions/jobtitle.actions", () => ({ createJobTitle: vi.fn() }));
-vi.mock("@/actions/job.actions", () => ({ createLocation: vi.fn() }));
describe("resolveImportCard — skills", () => {
const mockUser = { id: "user-1" };
@@ -340,12 +338,14 @@ describe("resolveImportCard — experience", () => {
vi.resetAllMocks();
(getCurrentUser as any).mockResolvedValue(mockUser);
(prisma.resume.findUnique as any).mockResolvedValue({ id: "resume-1" });
- (prisma.company.findFirst as any).mockResolvedValue(null);
+ (prisma.company.findUnique as any).mockResolvedValue(null);
(prisma.company.create as any).mockImplementation(({ data }: any) =>
Promise.resolve({ id: "company-1", ...data }),
);
- (createJobTitle as any).mockResolvedValue({ id: "title-1" });
- (createLocation as any).mockResolvedValue({ data: { id: "location-1" }, success: true });
+ (prisma.jobTitle.findUnique as any).mockResolvedValue(null);
+ (prisma.jobTitle.create as any).mockResolvedValue({ id: "title-1", label: "Engineer" });
+ (prisma.location.findUnique as any).mockResolvedValue(null);
+ (prisma.location.create as any).mockResolvedValue({ id: "location-1", label: "Remote" });
(prisma.resumeSection.findFirst as any).mockResolvedValue(null);
(prisma.resumeSection.create as any).mockResolvedValue({ id: "section-1" });
(prisma.resumeSection.update as any).mockResolvedValue({});
@@ -377,6 +377,29 @@ describe("resolveImportCard — experience", () => {
);
});
+ it("matches an existing company by its canonical (legal-suffix-stripped) value", async () => {
+ // Regression guard for the resume-import consolidation: company
+ // resolution now goes through resolveCompany, the same shared resolver
+ // add_job/automation use, so "Acme Inc." matches an existing "Acme"
+ // instead of creating a duplicate Company row.
+ (prisma.company.findUnique as any).mockResolvedValue({
+ id: "existing-company",
+ label: "Acme",
+ });
+
+ await resolveImportCard("resume-1", {
+ type: "experience",
+ data: { company: "Acme Inc.", jobTitle: "Engineer" },
+ } as any);
+
+ expect(prisma.company.findUnique).toHaveBeenCalledWith({
+ where: { value_createdBy: { value: "acme", createdBy: mockUser.id } },
+ });
+ expect(prisma.company.create).not.toHaveBeenCalled();
+ const call = (prisma.resumeSection.update as any).mock.calls[0][0];
+ expect(call.data.workExperiences.create.companyId).toBe("existing-company");
+ });
+
it("reuses an existing EXPERIENCE section instead of creating a new one", async () => {
(prisma.resumeSection.findFirst as any).mockResolvedValue({ id: "existing-section" });
@@ -392,9 +415,9 @@ describe("resolveImportCard — experience", () => {
});
it("falls back to a placeholder location when none is provided", async () => {
- (createLocation as any).mockResolvedValue({
- data: { id: "fallback-location" },
- success: true,
+ (prisma.location.create as any).mockResolvedValue({
+ id: "fallback-location",
+ label: "Not specified",
});
await resolveImportCard("resume-1", {
@@ -402,7 +425,30 @@ describe("resolveImportCard — experience", () => {
data: { company: "Acme", jobTitle: "Engineer" },
} as any);
- expect(createLocation).toHaveBeenLastCalledWith("Not specified");
+ expect(prisma.location.findUnique).toHaveBeenLastCalledWith({
+ where: { value_createdBy: { value: "not specified", createdBy: mockUser.id } },
+ });
+ const call = (prisma.resumeSection.update as any).mock.calls[0][0];
+ expect(call.data.workExperiences.create.locationId).toBe("fallback-location");
+ });
+
+ it("falls back to the placeholder location when the location is whitespace-only", async () => {
+ // Regression guard: a whitespace-only location is truthy, so the old
+ // `d.location || "Not specified"` fallback never fired and resolveEntity
+ // (having no empty-value guard) would silently create a blank Location.
+ (prisma.location.create as any).mockResolvedValue({
+ id: "fallback-location",
+ label: "Not specified",
+ });
+
+ await resolveImportCard("resume-1", {
+ type: "experience",
+ data: { company: "Acme", jobTitle: "Engineer", location: " " },
+ } as any);
+
+ expect(prisma.location.findUnique).toHaveBeenLastCalledWith({
+ where: { value_createdBy: { value: "not specified", createdBy: mockUser.id } },
+ });
const call = (prisma.resumeSection.update as any).mock.calls[0][0];
expect(call.data.workExperiences.create.locationId).toBe("fallback-location");
});
@@ -426,7 +472,7 @@ describe("resolveImportCard — experience", () => {
} as any);
expect(result.success).toBe(false);
- expect(createJobTitle).not.toHaveBeenCalled();
+ expect(prisma.jobTitle.findUnique).not.toHaveBeenCalled();
});
});
@@ -437,7 +483,8 @@ describe("resolveImportCard — education", () => {
vi.resetAllMocks();
(getCurrentUser as any).mockResolvedValue(mockUser);
(prisma.resume.findUnique as any).mockResolvedValue({ id: "resume-1" });
- (createLocation as any).mockResolvedValue({ data: { id: "location-1" }, success: true });
+ (prisma.location.findUnique as any).mockResolvedValue(null);
+ (prisma.location.create as any).mockResolvedValue({ id: "location-1", label: "Somewhere" });
(prisma.resumeSection.findFirst as any).mockResolvedValue(null);
(prisma.resumeSection.create as any).mockResolvedValue({ id: "section-1" });
(prisma.resumeSection.update as any).mockResolvedValue({});
@@ -472,10 +519,10 @@ describe("resolveImportCard — education", () => {
);
});
- it("falls back to a placeholder location when none is provided or resolvable", async () => {
- (createLocation as any).mockResolvedValue({
- data: { id: "fallback-location" },
- success: true,
+ it("falls back to a placeholder location when none is provided", async () => {
+ (prisma.location.create as any).mockResolvedValue({
+ id: "fallback-location",
+ label: "Not specified",
});
await resolveImportCard("resume-1", {
@@ -483,7 +530,27 @@ describe("resolveImportCard — education", () => {
data: { institution: "State University" },
} as any);
- expect(createLocation).toHaveBeenLastCalledWith("Not specified");
+ expect(prisma.location.findUnique).toHaveBeenLastCalledWith({
+ where: { value_createdBy: { value: "not specified", createdBy: mockUser.id } },
+ });
+ const call = (prisma.resumeSection.update as any).mock.calls[0][0];
+ expect(call.data.educations.create.locationId).toBe("fallback-location");
+ });
+
+ it("falls back to the placeholder location when the location is whitespace-only", async () => {
+ (prisma.location.create as any).mockResolvedValue({
+ id: "fallback-location",
+ label: "Not specified",
+ });
+
+ await resolveImportCard("resume-1", {
+ type: "education",
+ data: { institution: "State University", location: " " },
+ } as any);
+
+ expect(prisma.location.findUnique).toHaveBeenLastCalledWith({
+ where: { value_createdBy: { value: "not specified", createdBy: mockUser.id } },
+ });
const call = (prisma.resumeSection.update as any).mock.calls[0][0];
expect(call.data.educations.create.locationId).toBe("fallback-location");
});
diff --git a/__tests__/scraper-mapper.spec.ts b/__tests__/scraper-mapper.spec.ts
index bee2255..9776535 100644
--- a/__tests__/scraper-mapper.spec.ts
+++ b/__tests__/scraper-mapper.spec.ts
@@ -1,25 +1,15 @@
import { mapScrapedJobToJobRecord, normalizeWorkplaceType } from "@/lib/scraper/mapper";
import db from "@/lib/db";
+import {
+ resolveCompany,
+ resolveJobTitle,
+ resolveLocation,
+ resolveJobSource,
+} from "@/lib/jobs/resolve";
import type { ScrapedJobData } from "@/models/automation.model";
vi.mock("@/lib/db", () => {
const mockPrisma = {
- jobTitle: {
- findFirst: vi.fn(),
- create: vi.fn(),
- },
- location: {
- findFirst: vi.fn(),
- create: vi.fn(),
- },
- company: {
- findFirst: vi.fn(),
- create: vi.fn(),
- },
- jobSource: {
- findFirst: vi.fn(),
- create: vi.fn(),
- },
jobStatus: {
findFirst: vi.fn(),
create: vi.fn(),
@@ -28,6 +18,16 @@ vi.mock("@/lib/db", () => {
return { default: mockPrisma };
});
+// The mapper now delegates entity resolution to the shared resolve-or-create
+// helpers (same ones the add_job path uses); mock them so this spec asserts
+// mapping/precedence, not resolution internals (covered by resolve/canonicalize).
+vi.mock("@/lib/jobs/resolve", () => ({
+ resolveCompany: vi.fn(),
+ resolveJobTitle: vi.fn(),
+ resolveLocation: vi.fn(),
+ resolveJobSource: vi.fn(),
+}));
+
const mockedDb = vi.mocked(db, true);
const scrapedJob: ScrapedJobData = {
@@ -49,13 +49,10 @@ const baseInput = {
beforeEach(() => {
vi.clearAllMocks();
- mockedDb.location.findFirst.mockResolvedValue(null);
- mockedDb.location.create.mockResolvedValue({ id: "location-1" } as never);
- mockedDb.company.findFirst.mockResolvedValue(null);
- mockedDb.company.create.mockResolvedValue({ id: "company-1" } as never);
- mockedDb.jobSource.findFirst.mockResolvedValue({
- id: "source-1",
- } as never);
+ (resolveJobTitle as any).mockResolvedValue({ id: "title-1", label: "", created: true });
+ (resolveLocation as any).mockResolvedValue({ id: "location-1", label: "", created: true });
+ (resolveCompany as any).mockResolvedValue({ id: "company-1", label: "", created: true });
+ (resolveJobSource as any).mockResolvedValue({ id: "source-1", label: "", created: true });
mockedDb.jobStatus.findFirst.mockResolvedValue({ id: "status-1" } as never);
});
@@ -74,10 +71,6 @@ describe("normalizeWorkplaceType", () => {
});
describe("mapScrapedJobToJobRecord - workplaceType precedence", () => {
- beforeEach(() => {
- mockedDb.jobTitle.findFirst.mockResolvedValue({ id: "title-1" } as never);
- });
-
it("prefers an explicit workplaceType (Lever) over the isRemote boolean", async () => {
const result = await mapScrapedJobToJobRecord({
...baseInput,
@@ -109,75 +102,37 @@ describe("mapScrapedJobToJobRecord - workplaceType precedence", () => {
});
});
-describe("mapScrapedJobToJobRecord - job title matching", () => {
- it("does not reuse a title that only shares one unrelated keyword", async () => {
- mockedDb.jobTitle.findFirst.mockResolvedValue(null);
- mockedDb.jobTitle.create.mockResolvedValue({
- id: "new-title-id",
- } as never);
-
+describe("mapScrapedJobToJobRecord - entity delegation", () => {
+ it("maps resolver ids onto the job record", async () => {
const result = await mapScrapedJobToJobRecord(baseInput);
- expect(result.jobTitleId).toBe("new-title-id");
- expect(mockedDb.jobTitle.findFirst).toHaveBeenLastCalledWith({
- where: {
- createdBy: "user-1",
- AND: [
- { value: { contains: "open" } },
- { value: { contains: "source" } },
- { value: { contains: "developer" } },
- ],
- },
- });
- expect(mockedDb.jobTitle.create).toHaveBeenCalledWith({
- data: {
- label: "Open Source Developer",
- value: "open-source-developer",
- createdBy: "user-1",
- },
- });
+ expect(result.jobTitleId).toBe("title-1");
+ expect(result.locationId).toBe("location-1");
+ expect(result.companyId).toBe("company-1");
+ expect(result.jobSourceId).toBe("source-1");
+ expect(result.statusId).toBe("status-1");
});
- it("reuses an existing title when all keywords match", async () => {
- mockedDb.jobTitle.findFirst
- .mockResolvedValueOnce(null)
- .mockResolvedValueOnce({ id: "existing-title-id" } as never);
+ it("resolves title/company by their raw labels for the user", async () => {
+ await mapScrapedJobToJobRecord(baseInput);
- const result = await mapScrapedJobToJobRecord(baseInput);
-
- expect(result.jobTitleId).toBe("existing-title-id");
- expect(mockedDb.jobTitle.create).not.toHaveBeenCalled();
+ expect(resolveJobTitle).toHaveBeenCalledWith("Open Source Developer", "user-1");
+ expect(resolveCompany).toHaveBeenCalledWith("Figma", "user-1");
});
- it("reuses an exact normalized title match without fuzzy matching", async () => {
- mockedDb.jobTitle.findFirst.mockResolvedValueOnce({
- id: "exact-match-id",
- } as never);
-
- const result = await mapScrapedJobToJobRecord(baseInput);
+ it("title-cases the source board into the JobSource label", async () => {
+ await mapScrapedJobToJobRecord(baseInput);
- expect(result.jobTitleId).toBe("exact-match-id");
- expect(mockedDb.jobTitle.findFirst).toHaveBeenCalledTimes(1);
- expect(mockedDb.jobTitle.create).not.toHaveBeenCalled();
+ expect(resolveJobSource).toHaveBeenCalledWith("Greenhouse", "user-1");
});
-});
-
-describe("mapScrapedJobToJobRecord - company matching", () => {
- it("does not reuse a company that only shares one unrelated keyword", async () => {
- mockedDb.jobTitle.findFirst.mockResolvedValue({ id: "title-1" } as never);
- mockedDb.company.findFirst.mockResolvedValue(null);
- mockedDb.company.create.mockResolvedValue({
- id: "new-company-id",
- } as never);
-
- const result = await mapScrapedJobToJobRecord(baseInput);
- expect(result.companyId).toBe("new-company-id");
- expect(mockedDb.company.findFirst).toHaveBeenLastCalledWith({
- where: {
- createdBy: "user-1",
- AND: [{ label: { contains: "figma" } }],
- },
+ it("skips location resolution and stores null when location is empty", async () => {
+ const result = await mapScrapedJobToJobRecord({
+ ...baseInput,
+ scrapedJob: { ...scrapedJob, location: "" },
});
+
+ expect(resolveLocation).not.toHaveBeenCalled();
+ expect(result.locationId).toBeNull();
});
});
diff --git a/__tests__/scraper-utils.spec.ts b/__tests__/scraper-utils.spec.ts
index 82a7f7e..619cf0f 100644
--- a/__tests__/scraper-utils.spec.ts
+++ b/__tests__/scraper-utils.spec.ts
@@ -1,8 +1,5 @@
import {
normalizeJobUrl,
- normalizeForSearch,
- extractKeywords,
- extractCityName,
jobDedupeKey,
dedupeJobs,
} from "@/lib/scraper/utils";
@@ -122,6 +119,18 @@ describe("jobDedupeKey", () => {
const b = jobDedupeKey({ title: "Designer", company: "Acme" });
expect(a).not.toBe(b);
});
+
+ it("folds a company legal suffix, matching resolveCompany's canonical value", () => {
+ const a = jobDedupeKey({ title: "Engineer", company: "Acme Inc." });
+ const b = jobDedupeKey({ title: "Engineer", company: "Acme" });
+ expect(a).toBe(b);
+ });
+
+ it("folds diacritics in the meta key, matching entity resolution", () => {
+ const a = jobDedupeKey({ title: "Engineer", company: "Acme", location: "São Paulo" });
+ const b = jobDedupeKey({ title: "Engineer", company: "Acme", location: "Sao Paulo" });
+ expect(a).toBe(b);
+ });
});
describe("dedupeJobs", () => {
@@ -158,156 +167,3 @@ describe("dedupeJobs", () => {
expect(dedupeJobs(jobs, new Set())).toHaveLength(2);
});
});
-
-describe("normalizeForSearch", () => {
- it("converts to lowercase", () => {
- expect(normalizeForSearch("Software Engineer")).toBe("software-engineer");
- });
-
- it("trims whitespace", () => {
- expect(normalizeForSearch(" hello ")).toBe("hello");
- });
-
- it("replaces spaces with dashes", () => {
- expect(normalizeForSearch("full stack developer")).toBe(
- "full-stack-developer"
- );
- });
-
- it("removes special characters", () => {
- expect(normalizeForSearch("C++ Developer (Senior)")).toBe(
- "c-developer-senior"
- );
- });
-
- it("collapses multiple spaces into single dash", () => {
- expect(normalizeForSearch("word1 word2")).toBe("word1-word2");
- });
-
- it("handles empty string", () => {
- expect(normalizeForSearch("")).toBe("");
- });
-
- it("preserves numbers", () => {
- expect(normalizeForSearch("Level 3 Support")).toBe("level-3-support");
- });
-
- it("removes punctuation", () => {
- expect(normalizeForSearch("Sr. Engineer, Backend")).toBe(
- "sr-engineer-backend"
- );
- });
-});
-
-describe("extractKeywords", () => {
- it("extracts words longer than 2 characters", () => {
- const result = extractKeywords("Software Developer");
- expect(result).toEqual(["software", "developer"]);
- });
-
- it("filters out words with 2 or fewer characters", () => {
- const result = extractKeywords("I am a dev");
- expect(result).toEqual(["dev"]);
- });
-
- it("filters out stop words", () => {
- const result = extractKeywords("Senior Software Engineer at the Company");
- expect(result).toEqual(["software", "engineer"]);
- });
-
- it("splits on spaces", () => {
- expect(extractKeywords("react node python")).toEqual([
- "react",
- "node",
- "python",
- ]);
- });
-
- it("splits on dashes", () => {
- expect(extractKeywords("full-stack-developer")).toEqual([
- "full",
- "stack",
- "developer",
- ]);
- });
-
- it("splits on underscores", () => {
- expect(extractKeywords("data_science_role")).toEqual([
- "data",
- "science",
- "role",
- ]);
- });
-
- it("splits on commas", () => {
- expect(extractKeywords("react,node,python")).toEqual([
- "react",
- "node",
- "python",
- ]);
- });
-
- it("splits on dots", () => {
- expect(extractKeywords("google.amazon.meta")).toEqual([
- "google",
- "amazon",
- "meta",
- ]);
- });
-
- it("converts to lowercase", () => {
- expect(extractKeywords("React Node")).toEqual(["react", "node"]);
- });
-
- it("filters all stop words", () => {
- const stopWords = [
- "senior",
- "jr",
- "junior",
- "sr",
- "inc",
- "llc",
- "ltd",
- "corp",
- "corporation",
- "the",
- "a",
- "an",
- "co",
- "company",
- ];
- stopWords.forEach((word) => {
- expect(extractKeywords(word)).toEqual([]);
- });
- });
-
- it("returns empty array for empty string", () => {
- expect(extractKeywords("")).toEqual([]);
- });
-});
-
-describe("extractCityName", () => {
- it("extracts city from city, state format", () => {
- expect(extractCityName("Calgary, AB")).toBe("calgary");
- });
-
- it("extracts city from city, state, country format", () => {
- expect(extractCityName("New York, NY, USA")).toBe("new york");
- });
-
- it("returns lowercase city name", () => {
- expect(extractCityName("TORONTO, ON")).toBe("toronto");
- });
-
- it("trims whitespace from city name", () => {
- expect(extractCityName(" Vancouver , BC")).toBe("vancouver");
- });
-
- it("returns the full string (lowercase) when no comma", () => {
- expect(extractCityName("Remote")).toBe("remote");
- });
-
- it("handles empty string", () => {
- expect(extractCityName("")).toBe("");
- });
-});
diff --git a/src/actions/company.actions.ts b/src/actions/company.actions.ts
index e582f8e..fd4bc03 100644
--- a/src/actions/company.actions.ts
+++ b/src/actions/company.actions.ts
@@ -1,6 +1,7 @@
"use server";
import prisma from "@/lib/db";
import { handleError } from "@/lib/utils";
+import { canonicalizeEntityValue } from "@/lib/jobs/canonicalize";
import { AddCompanyFormSchema } from "@/models/addCompanyForm.schema";
import { getCurrentUser } from "@/utils/user.utils";
import { APP_CONSTANTS } from "@/lib/constants";
@@ -161,7 +162,7 @@ export const addCompany = async (
);
}
- const value = company.trim().toLowerCase();
+ const value = canonicalizeEntityValue(company.trim(), { stripLegalSuffix: true });
const companyExists = await prisma.company.findFirst({
where: {
@@ -213,7 +214,7 @@ export const updateCompany = async (
);
}
- const value = company.trim().toLowerCase();
+ const value = canonicalizeEntityValue(company.trim(), { stripLegalSuffix: true });
const companyExists = await prisma.company.findFirst({
where: {
diff --git a/src/actions/job.actions.ts b/src/actions/job.actions.ts
index f5c89e7..f9d9c4d 100644
--- a/src/actions/job.actions.ts
+++ b/src/actions/job.actions.ts
@@ -1,6 +1,7 @@
"use server";
import prisma from "@/lib/db";
import { handleError } from "@/lib/utils";
+import { canonicalizeEntityValue } from "@/lib/jobs/canonicalize";
import { AddJobFormSchema } from "@/models/addJobForm.schema";
import { JOB_TYPES, JobStatus } from "@/models/job.model";
import { getCurrentUser } from "@/utils/user.utils";
@@ -262,7 +263,7 @@ export const createLocation = async (
throw new Error("Not authenticated");
}
- const value = label.trim().toLowerCase();
+ const value = canonicalizeEntityValue(label.trim());
if (!value) {
throw new Error("Please provide location name");
@@ -296,7 +297,7 @@ export const createJobSource = async (
throw new Error("Not authenticated");
}
- const value = label.trim().toLowerCase();
+ const value = canonicalizeEntityValue(label.trim());
if (!value) {
throw new Error("Please provide job source name");
diff --git a/src/actions/jobtitle.actions.ts b/src/actions/jobtitle.actions.ts
index 913f692..53386d4 100644
--- a/src/actions/jobtitle.actions.ts
+++ b/src/actions/jobtitle.actions.ts
@@ -3,6 +3,7 @@ import prisma from "@/lib/db";
import { handleError } from "@/lib/utils";
import { getCurrentUser } from "@/utils/user.utils";
import { APP_CONSTANTS } from "@/lib/constants";
+import { canonicalizeEntityValue } from "@/lib/jobs/canonicalize";
export const getAllJobTitles = async (): Promise<any | undefined> => {
try {
@@ -115,7 +116,7 @@ export const createJobTitle = async (
throw new Error("Not authenticated");
}
- const value = label.trim().toLowerCase();
+ const value = canonicalizeEntityValue(label.trim());
const upsertedTitle = await prisma.jobTitle.upsert({
where: { value_createdBy: { value, createdBy: user.id } },
diff --git a/src/actions/resumeImport.actions.ts b/src/actions/resumeImport.actions.ts
index 5cda988..e532c98 100644
--- a/src/actions/resumeImport.actions.ts
+++ b/src/actions/resumeImport.actions.ts
@@ -4,9 +4,12 @@ import prisma from "@/lib/db";
import { getCurrentUser } from "@/utils/user.utils";
import { handleError } from "@/lib/utils";
import { SectionType } from "@/models/profile.model";
-import { createJobTitle } from "@/actions/jobtitle.actions";
-import { createLocation } from "@/actions/job.actions";
-import { resolveTag } from "@/lib/jobs/resolve";
+import {
+ resolveCompany,
+ resolveJobTitle,
+ resolveLocation,
+ resolveTag,
+} from "@/lib/jobs/resolve";
import { APP_CONSTANTS } from "@/lib/constants";
import {
ImportContactInfo,
@@ -77,15 +80,22 @@ function wrapAsHtml(text: string | undefined): string {
.join("");
}
-// Upsert a company scoped to the current user
-async function upsertCompany(label: string, userId: string) {
- const value = label.trim().toLowerCase();
- const existing = await prisma.company.findFirst({
- where: { value, createdBy: userId },
+// Find-or-create a ResumeSection by type — shared by experience, education,
+// and certification, which (unlike summary) don't need to branch on whether
+// a nested child record already exists.
+async function getOrCreateResumeSection(
+ resumeId: string,
+ sectionType: SectionType,
+ sectionTitle: string,
+): Promise<{ id: string }> {
+ const existing = await prisma.resumeSection.findFirst({
+ where: { resumeId, sectionType },
+ select: { id: true },
});
if (existing) return existing;
- return prisma.company.create({
- data: { label: label.trim(), value, createdBy: userId },
+ return prisma.resumeSection.create({
+ data: { resumeId, sectionTitle, sectionType },
+ select: { id: true },
});
}
@@ -167,40 +177,21 @@ export async function resolveImportCard(
case "experience": {
const d = card.data;
- const [company, jobTitleResult, locationResult] = await Promise.all([
- upsertCompany(d.company, user.id),
- createJobTitle(d.jobTitle),
- d.location ? createLocation(d.location) : Promise.resolve(null),
- ]);
-
- if (!company?.id) throw new Error("Failed to resolve company");
- if (!jobTitleResult?.id) throw new Error("Failed to resolve job title");
-
// Location is required by schema — use a placeholder if none provided
- let locationId: string;
- if (locationResult && "data" in locationResult && locationResult.data?.id) {
- locationId = locationResult.data.id;
- } else if (locationResult && "id" in locationResult && locationResult.id) {
- locationId = (locationResult as any).id;
- } else {
- const fallback = await createLocation(d.location || "Not specified");
- locationId = (fallback as any)?.data?.id ?? (fallback as any)?.id;
- if (!locationId) throw new Error("Failed to resolve location");
- }
+ const [company, jobTitle, location] = await Promise.all([
+ resolveCompany(d.company, user.id),
+ resolveJobTitle(d.jobTitle, user.id),
+ resolveLocation(d.location?.trim() || "Not specified", user.id),
+ ]);
const startDate = parseImportDate(d.startDate) ?? new Date(2000, 0, 1);
const endDate = parseImportDate(d.endDate);
- // Find or create the experience section
- let section = await prisma.resumeSection.findFirst({
- where: { resumeId, sectionType: SectionType.EXPERIENCE },
- select: { id: true },
- });
- if (!section) {
- section = await prisma.resumeSection.create({
- data: { resumeId, sectionTitle: "Experience", sectionType: SectionType.EXPERIENCE },
- });
- }
+ const section = await getOrCreateResumeSection(
+ resumeId,
+ SectionType.EXPERIENCE,
+ "Experience",
+ );
await prisma.resumeSection.update({
where: { id: section.id, Resume: { profile: { userId: user.id } } },
@@ -208,8 +199,8 @@ export async function resolveImportCard(
workExperiences: {
create: {
companyId: company.id,
- jobTitleId: jobTitleResult.id,
- locationId,
+ jobTitleId: jobTitle.id,
+ locationId: location.id,
startDate,
endDate: endDate ?? undefined,
description: wrapAsHtml(d.description),
@@ -223,30 +214,16 @@ export async function resolveImportCard(
case "education": {
const d = card.data;
- let locationId: string | undefined;
- if (d.location) {
- const locResult = await createLocation(d.location);
- locationId =
- (locResult as any)?.data?.id ?? (locResult as any)?.id;
- }
- if (!locationId) {
- const fallback = await createLocation("Not specified");
- locationId = (fallback as any)?.data?.id ?? (fallback as any)?.id;
- }
- if (!locationId) throw new Error("Failed to resolve location");
+ const location = await resolveLocation(d.location?.trim() || "Not specified", user.id);
const startDate = parseImportDate(d.startDate) ?? new Date(2000, 0, 1);
const endDate = parseImportDate(d.endDate);
- let section = await prisma.resumeSection.findFirst({
- where: { resumeId, sectionType: SectionType.EDUCATION },
- select: { id: true },
- });
- if (!section) {
- section = await prisma.resumeSection.create({
- data: { resumeId, sectionTitle: "Education", sectionType: SectionType.EDUCATION },
- });
- }
+ const section = await getOrCreateResumeSection(
+ resumeId,
+ SectionType.EDUCATION,
+ "Education",
+ );
await prisma.resumeSection.update({
where: { id: section.id, Resume: { profile: { userId: user.id } } },
@@ -256,7 +233,7 @@ export async function resolveImportCard(
institution: d.institution,
degree: d.degree ?? "",
fieldOfStudy: d.fieldOfStudy ?? "",
- locationId,
+ locationId: location.id,
startDate,
endDate: endDate ?? undefined,
description: wrapAsHtml(d.description),
@@ -270,19 +247,11 @@ export async function resolveImportCard(
case "certification": {
const d = card.data;
- let section = await prisma.resumeSection.findFirst({
- where: { resumeId, sectionType: SectionType.CERTIFICATION },
- select: { id: true },
- });
- if (!section) {
- section = await prisma.resumeSection.create({
- data: {
- resumeId,
- sectionTitle: "Certifications",
- sectionType: SectionType.CERTIFICATION,
- },
- });
- }
+ const section = await getOrCreateResumeSection(
+ resumeId,
+ SectionType.CERTIFICATION,
+ "Certifications",
+ );
await prisma.resumeSection.update({
where: { id: section.id, Resume: { profile: { userId: user.id } } },
diff --git a/src/lib/jobs/canonicalize.ts b/src/lib/jobs/canonicalize.ts
new file mode 100644
index 0000000..837e3ad
--- /dev/null
+++ b/src/lib/jobs/canonicalize.ts
@@ -0,0 +1,50 @@
+// Deterministic canonical form used as the match key when resolving-or-creating
+// Company / JobTitle / Location / JobSource. Shared by both entry points (the
+// add_job path in resolve.ts and automation discovery in the scraper mapper) so
+// identical input always resolves to the same record. Folds only variations
+// that never change identity — case, diacritics, comma separators, and
+// surrounding/repeated whitespace — plus, for companies, trailing legal
+// suffixes like "Inc"/"LLC". Other punctuation is left intact so distinct
+// labels such as "C++" vs "C#" can never collapse into one record.
+
+const LEGAL_SUFFIXES = [
+ "inc",
+ "llc",
+ "ltd",
+ "limited",
+ "corp",
+ "corporation",
+ "co",
+ "company",
+ "gmbh",
+ "plc",
+];
+
+// Matches a single trailing legal-suffix token (optionally followed by a dot).
+// Applied repeatedly so stacked suffixes ("Foo Inc Co") fully unwind.
+const LEGAL_SUFFIX_RE = new RegExp(`\\s(?:${LEGAL_SUFFIXES.join("|")})\\.?$`);
+
+export function canonicalizeEntityValue(
+ label: string,
+ opts: { stripLegalSuffix?: boolean } = {},
+): string {
+ const base = label
+ .normalize("NFKD")
+ .replace(/[\u0300-\u036f]/g, "")
+ .toLowerCase()
+ .replace(/,/g, " ")
+ .replace(/\s+/g, " ")
+ .trim();
+
+ if (!opts.stripLegalSuffix) return base;
+
+ let value = base;
+ let prev: string;
+ do {
+ prev = value;
+ value = value.replace(LEGAL_SUFFIX_RE, "").trim();
+ } while (value !== prev && value.length > 0);
+
+ // Guard against a label that is nothing but a suffix (e.g. "Inc").
+ return value.length > 0 ? value : base;
+}
diff --git a/src/lib/jobs/createJobFromNames.ts b/src/lib/jobs/createJobFromNames.ts
index 79aa552..1ca6aa0 100644
--- a/src/lib/jobs/createJobFromNames.ts
+++ b/src/lib/jobs/createJobFromNames.ts
@@ -2,6 +2,7 @@ import MarkdownIt from "markdown-it";
import prisma from "@/lib/db";
import { APP_CONSTANTS } from "@/lib/constants";
import { normalizeJobUrl } from "@/lib/scraper/utils";
+import { findExistingJobByUrl } from "@/lib/jobs/jobDedupe";
import { createJobRecord } from "@/lib/jobs/createJobRecord";
import {
resolveCompany,
@@ -158,16 +159,13 @@ async function detectDuplicate(
jobTitleId: string,
jobUrl?: string,
): Promise<{ id: string; title: string; company: string } | null> {
- // Tier 1 — URL match (no time window)
+ // Tier 1 — URL match (no time window). Uses the same canonical key as
+ // automation dedup (jobDedupeKey folds host case, leading "www.", param
+ // order, and tracking params), so URL variants of the same posting are
+ // caught — not just exact-string matches.
if (jobUrl) {
- const normalized = normalizeJobUrl(jobUrl);
- const byUrl = await prisma.job.findFirst({
- where: { userId, jobUrl: normalized },
- select: { id: true, JobTitle: { select: { label: true } }, Company: { select: { label: true } } },
- });
- if (byUrl) {
- return { id: byUrl.id, title: byUrl.JobTitle.label, company: byUrl.Company.label };
- }
+ const byUrl = await findExistingJobByUrl(userId, jobUrl);
+ if (byUrl) return byUrl;
}
// Tier 2 — company + title within window
diff --git a/src/lib/jobs/jobDedupe.ts b/src/lib/jobs/jobDedupe.ts
new file mode 100644
index 0000000..29c9072
--- /dev/null
+++ b/src/lib/jobs/jobDedupe.ts
@@ -0,0 +1,75 @@
+import prisma from "@/lib/db";
+import { jobDedupeKey } from "@/lib/scraper/utils";
+
+export interface ExistingJobRef {
+ id: string;
+ title: string;
+ company: string;
+}
+
+// Builds a dedupe-key -> job identity map over a user's saved jobs, using the
+// same canonical key (jobDedupeKey) as batch dedup. Shared by the automation
+// runner (keys only, via Map.has) and add_job duplicate detection (needs the
+// identity to report which existing job matched).
+export async function getExistingJobDedupeMap(
+ userId: string,
+): Promise<Map<string, ExistingJobRef>> {
+ const jobs = await prisma.job.findMany({
+ where: { userId },
+ select: {
+ id: true,
+ jobUrl: true,
+ JobTitle: { select: { label: true } },
+ Company: { select: { label: true } },
+ Location: { select: { label: true } },
+ },
+ });
+
+ const map = new Map<string, ExistingJobRef>();
+ for (const job of jobs) {
+ const key = jobDedupeKey({
+ url: job.jobUrl,
+ title: job.JobTitle?.label,
+ company: job.Company?.label,
+ location: job.Location?.label ?? undefined,
+ });
+ if (!map.has(key)) {
+ map.set(key, {
+ id: job.id,
+ title: job.JobTitle?.label ?? "",
+ company: job.Company?.label ?? "",
+ });
+ }
+ }
+ return map;
+}
+
+// Single-URL lookup for the add_job path: unlike getExistingJobDedupeMap
+// (which the batch automation runner needs, since it must also dedupe
+// URL-less jobs by title/company/location), a single add only ever checks
+// one URL, so this skips the Location join and jobs that have no URL at all.
+export async function findExistingJobByUrl(
+ userId: string,
+ jobUrl: string,
+): Promise<ExistingJobRef | null> {
+ const key = jobDedupeKey({ url: jobUrl });
+ const jobs = await prisma.job.findMany({
+ where: { userId, jobUrl: { not: null } },
+ select: {
+ id: true,
+ jobUrl: true,
+ JobTitle: { select: { label: true } },
+ Company: { select: { label: true } },
+ },
+ });
+ for (const job of jobs) {
+ if (jobDedupeKey({ url: job.jobUrl }) === key) {
+ return {
+ id: job.id,
+ title: job.JobTitle.label,
+ company: job.Company.label,
+ };
+ }
+ }
+ return null;
+}
diff --git a/src/lib/jobs/resolve.ts b/src/lib/jobs/resolve.ts
index 6f40c0d..cdf5c92 100644
--- a/src/lib/jobs/resolve.ts
+++ b/src/lib/jobs/resolve.ts
@@ -1,5 +1,6 @@
import prisma from "@/lib/db";
import { JOB_TYPES, WORKPLACE_TYPES } from "@/models/job.model";
+import { canonicalizeEntityValue } from "./canonicalize";
export interface ResolvedEntity {
id: string;
@@ -15,68 +16,82 @@ export interface ResolvedTag extends ResolvedEntity {
// Bucket A — resolve-or-create by exact-normalized value
-export async function resolveCompany(
+interface EntityRow {
+ id: string;
+ label: string;
+}
+
+interface EntityDelegate {
+ findUnique(args: {
+ where: { value_createdBy: { value: string; createdBy: string } };
+ }): Promise<EntityRow | null>;
+ create(args: {
+ data: { label: string; value: string; createdBy: string };
+ }): Promise<EntityRow>;
+}
+
+// findUnique-then-create is a check-then-act race under the
+// value_createdBy unique constraint: if two concurrent calls resolve the
+// same value, the loser's create throws P2002. Re-fetch and return the
+// winner's row instead of propagating, so a benign race never surfaces as
+// an error to the caller (e.g. runner.ts's job-save catch, which can't
+// distinguish this from an unrelated Job.jobUrl race).
+async function resolveEntity(
+ delegate: EntityDelegate,
label: string,
userId: string,
+ opts: { stripLegalSuffix?: boolean } = {},
): Promise<ResolvedEntity> {
const trimmed = label.trim();
- const value = trimmed.toLowerCase();
- const existing = await prisma.company.findUnique({
+ const value = canonicalizeEntityValue(trimmed, opts);
+ if (!value) throw new Error("A non-empty label is required");
+ const existing = await delegate.findUnique({
where: { value_createdBy: { value, createdBy: userId } },
});
if (existing) return { id: existing.id, label: existing.label, created: false };
- const created = await prisma.company.create({
- data: { label: trimmed, value, createdBy: userId },
- });
- return { id: created.id, label: created.label, created: true };
+
+ try {
+ const created = await delegate.create({
+ data: { label: trimmed, value, createdBy: userId },
+ });
+ return { id: created.id, label: created.label, created: true };
+ } catch (err: any) {
+ if (err?.code === "P2002") {
+ const winner = await delegate.findUnique({
+ where: { value_createdBy: { value, createdBy: userId } },
+ });
+ if (winner) return { id: winner.id, label: winner.label, created: false };
+ }
+ throw err;
+ }
}
-export async function resolveJobTitle(
+export function resolveCompany(
label: string,
userId: string,
): Promise<ResolvedEntity> {
- const trimmed = label.trim();
- const value = trimmed.toLowerCase();
- const existing = await prisma.jobTitle.findUnique({
- where: { value_createdBy: { value, createdBy: userId } },
- });
- if (existing) return { id: existing.id, label: existing.label, created: false };
- const created = await prisma.jobTitle.create({
- data: { label: trimmed, value, createdBy: userId },
- });
- return { id: created.id, label: created.label, created: true };
+ return resolveEntity(prisma.company, label, userId, { stripLegalSuffix: true });
}
-export async function resolveLocation(
+export function resolveJobTitle(
label: string,
userId: string,
): Promise<ResolvedEntity> {
- const trimmed = label.trim();
- const value = trimmed.toLowerCase();
- const existing = await prisma.location.findUnique({
- where: { value_createdBy: { value, createdBy: userId } },
- });
- if (existing) return { id: existing.id, label: existing.label, created: false };
- const record = await prisma.location.create({
- data: { label: trimmed, value, createdBy: userId },
- });
- return { id: record.id, label: record.label, created: true };
+ return resolveEntity(prisma.jobTitle, label, userId);
}
-export async function resolveJobSource(
+export function resolveLocation(
label: string,
userId: string,
): Promise<ResolvedEntity> {
- const trimmed = label.trim();
- const value = trimmed.toLowerCase();
- const existing = await prisma.jobSource.findUnique({
- where: { value_createdBy: { value, createdBy: userId } },
- });
- if (existing) return { id: existing.id, label: existing.label, created: false };
- const record = await prisma.jobSource.create({
- data: { label: trimmed, value, createdBy: userId },
- });
- return { id: record.id, label: record.label, created: true };
+ return resolveEntity(prisma.location, label, userId);
+}
+
+export function resolveJobSource(
+ label: string,
+ userId: string,
+): Promise<ResolvedEntity> {
+ return resolveEntity(prisma.jobSource, label, userId);
}
export async function resolveTag(
@@ -84,15 +99,27 @@ export async function resolveTag(
userId: string,
): Promise<ResolvedTag> {
const trimmed = label.trim();
- const value = trimmed.toLowerCase();
+ const value = canonicalizeEntityValue(trimmed);
const existing = await prisma.tag.findUnique({
where: { value_createdBy: { value, createdBy: userId } },
});
if (existing) return { id: existing.id, label: existing.label, value: existing.value, createdBy: existing.createdBy, created: false };
- const tag = await prisma.tag.create({
- data: { label: trimmed, value, createdBy: userId },
- });
- return { id: tag.id, label: tag.label, value: tag.value, createdBy: tag.createdBy, created: true };
+ try {
+ const tag = await prisma.tag.create({
+ data: { label: trimmed, value, createdBy: userId },
+ });
+ return { id: tag.id, label: tag.label, value: tag.value, createdBy: tag.createdBy, created: true };
+ } catch (err: any) {
+ if (err?.code === "P2002") {
+ const winner = await prisma.tag.findUnique({
+ where: { value_createdBy: { value, createdBy: userId } },
+ });
+ if (winner) {
+ return { id: winner.id, label: winner.label, value: winner.value, createdBy: winner.createdBy, created: false };
+ }
+ }
+ throw err;
+ }
}
export async function resolveTags(
@@ -105,7 +132,7 @@ export async function resolveTags(
for (const l of labels) {
const trimmed = l.trim();
if (!trimmed) continue;
- const key = trimmed.toLowerCase();
+ const key = canonicalizeEntityValue(trimmed);
if (!seen.has(key)) {
seen.add(key);
unique.push(trimmed);
diff --git a/src/lib/scraper/mapper.ts b/src/lib/scraper/mapper.ts
index d2cb2ad..3c8d81c 100644
--- a/src/lib/scraper/mapper.ts
+++ b/src/lib/scraper/mapper.ts
@@ -1,6 +1,12 @@
import type { ScrapedJobData, DiscoveryStatus } from "@/models/automation.model";
import db from "@/lib/db";
-import { normalizeForSearch, extractKeywords, extractCityName } from "./utils";
+import { capitalize } from "@/lib/utils";
+import {
+ resolveCompany,
+ resolveJobTitle,
+ resolveLocation,
+ resolveJobSource,
+} from "@/lib/jobs/resolve";
// Maps source employment-type strings (JSearch's "FULLTIME"/"CONTRACTOR",
// Greenhouse's absence of the field, etc.) to JOB_TYPES enum keys. Defaults
@@ -58,10 +64,19 @@ export async function mapScrapedJobToJobRecord(
): Promise<MapperOutput> {
const { scrapedJob, userId, automationId, matchScore, matchData } = input;
- const jobTitleId = await findOrCreateJobTitle(scrapedJob.title, userId);
- const locationId = await findOrCreateLocation(scrapedJob.location, userId);
- const companyId = await findOrCreateCompany(scrapedJob.company, userId);
- const jobSourceId = await getOrCreateJobSource(scrapedJob.sourceBoard, userId);
+ // Shares the same resolve-or-create helpers (and canonical match key) as the
+ // add_job path, so a company/title discovered here resolves to the same
+ // record a manual add would. Source board is title-cased for a readable
+ // JobSource label ("greenhouse" -> "Greenhouse"); the canonical value is
+ // case-insensitive so it still matches.
+ const [title, location, company, source] = await Promise.all([
+ resolveJobTitle(scrapedJob.title, userId),
+ scrapedJob.location
+ ? resolveLocation(scrapedJob.location, userId)
+ : Promise.resolve(null),
+ resolveCompany(scrapedJob.company, userId),
+ resolveJobSource(capitalize(scrapedJob.sourceBoard), userId),
+ ]);
const statusId = await getDefaultJobStatus();
return {
@@ -75,10 +90,10 @@ export async function mapScrapedJobToJobRecord(
createdAt: new Date(),
applied: false,
statusId,
- jobTitleId,
- companyId,
- jobSourceId,
- locationId,
+ jobTitleId: title.id,
+ companyId: company.id,
+ jobSourceId: source.id,
+ locationId: location?.id ?? null,
matchScore,
matchData,
discoveryStatus: "new",
@@ -86,147 +101,6 @@ export async function mapScrapedJobToJobRecord(
};
}
-async function findOrCreateJobTitle(
- title: string,
- userId: string
-): Promise<string> {
- const normalized = normalizeForSearch(title);
-
- let existing = await db.jobTitle.findFirst({
- where: { value: normalized, createdBy: userId },
- });
-
- if (!existing) {
- const keywords = extractKeywords(title);
- if (keywords.length > 0) {
- existing = await db.jobTitle.findFirst({
- where: {
- createdBy: userId,
- AND: keywords.map((keyword) => ({
- value: { contains: keyword },
- })),
- },
- });
- }
- }
-
- if (existing) {
- return existing.id;
- }
-
- const newTitle = await db.jobTitle.create({
- data: {
- label: title,
- value: normalized,
- createdBy: userId,
- },
- });
- return newTitle.id;
-}
-
-async function findOrCreateLocation(
- location: string,
- userId: string
-): Promise<string | null> {
- if (!location) return null;
-
- const normalized = normalizeForSearch(location);
- const cityName = extractCityName(location);
-
- let existing = await db.location.findFirst({
- where: {
- value: normalized,
- createdBy: userId,
- },
- });
-
- if (!existing && cityName) {
- existing = await db.location.findFirst({
- where: {
- createdBy: userId,
- OR: [
- { value: { contains: cityName } },
- { label: { contains: cityName } },
- ],
- },
- });
- }
-
- if (existing) {
- return existing.id;
- }
-
- const newLocation = await db.location.create({
- data: {
- label: location,
- value: normalized,
- createdBy: userId,
- },
- });
- return newLocation.id;
-}
-
-async function findOrCreateCompany(
- company: string,
- userId: string
-): Promise<string> {
- const normalized = normalizeForSearch(company);
-
- let existing = await db.company.findFirst({
- where: { value: normalized, createdBy: userId },
- });
-
- if (!existing) {
- const companyKeywords = extractKeywords(company);
- if (companyKeywords.length > 0) {
- existing = await db.company.findFirst({
- where: {
- createdBy: userId,
- AND: companyKeywords.map((keyword) => ({
- label: { contains: keyword },
- })),
- },
- });
- }
- }
-
- if (existing) {
- return existing.id;
- }
-
- const newCompany = await db.company.create({
- data: {
- label: company,
- value: normalized,
- createdBy: userId,
- },
- });
- return newCompany.id;
-}
-
-async function getOrCreateJobSource(
- sourceBoard: string,
- userId: string
-): Promise<string> {
- const normalized = sourceBoard.toLowerCase();
-
- let jobSource = await db.jobSource.findFirst({
- where: { value: normalized, createdBy: userId },
- });
-
- if (!jobSource) {
- jobSource = await db.jobSource.create({
- data: {
- label: sourceBoard.charAt(0).toUpperCase() + sourceBoard.slice(1),
- value: normalized,
- createdBy: userId,
- },
- });
- }
-
- return jobSource.id;
-}
-
async function getDefaultJobStatus(): Promise<string> {
let status = await db.jobStatus.findFirst({ where: { value: "new" } });
diff --git a/src/lib/scraper/runner.ts b/src/lib/scraper/runner.ts
index b73b0a5..86950d7 100644
--- a/src/lib/scraper/runner.ts
+++ b/src/lib/scraper/runner.ts
@@ -15,7 +15,8 @@ import type { AtsProvider } from "./ats/types";
import { runGreenhousePipeline } from "./greenhouse/pipeline";
import type { ScoredJob } from "./greenhouse/pipeline";
import { mapScrapedJobToJobRecord } from "./mapper";
-import { normalizeJobUrl, dedupeJobs, jobDedupeKey } from "./utils";
+import { normalizeJobUrl, dedupeJobs } from "./utils";
+import { getExistingJobDedupeMap } from "@/lib/jobs/jobDedupe";
import { calculateNextRunAt } from "./schedule";
import { APP_CONSTANTS } from "@/lib/constants";
import {
@@ -403,7 +404,7 @@ export async function runAutomation(
"Checking for duplicate jobs...",
);
- const existingKeys = await getExistingJobKeys(automation.userId);
+ const existingKeys = await getExistingJobDedupeMap(automation.userId);
const newJobs = dedupeJobs(searchResult.data, existingKeys);
const jobsDeduplicated = newJobs.length;
@@ -647,31 +648,6 @@ export async function runAutomation(
}
}
-async function getExistingJobKeys(userId: string): Promise<Set<string>> {
- const existingJobs = await db.job.findMany({
- where: { userId },
- select: {
- jobUrl: true,
- JobTitle: { select: { label: true } },
- Company: { select: { label: true } },
- Location: { select: { label: true } },
- },
- });
-
- const keys = new Set<string>();
- for (const job of existingJobs) {
- keys.add(
- jobDedupeKey({
- url: job.jobUrl,
- title: job.JobTitle?.label,
- company: job.Company?.label,
- location: job.Location?.label ?? undefined,
- }),
- );
- }
- return keys;
-}
-
interface AtsRunConfig {
companies: { name: string; token: string; host?: "default" | "eu" }[];
targetTitles: string[];
@@ -826,7 +802,7 @@ async function runAtsRun(
);
// Dedup against existing jobs and within this batch.
- const existingKeys = await getExistingJobKeys(automation.userId);
+ const existingKeys = await getExistingJobDedupeMap(automation.userId);
const dedupedJobs = dedupeJobs(jobs, existingKeys);
const jobsDeduplicated = dedupedJobs.length;
diff --git a/src/lib/scraper/utils.ts b/src/lib/scraper/utils.ts
index 8a44919..437733b 100644
--- a/src/lib/scraper/utils.ts
+++ b/src/lib/scraper/utils.ts
@@ -1,4 +1,5 @@
import type { ScraperError } from "./types";
+import { canonicalizeEntityValue } from "@/lib/jobs/canonicalize";
// Allowlist for ATS board tokens (shared by Greenhouse + Lever). Rejects
// path/query injection before the token is interpolated into a fetch URL.
@@ -78,20 +79,28 @@ interface DedupableJob {
// One key per job, shared by both the incoming batch and existing DB records so
// they compare identically. Falls back to a title/company/location signature
// when a job has no usable URL (otherwise linkless jobs re-add every run).
+// Uses the same canonicalization as entity resolution (resolve.ts) — in
+// particular the company legal-suffix stripping — so two postings that
+// resolve to the same Company/JobTitle/Location record also dedupe as the
+// same job.
export function jobDedupeKey(job: DedupableJob): string {
const url = job.url?.trim();
if (url) return `url:${urlDedupeKey(url)}`;
- const meta = [job.title, job.company, job.location]
- .map((part) => normalizeForSearch(part ?? ""))
- .join("|");
+ const meta = [
+ canonicalizeEntityValue(job.title ?? ""),
+ canonicalizeEntityValue(job.company ?? "", { stripLegalSuffix: true }),
+ canonicalizeEntityValue(job.location ?? ""),
+ ].join("|");
return `meta:${meta}`;
}
// Removes jobs already saved (existingKeys) and collapses duplicates within the
// batch itself. Both source paths (JSearch, Greenhouse) run through here.
+// Accepts any key lookup with `.has` so callers can pass a Set or the
+// getExistingJobDedupeMap Map directly.
export function dedupeJobs<T extends DedupableJob>(
jobs: T[],
- existingKeys: Set<string>,
+ existingKeys: { has(key: string): boolean },
): T[] {
const seen = new Set<string>();
const result: T[] = [];
@@ -103,43 +112,3 @@ export function dedupeJobs<T extends DedupableJob>(
}
return result;
}
-
-export function normalizeForSearch(str: string): string {
- return str
- .toLowerCase()
- .trim()
- .replace(/\s+/g, "-")
- .replace(/[^a-z0-9-]/g, "");
-}
-
-const STOP_WORDS = [
- "senior",
- "jr",
- "junior",
- "sr",
- "inc",
- "llc",
- "ltd",
- "corp",
- "corporation",
- "the",
- "a",
- "an",
- "co",
- "company",
-];
-
-export function extractKeywords(str: string): string[] {
- return str
- .toLowerCase()
- .split(/[\s\-_,\.]+/)
- .filter((word) => word.length > 2 && !STOP_WORDS.includes(word));
-}
-
-export function extractCityName(location: string): string | null {
- const parts = location.split(",");
- if (parts.length > 0) {
- return parts[0].trim().toLowerCase();
- }
- return null;
-}
diff --git a/src/lib/utils.ts b/src/lib/utils.ts
index c48c1f5..8387c0c 100644
--- a/src/lib/utils.ts
+++ b/src/lib/utils.ts
@@ -36,6 +36,10 @@ export function formatUrl(url: string) {
return url;
}
+export function capitalize(str: string): string {
+ return str.charAt(0).toUpperCase() + str.slice(1);
+}
+
export function handleError(error: unknown, msg = "Server Error.") {
console.error(error, msg);
if (error instanceof Error) {