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(profile): add copyResume server action

Khuram Niaz541933637bff
diff --git a/__tests__/resumeCopy.actions.spec.ts b/__tests__/resumeCopy.actions.spec.ts
index 8fead82..0475ae4 100644
--- a/__tests__/resumeCopy.actions.spec.ts
+++ b/__tests__/resumeCopy.actions.spec.ts
@@ -1,4 +1,7 @@
-import { getResumeCopyTitleSuggestion } from "@/actions/profile.actions";
+import {
+ copyResume,
+ getResumeCopyTitleSuggestion,
+} from "@/actions/profile.actions";
import { getCurrentUser } from "@/utils/user.utils";
import { PrismaClient } from "@prisma/client";
@@ -83,3 +86,179 @@ describe("getResumeCopyTitleSuggestion", () => {
expect(result.success).toBe(false);
});
});
+
+describe("copyResume", () => {
+ const source = {
+ id: "resume-1",
+ profileId: "profile-1",
+ title: "Backend Resume",
+ ContactInfo: {
+ id: "contact-1",
+ firstName: "Ada",
+ lastName: "Lovelace",
+ headline: "Engineer",
+ email: "[email protected]",
+ phone: "555",
+ address: "London",
+ url1: "https://a.example",
+ url1Label: "Site",
+ url2: null,
+ url2Label: null,
+ },
+ ResumeSections: [
+ {
+ id: "section-1",
+ sectionTitle: "Summary",
+ sectionType: "summary",
+ summary: { id: "summary-1", content: "<p>Hi</p>" },
+ workExperiences: [],
+ educations: [],
+ licenseOrCertifications: [],
+ others: [],
+ skills: [],
+ },
+ {
+ id: "section-2",
+ sectionTitle: "Experience",
+ sectionType: "experience",
+ summary: null,
+ workExperiences: [
+ {
+ id: "we-1",
+ companyId: "company-1",
+ jobTitleId: "title-1",
+ locationId: "location-1",
+ startDate: new Date("2020-01-01"),
+ endDate: null,
+ description: "<p>Work</p>",
+ },
+ ],
+ educations: [],
+ licenseOrCertifications: [],
+ others: [],
+ skills: [],
+ },
+ ],
+ };
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ (getCurrentUser as any).mockResolvedValue({ id: "user-1" });
+ (prisma.resume.findUnique as any).mockResolvedValue(source);
+ (prisma.resume.findMany as any).mockResolvedValue([
+ { title: "Backend Resume" },
+ ]);
+ (prisma.resume.create as any).mockResolvedValue({ id: "resume-2" });
+ (prisma.resumeSection.create as any)
+ .mockResolvedValueOnce({ id: "new-section-1" })
+ .mockResolvedValueOnce({ id: "new-section-2" });
+ (prisma.$transaction as any).mockImplementation((cb: any) => cb(prisma));
+ });
+
+ it("creates the new resume without file, review data or job links", async () => {
+ const result = await copyResume("resume-1", "Backend Resume (Copy)");
+
+ expect(result.success).toBe(true);
+ expect(result.data).toEqual({
+ id: "resume-2",
+ title: "Backend Resume (Copy)",
+ });
+
+ const createArgs = (prisma.resume.create as any).mock.calls[0][0];
+ expect(createArgs.data).toEqual({
+ profileId: "profile-1",
+ title: "Backend Resume (Copy)",
+ });
+ expect(createArgs.data).not.toHaveProperty("FileId");
+ expect(createArgs.data).not.toHaveProperty("reviewData");
+ });
+
+ it("clones contact info onto the new resume", async () => {
+ await copyResume("resume-1", "Backend Resume (Copy)");
+
+ const args = (prisma.contactInfo.create as any).mock.calls[0][0];
+ expect(args.data).toMatchObject({
+ resumeId: "resume-2",
+ firstName: "Ada",
+ lastName: "Lovelace",
+ email: "[email protected]",
+ url1: "https://a.example",
+ });
+ expect(args.data).not.toHaveProperty("id");
+ });
+
+ it("clones each section, its summary and its work experiences", async () => {
+ await copyResume("resume-1", "Backend Resume (Copy)");
+
+ expect(prisma.resumeSection.create).toHaveBeenCalledTimes(2);
+ const first = (prisma.resumeSection.create as any).mock.calls[0][0];
+ expect(first.data).toMatchObject({
+ Resume: { connect: { id: "resume-2" } },
+ sectionTitle: "Summary",
+ sectionType: "summary",
+ summary: { create: { content: "<p>Hi</p>" } },
+ });
+
+ const we = (prisma.workExperience.createMany as any).mock.calls[0][0];
+ expect(we.data).toEqual([
+ {
+ resumeSectionId: "new-section-2",
+ companyId: "company-1",
+ jobTitleId: "title-1",
+ locationId: "location-1",
+ startDate: new Date("2020-01-01"),
+ endDate: null,
+ description: "<p>Work</p>",
+ },
+ ]);
+ });
+
+ it("runs every write inside one transaction", async () => {
+ await copyResume("resume-1", "Backend Resume (Copy)");
+ expect(prisma.$transaction).toHaveBeenCalledTimes(1);
+ });
+
+ it("uniquifies a user-supplied title that is already taken", async () => {
+ (prisma.resume.findMany as any).mockResolvedValue([
+ { title: "Backend Resume" },
+ { title: "My Copy" },
+ ]);
+
+ const result = await copyResume("resume-1", "My Copy");
+
+ expect(result.data.title).toBe("My Copy (2)");
+ expect((prisma.resume.create as any).mock.calls[0][0].data.title).toBe(
+ "My Copy (2)",
+ );
+ });
+
+ it("rejects a source resume with too few sections", async () => {
+ (prisma.resume.findUnique as any).mockResolvedValue({
+ ...source,
+ ResumeSections: [source.ResumeSections[0]],
+ });
+
+ const result = await copyResume("resume-1", "Backend Resume (Copy)");
+
+ expect(result.success).toBe(false);
+ expect(prisma.$transaction).not.toHaveBeenCalled();
+ });
+
+ it("rejects a resume owned by another user", async () => {
+ (prisma.resume.findUnique as any).mockResolvedValue(null);
+
+ const result = await copyResume("someone-elses-resume", "Copy");
+
+ expect(result.success).toBe(false);
+ expect(prisma.$transaction).not.toHaveBeenCalled();
+ });
+
+ it("rejects when not authenticated", async () => {
+ (getCurrentUser as any).mockResolvedValue(null);
+
+ const result = await copyResume("resume-1", "Copy");
+
+ expect(result.success).toBe(false);
+ expect(prisma.$transaction).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/actions/profile.actions.ts b/src/actions/profile.actions.ts
index ea5534f..74eb1a0 100644
--- a/src/actions/profile.actions.ts
+++ b/src/actions/profile.actions.ts
@@ -50,6 +50,25 @@ const resumeListSelect = {
},
} as const;
+// Full section tree for cloning. Unlike resumeDetailInclude this includes
+// `others` and omits File/Tag joins, which a copy does not need.
+const resumeCopySelect = {
+ id: true,
+ profileId: true,
+ title: true,
+ ContactInfo: true,
+ ResumeSections: {
+ include: {
+ summary: true,
+ workExperiences: true,
+ educations: true,
+ licenseOrCertifications: true,
+ others: true,
+ skills: true,
+ },
+ },
+} as const;
+
export const getResumeList = async (
page: number = 1,
limit: number = APP_CONSTANTS.RECORDS_PER_PAGE,
@@ -498,6 +517,156 @@ export const getResumeCopyTitleSuggestion = async (
}
};
+export const copyResume = async (
+ resumeId: string,
+ title: string,
+): Promise<any | undefined> => {
+ try {
+ const user = await getCurrentUser();
+ if (!user) {
+ throw new Error("Not authenticated");
+ }
+
+ // Read outside the transaction so the SQLite write lock is held briefly
+ const source = await prisma.resume.findUnique({
+ where: { id: resumeId, profile: { userId: user.id } },
+ select: resumeCopySelect,
+ });
+ if (!source) {
+ throw new Error("Resume not found or access denied");
+ }
+
+ if (!hasMinResumeSections(source.ResumeSections.length)) {
+ throw new Error(buildInsufficientSectionsMessage("creating a copy"));
+ }
+
+ const existing = await prisma.resume.findMany({
+ where: { profile: { userId: user.id } },
+ select: { title: true },
+ });
+ const uniqueTitle = ensureUniqueTitle(
+ title,
+ existing.map((r) => r.title),
+ );
+
+ const created = await prisma.$transaction(
+ async (tx) => {
+ const newResume = await tx.resume.create({
+ data: { profileId: source.profileId, title: uniqueTitle },
+ select: { id: true },
+ });
+
+ if (source.ContactInfo) {
+ const c = source.ContactInfo;
+ await tx.contactInfo.create({
+ data: {
+ resumeId: newResume.id,
+ firstName: c.firstName,
+ lastName: c.lastName,
+ headline: c.headline,
+ email: c.email,
+ phone: c.phone,
+ address: c.address,
+ url1: c.url1,
+ url1Label: c.url1Label,
+ url2: c.url2,
+ url2Label: c.url2Label,
+ },
+ });
+ }
+
+ for (const section of source.ResumeSections) {
+ const newSection = await tx.resumeSection.create({
+ data: {
+ // Connect (not resumeId) because Prisma forbids mixing a scalar
+ // FK with the nested summary create below
+ Resume: { connect: { id: newResume.id } },
+ sectionTitle: section.sectionTitle,
+ sectionType: section.sectionType,
+ // Summary is 1:1 via summaryId, so nest it with the section
+ ...(section.summary
+ ? { summary: { create: { content: section.summary.content } } }
+ : {}),
+ },
+ select: { id: true },
+ });
+
+ if (section.workExperiences.length > 0) {
+ await tx.workExperience.createMany({
+ data: section.workExperiences.map((w) => ({
+ resumeSectionId: newSection.id,
+ companyId: w.companyId,
+ jobTitleId: w.jobTitleId,
+ locationId: w.locationId,
+ startDate: w.startDate,
+ endDate: w.endDate,
+ description: w.description,
+ })),
+ });
+ }
+
+ if (section.educations.length > 0) {
+ await tx.education.createMany({
+ data: section.educations.map((e) => ({
+ resumeSectionId: newSection.id,
+ institution: e.institution,
+ degree: e.degree,
+ fieldOfStudy: e.fieldOfStudy,
+ locationId: e.locationId,
+ startDate: e.startDate,
+ endDate: e.endDate,
+ description: e.description,
+ })),
+ });
+ }
+
+ if (section.licenseOrCertifications.length > 0) {
+ await tx.licenseOrCertification.createMany({
+ data: section.licenseOrCertifications.map((l) => ({
+ resumeSectionId: newSection.id,
+ title: l.title,
+ organization: l.organization,
+ issueDate: l.issueDate,
+ expirationDate: l.expirationDate,
+ credentialUrl: l.credentialUrl,
+ })),
+ });
+ }
+
+ if (section.others.length > 0) {
+ await tx.otherSection.createMany({
+ data: section.others.map((o) => ({
+ resumeSectionId: newSection.id,
+ title: o.title,
+ content: o.content,
+ })),
+ });
+ }
+
+ if (section.skills.length > 0) {
+ await tx.skill.createMany({
+ data: section.skills.map((s) => ({
+ resumeSectionId: newSection.id,
+ tagId: s.tagId,
+ category: s.category,
+ order: s.order,
+ })),
+ });
+ }
+ }
+
+ return newResume;
+ },
+ { timeout: 15000 },
+ );
+
+ return { success: true, data: { id: created.id, title: uniqueTitle } };
+ } catch (error) {
+ const msg = "Failed to copy resume.";
+ return handleError(error, msg);
+ }
+};
+
export const deleteResumeById = async (
resumeId: string,
): Promise<any | undefined> => {