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.

feat: add ownership-scoped job lookup for the agent chat

Khuram Niaz07962719c610
diff --git a/__tests__/agentJobLookup.spec.ts b/__tests__/agentJobLookup.spec.ts
new file mode 100644
index 0000000..2700406
--- /dev/null
+++ b/__tests__/agentJobLookup.spec.ts
@@ -0,0 +1,64 @@
+import { resolveJobForAgent } from "@/lib/agent/jobLookup";
+import prisma from "@/lib/db";
+
+vi.mock("@/lib/db", () => ({
+ default: { job: { findFirst: vi.fn() } },
+}));
+
+const db = prisma as unknown as {
+ job: { findFirst: ReturnType<typeof vi.fn> };
+};
+
+const job = {
+ id: "job-1",
+ description: "<p>Build things</p>",
+ resumeId: "r9",
+ workplaceType: "remote",
+ JobTitle: { label: "Senior Backend Engineer" },
+ Company: { label: "Northwind Cloud" },
+ Location: { label: "Toronto" },
+};
+
+describe("resolveJobForAgent", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ db.job.findFirst.mockResolvedValue(job);
+ });
+
+ it("returns no_job when there is no page job id", async () => {
+ const result = await resolveJobForAgent("user-1", undefined);
+ expect(result).toEqual({ status: "no_job" });
+ expect(db.job.findFirst).not.toHaveBeenCalled();
+ });
+
+ // pageContext is client-supplied, so the userId in the where clause is the
+ // only thing standing between the caller and someone else's job.
+ it("scopes the lookup to the caller", async () => {
+ await resolveJobForAgent("user-1", "job-1");
+ expect(db.job.findFirst).toHaveBeenCalledWith(
+ expect.objectContaining({ where: { id: "job-1", userId: "user-1" } }),
+ );
+ });
+
+ it("includes the relations convertJobToText reads", async () => {
+ await resolveJobForAgent("user-1", "job-1");
+ const args = db.job.findFirst.mock.calls[0][0];
+ expect(args.include).toEqual({
+ JobTitle: true,
+ Company: true,
+ Location: true,
+ });
+ });
+
+ it("returns the job when it belongs to the caller", async () => {
+ const result = await resolveJobForAgent("user-1", "job-1");
+ expect(result.status).toBe("ok");
+ if (result.status === "ok") expect(result.job.id).toBe("job-1");
+ });
+
+ it("returns no_job when the id belongs to someone else", async () => {
+ db.job.findFirst.mockResolvedValue(null);
+ const result = await resolveJobForAgent("user-1", "someone-elses-job");
+ expect(result).toEqual({ status: "no_job" });
+ });
+});
diff --git a/src/lib/agent/jobLookup.ts b/src/lib/agent/jobLookup.ts
new file mode 100644
index 0000000..3383497
--- /dev/null
+++ b/src/lib/agent/jobLookup.ts
@@ -0,0 +1,27 @@
+import prisma from "@/lib/db";
+import type { JobResponse } from "@/models/job.model";
+
+export type JobLookup =
+ | { status: "ok"; job: JobResponse }
+ | { status: "no_job" };
+
+// There is no lookup by name here on purpose: match_job scores the job the
+// user is looking at, so the id comes from page context and never from the
+// model. pageContext is client-supplied, which is why userId is in the where
+// clause rather than checked afterwards.
+export async function resolveJobForAgent(
+ userId: string,
+ pageJobId?: string,
+): Promise<JobLookup> {
+ if (!pageJobId) return { status: "no_job" };
+
+ const job = await prisma.job.findFirst({
+ where: { id: pageJobId, userId },
+ include: { JobTitle: true, Company: true, Location: true },
+ });
+ if (!job) return { status: "no_job" };
+
+ // Prisma's generated shape is structurally what convertJobToText reads but
+ // is not nominally job.model's JobResponse — same cast as resumeLookup.
+ return { status: "ok", job: job as unknown as JobResponse };
+}