|
| 1 | +import { Injectable } from "@nestjs/common"; |
| 2 | +import { PrismaClient } from "@prisma/client"; |
| 3 | + |
| 4 | +const prisma = new PrismaClient(); |
| 5 | + |
| 6 | +@Injectable() |
| 7 | +export class DashboardService { |
| 8 | + async getStats(workspaceId: string) { |
| 9 | + // Get all projects in workspace |
| 10 | + const projects = await prisma.project.findMany({ |
| 11 | + where: { workspaceId, isTrash: false }, |
| 12 | + select: { id: true, name: true }, |
| 13 | + }); |
| 14 | + |
| 15 | + const projectIds = projects.map((p) => p.id); |
| 16 | + |
| 17 | + // Get all active tasks |
| 18 | + const tasks = await prisma.task.findMany({ |
| 19 | + where: { |
| 20 | + isTrash: false, |
| 21 | + projectId: { in: projectIds }, |
| 22 | + }, |
| 23 | + select: { |
| 24 | + id: true, |
| 25 | + status: true, |
| 26 | + priority: true, |
| 27 | + projectId: true, |
| 28 | + dueDate: true, |
| 29 | + createdAt: true, |
| 30 | + finishDate: true, |
| 31 | + taskAssignees: { |
| 32 | + select: { |
| 33 | + memberId: true, |
| 34 | + member: { select: { name: true, photo: true } }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }); |
| 39 | + |
| 40 | + // Get team members |
| 41 | + const team = await prisma.teamMember.findMany({ |
| 42 | + where: { workspaceId, isTrash: false }, |
| 43 | + select: { id: true, name: true, photo: true, role: true }, |
| 44 | + }); |
| 45 | + |
| 46 | + // ── Compute stats ── |
| 47 | + const statusCounts: Record<string, number> = {}; |
| 48 | + const priorityCounts: Record<string, number> = {}; |
| 49 | + const projectTaskCounts: Record<string, number> = {}; |
| 50 | + const memberTaskCounts: Record<string, number> = {}; |
| 51 | + let overdue = 0; |
| 52 | + let dueToday = 0; |
| 53 | + let dueSoon = 0; // within 3 days |
| 54 | + |
| 55 | + const now = new Date(); |
| 56 | + const todayStr = now.toISOString().slice(0, 10); |
| 57 | + const soonDate = new Date(); |
| 58 | + soonDate.setDate(now.getDate() + 3); |
| 59 | + const soonStr = soonDate.toISOString().slice(0, 10); |
| 60 | + |
| 61 | + // Tasks created in last 7 days (for velocity) |
| 62 | + const weekAgo = new Date(); |
| 63 | + weekAgo.setDate(now.getDate() - 7); |
| 64 | + let createdThisWeek = 0; |
| 65 | + let completedThisWeek = 0; |
| 66 | + |
| 67 | + for (const t of tasks) { |
| 68 | + // Status count |
| 69 | + statusCounts[t.status] = (statusCounts[t.status] || 0) + 1; |
| 70 | + |
| 71 | + // Priority count |
| 72 | + const pri = t.priority ?? "none"; |
| 73 | + priorityCounts[pri] = (priorityCounts[pri] || 0) + 1; |
| 74 | + |
| 75 | + // Project count |
| 76 | + if (t.projectId) { |
| 77 | + projectTaskCounts[t.projectId] = |
| 78 | + (projectTaskCounts[t.projectId] || 0) + 1; |
| 79 | + } |
| 80 | + |
| 81 | + // Member workload |
| 82 | + for (const a of t.taskAssignees) { |
| 83 | + memberTaskCounts[a.memberId] = (memberTaskCounts[a.memberId] || 0) + 1; |
| 84 | + } |
| 85 | + |
| 86 | + // Due dates |
| 87 | + if (t.dueDate && t.status !== "done") { |
| 88 | + if (t.dueDate < todayStr) overdue++; |
| 89 | + else if (t.dueDate === todayStr) dueToday++; |
| 90 | + else if (t.dueDate <= soonStr) dueSoon++; |
| 91 | + } |
| 92 | + |
| 93 | + // Velocity |
| 94 | + if (t.createdAt >= weekAgo) createdThisWeek++; |
| 95 | + if (t.status === "done" && t.finishDate && t.finishDate >= weekAgo) { |
| 96 | + completedThisWeek++; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Build structured response |
| 101 | + return { |
| 102 | + totalTasks: tasks.length, |
| 103 | + totalProjects: projects.length, |
| 104 | + totalMembers: team.length, |
| 105 | + statusCounts, |
| 106 | + priorityCounts, |
| 107 | + overdue, |
| 108 | + dueToday, |
| 109 | + dueSoon, |
| 110 | + createdThisWeek, |
| 111 | + completedThisWeek, |
| 112 | + projectStats: projects.map((p) => ({ |
| 113 | + id: p.id, |
| 114 | + name: p.name, |
| 115 | + taskCount: projectTaskCounts[p.id] || 0, |
| 116 | + })), |
| 117 | + memberWorkload: team.map((m) => ({ |
| 118 | + id: m.id, |
| 119 | + name: m.name, |
| 120 | + photo: m.photo, |
| 121 | + role: m.role, |
| 122 | + taskCount: memberTaskCounts[m.id] || 0, |
| 123 | + })), |
| 124 | + }; |
| 125 | + } |
| 126 | +} |
0 commit comments