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): link and unlink stage interviewers, upserting the job's interviewer contact link
Khuram Niaz7ebbefa58556
diff --git a/__tests__/jobStageInterviewers.spec.ts b/__tests__/jobStageInterviewers.spec.tsnew file mode 100644index 0000000..ea86ea0--- /dev/null+++ b/__tests__/jobStageInterviewers.spec.ts@@ -0,0 +1,145 @@+import {+ linkStageInterviewer,+ unlinkStageInterviewer,+} from "@/actions/jobStage.actions";+import { getCurrentUser } from "@/utils/user.utils";+import prisma from "@/lib/db";+import { resolveContactRole } from "@/lib/jobs/resolve";++vi.mock("@/lib/db", () => ({+ default: {+ jobStage: { findFirst: vi.fn() },+ contact: { count: vi.fn() },+ jobStageInterviewer: {+ create: vi.fn(),+ findFirst: vi.fn(),+ deleteMany: vi.fn(),+ },+ jobContact: { findFirst: vi.fn(), create: vi.fn() },+ },+}));+vi.mock("@/utils/user.utils", () => ({ getCurrentUser: vi.fn() }));+vi.mock("@/lib/jobs/resolve", () => ({ resolveContactRole: vi.fn() }));++const db = prisma as any;+const user = { id: "user-1" };++const interviewStage = {+ id: "st1",+ jobId: "j1",+ occurredAt: null,+ isCurrent: true,+ StageType: {+ id: "t-int",+ label: "Final / Onsite Interview",+ statusId: "s-int",+ Status: { value: "interview" },+ },+};++beforeEach(() => {+ vi.clearAllMocks();+ (getCurrentUser as any).mockResolvedValue(user);+ (resolveContactRole as any).mockResolvedValue({ id: "r-int", label: "Interviewer" });+ db.jobStage.findFirst.mockResolvedValue(interviewStage);+ db.contact.count.mockResolvedValue(1);+ db.jobContact.findFirst.mockResolvedValue(null);+ db.jobStageInterviewer.create.mockResolvedValue({ id: "link1" });+});++describe("linkStageInterviewer", () => {+ it("refuses a stage the caller does not own", async () => {+ db.jobStage.findFirst.mockResolvedValue(null);++ const res = await linkStageInterviewer("someone-elses-stage", "c1");++ expect(res.success).toBe(false);+ expect(db.jobStageInterviewer.create).not.toHaveBeenCalled();+ });++ // Owning the stage is not permission to attach someone else's contact.+ it("refuses a contact the caller does not own", async () => {+ db.contact.count.mockResolvedValue(0);++ const res = await linkStageInterviewer("st1", "someone-elses-contact");++ expect(res.success).toBe(false);+ expect(db.jobStageInterviewer.create).not.toHaveBeenCalled();+ });++ it("refuses a stage that is not an interview stage", async () => {+ db.jobStage.findFirst.mockResolvedValue({+ ...interviewStage,+ StageType: {+ id: "t-app",+ label: "Applied",+ statusId: "s-app",+ Status: { value: "applied" },+ },+ });++ const res = await linkStageInterviewer("st1", "c1");++ expect(res.success).toBe(false);+ expect(res.message).toContain("not an interview stage");+ expect(db.jobStageInterviewer.create).not.toHaveBeenCalled();+ });++ it("links and upserts a JobContact with the interviewer role", async () => {+ const res = await linkStageInterviewer("st1", "c1");++ expect(res.success).toBe(true);+ expect(db.jobStageInterviewer.create.mock.calls[0][0].data).toEqual({+ stageId: "st1",+ contactId: "c1",+ });+ // Resolve-or-create: the seeded role is renameable and deletable.+ expect(resolveContactRole).toHaveBeenCalledWith("Interviewer", user.id);+ expect(db.jobContact.create.mock.calls[0][0].data).toEqual({+ jobId: "j1",+ contactId: "c1",+ roleId: "r-int",+ });+ });++ it("does not duplicate an existing JobContact for that role", async () => {+ db.jobContact.findFirst.mockResolvedValue({ id: "jc1" });++ await linkStageInterviewer("st1", "c1");++ expect(db.jobContact.create).not.toHaveBeenCalled();+ });++ it("reports a friendly message when the contact is already on the stage", async () => {+ db.jobStageInterviewer.create.mockRejectedValue({ code: "P2002" });++ const res = await linkStageInterviewer("st1", "c1");++ expect(res.success).toBe(false);+ expect(res.message).toContain("already");+ });+});++describe("unlinkStageInterviewer", () => {+ it("deletes through both ownership chains and leaves the JobContact alone", async () => {+ db.jobStageInterviewer.deleteMany.mockResolvedValue({ count: 1 });++ const res = await unlinkStageInterviewer("link1");++ expect(res.success).toBe(true);+ expect(db.jobStageInterviewer.deleteMany.mock.calls[0][0].where).toEqual({+ id: "link1",+ Stage: { Job: { userId: user.id } },+ Contact: { createdBy: user.id },+ });+ expect(db.jobContact.create).not.toHaveBeenCalled();+ });++ it("fails when nothing matched", async () => {+ db.jobStageInterviewer.deleteMany.mockResolvedValue({ count: 0 });++ const res = await unlinkStageInterviewer("nope");++ expect(res.success).toBe(false);+ });+});diff --git a/src/actions/jobStage.actions.ts b/src/actions/jobStage.actions.tsindex cf0170e..74d771c 100644--- a/src/actions/jobStage.actions.ts+++ b/src/actions/jobStage.actions.ts@@ -8,3 +8,7 @@ export {setCurrentJobStage,setStageNotes,} from "./jobStage/mutations";+export {+ linkStageInterviewer,+ unlinkStageInterviewer,+} from "./jobStage/interviewers";diff --git a/src/actions/jobStage/interviewers.ts b/src/actions/jobStage/interviewers.tsnew file mode 100644index 0000000..51d1e09--- /dev/null+++ b/src/actions/jobStage/interviewers.ts@@ -0,0 +1,89 @@+"use server";+import prisma from "@/lib/db";+import { handleError } from "@/lib/utils";+import { requireUser } from "../shared";+import { resolveContactRole } from "@/lib/jobs/resolve";+import { assertInterviewStage, assertStageOwned } from "./shared";++const INTERVIEWER_ROLE_LABEL = "Interviewer";++const INTERVIEWER_INCLUDE = {+ Contact: {+ select: {+ id: true,+ name: true,+ title: true,+ email: true,+ phone: true,+ linkedinUrl: true,+ Company: { select: { id: true, label: true } },+ },+ },+};++export const linkStageInterviewer = async (+ stageId: string,+ contactId: string,+): Promise<any | undefined> => {+ try {+ const user = await requireUser();++ // Two sides, checked independently: JobStageInterviewer has no owner+ // column, so owning the stage is not permission to attach this contact.+ const [stage, contact] = await Promise.all([+ assertStageOwned(stageId, user.id),+ prisma.contact.count({ where: { id: contactId, createdBy: user.id } }),+ ]);+ if (contact === 0) throw new Error("Contact not found");+ assertInterviewStage(stage);++ const data = await prisma.jobStageInterviewer.create({+ data: { stageId, contactId },+ include: INTERVIEWER_INCLUDE,+ });++ // The job's Contacts tab stays the single roster of everyone involved.+ // Resolve-or-create: the seeded role is renameable and deletable.+ const role = await resolveContactRole(INTERVIEWER_ROLE_LABEL, user.id);+ const existing = await prisma.jobContact.findFirst({+ where: { jobId: stage.jobId, contactId, roleId: role.id },+ select: { id: true },+ });+ if (!existing) {+ await prisma.jobContact.create({+ data: { jobId: stage.jobId, contactId, roleId: role.id },+ });+ }++ return { success: true, data };+ } catch (error: any) {+ if (error?.code === "P2002") {+ return {+ success: false,+ message: "That contact is already an interviewer on this stage.",+ };+ }+ return handleError(error, "Failed to link interviewer.");+ }+};++export const unlinkStageInterviewer = async (+ linkId: string,+): Promise<any | undefined> => {+ try {+ const user = await requireUser();+ // The JobContact is deliberately left in place: the person may hold other+ // roles on this job, and unlinking a stage is not unlinking the job.+ const res = await prisma.jobStageInterviewer.deleteMany({+ where: {+ id: linkId,+ Stage: { Job: { userId: user.id } },+ Contact: { createdBy: user.id },+ },+ });+ if (res.count === 0) throw new Error("Interviewer link not found");+ return { success: true };+ } catch (error) {+ return handleError(error, "Failed to unlink interviewer.");+ }+};