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 shared locations issue

Khuram Niazf9118b31020c
diff --git a/__tests__/jobLocation.actions.spec.ts b/__tests__/jobLocation.actions.spec.ts
index e99b474..2464ae6 100644
--- a/__tests__/jobLocation.actions.spec.ts
+++ b/__tests__/jobLocation.actions.spec.ts
@@ -44,7 +44,8 @@ describe("Job Location Actions", () => {
});
describe("getAllJobLocations", () => {
- it("should return all job locations", async () => {
+ it("should return job locations for authenticated user", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
const mockLocations = [
{ id: "loc-1", label: "New York", value: "new york" },
{ id: "loc-2", label: "Remote", value: "remote" },
@@ -54,10 +55,22 @@ describe("Job Location Actions", () => {
const result = await getAllJobLocations();
expect(result).toEqual(mockLocations);
- expect(prisma.location.findMany).toHaveBeenCalled();
+ expect(prisma.location.findMany).toHaveBeenCalledWith({
+ where: { createdBy: mockUser.id },
+ });
+ });
+
+ it("should return error for unauthenticated user", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ const result = await getAllJobLocations();
+
+ expect(result).toEqual({ success: false, message: "Not authenticated" });
+ expect(prisma.location.findMany).not.toHaveBeenCalled();
});
it("should handle errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
(prisma.location.findMany as jest.Mock).mockRejectedValue(
new Error("Database error")
);
diff --git a/prisma/migrations/20260217_add_location_unique_constraint/migration.sql b/prisma/migrations/20260217_add_location_unique_constraint/migration.sql
new file mode 100644
index 0000000..5e32274
--- /dev/null
+++ b/prisma/migrations/20260217_add_location_unique_constraint/migration.sql
@@ -0,0 +1,62 @@
+-- Reassign jobs referencing duplicate locations to the canonical (earliest) one
+UPDATE "Job"
+SET "locationId" = (
+ SELECT l2."id"
+ FROM "Location" l2
+ WHERE l2."value" = (SELECT "value" FROM "Location" WHERE "id" = "Job"."locationId")
+ AND l2."createdBy" = (SELECT "createdBy" FROM "Location" WHERE "id" = "Job"."locationId")
+ ORDER BY l2."id"
+ LIMIT 1
+)
+WHERE "locationId" IN (
+ SELECT l."id" FROM "Location" l
+ WHERE EXISTS (
+ SELECT 1 FROM "Location" l2
+ WHERE l2."value" = l."value" AND l2."createdBy" = l."createdBy" AND l2."id" < l."id"
+ )
+);
+
+-- Reassign work experiences referencing duplicate locations
+UPDATE "WorkExperience"
+SET "locationId" = (
+ SELECT l2."id"
+ FROM "Location" l2
+ WHERE l2."value" = (SELECT "value" FROM "Location" WHERE "id" = "WorkExperience"."locationId")
+ AND l2."createdBy" = (SELECT "createdBy" FROM "Location" WHERE "id" = "WorkExperience"."locationId")
+ ORDER BY l2."id"
+ LIMIT 1
+)
+WHERE "locationId" IS NOT NULL AND "locationId" IN (
+ SELECT l."id" FROM "Location" l
+ WHERE EXISTS (
+ SELECT 1 FROM "Location" l2
+ WHERE l2."value" = l."value" AND l2."createdBy" = l."createdBy" AND l2."id" < l."id"
+ )
+);
+
+-- Reassign educations referencing duplicate locations
+UPDATE "Education"
+SET "locationId" = (
+ SELECT l2."id"
+ FROM "Location" l2
+ WHERE l2."value" = (SELECT "value" FROM "Location" WHERE "id" = "Education"."locationId")
+ AND l2."createdBy" = (SELECT "createdBy" FROM "Location" WHERE "id" = "Education"."locationId")
+ ORDER BY l2."id"
+ LIMIT 1
+)
+WHERE "locationId" IS NOT NULL AND "locationId" IN (
+ SELECT l."id" FROM "Location" l
+ WHERE EXISTS (
+ SELECT 1 FROM "Location" l2
+ WHERE l2."value" = l."value" AND l2."createdBy" = l."createdBy" AND l2."id" < l."id"
+ )
+);
+
+-- Delete duplicate locations (keep the one with the lowest id per value+createdBy)
+DELETE FROM "Location"
+WHERE "id" NOT IN (
+ SELECT MIN("id") FROM "Location" GROUP BY "value", "createdBy"
+);
+
+-- CreateIndex
+CREATE UNIQUE INDEX "Location_value_createdBy_key" ON "Location"("value", "createdBy");
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index a523204..07678aa 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -203,6 +203,8 @@ model Location {
user User @relation(fields: [createdBy], references: [id])
educations Education[]
workExperience WorkExperience[]
+
+ @@unique([value, createdBy])
}
model Company {
diff --git a/src/actions/jobLocation.actions.ts b/src/actions/jobLocation.actions.ts
index 52f9e7b..74eb40f 100644
--- a/src/actions/jobLocation.actions.ts
+++ b/src/actions/jobLocation.actions.ts
@@ -6,7 +6,15 @@ import { APP_CONSTANTS } from "@/lib/constants";
export const getAllJobLocations = async (): Promise<any | undefined> => {
try {
- const list = await prisma.location.findMany();
+ const user = await getCurrentUser();
+ if (!user) {
+ throw new Error("Not authenticated");
+ }
+ const list = await prisma.location.findMany({
+ where: {
+ createdBy: user.id,
+ },
+ });
return list;
} catch (error) {
const msg = "Failed to fetch job location list. ";