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.

fix(company): improve update logic to prevent duplicate companies and handle unchanged labels

Khuram Niaze6cd1113aaa5
diff --git a/__tests__/company.actions.spec.ts b/__tests__/company.actions.spec.ts
index 7b64df3..dad07b1 100644
--- a/__tests__/company.actions.spec.ts
+++ b/__tests__/company.actions.spec.ts
@@ -499,7 +499,14 @@ describe("Company Actions", () => {
it("should update a company successfully", async () => {
(getCurrentUser as any).mockResolvedValue(mockUser);
- (prisma.company.findFirst as any).mockResolvedValue(null);
+ (prisma.company.findFirst as any)
+ .mockResolvedValueOnce({
+ id: "company-id",
+ label: "Old Employer",
+ value: "old employer",
+ createdBy: mockUser.id,
+ })
+ .mockResolvedValueOnce(null);
const mockUpdatedCompany = {
id: "company-id",
@@ -512,7 +519,10 @@ describe("Company Actions", () => {
expect(result).toEqual({ success: true, data: mockUpdatedCompany });
- expect(prisma.company.findFirst).toHaveBeenCalledWith({
+ expect(prisma.company.findFirst).toHaveBeenNthCalledWith(1, {
+ where: { id: "company-id", createdBy: mockUser.id },
+ });
+ expect(prisma.company.findFirst).toHaveBeenNthCalledWith(2, {
where: { value: "updated employer", createdBy: mockUser.id },
});
@@ -526,6 +536,41 @@ describe("Company Actions", () => {
});
});
+ it("does not recompute the value or check for duplicates when the label is unchanged", async () => {
+ // Regression guard: a company whose stored `value` isn't derivable
+ // from its label (e.g. mock-seeded rows with a `__mock__` prefix)
+ // must not have its value clobbered by an unrelated logo-only save.
+ (getCurrentUser as any).mockResolvedValue(mockUser);
+
+ (prisma.company.findFirst as any).mockResolvedValueOnce({
+ id: "company-id",
+ label: "Amazon",
+ value: "__mock__amazon",
+ createdBy: mockUser.id,
+ });
+
+ const mockUpdatedCompany = { id: "company-id", value: "__mock__amazon" };
+ (prisma.company.update as any).mockResolvedValue(mockUpdatedCompany);
+
+ const result = await updateCompany({
+ id: "company-id",
+ company: "Amazon",
+ logoUrl: "http://example.com/logo.png",
+ createdBy: "user-id",
+ });
+
+ expect(result).toEqual({ success: true, data: mockUpdatedCompany });
+ expect(prisma.company.findFirst).toHaveBeenCalledTimes(1);
+ expect(prisma.company.update).toHaveBeenCalledWith({
+ where: { id: "company-id", createdBy: "user-id" },
+ data: {
+ value: "__mock__amazon",
+ label: "Amazon",
+ 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 = {
@@ -534,14 +579,21 @@ describe("Company Actions", () => {
value: "acme",
createdBy: mockUser.id,
};
- (prisma.company.findFirst as any).mockResolvedValue(mockExistingCompany);
+ (prisma.company.findFirst as any)
+ .mockResolvedValueOnce({
+ id: "company-id",
+ label: "Something Else",
+ value: "something else",
+ createdBy: mockUser.id,
+ })
+ .mockResolvedValueOnce(mockExistingCompany);
const result = await updateCompany({
...validData,
company: "Acme Inc.",
});
- expect(prisma.company.findFirst).toHaveBeenCalledWith({
+ expect(prisma.company.findFirst).toHaveBeenNthCalledWith(2, {
where: { value: "acme", createdBy: mockUser.id },
});
expect(result).toEqual({
@@ -635,7 +687,12 @@ describe("Company Actions", () => {
it("should allow empty logo URL", async () => {
(getCurrentUser as any).mockResolvedValue(mockUser);
- (prisma.company.findFirst as any).mockResolvedValue(null);
+ (prisma.company.findFirst as any).mockResolvedValueOnce({
+ id: "company-id",
+ label: "Updated Employer",
+ value: "updated employer",
+ createdBy: mockUser.id,
+ });
const mockUpdatedCompany = {
id: "company-id",
diff --git a/src/actions/company.actions.ts b/src/actions/company.actions.ts
index 37233d2..55579dd 100644
--- a/src/actions/company.actions.ts
+++ b/src/actions/company.actions.ts
@@ -219,17 +219,37 @@ export const updateCompany = async (
);
}
- const value = canonicalizeEntityValue(company.trim(), { stripLegalSuffix: true });
-
- const companyExists = await prisma.company.findFirst({
+ const existingCompany = await prisma.company.findFirst({
where: {
- value,
+ id,
createdBy: user.id,
},
});
- if (companyExists && companyExists.id !== id) {
- throw new Error("Company already exists!");
+ if (!existingCompany) {
+ throw new Error("Company not found");
+ }
+
+ const trimmedLabel = company.trim();
+
+ // Only recompute the match key when the label actually changed. Some
+ // rows (e.g. mock-seeded companies) intentionally hold a `value` that
+ // isn't derivable from their label — recomputing on every save would
+ // collide with an unrelated company sharing the same canonical label.
+ let value = existingCompany.value;
+ if (trimmedLabel !== existingCompany.label) {
+ value = canonicalizeEntityValue(trimmedLabel, { stripLegalSuffix: true });
+
+ const companyExists = await prisma.company.findFirst({
+ where: {
+ value,
+ createdBy: user.id,
+ },
+ });
+
+ if (companyExists && companyExists.id !== id) {
+ throw new Error("Company already exists!");
+ }
}
const res = await prisma.company.update({