Spaces:
Runtime error
Runtime error
File size: 5,568 Bytes
1804b24 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | import { Router } from "express";
import { db, projectsTable, clientsTable, tasksTable, timeEntriesTable } from "@workspace/db";
import { eq, sql, gte, and, asc } from "drizzle-orm";
import { GetRecentActivityQueryParams } from "@workspace/api-zod";
const router = Router();
router.get("/dashboard/stats", async (req, res) => {
const [{ activeProjects }] = await db
.select({ activeProjects: sql<number>`cast(count(*) as integer)` })
.from(projectsTable)
.where(
and(
eq(projectsTable.userId, req.user!.id),
sql`${projectsTable.status} NOT IN ('delivered', 'archived')`
)
);
const [{ totalClients }] = await db
.select({ totalClients: sql<number>`cast(count(*) as integer)` })
.from(clientsTable)
.where(eq(clientsTable.userId, req.user!.id));
const [{ pendingTasks }] = await db
.select({ pendingTasks: sql<number>`cast(count(*) as integer)` })
.from(tasksTable)
.where(and(eq(tasksTable.done, false), eq(tasksTable.userId, req.user!.id)));
const now = new Date();
const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1).toISOString();
const [{ hoursThisMonth }] = await db
.select({ hoursThisMonth: sql<number>`coalesce(sum(${timeEntriesTable.hours}), 0)` })
.from(timeEntriesTable)
.where(and(gte(timeEntriesTable.createdAt, new Date(startOfMonth)), eq(timeEntriesTable.userId, req.user!.id)));
const projectsByStatusRaw = await db
.select({
status: projectsTable.status,
count: sql<number>`cast(count(*) as integer)`,
})
.from(projectsTable)
.where(eq(projectsTable.userId, req.user!.id))
.groupBy(projectsTable.status);
const statuses = ["briefing", "concept", "design", "review", "delivered", "archived"];
const projectsByStatus = statuses.map((status) => ({
status,
count: projectsByStatusRaw.find((r) => r.status === status)?.count ?? 0,
}));
const deliveredProjects = await db
.select({ budget: projectsTable.budget })
.from(projectsTable)
.where(
and(
eq(projectsTable.status, "delivered"),
gte(projectsTable.updatedAt, new Date(startOfMonth)),
eq(projectsTable.userId, req.user!.id)
)
);
const revenueThisMonth = deliveredProjects.reduce((sum, p) => sum + (p.budget ?? 0), 0);
res.json({
activeProjects,
totalClients,
hoursThisMonth,
pendingTasks,
projectsByStatus,
revenueThisMonth,
});
});
router.get("/dashboard/activity", async (req, res) => {
const query = GetRecentActivityQueryParams.safeParse(req.query);
const limit = query.success ? (query.data.limit ?? 10) : 10;
const recentProjects = await db
.select({
id: projectsTable.id,
title: projectsTable.title,
status: projectsTable.status,
createdAt: projectsTable.createdAt,
updatedAt: projectsTable.updatedAt,
})
.from(projectsTable)
.where(eq(projectsTable.userId, req.user!.id))
.orderBy(sql`${projectsTable.updatedAt} DESC`)
.limit(5);
const recentTasks = await db
.select({
id: tasksTable.id,
title: tasksTable.title,
done: tasksTable.done,
createdAt: tasksTable.createdAt,
updatedAt: tasksTable.updatedAt,
})
.from(tasksTable)
.where(and(eq(tasksTable.done, true), eq(tasksTable.userId, req.user!.id)))
.orderBy(sql`${tasksTable.updatedAt} DESC`)
.limit(5);
const recentClients = await db
.select({ id: clientsTable.id, name: clientsTable.name, createdAt: clientsTable.createdAt })
.from(clientsTable)
.where(eq(clientsTable.userId, req.user!.id))
.orderBy(sql`${clientsTable.createdAt} DESC`)
.limit(3);
const activities = [
...recentProjects.map((p) => ({
id: `project-${p.id}-${p.updatedAt}`,
type: "project_updated" as const,
description: `Project "${p.title}" moved to ${p.status}`,
entityId: p.id,
createdAt: p.updatedAt.toISOString(),
})),
...recentTasks.map((t) => ({
id: `task-${t.id}-${t.updatedAt}`,
type: "task_completed" as const,
description: `Task completed: "${t.title}"`,
entityId: t.id,
createdAt: (t.updatedAt as Date).toISOString(),
})),
...recentClients.map((c) => ({
id: `client-${c.id}-${c.createdAt}`,
type: "client_added" as const,
description: `New client added: "${c.name}"`,
entityId: c.id,
createdAt: c.createdAt.toISOString(),
})),
]
.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())
.slice(0, limit);
res.json(activities);
});
router.get("/dashboard/deadlines", async (req, res) => {
const projects = await db
.select({
id: projectsTable.id,
title: projectsTable.title,
description: projectsTable.description,
clientId: projectsTable.clientId,
clientName: clientsTable.name,
status: projectsTable.status,
priority: projectsTable.priority,
budget: projectsTable.budget,
deadline: projectsTable.deadline,
createdAt: projectsTable.createdAt,
updatedAt: projectsTable.updatedAt,
})
.from(projectsTable)
.leftJoin(clientsTable, eq(projectsTable.clientId, clientsTable.id))
.where(
and(
sql`${projectsTable.deadline} IS NOT NULL`,
sql`${projectsTable.status} NOT IN ('delivered', 'archived')`,
eq(projectsTable.userId, req.user!.id)
)
)
.orderBy(asc(projectsTable.deadline))
.limit(10);
res.json(projects.map((p) => ({ ...p, totalHours: null })));
});
export default router;
|