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.

Edit resume with file update

Khuram Niaz8899a4b4bce2
diff --git a/src/actions/profile.actions.ts b/src/actions/profile.actions.ts
index f4b193c..99a7ac0 100644
--- a/src/actions/profile.actions.ts
+++ b/src/actions/profile.actions.ts
@@ -12,6 +12,7 @@ import { revalidatePath } from "next/cache";
import { z } from "zod";
import path from "path";
import fs from "fs";
+import { writeFile } from "fs/promises";
export const getResumeList = async (
page = 1,
@@ -233,29 +234,41 @@ const createFileEntry = async (
};
export const editResume = async (
- data: z.infer<typeof CreateResumeFormSchema>
+ id: string,
+ title: string,
+ fileId?: string,
+ fileName?: string,
+ filePath?: string
): Promise<any | undefined> => {
try {
- const user = await getCurrentUser();
+ let resolvedFileId = fileId;
- if (!user) {
- throw new Error("Not authenticated");
+ if (!fileId && fileName && filePath) {
+ resolvedFileId = await createFileEntry(fileName, filePath);
}
- const { id, title } = data;
+ if (resolvedFileId) {
+ const isValidFileId = await prisma.file.findFirst({
+ where: { id: resolvedFileId },
+ });
+
+ if (!isValidFileId) {
+ throw new Error(
+ `The provided FileId "${resolvedFileId}" does not exist.`
+ );
+ }
+ }
const res = await prisma.resume.update({
- where: {
- id,
- },
+ where: { id },
data: {
title,
+ FileId: resolvedFileId || null,
},
});
-
return { success: true, data: res };
} catch (error) {
- const msg = "Failed to update resume.";
+ const msg = "Failed to update resume or file.";
return handleError(error, msg);
}
};
@@ -271,20 +284,7 @@ export const deleteResumeById = async (
throw new Error("Not authenticated");
}
if (fileId) {
- const file = await prisma.file.findFirst({
- where: {
- id: fileId,
- },
- });
-
- const filePath = file?.filePath as string;
-
- const fullFilePath = path.join(filePath);
- if (!fs.existsSync(filePath)) {
- throw new Error("File not found");
- }
- fs.unlinkSync(filePath);
- console.log("file deleted successfully!");
+ await deleteFile(fileId);
}
await prisma.$transaction(async (prisma) => {
@@ -327,14 +327,6 @@ export const deleteResumeById = async (
await prisma.resume.delete({
where: { id: resumeId },
});
-
- if (fileId) {
- await prisma.file.delete({
- where: {
- id: fileId,
- },
- });
- }
});
return { success: true };
} catch (error) {
@@ -343,6 +335,46 @@ export const deleteResumeById = async (
}
};
+export const uploadFile = async (file: File, dir: string, path: string) => {
+ const bytes = await file.arrayBuffer();
+ const buffer = new Uint8Array(bytes);
+
+ if (!fs.existsSync(dir)) {
+ fs.mkdirSync(dir, { recursive: true });
+ }
+
+ await writeFile(path, buffer);
+};
+
+export const deleteFile = async (fileId: string) => {
+ try {
+ const file = await prisma.file.findFirst({
+ where: {
+ id: fileId,
+ },
+ });
+
+ const filePath = file?.filePath as string;
+
+ const fullFilePath = path.join(filePath);
+ if (!fs.existsSync(filePath)) {
+ throw new Error("File not found");
+ }
+ fs.unlinkSync(filePath);
+
+ await prisma.file.delete({
+ where: {
+ id: fileId,
+ },
+ });
+
+ console.log("file deleted successfully!");
+ } catch (error) {
+ const msg = "Failed to delete file.";
+ return handleError(error, msg);
+ }
+};
+
export const addResumeSummary = async (
data: z.infer<typeof AddSummarySectionFormSchema>
): Promise<any | undefined> => {
diff --git a/src/app/api/profile/resume/route.ts b/src/app/api/profile/resume/route.ts
index d429938..7c5f454 100644
--- a/src/app/api/profile/resume/route.ts
+++ b/src/app/api/profile/resume/route.ts
@@ -1,19 +1,22 @@
import { auth } from "@/auth";
import { NextRequest, NextResponse } from "next/server";
import { NextApiRequest, NextApiResponse } from "next";
-import { createResumeProfile } from "@/actions/profile.actions";
+import {
+ createResumeProfile,
+ deleteFile,
+ editResume,
+ uploadFile,
+} from "@/actions/profile.actions";
import path from "path";
import fs from "fs";
-import { writeFile } from "fs/promises";
import { getTimestampedFileName } from "@/lib/utils";
export const POST = async (req: NextRequest, res: NextResponse) => {
const session = await auth();
const userId = session?.accessToken.sub;
+ const dataPath = process.env.NODE_ENV !== "production" ? "data" : "/data";
let filePath;
- // try using same endpoint to edit, use id and fileid to update
-
try {
if (!session || !session.user) {
return NextResponse.json(
@@ -28,25 +31,32 @@ export const POST = async (req: NextRequest, res: NextResponse) => {
const formData = await req.formData();
const title = formData.get("title") as string;
const file = formData.get("file") as File;
-
+ const resumeId = (formData.get("id") as string) ?? null;
+ let fileId: string | undefined =
+ (formData.get("fileId") as string) ?? undefined;
if (file && file.name) {
- const bytes = await file.arrayBuffer();
- const buffer = new Uint8Array(bytes);
-
- const dataPath = process.env.NODE_ENV !== "production" ? "data" : "/data";
-
const uploadDir = path.join(dataPath, "files", "resumes");
-
- if (!fs.existsSync(uploadDir)) {
- fs.mkdirSync(uploadDir, { recursive: true });
- }
-
const timestampedFileName = getTimestampedFileName(file.name);
-
filePath = path.join(uploadDir, timestampedFileName);
+ await uploadFile(file, uploadDir, filePath);
+ }
+
+ if (resumeId && title) {
+ if (fileId && file?.name) {
+ await deleteFile(fileId);
+ fileId = undefined;
+ }
- await writeFile(filePath, buffer);
+ const res = await editResume(
+ resumeId,
+ title,
+ fileId,
+ file?.name,
+ filePath
+ );
+ return NextResponse.json(res, { status: 200 });
}
+
const response = await createResumeProfile(
title,
file.name ?? null,
@@ -58,7 +68,7 @@ export const POST = async (req: NextRequest, res: NextResponse) => {
if (error instanceof Error) {
return NextResponse.json(
{
- error: error.message ?? "File upload failed",
+ error: error.message ?? "Resume update or File upload failed",
},
{
status: 500,
diff --git a/src/components/profile/CreateResume.tsx b/src/components/profile/CreateResume.tsx
index 378d650..2148b3c 100644
--- a/src/components/profile/CreateResume.tsx
+++ b/src/components/profile/CreateResume.tsx
@@ -59,28 +59,29 @@ function CreateResume({
const closeDialog = () => setResumeDialogOpen(false);
useEffect(() => {
- reset(
- {
- id: resumeToEdit?.id ?? undefined,
- title: resumeToEdit?.title ?? "",
- fileId: resumeToEdit?.FileId ?? undefined,
- },
- { keepDefaultValues: true }
- );
+ reset({
+ id: resumeToEdit?.id ?? undefined,
+ title: resumeToEdit?.title ?? "",
+ fileId: resumeToEdit?.FileId ?? undefined,
+ });
}, [resumeToEdit, reset]);
const onSubmit = (data: z.infer<typeof CreateResumeFormSchema>) => {
const formData = new FormData();
formData.append("file", data.file as File);
formData.append("title", data.title);
+ if (resumeToEdit) {
+ formData.append("id", data.id as string);
+ if (resumeToEdit.FileId) {
+ formData.append("fileId", data.fileId as string);
+ }
+ }
startTransition(async () => {
- const res = resumeToEdit
- ? await editResume(data)
- : await fetch("/api/profile/resume", {
- method: "POST",
- body: formData,
- });
+ const res = await fetch("/api/profile/resume", {
+ method: "POST",
+ body: formData,
+ });
const response = await res.json();
if (!response.success) {
toast({
diff --git a/src/components/profile/ProfileContainer.tsx b/src/components/profile/ProfileContainer.tsx
index 18883be..7b21577 100644
--- a/src/components/profile/ProfileContainer.tsx
+++ b/src/components/profile/ProfileContainer.tsx
@@ -65,7 +65,11 @@ function ActivitiesContainer() {
};
const onEditResume = (resume: Resume) => {
- const _resumeToEdit = { id: resume.id, title: resume.title };
+ const _resumeToEdit = {
+ id: resume.id,
+ title: resume.title,
+ FileId: resume.FileId,
+ };
setResumeToEdit(_resumeToEdit);
setResumeDialogOpen(true);
};