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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
"use server";
import db from "@/db/db";
import { sql, eq, or, and, count, sum, inArray } from "drizzle-orm";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import { getTablesByDomain } from "@/config/dashboard-table";
import { TableConfig } from "@/types/dashboard";
export interface DashboardStats {
tableName: string;
displayName: string;
total: number;
pending: number;
inProgress: number;
completed: number;
}
export interface UserDashboardStats extends DashboardStats {
myTotal: number;
myPending: number;
myInProgress: number;
myCompleted: number;
}
export interface DashboardData {
domain: string;
teamStats: DashboardStats[];
userStats: UserDashboardStats[];
summary: {
totalTasks: number;
myTasks: number;
teamPending: number;
teamInProgress: number;
teamCompleted: number;
myPending: number;
myInProgress: number;
myCompleted: number;
};
}
// 팀 대시보드 데이터 조회
export async function getTeamDashboardData(domain: string): Promise<DashboardStats[]> {
try {
const tables = getTablesByDomain(domain);
if (tables.length === 0) {
console.warn(`도메인 '${domain}'에 대한 테이블이 없습니다.`);
return [];
}
// 병렬 처리로 성능 향상
const results = await Promise.allSettled(
tables.map(tableConfig => getTableStats(tableConfig))
);
// 성공한 결과만 반환, 실패한 것들은 로그 출력
const successfulResults: DashboardStats[] = [];
results.forEach((result, index) => {
if (result.status === 'fulfilled') {
successfulResults.push(result.value);
} else {
console.error(`테이블 ${tables[index].tableName} 통계 조회 실패:`, result.reason);
}
});
console.log('📊 팀 대시보드 결과:', successfulResults);
return successfulResults;
} catch (error) {
console.error("팀 대시보드 데이터 조회 실패:", error);
throw new Error("팀 대시보드 데이터를 불러오는데 실패했습니다.");
}
}
// 사용자 대시보드 데이터 조회
export async function getUserDashboardData(domain: string): Promise<UserDashboardStats[]> {
try {
// 현재 사용자 정보 가져오기
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
throw new Error("인증되지 않은 사용자입니다.");
}
const userId = session.user.id;
const tables = getTablesByDomain(domain);
if (tables.length === 0) {
console.warn(`도메인 '${domain}'에 대한 테이블이 없습니다.`);
return [];
}
console.log(`👤 사용자 ID: ${userId}`);
// 병렬 처리로 성능 향상
const results = await Promise.allSettled(
tables.map(async (tableConfig) => {
const [teamStats, userStats] = await Promise.all([
getTableStats(tableConfig),
getUserTableStats(tableConfig, userId)
]);
return {
...teamStats,
myTotal: userStats.total,
myPending: userStats.pending,
myInProgress: userStats.inProgress,
myCompleted: userStats.completed
} as UserDashboardStats;
})
);
// 성공한 결과만 반환
const successfulResults: UserDashboardStats[] = [];
results.forEach((result, index) => {
if (result.status === 'fulfilled') {
successfulResults.push(result.value);
} else {
console.error(`테이블 ${tables[index].tableName} 사용자 통계 조회 실패:`, result.reason);
}
});
return successfulResults;
} catch (error) {
console.error("사용자 대시보드 데이터 조회 실패:", error);
throw new Error("사용자 대시보드 데이터를 불러오는데 실패했습니다.");
}
}
export async function refreshDashboardData(department: string = "engineering") {
try {
return await getDashboardData(department);
} catch (error) {
console.error("Dashboard refresh error:", error);
throw error;
}
}
// 전체 대시보드 데이터 조회 (팀 + 개인)
export async function getDashboardData(domain: string): Promise<DashboardData> {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
throw new Error("인증되지 않은 사용자입니다.");
}
// 병렬 처리로 성능 향상
const [teamStats, userStats] = await Promise.all([
getTeamDashboardData(domain),
getUserDashboardData(domain)
]);
// 요약 통계 계산
const summary = {
totalTasks: teamStats.reduce((sum, stat) => sum + stat.total, 0),
myTasks: userStats.reduce((sum, stat) => sum + stat.myTotal, 0),
teamPending: teamStats.reduce((sum, stat) => sum + stat.pending, 0),
teamInProgress: teamStats.reduce((sum, stat) => sum + stat.inProgress, 0),
teamCompleted: teamStats.reduce((sum, stat) => sum + stat.completed, 0),
myPending: userStats.reduce((sum, stat) => sum + stat.myPending, 0),
myInProgress: userStats.reduce((sum, stat) => sum + stat.myInProgress, 0),
myCompleted: userStats.reduce((sum, stat) => sum + stat.myCompleted, 0)
};
return {
domain,
teamStats,
userStats,
summary
};
} catch (error) {
console.error("대시보드 데이터 조회 실패:", error);
throw new Error("대시보드 데이터를 불러오는데 실패했습니다.");
}
}
// 테이블별 전체 통계 조회 (완전히 수정된 버전)
async function getTableStats(config: TableConfig): Promise<DashboardStats> {
try {
const totalQuery = `SELECT COUNT(*)::INTEGER as total FROM "${config.tableName}"`;
const totalResult = await db.execute(sql.raw(totalQuery));
const statusQuery = `
SELECT "${config.statusField}" as status, COUNT(*) as count
FROM "${config.tableName}"
WHERE "${config.statusField}" IS NOT NULL
GROUP BY "${config.statusField}"
ORDER BY count DESC
`;
const statusResult = await db.execute(sql.raw(statusQuery));
const pendingValues = Object.entries(config.statusMapping)
.filter(([_, mapped]) => mapped === 'pending')
.map(([original]) => original);
const inProgressValues = Object.entries(config.statusMapping)
.filter(([_, mapped]) => mapped === 'in_progress')
.map(([original]) => original);
const completedValues = Object.entries(config.statusMapping)
.filter(([_, mapped]) => mapped === 'completed')
.map(([original]) => original);
// 개별 쿼리로 정확한 개수 조회
let pendingCount = 0;
let inProgressCount = 0;
let completedCount = 0;
// Pending 개수
if (pendingValues.length > 0) {
const pendingValuesList = pendingValues.map(v => `'${v.replace(/'/g, "''")}'`).join(',');
const pendingQuery = `
SELECT COUNT(*)::INTEGER as count
FROM "${config.tableName}"
WHERE "${config.statusField}" IN (${pendingValuesList})
`;
const pendingResult = await db.execute(sql.raw(pendingQuery));
pendingCount = parseInt(pendingResult.rows[0]?.count || '0');
}
// In Progress 개수
if (inProgressValues.length > 0) {
const inProgressValuesList = inProgressValues.map(v => `'${v.replace(/'/g, "''")}'`).join(',');
const inProgressQuery = `
SELECT COUNT(*)::INTEGER as count
FROM "${config.tableName}"
WHERE "${config.statusField}" IN (${inProgressValuesList})
`;
const inProgressResult = await db.execute(sql.raw(inProgressQuery));
inProgressCount = parseInt(inProgressResult.rows[0]?.count || '0');
}
// Completed 개수
if (completedValues.length > 0) {
const completedValuesList = completedValues.map(v => `'${v.replace(/'/g, "''")}'`).join(',');
const completedQuery = `
SELECT COUNT(*)::INTEGER as count
FROM "${config.tableName}"
WHERE "${config.statusField}" IN (${completedValuesList})
`;
const completedResult = await db.execute(sql.raw(completedQuery));
completedCount = parseInt(completedResult.rows[0]?.count || '0');
}
const stats = {
tableName: config.tableName,
displayName: config.displayName,
total: parseInt(totalResult.rows[0]?.total || '0'),
pending: pendingCount,
inProgress: inProgressCount,
completed: completedCount
};
console.log(`✅ ${config.tableName} 최종 통계:`, stats);
return stats;
} catch (error) {
console.error(`❌ 테이블 ${config.tableName} 통계 조회 중 오류:`, error);
// 에러 발생 시 빈 통계 반환
return createEmptyStats(config);
}
}
// 사용자별 테이블 통계 조회 (수정된 버전)
async function getUserTableStats(config: TableConfig, userId: string): Promise<DashboardStats> {
try {
// 사용자 필드가 없는 경우 빈 통계 반환
if (!hasUserFields(config)) {
console.log(`⚠️ 테이블 ${config.tableName}에 사용자 필드가 없습니다.`);
return createEmptyStats(config);
}
console.log(`\n👤 사용자 ${userId}의 ${config.tableName} 통계 조회`);
// 사용자 조건 생성
const userConditions = [];
if (config.userFields.creator) {
userConditions.push(`"${config.userFields.creator}" = '${userId}'`);
}
if (config.userFields.updater) {
userConditions.push(`"${config.userFields.updater}" = '${userId}'`);
}
if (config.userFields.assignee) {
userConditions.push(`"${config.userFields.assignee}" = '${userId}'`);
}
if (userConditions.length === 0) {
return createEmptyStats(config);
}
const userConditionStr = userConditions.join(' OR ');
// 1. 사용자 총 개수
const userTotalQuery = `
SELECT COUNT(*)::INTEGER as total
FROM "${config.tableName}"
WHERE ${userConditionStr}
`;
const userTotalResult = await db.execute(sql.raw(userTotalQuery));
// 2. 사용자 상태별 개수
const pendingValues = Object.entries(config.statusMapping)
.filter(([_, mapped]) => mapped === 'pending')
.map(([original]) => original);
const inProgressValues = Object.entries(config.statusMapping)
.filter(([_, mapped]) => mapped === 'in_progress')
.map(([original]) => original);
const completedValues = Object.entries(config.statusMapping)
.filter(([_, mapped]) => mapped === 'completed')
.map(([original]) => original);
let userPendingCount = 0;
let userInProgressCount = 0;
let userCompletedCount = 0;
// User Pending 개수
if (pendingValues.length > 0) {
const pendingValuesList = pendingValues.map(v => `'${v.replace(/'/g, "''")}'`).join(',');
const userPendingQuery = `
SELECT COUNT(*)::INTEGER as count
FROM "${config.tableName}"
WHERE (${userConditionStr}) AND "${config.statusField}" IN (${pendingValuesList})
`;
const userPendingResult = await db.execute(sql.raw(userPendingQuery));
userPendingCount = parseInt(userPendingResult.rows[0]?.count || '0');
console.log("User Pending 개수:", userPendingCount);
}
// User In Progress 개수
if (inProgressValues.length > 0) {
const inProgressValuesList = inProgressValues.map(v => `'${v.replace(/'/g, "''")}'`).join(',');
const userInProgressQuery = `
SELECT COUNT(*)::INTEGER as count
FROM "${config.tableName}"
WHERE (${userConditionStr}) AND "${config.statusField}" IN (${inProgressValuesList})
`;
const userInProgressResult = await db.execute(sql.raw(userInProgressQuery));
userInProgressCount = parseInt(userInProgressResult.rows[0]?.count || '0');
}
// User Completed 개수
if (completedValues.length > 0) {
const completedValuesList = completedValues.map(v => `'${v.replace(/'/g, "''")}'`).join(',');
const userCompletedQuery = `
SELECT COUNT(*)::INTEGER as count
FROM "${config.tableName}"
WHERE (${userConditionStr}) AND "${config.statusField}" IN (${completedValuesList})
`;
const userCompletedResult = await db.execute(sql.raw(userCompletedQuery));
userCompletedCount = parseInt(userCompletedResult.rows[0]?.count || '0');
}
const stats = {
tableName: config.tableName,
displayName: config.displayName,
total: parseInt(userTotalResult.rows[0]?.total || '0'),
pending: userPendingCount,
inProgress: userInProgressCount,
completed: userCompletedCount
};
return stats;
} catch (error) {
console.error(`❌ 테이블 ${config.tableName} 사용자 통계 조회 중 오류:`, error);
return createEmptyStats(config);
}
}
// 유틸리티 함수들
function createEmptyStats(config: TableConfig): DashboardStats {
return {
tableName: config.tableName,
displayName: config.displayName,
total: 0,
pending: 0,
inProgress: 0,
completed: 0
};
}
function hasUserFields(config: TableConfig): boolean {
return !!(config.userFields.creator || config.userFields.updater || config.userFields.assignee);
}
// 디버깅 함수: 단순한 테스트
export async function simpleTest(tableName: string, statusField: string) {
try {
// 1. 총 개수
const totalQuery = `SELECT COUNT(*) as total FROM "${tableName}"`;
const totalResult = await db.execute(sql.raw(totalQuery));
// 2. 상태 분포
const statusQuery = `
SELECT "${statusField}" as status, COUNT(*) as count
FROM "${tableName}"
GROUP BY "${statusField}"
ORDER BY count DESC
`;
const statusResult = await db.execute(sql.raw(statusQuery));
console.log("상태 분포:", statusResult.rows);
// 3. 특정 상태 테스트
const draftQuery = `SELECT COUNT(*) as count FROM "${tableName}" WHERE "${statusField}" = 'DRAFT'`;
const draftResult = await db.execute(sql.raw(draftQuery));
console.log("DRAFT 개수:", draftResult.rows[0]);
const docConfirmedQuery = `SELECT COUNT(*) as count FROM "${tableName}" WHERE "${statusField}" = 'Doc. Confirmed'`;
const docConfirmedResult = await db.execute(sql.raw(docConfirmedQuery));
console.log("Doc. Confirmed 개수:", docConfirmedResult.rows[0]);
return {
total: totalResult.rows[0],
statusDistribution: statusResult.rows,
draft: draftResult.rows[0],
docConfirmed: docConfirmedResult.rows[0]
};
} catch (error) {
console.error("간단한 테스트 실패:", error);
return null;
}
}
|