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(stages): add job-stage read, add, edit, delete and promote actions behind a barrel

Khuram Niazeef761318b08
diff --git a/__tests__/jobStage.actions.spec.ts b/__tests__/jobStage.actions.spec.ts
new file mode 100644
index 0000000..8849a8b
--- /dev/null
+++ b/__tests__/jobStage.actions.spec.ts
@@ -0,0 +1,378 @@
+import {
+ getJobStages,
+ addJobStage,
+ deleteJobStage,
+ setCurrentJobStage,
+ setStageNotes,
+} from "@/actions/jobStage.actions";
+import { getCurrentUser } from "@/utils/user.utils";
+import prisma from "@/lib/db";
+import { resolveJobStageType } from "@/lib/jobs/resolve";
+
+vi.mock("@/lib/db", () => {
+ const jobStage = {
+ findMany: vi.fn(),
+ findFirst: vi.fn(),
+ create: vi.fn(),
+ update: vi.fn(),
+ updateMany: vi.fn(),
+ delete: vi.fn(),
+ };
+ const job = { count: vi.fn(), findFirst: vi.fn(), update: vi.fn() };
+ const jobStageType = { findFirst: vi.fn() };
+ const client = {
+ jobStage,
+ job,
+ jobStageType,
+ $transaction: vi.fn(async (fn: any) => fn(client)),
+ };
+ return { default: client };
+});
+
+vi.mock("@/utils/user.utils", () => ({ getCurrentUser: vi.fn() }));
+vi.mock("@/lib/jobs/resolve", () => ({
+ resolveJobStageType: vi.fn(),
+ resolveStageTypeForStatusId: vi.fn(),
+}));
+vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
+
+const db = prisma as any;
+const user = { id: "user-1" };
+const at = (iso: string) => new Date(iso);
+
+const interviewType = {
+ id: "t-int",
+ label: "Final / Onsite Interview",
+ statusId: "s-int",
+ Status: { value: "interview" },
+};
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ (getCurrentUser as any).mockResolvedValue(user);
+ db.job.count.mockResolvedValue(1);
+ db.job.findFirst.mockResolvedValue({ appliedDate: null });
+ db.jobStage.updateMany.mockResolvedValue({ count: 1 });
+});
+
+describe("getJobStages", () => {
+ it("scopes the read through the job's owner and sorts undated stages last", async () => {
+ db.jobStage.findMany.mockResolvedValue([
+ { id: "c", occurredAt: null, createdAt: at("2026-01-01T00:00:00Z") },
+ { id: "a", occurredAt: at("2026-09-01T00:00:00Z"), createdAt: at("2026-01-01T00:00:00Z") },
+ ]);
+
+ const stages = await getJobStages("j1");
+
+ expect(db.jobStage.findMany.mock.calls[0][0].where).toEqual({
+ jobId: "j1",
+ Job: { userId: user.id },
+ });
+ expect(stages.map((s: any) => s.id)).toEqual(["a", "c"]);
+ });
+});
+
+describe("addJobStage", () => {
+ it("refuses a job the caller does not own", async () => {
+ db.job.count.mockResolvedValue(0);
+
+ const res = await addJobStage({
+ jobId: "someone-elses",
+ stageTypeId: "t-int",
+ setAsCurrent: true,
+ } as any);
+
+ expect(res.success).toBe(false);
+ expect(db.jobStage.create).not.toHaveBeenCalled();
+ });
+
+ it("refuses a stage type the caller does not own", async () => {
+ db.jobStageType.findFirst.mockResolvedValue(null);
+
+ const res = await addJobStage({
+ jobId: "j1",
+ stageTypeId: "someone-elses-type",
+ setAsCurrent: true,
+ } as any);
+
+ expect(res.success).toBe(false);
+ expect(db.jobStage.create).not.toHaveBeenCalled();
+ });
+
+ it("creates a custom type through the resolver and uses its id", async () => {
+ (resolveJobStageType as any).mockResolvedValue({
+ id: "t-new",
+ label: "Panel Interview",
+ created: true,
+ });
+ db.jobStage.create.mockResolvedValue({ id: "st1" });
+ db.jobStage.findFirst.mockResolvedValue({
+ id: "st1",
+ occurredAt: null,
+ StageType: { statusId: "s-int", Status: { value: "interview" } },
+ });
+
+ const res = await addJobStage({
+ jobId: "j1",
+ customLabel: "Panel Interview",
+ customStatusId: "s-int",
+ setAsCurrent: true,
+ } as any);
+
+ expect(resolveJobStageType).toHaveBeenCalledWith(
+ "Panel Interview",
+ user.id,
+ "s-int",
+ );
+ expect(res.success).toBe(true);
+ expect(db.jobStage.create.mock.calls[0][0].data.stageTypeId).toBe("t-new");
+ });
+
+ it("clears the old current stage and rewrites the job status when set as current", async () => {
+ db.jobStageType.findFirst.mockResolvedValue(interviewType);
+ db.jobStage.create.mockResolvedValue({ id: "st2" });
+ db.jobStage.findFirst.mockResolvedValue({
+ id: "st2",
+ occurredAt: null,
+ StageType: { statusId: "s-int", Status: { value: "interview" } },
+ });
+
+ const res = await addJobStage({
+ jobId: "j1",
+ stageTypeId: "t-int",
+ setAsCurrent: true,
+ } as any);
+
+ expect(res.success).toBe(true);
+ // D8: promoteStage names the owner in both of its updateMany calls.
+ expect(db.jobStage.updateMany).toHaveBeenCalledWith({
+ where: {
+ jobId: "j1",
+ isCurrent: true,
+ NOT: { id: "st2" },
+ Job: { userId: user.id },
+ },
+ data: { isCurrent: false },
+ });
+ expect(db.job.update.mock.calls[0][0].data).toEqual({
+ statusId: "s-int",
+ applied: true,
+ });
+ });
+
+ it("leaves the current stage and the job status alone when not set as current", async () => {
+ db.jobStageType.findFirst.mockResolvedValue({
+ id: "t-app",
+ label: "Applied",
+ statusId: "s-app",
+ Status: { value: "applied" },
+ });
+ db.jobStage.create.mockResolvedValue({ id: "st3" });
+ db.jobStage.findFirst.mockResolvedValue({ id: "st3" });
+
+ const res = await addJobStage({
+ jobId: "j1",
+ stageTypeId: "t-app",
+ setAsCurrent: false,
+ } as any);
+
+ expect(res.success).toBe(true);
+ expect(db.jobStage.updateMany).not.toHaveBeenCalled();
+ expect(db.job.update).not.toHaveBeenCalled();
+ });
+
+ it("sets appliedDate from a backdated applied stage", async () => {
+ const backdated = at("2026-08-12T00:00:00Z");
+ db.jobStageType.findFirst.mockResolvedValue({
+ id: "t-app",
+ label: "Applied",
+ statusId: "s-app",
+ Status: { value: "applied" },
+ });
+ db.jobStage.create.mockResolvedValue({ id: "st4" });
+ db.jobStage.findFirst.mockResolvedValue({
+ id: "st4",
+ occurredAt: backdated,
+ StageType: { statusId: "s-app", Status: { value: "applied" } },
+ });
+
+ await addJobStage({
+ jobId: "j1",
+ stageTypeId: "t-app",
+ date: backdated,
+ setAsCurrent: true,
+ } as any);
+
+ expect(db.job.update.mock.calls[0][0].data.appliedDate).toEqual(backdated);
+ });
+});
+
+describe("deleteJobStage", () => {
+ it("promotes the previous stage and rewrites the status when deleting the current one", async () => {
+ db.jobStage.findFirst
+ // assertStageOwned
+ .mockResolvedValueOnce({
+ id: "st-cur",
+ jobId: "j1",
+ occurredAt: null,
+ isCurrent: true,
+ StageType: interviewType,
+ })
+ // syncJobFromCurrentStage's own scoped read
+ .mockResolvedValue({
+ occurredAt: at("2026-09-03T00:00:00Z"),
+ StageType: { statusId: "s-app", Status: { value: "applied" } },
+ });
+ db.jobStage.findMany.mockResolvedValue([
+ { id: "st-prev", occurredAt: at("2026-09-03T00:00:00Z"), createdAt: at("2026-09-03T00:00:00Z") },
+ ]);
+
+ const res = await deleteJobStage("st-cur");
+
+ expect(res.success).toBe(true);
+ expect(db.jobStage.delete).toHaveBeenCalledWith({ where: { id: "st-cur" } });
+ expect(db.jobStage.updateMany).toHaveBeenCalledWith({
+ where: { id: "st-prev", Job: { userId: user.id } },
+ data: { isCurrent: true },
+ });
+ expect(db.job.update.mock.calls[0][0].data.statusId).toBe("s-app");
+ });
+
+ // Undated stages sort last, so the tail of the sorted list is the wrong
+ // successor whenever the job holds a dateless stage.
+ it("promotes the most recent dated stage, not a dateless one", async () => {
+ db.jobStage.findFirst
+ .mockResolvedValueOnce({
+ id: "st-cur",
+ jobId: "j1",
+ occurredAt: at("2026-10-01T00:00:00Z"),
+ isCurrent: true,
+ StageType: interviewType,
+ })
+ .mockResolvedValue({
+ occurredAt: at("2026-09-03T00:00:00Z"),
+ StageType: { statusId: "s-app", Status: { value: "applied" } },
+ });
+ db.jobStage.findMany.mockResolvedValue([
+ { id: "st-applied", occurredAt: at("2026-09-03T00:00:00Z"), createdAt: at("2026-09-03T00:00:00Z") },
+ { id: "st-undated", occurredAt: null, createdAt: at("2026-09-20T00:00:00Z") },
+ ]);
+
+ await deleteJobStage("st-cur");
+
+ expect(db.jobStage.updateMany).toHaveBeenCalledWith({
+ where: { id: "st-applied", Job: { userId: user.id } },
+ data: { isCurrent: true },
+ });
+ });
+
+ it("leaves the status untouched when the deleted stage was the only one", async () => {
+ db.jobStage.findFirst.mockResolvedValueOnce({
+ id: "st-only",
+ jobId: "j1",
+ occurredAt: null,
+ isCurrent: true,
+ StageType: interviewType,
+ });
+ db.jobStage.findMany.mockResolvedValue([]);
+
+ const res = await deleteJobStage("st-only");
+
+ expect(res.success).toBe(true);
+ expect(db.job.update).not.toHaveBeenCalled();
+ });
+
+ it("leaves the status untouched when deleting a non-current stage", async () => {
+ db.jobStage.findFirst.mockResolvedValueOnce({
+ id: "st-old",
+ jobId: "j1",
+ occurredAt: at("2026-09-01T00:00:00Z"),
+ isCurrent: false,
+ StageType: interviewType,
+ });
+
+ const res = await deleteJobStage("st-old");
+
+ expect(res.success).toBe(true);
+ expect(db.jobStage.findMany).not.toHaveBeenCalled();
+ expect(db.job.update).not.toHaveBeenCalled();
+ });
+
+ it("refuses a stage the caller does not own", async () => {
+ db.jobStage.findFirst.mockResolvedValueOnce(null);
+
+ const res = await deleteJobStage("someone-elses");
+
+ expect(res.success).toBe(false);
+ expect(db.jobStage.delete).not.toHaveBeenCalled();
+ });
+});
+
+describe("setCurrentJobStage", () => {
+ it("promotes the stage and rewrites the job status from its type", async () => {
+ db.jobStage.findFirst
+ // assertStageOwned
+ .mockResolvedValueOnce({
+ id: "st9",
+ jobId: "j1",
+ occurredAt: null,
+ isCurrent: false,
+ StageType: interviewType,
+ })
+ // syncJobFromCurrentStage's own scoped read
+ .mockResolvedValue({
+ occurredAt: null,
+ StageType: { statusId: "s-int", Status: { value: "interview" } },
+ });
+
+ const res = await setCurrentJobStage("st9");
+
+ expect(res.success).toBe(true);
+ // D8: every stage write names the owner, so this is updateMany with the
+ // ownership chain, not a bare update by id.
+ expect(db.jobStage.updateMany).toHaveBeenCalledWith({
+ where: { id: "st9", Job: { userId: user.id } },
+ data: { isCurrent: true },
+ });
+ expect(db.job.update.mock.calls[0][0].where).toEqual({
+ id: "j1",
+ userId: user.id,
+ });
+ expect(db.job.update.mock.calls[0][0].data.statusId).toBe("s-int");
+ });
+});
+
+describe("setStageNotes", () => {
+ it("saves a note through both the ownership check and the scoped write", async () => {
+ db.jobStage.findFirst.mockResolvedValueOnce({
+ id: "st1",
+ jobId: "j1",
+ occurredAt: null,
+ isCurrent: true,
+ StageType: interviewType,
+ });
+
+ const res = await setStageNotes("st1", " They pushed hard on caching. ");
+
+ expect(res.success).toBe(true);
+ const call = db.jobStage.updateMany.mock.calls.at(-1)[0];
+ expect(call.where).toEqual({ id: "st1", Job: { userId: user.id } });
+ expect(call.data).toEqual({ notes: "They pushed hard on caching." });
+ });
+
+ it("stores an emptied note as null rather than an empty string", async () => {
+ db.jobStage.findFirst.mockResolvedValueOnce({
+ id: "st1",
+ jobId: "j1",
+ occurredAt: null,
+ isCurrent: true,
+ StageType: interviewType,
+ });
+
+ await setStageNotes("st1", " ");
+
+ expect(db.jobStage.updateMany.mock.calls.at(-1)[0].data).toEqual({
+ notes: null,
+ });
+ });
+});
diff --git a/src/actions/jobStage.actions.ts b/src/actions/jobStage.actions.ts
new file mode 100644
index 0000000..cf0170e
--- /dev/null
+++ b/src/actions/jobStage.actions.ts
@@ -0,0 +1,10 @@
+// Barrel only: each module below carries its own "use server". Explicit named
+// re-exports, because `export *` is not reliable from a "use server" file.
+export { getJobStages } from "./jobStage/queries";
+export {
+ addJobStage,
+ updateJobStage,
+ deleteJobStage,
+ setCurrentJobStage,
+ setStageNotes,
+} from "./jobStage/mutations";
diff --git a/src/actions/jobStage/mutations.ts b/src/actions/jobStage/mutations.ts
new file mode 100644
index 0000000..8291613
--- /dev/null
+++ b/src/actions/jobStage/mutations.ts
@@ -0,0 +1,209 @@
+"use server";
+import prisma from "@/lib/db";
+import { handleError, combineDateAndTime } from "@/lib/utils";
+import { revalidatePath } from "next/cache";
+import { requireUser } from "../shared";
+import { resolveJobStageType } from "@/lib/jobs/resolve";
+import {
+ AddJobStageFormSchema,
+ type AddJobStageValues,
+} from "@/models/jobStage.model";
+import {
+ STAGE_DETAIL_INCLUDE,
+ assertStageOwned,
+ promoteStage,
+ sortStages,
+ syncJobFromCurrentStage,
+} from "./shared";
+
+// Either the picked type (checked for ownership) or a resolved custom name.
+const resolveStageTypeId = async (
+ values: AddJobStageValues,
+ userId: string,
+): Promise<string> => {
+ const custom = values.customLabel?.trim();
+ if (custom) {
+ const resolved = await resolveJobStageType(
+ custom,
+ userId,
+ values.customStatusId!,
+ );
+ return resolved.id;
+ }
+ const owned = await prisma.jobStageType.findFirst({
+ where: { id: values.stageTypeId!, createdBy: userId },
+ select: { id: true },
+ });
+ if (!owned) throw new Error("Stage type not found");
+ return owned.id;
+};
+
+const stageDataFrom = (values: AddJobStageValues, stageTypeId: string) => ({
+ stageTypeId,
+ occurredAt: values.date
+ ? values.time
+ ? combineDateAndTime(values.date, values.time)
+ : values.date
+ : null,
+ outcome: values.outcome ?? null,
+ notes: values.notes?.trim() || null,
+ durationMins: values.durationMins ?? null,
+ format: values.format?.trim() || null,
+ location: values.location?.trim() || null,
+});
+
+export const addJobStage = async (
+ values: AddJobStageValues,
+): Promise<any | undefined> => {
+ try {
+ const user = await requireUser();
+ const parsed = AddJobStageFormSchema.parse(values);
+
+ const job = await prisma.job.count({
+ where: { id: parsed.jobId, userId: user.id },
+ });
+ if (job === 0) throw new Error("Job not found");
+
+ const stageTypeId = await resolveStageTypeId(parsed, user.id);
+
+ const created = await prisma.$transaction(async (tx: any) => {
+ const stage = await tx.jobStage.create({
+ data: { jobId: parsed.jobId, ...stageDataFrom(parsed, stageTypeId) },
+ });
+ if (parsed.setAsCurrent) {
+ await promoteStage(tx, parsed.jobId, stage.id, user.id);
+ await syncJobFromCurrentStage(tx, parsed.jobId, stage.id, user.id);
+ }
+ return tx.jobStage.findFirst({
+ where: { id: stage.id },
+ include: STAGE_DETAIL_INCLUDE,
+ });
+ });
+
+ revalidatePath("/dashboard");
+ return { success: true, data: created };
+ } catch (error) {
+ return handleError(error, "Failed to add stage.");
+ }
+};
+
+export const updateJobStage = async (
+ values: AddJobStageValues,
+): Promise<any | undefined> => {
+ try {
+ const user = await requireUser();
+ if (!values.id) throw new Error("Stage id is required");
+ const parsed = AddJobStageFormSchema.parse(values);
+
+ const existing = await assertStageOwned(values.id, user.id);
+ const stageTypeId = await resolveStageTypeId(parsed, user.id);
+
+ const updated = await prisma.$transaction(async (tx: any) => {
+ await tx.jobStage.update({
+ where: { id: values.id },
+ data: stageDataFrom(parsed, stageTypeId),
+ });
+ if (parsed.setAsCurrent) {
+ await promoteStage(tx, existing.jobId, values.id!, user.id);
+ }
+ // A current stage whose type or date changed re-derives the job's
+ // status, so editing the type of the current stage is a status change.
+ if (parsed.setAsCurrent || existing.isCurrent) {
+ await syncJobFromCurrentStage(tx, existing.jobId, values.id!, user.id);
+ }
+ return tx.jobStage.findFirst({
+ where: { id: values.id },
+ include: STAGE_DETAIL_INCLUDE,
+ });
+ });
+
+ revalidatePath("/dashboard");
+ return { success: true, data: updated };
+ } catch (error) {
+ return handleError(error, "Failed to update stage.");
+ }
+};
+
+export const deleteJobStage = async (
+ stageId: string,
+): Promise<any | undefined> => {
+ try {
+ const user = await requireUser();
+ const stage = await assertStageOwned(stageId, user.id);
+
+ await prisma.$transaction(async (tx: any) => {
+ await tx.jobStage.delete({ where: { id: stageId } });
+ if (!stage.isCurrent) return;
+
+ // Deleting the current stage promotes the most recent DATED stage; with
+ // no stages left the job keeps its status and has no current stage.
+ // Undated stages sort last (D2), so simply taking the tail would promote
+ // a dateless stage over a recent real one and set the wrong status.
+ const remaining = sortStages<{
+ id: string;
+ occurredAt: Date | null;
+ createdAt: Date;
+ }>(
+ await tx.jobStage.findMany({
+ where: { jobId: stage.jobId },
+ select: { id: true, occurredAt: true, createdAt: true },
+ }),
+ );
+ const dated = remaining.filter((s) => s.occurredAt);
+ const successor = dated[dated.length - 1] ?? remaining[remaining.length - 1];
+ if (!successor) return;
+
+ await promoteStage(tx, stage.jobId, successor.id, user.id);
+ await syncJobFromCurrentStage(tx, stage.jobId, successor.id, user.id);
+ });
+
+ revalidatePath("/dashboard");
+ return { success: true };
+ } catch (error) {
+ return handleError(error, "Failed to delete stage.");
+ }
+};
+
+export const setCurrentJobStage = async (
+ stageId: string,
+): Promise<any | undefined> => {
+ try {
+ const user = await requireUser();
+ const stage = await assertStageOwned(stageId, user.id);
+
+ await prisma.$transaction(async (tx: any) => {
+ await promoteStage(tx, stage.jobId, stageId, user.id);
+ await syncJobFromCurrentStage(tx, stage.jobId, stageId, user.id);
+ });
+
+ revalidatePath("/dashboard");
+ return { success: true };
+ } catch (error) {
+ return handleError(error, "Failed to set the current stage.");
+ }
+};
+
+// The detail panel edits notes in place, the way its prep checkboxes already
+// save in place (Task 5.3). Its own action rather than updateJobStage: that
+// one parses the whole Add Stage form, so a notes-only save would have to
+// reconstruct a stage type, a date and a set-as-current flag it is not
+// changing — and re-running promoteStage as a side effect of typing a note.
+export const setStageNotes = async (
+ stageId: string,
+ notes: string,
+): Promise<any | undefined> => {
+ try {
+ const user = await requireUser();
+ await assertStageOwned(stageId, user.id);
+
+ const res = await prisma.jobStage.updateMany({
+ where: { id: stageId, Job: { userId: user.id } },
+ data: { notes: notes.trim() || null },
+ });
+ if (res.count === 0) throw new Error("Stage not found");
+
+ return { success: true };
+ } catch (error) {
+ return handleError(error, "Failed to save the note.");
+ }
+};
diff --git a/src/actions/jobStage/queries.ts b/src/actions/jobStage/queries.ts
new file mode 100644
index 0000000..19e034d
--- /dev/null
+++ b/src/actions/jobStage/queries.ts
@@ -0,0 +1,20 @@
+"use server";
+import prisma from "@/lib/db";
+import { handleError } from "@/lib/utils";
+import { requireUser } from "../shared";
+import { STAGE_DETAIL_INCLUDE, sortStages } from "./shared";
+
+export const getJobStages = async (
+ jobId: string,
+): Promise<any | undefined> => {
+ try {
+ const user = await requireUser();
+ const stages = await prisma.jobStage.findMany({
+ where: { jobId, Job: { userId: user.id } },
+ include: STAGE_DETAIL_INCLUDE,
+ });
+ return sortStages(stages);
+ } catch (error) {
+ return handleError(error, "Failed to fetch job stages. ");
+ }
+};