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.

Calculate trend for jobsApplied

Khuram Niazff2d925c5318
diff --git a/__tests__/company.actions.spec.ts b/__tests__/company.actions.spec.ts
index 8d7154e..67be402 100644
--- a/__tests__/company.actions.spec.ts
+++ b/__tests__/company.actions.spec.ts
@@ -112,9 +112,7 @@ describe("Company Actions", () => {
select: {
jobsApplied: {
where: {
- Status: {
- value: "applied",
- },
+ applied: true,
},
},
},
diff --git a/src/actions/dashboard.actions.ts b/src/actions/dashboard.actions.ts
index d6aaf4e..9173214 100644
--- a/src/actions/dashboard.actions.ts
+++ b/src/actions/dashboard.actions.ts
@@ -1,5 +1,6 @@
import prisma from "@/lib/db";
import { getCurrentUser } from "@/utils/user.utils";
+import { Prisma } from "@prisma/client";
import { format, subDays } from "date-fns";
export const getJobsAppliedForPeriod = async (
@@ -12,28 +13,49 @@ export const getJobsAppliedForPeriod = async (
}
try {
- const startDate = subDays(new Date(), daysAgo);
+ const startDate1 = subDays(new Date(), daysAgo);
+ const startDate2 = subDays(new Date(), daysAgo * 2);
const endDate = new Date();
-
- const count = await prisma.job.count({
+ const query = (date: Date): Prisma.JobCountArgs => ({
where: {
userId: user.id,
applied: true,
appliedDate: {
- gte: startDate,
+ gte: date,
lt: endDate,
},
},
});
- return count;
+ const [count, count2] = await prisma.$transaction([
+ prisma.job.count(query(startDate1)),
+ prisma.job.count(query(startDate2)),
+ ]);
+ const difference = Math.abs(count2 - count);
+ const trend = calculatePercentageDifference(difference, count);
+ return { count, trend };
} catch (error) {
- const msg = "Failed to job count for this week ";
+ const msg = "Failed to calculate job count";
console.error(msg, error);
throw new Error(msg);
}
};
+const calculatePercentageDifference = (
+ value1: number,
+ value2: number
+): number | null => {
+ if (value1 === 0 && value2 === 0) {
+ return 0;
+ }
+ if (value1 === 0) {
+ return value2 !== 0 ? 100 : 0;
+ }
+
+ const difference = ((value2 - value1) / Math.abs(value1)) * 100;
+ return Math.round(difference);
+};
+
export const getRecentJobs = async (): Promise<any | undefined> => {
try {
const user = await getCurrentUser();
diff --git a/src/app/dashboard/page.tsx b/src/app/dashboard/page.tsx
index b975d14..68f1469 100644
--- a/src/app/dashboard/page.tsx
+++ b/src/app/dashboard/page.tsx
@@ -18,8 +18,8 @@ export const metadata: Metadata = {
export default async function Dashboard() {
const [
- jobsAppliedLast7Days,
- jobsAppliedLast30Days,
+ { count: jobsAppliedLast7Days, trend: trendFor7Days },
+ { count: jobsAppliedLast30Days, trend: trendFor30Days },
recentJobs,
weeklyData,
activityCalendarData,
@@ -38,14 +38,12 @@ export default async function Dashboard() {
<NumberCard
label="Last 7 days"
num={jobsAppliedLast7Days}
- desc="+35%"
- progress={45}
+ trend={trendFor7Days}
/>
<NumberCard
label="Last 30 days"
num={jobsAppliedLast30Days}
- desc="+20%"
- progress={25}
+ trend={trendFor30Days}
/>
</div>
<div className="flex flex-col justify-center">
diff --git a/src/components/dashboard/NumberCard.tsx b/src/components/dashboard/NumberCard.tsx
index 870ce41..c4155c8 100644
--- a/src/components/dashboard/NumberCard.tsx
+++ b/src/components/dashboard/NumberCard.tsx
@@ -7,20 +7,15 @@ import {
CardTitle,
} from "@/components/ui/card";
import { Progress } from "@/components/ui/progress";
+import { TrendingDown, TrendingUp } from "lucide-react";
interface NumberCardProps {
label: string;
num: number;
- desc: string;
- progress: number;
+ trend: number;
}
-export default function NumberCard({
- label,
- num,
- desc,
- progress,
-}: NumberCardProps) {
+export default function NumberCard({ label, num, trend }: NumberCardProps) {
return (
<Card>
<CardHeader className="pb-2">
@@ -28,10 +23,17 @@ export default function NumberCard({
<CardTitle className="text-4xl">{num}</CardTitle>
</CardHeader>
<CardContent>
- <div className="text-xs text-muted-foreground">{desc}</div>
+ <div className="flex gap-2 text-xs text-muted-foreground">
+ {trend}%{" "}
+ {trend > 0 ? (
+ <TrendingUp className="h-4 w-4" />
+ ) : (
+ <TrendingDown className="h-4 w-4" />
+ )}
+ </div>
</CardContent>
<CardFooter>
- <Progress value={progress} aria-label="25% increase" />
+ <Progress value={trend} aria-label={`${trend}% increase`} />
</CardFooter>
</Card>
);