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.

Export and download jobs list as csv file

Khuram Niaz59df46852073
diff --git a/package-lock.json b/package-lock.json
index 8df578f..ed03a9b 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -45,6 +45,7 @@
"next": "^14.2.18",
"next-auth": "^5.0.0-beta.18",
"next-themes": "^0.3.0",
+ "papaparse": "^5.4.1",
"react": "^18.3.1",
"react-day-picker": "^8.10.1",
"react-dom": "^18.3.1",
@@ -61,6 +62,7 @@
"@testing-library/user-event": "^14.5.2",
"@types/bcrypt": "^5.0.2",
"@types/node": "^20.14.10",
+ "@types/papaparse": "^5.3.15",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"eslint": "^8.57.1",
@@ -4661,6 +4663,16 @@
"form-data": "^4.0.0"
}
},
+ "node_modules/@types/papaparse": {
+ "version": "5.3.15",
+ "resolved": "https://registry.npmjs.org/@types/papaparse/-/papaparse-5.3.15.tgz",
+ "integrity": "sha512-JHe6vF6x/8Z85nCX4yFdDslN11d+1pr12E526X8WAfhadOeaOTx5AuIkvDKIBopfvlzpzkdMx4YyvSKCM9oqtw==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "@types/node": "*"
+ }
+ },
"node_modules/@types/prop-types": {
"version": "15.7.12",
"resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.12.tgz",
@@ -10722,6 +10734,12 @@
"node": ">=6"
}
},
+ "node_modules/papaparse": {
+ "version": "5.4.1",
+ "resolved": "https://registry.npmjs.org/papaparse/-/papaparse-5.4.1.tgz",
+ "integrity": "sha512-HipMsgJkZu8br23pW15uvo6sib6wne/4woLZPlFf3rpDyMe9ywEXUsuD7+6K9PRkJlVT51j/sCOYDKGGS3ZJrw==",
+ "license": "MIT"
+ },
"node_modules/parent-module": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz",
diff --git a/package.json b/package.json
index 90b55dd..16b08a4 100644
--- a/package.json
+++ b/package.json
@@ -50,6 +50,7 @@
"next": "^14.2.18",
"next-auth": "^5.0.0-beta.18",
"next-themes": "^0.3.0",
+ "papaparse": "^5.4.1",
"react": "^18.3.1",
"react-day-picker": "^8.10.1",
"react-dom": "^18.3.1",
@@ -66,6 +67,7 @@
"@testing-library/user-event": "^14.5.2",
"@types/bcrypt": "^5.0.2",
"@types/node": "^20.14.10",
+ "@types/papaparse": "^5.3.15",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"eslint": "^8.57.1",
diff --git a/src/actions/job.actions.ts b/src/actions/job.actions.ts
index 0e11fe1..51829b2 100644
--- a/src/actions/job.actions.ts
+++ b/src/actions/job.actions.ts
@@ -91,6 +91,55 @@ export const getJobsList = async (
}
};
+export async function* getJobsIterator(filter?: string, pageSize = 200) {
+ const user = await getCurrentUser();
+ if (!user) {
+ throw new Error("Not authenticated");
+ }
+ let page = 1;
+ let fetchedCount = 0;
+
+ while (true) {
+ const skip = (page - 1) * pageSize;
+ const filterBy = filter
+ ? filter === Object.keys(JOB_TYPES)[1]
+ ? { status: filter }
+ : { type: filter }
+ : {};
+
+ const chunk = await prisma.job.findMany({
+ where: {
+ userId: user.id,
+ ...filterBy,
+ },
+ select: {
+ id: true,
+ createdAt: true,
+ JobSource: true,
+ JobTitle: true,
+ jobType: true,
+ Company: true,
+ Status: true,
+ Location: true,
+ dueDate: true,
+ applied: true,
+ appliedDate: true,
+ },
+ skip,
+ take: pageSize,
+ orderBy: { createdAt: "desc" },
+ });
+
+ if (!chunk.length) {
+ break;
+ }
+
+ yield chunk;
+ fetchedCount += chunk.length;
+ page++;
+ }
+}
+
export const getJobDetails = async (
jobId: string
): Promise<any | undefined> => {
diff --git a/src/app/api/jobs/export/route.ts b/src/app/api/jobs/export/route.ts
new file mode 100644
index 0000000..04dc2f6
--- /dev/null
+++ b/src/app/api/jobs/export/route.ts
@@ -0,0 +1,106 @@
+import { NextResponse } from "next/server";
+import Papa from "papaparse";
+import { PassThrough } from "node:stream";
+import { getJobsIterator } from "@/actions/job.actions";
+import { format } from "date-fns";
+
+const FIELDS: string[] = [
+ "index",
+ "createdAt",
+ "applied",
+ "appliedDate",
+ "Company",
+ "JobTitle",
+ "jobType",
+ "Location",
+ "JobSource",
+ "Status",
+];
+
+const extractLabel = (field: { label?: string } | undefined): string => {
+ return field?.label || "N/A";
+};
+
+const mapJobType = (type: string | undefined): string => {
+ switch (type) {
+ case "FT":
+ return "Full Time";
+ case "PT":
+ return "Part Time";
+ case "C":
+ return "Contract";
+ default:
+ return "Unknown";
+ }
+};
+
+const transformJobData = (
+ job: Record<string, any>,
+ index: number
+): Record<string, any> => {
+ return {
+ index: index + 1,
+ createdAt: job.createdAt
+ ? format(new Date(job.createdAt), "yyyy-MM-dd")
+ : "N/A",
+ applied: job.applied ? "Yes" : "No",
+ appliedDate: job.appliedDate
+ ? format(new Date(job.appliedDate), "yyyy-MM-dd")
+ : "N/A",
+ Company: extractLabel(job.Company),
+ JobTitle: extractLabel(job.JobTitle),
+ jobType: mapJobType(job.jobType),
+ Location: extractLabel(job.Location),
+ JobSource: extractLabel(job.JobSource),
+ Status: extractLabel(job.Status),
+ };
+};
+
+export const POST = async () => {
+ const passThrough = new PassThrough();
+ let isFirstChunk = true;
+ let hasError = false;
+
+ (async () => {
+ try {
+ let recordIndex = 0;
+ for await (const chunk of getJobsIterator()) {
+ if (hasError) break;
+ const transformedData = chunk.map((job, idx) =>
+ transformJobData(job, recordIndex + idx)
+ );
+ recordIndex += chunk.length;
+ const csv = Papa.unparse(
+ { fields: FIELDS, data: transformedData },
+ {
+ header: isFirstChunk,
+ }
+ );
+ passThrough.write((isFirstChunk ? "" : "\n") + csv);
+ isFirstChunk = false;
+ }
+ } catch (error) {
+ hasError = true;
+ console.error("Error streaming CSV:", error);
+ if (error instanceof Error) {
+ return NextResponse.json(
+ {
+ error: error.message ?? "Jobs download failed",
+ },
+ {
+ status: 500,
+ }
+ );
+ }
+ } finally {
+ passThrough.end();
+ }
+ })();
+
+ return new NextResponse(passThrough as any, {
+ headers: {
+ "Content-Type": "text/csv",
+ "Content-Disposition": 'attachment; filename="jobs.csv"',
+ },
+ });
+};
diff --git a/src/components/myjobs/JobsContainer.tsx b/src/components/myjobs/JobsContainer.tsx
index 95b8bab..1f7fc4e 100644
--- a/src/components/myjobs/JobsContainer.tsx
+++ b/src/components/myjobs/JobsContainer.tsx
@@ -39,6 +39,7 @@ import Loading from "../Loading";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { AddJob } from "./AddJob";
import MyJobsTable from "./MyJobsTable";
+import { format } from "date-fns";
type MyJobsProps = {
statuses: JobStatus[];
@@ -174,6 +175,39 @@ function JobsContainer({
}
};
+ const downloadJobsList = async () => {
+ try {
+ const res = await fetch("/api/jobs/export", {
+ method: "POST",
+ headers: {
+ "Content-Type": "text/csv",
+ },
+ });
+ if (!res.ok) {
+ throw new Error("Failed to download jobs!");
+ }
+ const blob = await res.blob();
+ const url = URL.createObjectURL(blob);
+ const link = document.createElement("a");
+ link.href = url;
+ link.download = `jobsync-${format(new Date(), "yyyy-MM-dd")}.csv`;
+ document.body.appendChild(link);
+ link.click();
+ document.body.removeChild(link);
+ toast({
+ variant: "success",
+ title: "Downloaded successfully!",
+ });
+ } catch (error) {
+ toast({
+ variant: "destructive",
+ title: "Error!",
+ description:
+ error instanceof Error ? error.message : "Unknown error occurred.",
+ });
+ }
+ };
+
return (
<>
<Card x-chunk="dashboard-06-chunk-0">
@@ -199,7 +233,13 @@ function JobsContainer({
</SelectGroup>
</SelectContent>
</Select>
- <Button size="sm" variant="outline" className="h-8 gap-1">
+ <Button
+ size="sm"
+ variant="outline"
+ className="h-8 gap-1"
+ disabled={loading}
+ onClick={downloadJobsList}
+ >
<File className="h-3.5 w-3.5" />
<span className="sr-only sm:not-sr-only sm:whitespace-nowrap">
Export