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(dashboard): add per-month daily goal values action and helpers

Khuram Niazeec524878a59
diff --git a/__tests__/dailyGoals.actions.spec.ts b/__tests__/dailyGoals.actions.spec.ts
new file mode 100644
index 0000000..7387acb
--- /dev/null
+++ b/__tests__/dailyGoals.actions.spec.ts
@@ -0,0 +1,87 @@
+import { getDailyGoalValues } from "@/actions/dailyGoals.actions";
+import { getCurrentUser } from "@/utils/user.utils";
+import { PrismaClient } from "@prisma/client";
+
+const prisma = new PrismaClient();
+
+vi.mock("@prisma/client", () => {
+ const mPrismaClient = {
+ job: { groupBy: vi.fn() },
+ activity: { findMany: vi.fn() },
+ };
+ return { PrismaClient: vi.fn(function () { return mPrismaClient; }) };
+});
+
+vi.mock("@/utils/user.utils", () => ({
+ getCurrentUser: vi.fn(),
+}));
+
+describe("getDailyGoalValues", () => {
+ const mockUser = { id: "user-id" };
+
+ beforeEach(() => {
+ vi.useFakeTimers();
+ vi.setSystemTime(new Date(2026, 9, 5, 12));
+ (getCurrentUser as any).mockResolvedValue(mockUser);
+ (prisma.job.groupBy as any).mockResolvedValue([]);
+ (prisma.activity.findMany as any).mockResolvedValue([]);
+ });
+
+ afterEach(() => {
+ vi.useRealTimers();
+ });
+
+ it("scopes both queries to the user and the month", async () => {
+ (prisma.job.groupBy as any).mockResolvedValue([
+ { appliedDate: new Date(2026, 8, 2, 9), _count: { _all: 3 } },
+ ]);
+ (prisma.activity.findMany as any).mockResolvedValue([
+ { startTime: new Date(2026, 8, 2, 10), duration: 150 },
+ ]);
+
+ const result = await getDailyGoalValues("2026-09");
+
+ const range = {
+ gte: new Date(2026, 8, 1, 0, 0, 0, 0),
+ lte: new Date(2026, 8, 30, 23, 59, 59, 999),
+ };
+ expect(prisma.job.groupBy).toHaveBeenCalledWith({
+ by: "appliedDate",
+ _count: { _all: true },
+ where: { userId: "user-id", applied: true, appliedDate: range },
+ });
+ expect(prisma.activity.findMany).toHaveBeenCalledWith({
+ where: {
+ userId: "user-id",
+ startTime: range,
+ duration: { not: null },
+ },
+ select: { startTime: true, duration: true },
+ });
+ expect(result).toEqual({
+ success: true,
+ data: { "2026-09-02": { jobs: 3, hours: 2.5 } },
+ });
+ });
+
+ it.each(["2026-13", "abc", "2026-11", "2025-10"])(
+ "rejects %s without querying",
+ async (month) => {
+ const result = await getDailyGoalValues(month);
+ expect(result).toEqual({ success: false, message: "Invalid month." });
+ expect(prisma.job.groupBy).not.toHaveBeenCalled();
+ expect(prisma.activity.findMany).not.toHaveBeenCalled();
+ },
+ );
+
+ it("accepts the oldest browsable month", async () => {
+ const result = await getDailyGoalValues("2025-11");
+ expect(result.success).toBe(true);
+ });
+
+ it("returns a failure when not signed in", async () => {
+ (getCurrentUser as any).mockResolvedValue(null);
+ const result = await getDailyGoalValues("2026-10");
+ expect(result).toEqual({ success: false, message: "Not authenticated" });
+ });
+});
diff --git a/__tests__/dailyGoals.spec.ts b/__tests__/dailyGoals.spec.ts
new file mode 100644
index 0000000..e4df99a
--- /dev/null
+++ b/__tests__/dailyGoals.spec.ts
@@ -0,0 +1,77 @@
+import {
+ buildGoalValues,
+ goalMonthRange,
+ isValidMonth,
+ monthBounds,
+ shiftMonth,
+} from "@/lib/dailyGoals";
+
+describe("dailyGoals helpers", () => {
+ describe("isValidMonth", () => {
+ it.each(["2026-10", "2025-01", "2026-12"])("accepts %s", (m) => {
+ expect(isValidMonth(m)).toBe(true);
+ });
+ it.each(["2026-13", "2026-00", "2026-1", "abc", "", 202610, null])(
+ "rejects %s",
+ (m) => {
+ expect(isValidMonth(m)).toBe(false);
+ },
+ );
+ });
+
+ describe("shiftMonth", () => {
+ it("crosses the year boundary backwards and forwards", () => {
+ expect(shiftMonth("2026-01", -1)).toBe("2025-12");
+ expect(shiftMonth("2025-12", 1)).toBe("2026-01");
+ expect(shiftMonth("2026-10", -11)).toBe("2025-11");
+ });
+ });
+
+ describe("monthBounds", () => {
+ it("spans the first to the last local day of the month", () => {
+ const { start, end } = monthBounds("2024-02");
+ expect(start).toEqual(new Date(2024, 1, 1, 0, 0, 0, 0));
+ expect(end).toEqual(new Date(2024, 1, 29, 23, 59, 59, 999));
+ });
+ });
+
+ describe("goalMonthRange", () => {
+ it("covers the current month and the 11 before it", () => {
+ expect(goalMonthRange("2026-10-05")).toEqual({
+ minMonth: "2025-11",
+ maxMonth: "2026-10",
+ });
+ });
+ });
+
+ describe("buildGoalValues", () => {
+ it("sums jobs per applied day and hours per start day", () => {
+ const result = buildGoalValues(
+ [
+ { appliedDate: new Date(2026, 9, 1, 9), _count: { _all: 2 } },
+ { appliedDate: new Date(2026, 9, 1, 15), _count: { _all: 1 } },
+ { appliedDate: new Date(2026, 9, 2, 9), _count: { _all: 4 } },
+ ],
+ [
+ { startTime: new Date(2026, 9, 1, 10), duration: 90 },
+ { startTime: new Date(2026, 9, 1, 13), duration: 45 },
+ { startTime: new Date(2026, 9, 3, 8), duration: 20 },
+ ],
+ );
+ expect(result).toEqual({
+ "2026-10-01": { jobs: 3, hours: 2.3 },
+ "2026-10-02": { jobs: 4, hours: 0 },
+ "2026-10-03": { jobs: 0, hours: 0.3 },
+ });
+ });
+
+ it("skips undated jobs and unfinished activities", () => {
+ expect(
+ buildGoalValues(
+ [{ appliedDate: null, _count: { _all: 5 } }],
+ [{ startTime: new Date(2026, 9, 1, 10), duration: null }],
+ ),
+ ).toEqual({});
+ });
+ });
+});
diff --git a/src/actions/dailyGoals.actions.ts b/src/actions/dailyGoals.actions.ts
new file mode 100644
index 0000000..10c2f1e
--- /dev/null
+++ b/src/actions/dailyGoals.actions.ts
@@ -0,0 +1,52 @@
+"use server";
+
+import { format } from "date-fns";
+import prisma from "@/lib/db";
+import { handleError } from "@/lib/utils";
+import { requireUser } from "@/actions/shared";
+import {
+ buildGoalValues,
+ goalMonthRange,
+ isValidMonth,
+ monthBounds,
+ type GoalValues,
+} from "@/lib/dailyGoals";
+
+export async function getDailyGoalValues(
+ month: string,
+): Promise<
+ { success: true; data: GoalValues } | { success: false; message: string }
+> {
+ try {
+ const user = await requireUser();
+ const { minMonth, maxMonth } = goalMonthRange(
+ format(new Date(), "yyyy-MM-dd"),
+ );
+ // Client-supplied: only the browsable window is queryable
+ if (!isValidMonth(month) || month < minMonth || month > maxMonth) {
+ return { success: false, message: "Invalid month." };
+ }
+ const { start, end } = monthBounds(month);
+ const range = { gte: start, lte: end };
+ const [jobs, activities] = await Promise.all([
+ prisma.job.groupBy({
+ by: "appliedDate",
+ _count: { _all: true },
+ where: { userId: user.id, applied: true, appliedDate: range },
+ }),
+ prisma.activity.findMany({
+ where: {
+ userId: user.id,
+ startTime: range,
+ duration: { not: null },
+ },
+ select: { startTime: true, duration: true },
+ }),
+ ]);
+ return { success: true, data: buildGoalValues(jobs, activities) };
+ } catch (error) {
+ // handleError types success as boolean; re-wrap to keep the union narrow.
+ const { message } = handleError(error, "Failed to load daily goals.");
+ return { success: false, message };
+ }
+}
diff --git a/src/lib/constants.ts b/src/lib/constants.ts
index 8af0884..2707597 100644
--- a/src/lib/constants.ts
+++ b/src/lib/constants.ts
@@ -448,3 +448,11 @@ export const AI_USAGE_CONSTANTS = {
// only warning available.
CONTEXT_WARN_RATIO: 0.9,
} as const;
+
+// Hard-coded until goals become a user setting
+export const DAILY_GOALS = {
+ JOBS_TARGET: 5,
+ HOURS_TARGET: 4,
+ // Months browsable before the current one
+ MONTHS_BACK: 11,
+} as const;
diff --git a/src/lib/dailyGoals.ts b/src/lib/dailyGoals.ts
new file mode 100644
index 0000000..ba3905b
--- /dev/null
+++ b/src/lib/dailyGoals.ts
@@ -0,0 +1,66 @@
+import { format } from "date-fns";
+import { DAILY_GOALS } from "@/lib/constants";
+
+// Directive-free: imported by the server action and the client calendar.
+
+export type GoalValues = Record<string, Record<string, number>>;
+
+export const GOAL_IDS = { JOBS: "jobs", HOURS: "hours" } as const;
+
+const MONTH_PATTERN = /^\d{4}-(0[1-9]|1[0-2])$/;
+
+export function isValidMonth(month: unknown): month is string {
+ return typeof month === "string" && MONTH_PATTERN.test(month);
+}
+
+export function shiftMonth(month: string, delta: number): string {
+ const [year, monthIndex] = month.split("-").map(Number);
+ return format(new Date(year, monthIndex - 1 + delta, 1), "yyyy-MM");
+}
+
+export function monthBounds(month: string): { start: Date; end: Date } {
+ const [year, monthIndex] = month.split("-").map(Number);
+ return {
+ start: new Date(year, monthIndex - 1, 1, 0, 0, 0, 0),
+ // Day 0 of the next month is the last day of this one
+ end: new Date(year, monthIndex, 0, 23, 59, 59, 999),
+ };
+}
+
+export function goalMonthRange(today: string): {
+ minMonth: string;
+ maxMonth: string;
+} {
+ const maxMonth = today.slice(0, 7);
+ return {
+ minMonth: shiftMonth(maxMonth, -DAILY_GOALS.MONTHS_BACK),
+ maxMonth,
+ };
+}
+
+export function buildGoalValues(
+ jobs: { appliedDate: Date | null; _count: { _all: number } }[],
+ activities: { startTime: Date; duration: number | null }[],
+): GoalValues {
+ const minutes: Record<string, number> = {};
+ const values: GoalValues = {};
+ const day = (key: string) =>
+ (values[key] ??= { [GOAL_IDS.JOBS]: 0, [GOAL_IDS.HOURS]: 0 });
+
+ for (const job of jobs) {
+ if (!job.appliedDate) continue;
+ day(format(job.appliedDate, "yyyy-MM-dd"))[GOAL_IDS.JOBS] +=
+ job._count._all;
+ }
+ for (const activity of activities) {
+ if (activity.duration === null) continue;
+ const key = format(activity.startTime, "yyyy-MM-dd");
+ day(key);
+ minutes[key] = (minutes[key] ?? 0) + activity.duration;
+ }
+ // Round once per day so summed fractions don't drift
+ for (const [key, total] of Object.entries(minutes)) {
+ values[key][GOAL_IDS.HOURS] = Math.round((total / 60) * 10) / 10;
+ }
+ return values;
+}