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
refactor(resumeImport): split actions into separate modules for better organization and maintainability
Khuram Niaz82d59902d367
diff --git a/src/actions/resumeImport.actions.ts b/src/actions/resumeImport.actions.tsindex e532c98..02f3981 100644--- a/src/actions/resumeImport.actions.ts+++ b/src/actions/resumeImport.actions.ts@@ -1,16 +1,12 @@"use server";+// The single resolveImportCard action, split into src/actions/resumeImport/:+// each card type's save logic lives in its own module, with parseImportDate,+// wrapAsHtml and getOrCreateResumeSection shared across them via ./resumeImport/shared.+import prisma from "@/lib/db";import { getCurrentUser } from "@/utils/user.utils";import { handleError } from "@/lib/utils";-import { SectionType } from "@/models/profile.model";-import {- resolveCompany,- resolveJobTitle,- resolveLocation,- resolveTag,-} from "@/lib/jobs/resolve";-import { APP_CONSTANTS } from "@/lib/constants";import {ImportContactInfo,ImportExperience,@@ -18,8 +14,12 @@ import {ImportCertification,ImportSkills,} from "@/models/resumeImport.schema";--// TYPES+import { saveContactInfoCard } from "./resumeImport/contactInfo";+import { saveSummaryCard } from "./resumeImport/summary";+import { saveExperienceCard } from "./resumeImport/experience";+import { saveEducationCard } from "./resumeImport/education";+import { saveCertificationCard } from "./resumeImport/certification";+import { saveSkillsCard } from "./resumeImport/skills";export type ImportCardPayload =| { type: "contactInfo"; data: ImportContactInfo }@@ -33,74 +33,6 @@ export type ResolveResult =| { success: true; status: "saved" }| { success: false; message: string };-// DATE PARSING--function parseImportDate(s: string | undefined | null): Date | null {- if (!s) return null;- const t = s.trim().toLowerCase();- if (["present", "current", "now", "ongoing", "-"].includes(t)) return null;-- // "Jan 2020" / "January 2020"- const monthYear = /^([a-z]+)\s+(\d{4})$/i.exec(s.trim());- if (monthYear) {- const d = new Date(`${monthYear[1]} 1, ${monthYear[2]}`);- if (!isNaN(d.getTime())) return d;- }-- // "2020"- if (/^\d{4}$/.test(s.trim())) {- return new Date(parseInt(s.trim()), 0, 1);- }-- // MM/YYYY or MM-YYYY- const mmyyyy = /^(\d{1,2})[/-](\d{4})$/.exec(s.trim());- if (mmyyyy) {- return new Date(parseInt(mmyyyy[2]), parseInt(mmyyyy[1]) - 1, 1);- }-- const d = new Date(s);- return isNaN(d.getTime()) ? null : d;-}--function escapeHtml(s: string): string {- return s- .replace(/&/g, "&")- .replace(/</g, "<")- .replace(/>/g, ">")- .replace(/"/g, """)- .replace(/'/g, "'");-}--// Wrap plain text in TipTap-compatible <p> paragraphs with HTML escaping-function wrapAsHtml(text: string | undefined): string {- if (!text?.trim()) return "<p></p>";- return text- .split(/\n+/)- .map((line) => `<p>${escapeHtml(line.trim())}</p>`)- .join("");-}--// Find-or-create a ResumeSection by type — shared by experience, education,-// and certification, which (unlike summary) don't need to branch on whether-// a nested child record already exists.-async function getOrCreateResumeSection(- resumeId: string,- sectionType: SectionType,- sectionTitle: string,-): Promise<{ id: string }> {- const existing = await prisma.resumeSection.findFirst({- where: { resumeId, sectionType },- select: { id: true },- });- if (existing) return existing;- return prisma.resumeSection.create({- data: { resumeId, sectionTitle, sectionType },- select: { id: true },- });-}--// MAIN ACTION-export async function resolveImportCard(resumeId: string,card: ImportCardPayload,@@ -117,207 +49,24 @@ export async function resolveImportCard(if (!owned) throw new Error("Resume not found or access denied");switch (card.type) {- case "contactInfo": {- const d = card.data;- await prisma.resume.update({- where: { id: resumeId, profile: { userId: user.id } },- data: {- ContactInfo: {- connectOrCreate: {- where: { resumeId },- create: {- firstName: d.firstName ?? "",- lastName: d.lastName ?? "",- headline: d.headline ?? "",- email: d.email ?? "",- phone: d.phone ?? "",- address: d.address,- },- },- },- },- });+ case "contactInfo":+ await saveContactInfoCard(resumeId, user.id, card.data);break;- }-- case "summary": {- // Look for existing summary section- const existingSection = await prisma.resumeSection.findFirst({- where: { resumeId, sectionType: SectionType.SUMMARY },- select: { id: true, summaryId: true },- });-- if (existingSection) {- // Update existing- if (existingSection.summaryId) {- await prisma.summary.update({- where: { id: existingSection.summaryId },- data: { content: wrapAsHtml(card.data) },- });- }- } else {- // Create new section + summary- const section = await prisma.resumeSection.create({- data: {- resumeId,- sectionTitle: "Summary",- sectionType: SectionType.SUMMARY,- },- });- await prisma.resumeSection.update({- where: { id: section.id },- data: {- summary: { create: { content: wrapAsHtml(card.data) } },- },- });- }+ case "summary":+ await saveSummaryCard(resumeId, card.data);break;- }-- case "experience": {- const d = card.data;-- // Location is required by schema — use a placeholder if none provided- const [company, jobTitle, location] = await Promise.all([- resolveCompany(d.company, user.id),- resolveJobTitle(d.jobTitle, user.id),- resolveLocation(d.location?.trim() || "Not specified", user.id),- ]);-- const startDate = parseImportDate(d.startDate) ?? new Date(2000, 0, 1);- const endDate = parseImportDate(d.endDate);-- const section = await getOrCreateResumeSection(- resumeId,- SectionType.EXPERIENCE,- "Experience",- );-- await prisma.resumeSection.update({- where: { id: section.id, Resume: { profile: { userId: user.id } } },- data: {- workExperiences: {- create: {- companyId: company.id,- jobTitleId: jobTitle.id,- locationId: location.id,- startDate,- endDate: endDate ?? undefined,- description: wrapAsHtml(d.description),- },- },- },- });+ case "experience":+ await saveExperienceCard(resumeId, user.id, card.data);break;- }-- case "education": {- const d = card.data;-- const location = await resolveLocation(d.location?.trim() || "Not specified", user.id);-- const startDate = parseImportDate(d.startDate) ?? new Date(2000, 0, 1);- const endDate = parseImportDate(d.endDate);-- const section = await getOrCreateResumeSection(- resumeId,- SectionType.EDUCATION,- "Education",- );-- await prisma.resumeSection.update({- where: { id: section.id, Resume: { profile: { userId: user.id } } },- data: {- educations: {- create: {- institution: d.institution,- degree: d.degree ?? "",- fieldOfStudy: d.fieldOfStudy ?? "",- locationId: location.id,- startDate,- endDate: endDate ?? undefined,- description: wrapAsHtml(d.description),- },- },- },- });+ case "education":+ await saveEducationCard(resumeId, user.id, card.data);break;- }-- case "certification": {- const d = card.data;-- const section = await getOrCreateResumeSection(- resumeId,- SectionType.CERTIFICATION,- "Certifications",- );-- await prisma.resumeSection.update({- where: { id: section.id, Resume: { profile: { userId: user.id } } },- data: {- licenseOrCertifications: {- create: {- title: d.title,- organization: d.organization ?? "",- issueDate: parseImportDate(d.issueDate) ?? undefined,- expirationDate: parseImportDate(d.expirationDate) ?? undefined,- credentialUrl: d.credentialUrl,- },- },- },- });+ case "certification":+ await saveCertificationCard(resumeId, user.id, card.data);break;- }-- case "skills": {- // Drop empty categories/skills and enforce the same caps as the- // manual skills form before resolving names to shared tags.- const categories = (card.data.categories ?? [])- .map((cat) => ({- label: cat.label?.trim() || null,- skills: (cat.skills ?? [])- .map((s) => s.trim())- .filter(Boolean)- .slice(0, APP_CONSTANTS.MAX_SKILLS_PER_CATEGORY),- }))- .filter((cat) => cat.skills.length > 0)- .slice(0, APP_CONSTANTS.MAX_SKILL_CATEGORIES);-- if (categories.length === 0) {- throw new Error("No skills to import");- }-- const section = await prisma.resumeSection.create({- data: {- resumeId,- sectionTitle: "Skills",- sectionType: SectionType.SKILLS,- },- });-- let order = 0;- const skillRows: {- tagId: string;- category: string | null;- order: number;- resumeSectionId: string;- }[] = [];- for (const cat of categories) {- for (const name of cat.skills) {- const tag = await resolveTag(name, user.id);- skillRows.push({- tagId: tag.id,- category: cat.label,- order: order++,- resumeSectionId: section.id,- });- }- }-- await prisma.skill.createMany({ data: skillRows });+ case "skills":+ await saveSkillsCard(resumeId, user.id, card.data);break;- }}return { success: true, status: "saved" };diff --git a/src/actions/resumeImport/certification.ts b/src/actions/resumeImport/certification.tsnew file mode 100644index 0000000..2bfd4b4--- /dev/null+++ b/src/actions/resumeImport/certification.ts@@ -0,0 +1,31 @@+import prisma from "@/lib/db";+import { SectionType } from "@/models/profile.model";+import type { ImportCertification } from "@/models/resumeImport.schema";+import { getOrCreateResumeSection, parseImportDate } from "./shared";++export async function saveCertificationCard(+ resumeId: string,+ userId: string,+ d: ImportCertification,+): Promise<void> {+ const section = await getOrCreateResumeSection(+ resumeId,+ SectionType.CERTIFICATION,+ "Certifications",+ );++ await prisma.resumeSection.update({+ where: { id: section.id, Resume: { profile: { userId } } },+ data: {+ licenseOrCertifications: {+ create: {+ title: d.title,+ organization: d.organization ?? "",+ issueDate: parseImportDate(d.issueDate) ?? undefined,+ expirationDate: parseImportDate(d.expirationDate) ?? undefined,+ credentialUrl: d.credentialUrl,+ },+ },+ },+ });+}diff --git a/src/actions/resumeImport/contactInfo.ts b/src/actions/resumeImport/contactInfo.tsnew file mode 100644index 0000000..364a4d6--- /dev/null+++ b/src/actions/resumeImport/contactInfo.ts@@ -0,0 +1,27 @@+import prisma from "@/lib/db";+import type { ImportContactInfo } from "@/models/resumeImport.schema";++export async function saveContactInfoCard(+ resumeId: string,+ userId: string,+ d: ImportContactInfo,+): Promise<void> {+ await prisma.resume.update({+ where: { id: resumeId, profile: { userId } },+ data: {+ ContactInfo: {+ connectOrCreate: {+ where: { resumeId },+ create: {+ firstName: d.firstName ?? "",+ lastName: d.lastName ?? "",+ headline: d.headline ?? "",+ email: d.email ?? "",+ phone: d.phone ?? "",+ address: d.address,+ },+ },+ },+ },+ });+}diff --git a/src/actions/resumeImport/education.ts b/src/actions/resumeImport/education.tsnew file mode 100644index 0000000..123f4d0--- /dev/null+++ b/src/actions/resumeImport/education.ts@@ -0,0 +1,42 @@+import prisma from "@/lib/db";+import { SectionType } from "@/models/profile.model";+import { resolveLocation } from "@/lib/jobs/resolve";+import type { ImportEducation } from "@/models/resumeImport.schema";+import { getOrCreateResumeSection, parseImportDate, wrapAsHtml } from "./shared";++export async function saveEducationCard(+ resumeId: string,+ userId: string,+ d: ImportEducation,+): Promise<void> {+ const location = await resolveLocation(+ d.location?.trim() || "Not specified",+ userId,+ );++ const startDate = parseImportDate(d.startDate) ?? new Date(2000, 0, 1);+ const endDate = parseImportDate(d.endDate);++ const section = await getOrCreateResumeSection(+ resumeId,+ SectionType.EDUCATION,+ "Education",+ );++ await prisma.resumeSection.update({+ where: { id: section.id, Resume: { profile: { userId } } },+ data: {+ educations: {+ create: {+ institution: d.institution,+ degree: d.degree ?? "",+ fieldOfStudy: d.fieldOfStudy ?? "",+ locationId: location.id,+ startDate,+ endDate: endDate ?? undefined,+ description: wrapAsHtml(d.description),+ },+ },+ },+ });+}diff --git a/src/actions/resumeImport/experience.ts b/src/actions/resumeImport/experience.tsnew file mode 100644index 0000000..80080e3--- /dev/null+++ b/src/actions/resumeImport/experience.ts@@ -0,0 +1,47 @@+import prisma from "@/lib/db";+import { SectionType } from "@/models/profile.model";+import {+ resolveCompany,+ resolveJobTitle,+ resolveLocation,+} from "@/lib/jobs/resolve";+import type { ImportExperience } from "@/models/resumeImport.schema";+import { getOrCreateResumeSection, parseImportDate, wrapAsHtml } from "./shared";++export async function saveExperienceCard(+ resumeId: string,+ userId: string,+ d: ImportExperience,+): Promise<void> {+ // Location is required by schema — use a placeholder if none provided+ const [company, jobTitle, location] = await Promise.all([+ resolveCompany(d.company, userId),+ resolveJobTitle(d.jobTitle, userId),+ resolveLocation(d.location?.trim() || "Not specified", userId),+ ]);++ const startDate = parseImportDate(d.startDate) ?? new Date(2000, 0, 1);+ const endDate = parseImportDate(d.endDate);++ const section = await getOrCreateResumeSection(+ resumeId,+ SectionType.EXPERIENCE,+ "Experience",+ );++ await prisma.resumeSection.update({+ where: { id: section.id, Resume: { profile: { userId } } },+ data: {+ workExperiences: {+ create: {+ companyId: company.id,+ jobTitleId: jobTitle.id,+ locationId: location.id,+ startDate,+ endDate: endDate ?? undefined,+ description: wrapAsHtml(d.description),+ },+ },+ },+ });+}diff --git a/src/actions/resumeImport/shared.ts b/src/actions/resumeImport/shared.tsnew file mode 100644index 0000000..8f64e9e--- /dev/null+++ b/src/actions/resumeImport/shared.ts@@ -0,0 +1,69 @@+import prisma from "@/lib/db";+import { SectionType } from "@/models/profile.model";++// Not a "use server" module: shared across src/actions/resumeImport/ modules,+// so it must stay importable without becoming a callable server-action endpoint.++export function parseImportDate(s: string | undefined | null): Date | null {+ if (!s) return null;+ const t = s.trim().toLowerCase();+ if (["present", "current", "now", "ongoing", "-"].includes(t)) return null;++ // "Jan 2020" / "January 2020"+ const monthYear = /^([a-z]+)\s+(\d{4})$/i.exec(s.trim());+ if (monthYear) {+ const d = new Date(`${monthYear[1]} 1, ${monthYear[2]}`);+ if (!isNaN(d.getTime())) return d;+ }++ // "2020"+ if (/^\d{4}$/.test(s.trim())) {+ return new Date(parseInt(s.trim()), 0, 1);+ }++ // MM/YYYY or MM-YYYY+ const mmyyyy = /^(\d{1,2})[/-](\d{4})$/.exec(s.trim());+ if (mmyyyy) {+ return new Date(parseInt(mmyyyy[2]), parseInt(mmyyyy[1]) - 1, 1);+ }++ const d = new Date(s);+ return isNaN(d.getTime()) ? null : d;+}++function escapeHtml(s: string): string {+ return s+ .replace(/&/g, "&")+ .replace(/</g, "<")+ .replace(/>/g, ">")+ .replace(/"/g, """)+ .replace(/'/g, "'");+}++// Wrap plain text in TipTap-compatible <p> paragraphs with HTML escaping+export function wrapAsHtml(text: string | undefined): string {+ if (!text?.trim()) return "<p></p>";+ return text+ .split(/\n+/)+ .map((line) => `<p>${escapeHtml(line.trim())}</p>`)+ .join("");+}++// Find-or-create a ResumeSection by type — shared by experience, education,+// and certification, which (unlike summary) don't need to branch on whether+// a nested child record already exists.+export async function getOrCreateResumeSection(+ resumeId: string,+ sectionType: SectionType,+ sectionTitle: string,+): Promise<{ id: string }> {+ const existing = await prisma.resumeSection.findFirst({+ where: { resumeId, sectionType },+ select: { id: true },+ });+ if (existing) return existing;+ return prisma.resumeSection.create({+ data: { resumeId, sectionTitle, sectionType },+ select: { id: true },+ });+}diff --git a/src/actions/resumeImport/skills.ts b/src/actions/resumeImport/skills.tsnew file mode 100644index 0000000..7971d4f--- /dev/null+++ b/src/actions/resumeImport/skills.ts@@ -0,0 +1,57 @@+import prisma from "@/lib/db";+import { SectionType } from "@/models/profile.model";+import { resolveTag } from "@/lib/jobs/resolve";+import { APP_CONSTANTS } from "@/lib/constants";+import type { ImportSkills } from "@/models/resumeImport.schema";++export async function saveSkillsCard(+ resumeId: string,+ userId: string,+ data: ImportSkills,+): Promise<void> {+ // Drop empty categories/skills and enforce the same caps as the+ // manual skills form before resolving names to shared tags.+ const categories = (data.categories ?? [])+ .map((cat) => ({+ label: cat.label?.trim() || null,+ skills: (cat.skills ?? [])+ .map((s) => s.trim())+ .filter(Boolean)+ .slice(0, APP_CONSTANTS.MAX_SKILLS_PER_CATEGORY),+ }))+ .filter((cat) => cat.skills.length > 0)+ .slice(0, APP_CONSTANTS.MAX_SKILL_CATEGORIES);++ if (categories.length === 0) {+ throw new Error("No skills to import");+ }++ const section = await prisma.resumeSection.create({+ data: {+ resumeId,+ sectionTitle: "Skills",+ sectionType: SectionType.SKILLS,+ },+ });++ let order = 0;+ const skillRows: {+ tagId: string;+ category: string | null;+ order: number;+ resumeSectionId: string;+ }[] = [];+ for (const cat of categories) {+ for (const name of cat.skills) {+ const tag = await resolveTag(name, userId);+ skillRows.push({+ tagId: tag.id,+ category: cat.label,+ order: order++,+ resumeSectionId: section.id,+ });+ }+ }++ await prisma.skill.createMany({ data: skillRows });+}diff --git a/src/actions/resumeImport/summary.ts b/src/actions/resumeImport/summary.tsnew file mode 100644index 0000000..9ad1aa0--- /dev/null+++ b/src/actions/resumeImport/summary.ts@@ -0,0 +1,39 @@+import prisma from "@/lib/db";+import { SectionType } from "@/models/profile.model";+import { wrapAsHtml } from "./shared";++export async function saveSummaryCard(+ resumeId: string,+ data: string,+): Promise<void> {+ // Look for existing summary section+ const existingSection = await prisma.resumeSection.findFirst({+ where: { resumeId, sectionType: SectionType.SUMMARY },+ select: { id: true, summaryId: true },+ });++ if (existingSection) {+ // Update existing+ if (existingSection.summaryId) {+ await prisma.summary.update({+ where: { id: existingSection.summaryId },+ data: { content: wrapAsHtml(data) },+ });+ }+ } else {+ // Create new section + summary+ const section = await prisma.resumeSection.create({+ data: {+ resumeId,+ sectionTitle: "Summary",+ sectionType: SectionType.SUMMARY,+ },+ });+ await prisma.resumeSection.update({+ where: { id: section.id },+ data: {+ summary: { create: { content: wrapAsHtml(data) } },+ },+ });+ }+}