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
"use server";
import prisma from "@/lib/db";
import { handleError } from "@/lib/utils";
import { AddQuestionFormSchema } from "@/models/addQuestionForm.schema";
import { requireUser } from "./shared";
import { APP_CONSTANTS } from "@/lib/constants";
import { z } from "zod";
// Questions on one interview round's prep list, owned through the job
function stagePrepLinks(stageId: string, userId: string) {
return { some: { stageId, Stage: { Job: { userId } } } };
}
export const getQuestionsList = async (
page: number = 1,
limit: number = APP_CONSTANTS.RECORDS_PER_PAGE,
filter?: string,
search?: string,
stageId?: string
): Promise<any | undefined> => {
try {
const user = await requireUser();
const offset = (page - 1) * limit;
const whereClause: any = {
createdBy: user.id,
};
if (filter) {
whereClause.tags = { some: { id: filter } };
}
if (stageId) {
whereClause.prepLinks = stagePrepLinks(stageId, user.id);
}
if (search) {
whereClause.OR = [
{ question: { contains: search } },
{ answer: { contains: search } },
];
}
const [data, total] = await Promise.all([
prisma.question.findMany({
where: whereClause,
include: {
tags: true,
},
orderBy: [{ createdAt: "desc" }],
skip: offset,
take: limit,
}),
prisma.question.count({ where: whereClause }),
]);
return { success: true, data, total };
} catch (error) {
return handleError(error, "Failed to fetch questions list.");
}
};
export const getPrepStageLabel = async (
stageId: string
): Promise<any | undefined> => {
try {
const user = await requireUser();
const stage = await prisma.jobStage.findFirst({
where: { id: stageId, Job: { userId: user.id } },
select: {
StageType: { select: { label: true } },
Job: {
select: {
JobTitle: { select: { label: true } },
Company: { select: { label: true } },
},
},
},
});
if (!stage) {
return { success: false, message: "Interview round not found" };
}
return {
success: true,
data: {
round: stage.StageType.label,
jobTitle: stage.Job.JobTitle.label,
company: stage.Job.Company.label,
},
};
} catch (error) {
return handleError(error, "Failed to fetch interview round.");
}
};
export const getQuestionById = async (
questionId: string
): Promise<any | undefined> => {
try {
const user = await requireUser();
const question = await prisma.question.findFirst({
where: { id: questionId, createdBy: user.id },
include: { tags: true },
});
if (!question) {
return { success: false, message: "Question not found" };
}
return { success: true, data: question };
} catch (error) {
return handleError(error, "Failed to fetch question.");
}
};
export const createQuestion = async (
data: z.infer<typeof AddQuestionFormSchema>
): Promise<any | undefined> => {
try {
const user = await requireUser();
const validatedData = AddQuestionFormSchema.parse(data);
const question = await prisma.question.create({
data: {
question: validatedData.question,
answer: validatedData.answer || null,
createdBy: user.id,
tags: validatedData.tagIds?.length
? { connect: validatedData.tagIds.map((id) => ({ id })) }
: undefined,
},
include: { tags: true },
});
return { success: true, data: question };
} catch (error) {
return handleError(error, "Failed to create question.");
}
};
export const updateQuestion = async (
data: z.infer<typeof AddQuestionFormSchema>
): Promise<any | undefined> => {
try {
const user = await requireUser();
if (!data.id) throw new Error("Question ID is required for update");
const validatedData = AddQuestionFormSchema.parse(data);
const question = await prisma.question.update({
where: { id: data.id, createdBy: user.id },
data: {
question: validatedData.question,
answer: validatedData.answer || null,
tags: {
set: validatedData.tagIds?.map((id) => ({ id })) || [],
},
},
include: { tags: true },
});
return { success: true, data: question };
} catch (error) {
return handleError(error, "Failed to update question.");
}
};
export const deleteQuestion = async (
questionId: string
): Promise<any | undefined> => {
try {
const user = await requireUser();
// Silently erasing the record that a question was asked in a real
// interview is worse than a delete that fails with a reason.
const prepRows = await prisma.jobStagePrepQuestion.count({
where: { questionId, Question: { createdBy: user.id } },
});
if (prepRows > 0) {
throw new Error(
`Question cannot be deleted: it is on the prep list of ${prepRows} interview stage${prepRows === 1 ? "" : "s"}. Remove it from those stages first.`,
);
}
await prisma.question.delete({
where: { id: questionId, createdBy: user.id },
});
return { success: true };
} catch (error) {
return handleError(error, "Failed to delete question.");
}
};
export const getTagsWithQuestionCounts = async (
stageId?: string
): Promise<any | undefined> => {
try {
const user = await requireUser();
const prepLinks = stageId ? stagePrepLinks(stageId, user.id) : undefined;
const tags = await prisma.tag.findMany({
where: { createdBy: user.id },
include: {
_count: {
select: { questions: prepLinks ? { where: { prepLinks } } : true },
},
},
orderBy: { label: "asc" },
});
const data = tags
.filter((tag) => tag._count.questions > 0)
.map((tag) => ({
id: tag.id,
label: tag.label,
value: tag.value,
questionCount: tag._count.questions,
}));
const totalQuestions = await prisma.question.count({
where: { createdBy: user.id, ...(prepLinks && { prepLinks }) },
});
return { success: true, data, totalQuestions };
} catch (error) {
return handleError(error, "Failed to fetch tags with question counts.");
}
};