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.
TypeScript ai-agentai-resume-analyzerapplication-trackerapplication-tracking-systematscareerjob-applicationjob-automationjob-huntingjob-searchjob-seeker-toolsjob-trackerjob-trackingjobsearchjobseekersmcpollamaresumeresume-reviewself-hosted
feat(stages): append a current stage when MCP updates a job's status, stop re-stamping appliedDate there, and assert stages on the job-details include
Khuram Niaz39e49be648d6
diff --git a/__tests__/job.actions.spec.ts b/__tests__/job.actions.spec.tsindex 7cb3d40..6f6324d 100644--- a/__tests__/job.actions.spec.ts+++ b/__tests__/job.actions.spec.ts@@ -850,6 +850,33 @@ describe("jobActions", () => {},orderBy: { createdAt: "asc" },},+ stages: {+ include: {+ StageType: { include: { Status: true } },+ interviewers: {+ include: {+ Contact: {+ select: {+ id: true,+ name: true,+ title: true,+ email: true,+ phone: true,+ linkedinUrl: true,+ Company: { select: { id: true, label: true } },+ },+ },+ },+ orderBy: { createdAt: "asc" },+ },+ prepQuestions: {+ include: {+ Question: { select: { id: true, question: true, tags: true } },+ },+ orderBy: { createdAt: "asc" },+ },+ },+ },},});});diff --git a/__tests__/updateJobFromNames.spec.ts b/__tests__/updateJobFromNames.spec.tsindex 6bafaee..fdce0d8 100644--- a/__tests__/updateJobFromNames.spec.ts+++ b/__tests__/updateJobFromNames.spec.ts@@ -8,15 +8,19 @@ import {resolveWorkplaceType,resolveJobStatus,resolveTags,+ resolveStageTypeForStatusId,} from "@/lib/jobs/resolve";import { PrismaClient } from "@prisma/client";const prisma = new PrismaClient();vi.mock("@prisma/client", () => {- const mPrismaClient = {+ const mPrismaClient: any = {job: { findFirst: vi.fn(), update: vi.fn() },+ jobStage: { findFirst: vi.fn(), create: vi.fn(), updateMany: vi.fn() },+ $transaction: vi.fn(),};+ mPrismaClient.$transaction.mockImplementation((fn: any) => fn(mPrismaClient));return { PrismaClient: vi.fn(function () { return mPrismaClient; }) };});@@ -29,6 +33,7 @@ vi.mock("@/lib/jobs/resolve", () => ({resolveWorkplaceType: vi.fn(),resolveJobStatus: vi.fn(),resolveTags: vi.fn(),+ resolveStageTypeForStatusId: vi.fn(),}));const userId = "user-1";@@ -50,6 +55,10 @@ describe("updateJobFromNames", () => {(resolveJobType as any).mockReturnValue("FT");(resolveWorkplaceType as any).mockReturnValue("REMOTE");(resolveTags as any).mockResolvedValue({ resolved: [], dropped: [] });+ (resolveStageTypeForStatusId as any).mockResolvedValue("t-offer");+ (prisma as any).$transaction.mockImplementation((fn: any) => fn(prisma));+ (prisma as any).jobStage.create.mockResolvedValue({ id: "st-new" });+ (prisma as any).jobStage.updateMany.mockResolvedValue({ count: 1 });});it("returns updated:false when the job is not the caller's MCP-created job", async () => {@@ -67,7 +76,7 @@ describe("updateJobFromNames", () => {expect(prisma.job.findFirst).toHaveBeenCalledWith({where: { id: "job-1", userId, createdVia: { not: null } },- select: { id: true, descriptionCompleteness: true },+ select: { id: true, descriptionCompleteness: true, appliedDate: true },});});@@ -167,4 +176,57 @@ describe("updateJobFromNames", () => {expect(result.message).toContain("No fields to update");expect(prisma.job.update).not.toHaveBeenCalled();});++ // D9: the sixth write path to Job.statusId keeps the timeline in step.+ it("appends a current stage when MCP changes the job's status", async () => {+ (resolveJobStatus as any).mockResolvedValue("s-offer");+ (prisma as any).jobStage.findFirst.mockResolvedValue({+ id: "st-int",+ StageType: { statusId: "s-int", Status: { value: "interview" } },+ });++ await updateJobFromNames({ jobId: "job-1", status: "offer" }, userId);++ expect((prisma as any).jobStage.create.mock.calls[0][0].data).toMatchObject({+ jobId: "job-1",+ stageTypeId: "t-offer",+ occurredAt: null,+ });+ });++ // Decision 16's rule, reached through MCP: the comparison is on parent+ // status, so re-sending a status the job already holds is a no-op.+ it("appends nothing when the current stage already carries that status", async () => {+ (resolveJobStatus as any).mockResolvedValue("s-int");+ (prisma as any).jobStage.findFirst.mockResolvedValue({+ id: "st-tech",+ StageType: { statusId: "s-int", Status: { value: "interview" } },+ });++ await updateJobFromNames({ jobId: "job-1", status: "interview" }, userId);++ expect((prisma as any).jobStage.create).not.toHaveBeenCalled();+ });++ it("leaves the timeline alone when no status field was sent", async () => {+ await updateJobFromNames({ jobId: "job-1", salaryRange: "100k" }, userId);++ expect(resolveStageTypeForStatusId).not.toHaveBeenCalled();+ expect((prisma as any).jobStage.create).not.toHaveBeenCalled();+ });++ // D5, now covering the MCP path as well.+ it("never re-stamps an appliedDate the job already has", async () => {+ (prisma.job.findFirst as any).mockResolvedValue({+ id: "job-1",+ descriptionCompleteness: null,+ appliedDate: new Date("2026-09-03T00:00:00Z"),+ });++ await updateJobFromNames({ jobId: "job-1", applied: true }, userId);++ expect((prisma.job.update as any).mock.calls[0][0].data).not.toHaveProperty(+ "appliedDate",+ );+ });});diff --git a/src/lib/jobs/updateJobFromNames.ts b/src/lib/jobs/updateJobFromNames.tsindex 7fdb878..b9af4bd 100644--- a/src/lib/jobs/updateJobFromNames.ts+++ b/src/lib/jobs/updateJobFromNames.ts@@ -13,8 +13,10 @@ import {resolveWorkplaceType,resolveJobStatus,resolveTags,+ resolveStageTypeForStatusId,type ResolvedEntity,} from "./resolve";+import { appendStatusStage } from "@/actions/job/shared";// Same renderer config as createJobFromNames — html:false escapes raw HTML.const md = new MarkdownIt({ html: false, linkify: false, breaks: true });@@ -63,7 +65,7 @@ export async function updateJobFromNames(// caller learns "not found" before any entity is created as a side effect.const existing = await prisma.job.findFirst({where: { id: jobId, userId, createdVia: { not: null } },- select: { id: true, descriptionCompleteness: true },+ select: { id: true, descriptionCompleteness: true, appliedDate: true },});if (!existing) {return {@@ -124,7 +126,13 @@ export async function updateJobFromNames(if (input.jobUrl !== undefined) data.jobUrl = normalizeJobUrl(input.jobUrl);if (input.applied !== undefined) data.applied = input.applied;if (input.appliedDate !== undefined) data.appliedDate = input.appliedDate;- if (input.applied === true && input.appliedDate === undefined) {+ // D5: an appliedDate already on the job is never overwritten, the same rule+ // updateJobStatus and every stage write path now follow.+ if (+ input.applied === true &&+ input.appliedDate === undefined &&+ !existing.appliedDate+ ) {data.appliedDate = new Date();}if (resolvedTagsResult) {@@ -151,10 +159,23 @@ export async function updateJobFromNames(};}+ // D4: this resolve can create a row and writes through the base client, so+ // it must not run while the transaction below holds SQLite's write lock.+ const stageTypeId = statusId+ ? await resolveStageTypeForStatusId(statusId, userId)+ : null;+try {- await prisma.job.update({- where: { id: jobId, userId, createdVia: { not: null } },- data,+ await prisma.$transaction(async (tx: any) => {+ // D9: the sixth write path to Job.statusId. Without this an agent can+ // move a job's status while its timeline stays where it was.+ if (statusId && stageTypeId) {+ await appendStatusStage(tx, jobId, statusId, stageTypeId, userId);+ }+ await tx.job.update({+ where: { id: jobId, userId, createdVia: { not: null } },+ data,+ });});} catch (error: any) {if (error?.code === "P2025") {