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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
|
"use server";
import { revalidateTag, unstable_noStore } from "next/cache";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import db from "@/db/db";
import { qna, qnaAnswer, qnaComments } from "@/db/schema/qna";
import { qnaView, qnaAnswerView, qnaCommentView } from "@/db/schema";
import {
eq,
desc,
asc,
and,
or,
ilike,
inArray,
gte,
lte,
isNull,
isNotNull,
count,
sql
} from "drizzle-orm";
import { unstable_cache } from "@/lib/unstable-cache";
import { filterColumns } from "@/lib/filter-columns";
import type {
GetQnaSchema,
CreateQnaSchema,
UpdateQnaSchema,
CreateAnswerSchema,
UpdateAnswerSchema,
CreateCommentSchema,
UpdateCommentSchema,
} from "./validation";
/* ================================================================
Helper Functions
================================================================ */
/**
* 인증된 사용자 정보 가져오기
*/
async function getAuthenticatedUser() {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
throw new Error("인증이 필요합니다.");
}
return parseInt(session.user.id);
}
/**
* 현재 사용자 ID 가져오기 (비로그인 허용)
*/
async function getCurrentUserId() {
try {
const session = await getServerSession(authOptions);
return session?.user?.id ? parseInt(session.user.id) : null;
} catch {
return null;
}
}
/* ================================================================
Repository Functions (트랜잭션 지원)
================================================================ */
/**
* Q&A 목록 조회 Repository
*/
async function selectQnaList(
tx: any,
options: {
where?: any;
orderBy?: any[];
offset: number;
limit: number;
}
) {
const { where, orderBy = [desc(qnaView.createdAt)], offset, limit } = options;
return await tx
.select()
.from(qnaView)
.where(where)
.orderBy(...orderBy)
.offset(offset)
.limit(limit);
}
/**
* Q&A 총 개수 조회 Repository
*/
async function countQnaList(tx: any, where?: any) {
const [result] = await tx
.select({ count: count() })
.from(qnaView)
.where(where);
return result.count;
}
/**
* Q&A 상세 조회 Repository
*/
async function selectQnaDetail(tx: any, id: number) {
const [question] = await tx
.select()
.from(qnaView)
.where(eq(qnaView.id, id));
return question || null;
}
/**
* 답변 목록 조회 Repository
*/
async function selectAnswersByQnaId(tx: any, qnaId: number) {
return await tx
.select()
.from(qnaAnswerView)
.where(eq(qnaAnswerView.qnaId, qnaId))
.orderBy(desc(qnaAnswerView.createdAt));
}
/**
* 댓글 목록 조회 Repository (계층형)
*/
async function selectCommentsByAnswerId(tx: any, answerId: number) {
return await tx
.select()
.from(qnaCommentView)
.where(eq(qnaCommentView.answerId, answerId))
.orderBy(asc(qnaCommentView.createdAt)); // 댓글은 오래된 순으로
}
/**
* 내가 답변한 질문 ID 목록 조회
*/
async function getMyAnsweredQnaIds(tx: any, userId: number, qnaIds: number[]) {
if (qnaIds.length === 0) return [];
const results = await tx
.select({ qnaId: qnaAnswer.qnaId })
.from(qnaAnswer)
.where(
and(
eq(qnaAnswer.author, userId),
inArray(qnaAnswer.qnaId, qnaIds),
eq(qnaAnswer.isDeleted, false)
)
);
return results.map(r => r.qnaId);
}
/* ================================================================
1) Q&A 목록 조회 (고급 필터링/정렬 지원)
================================================================ */
export async function getQnaList(input: GetQnaSchema) {
return unstable_cache(
async () => {
try {
const offset = (input.page - 1) * input.perPage;
const advancedTable = input.flags.includes("advancedTable") || true;
// 현재 사용자 ID (내 답변 여부 확인용)
const currentUserId = await getCurrentUserId();
// 고급 필터링 WHERE 절 구성
const advancedWhere = advancedTable
? filterColumns({
table: qnaView,
filters: input.filters,
joinOperator: input.joinOperator,
})
: undefined;
// 전역 검색 WHERE 절
let globalWhere;
if (input.search) {
const searchPattern = `%${input.search}%`;
globalWhere = or(
ilike(qnaView.title, searchPattern),
ilike(qnaView.content, searchPattern),
ilike(qnaView.authorName, searchPattern),
ilike(qnaView.companyName, searchPattern)
);
}
// Q&A 특화 필터링
const qnaSpecificWhere = and(
// 도메인 필터
input.authorDomain.length > 0
? inArray(qnaView.authorDomain, input.authorDomain)
: undefined,
// 벤더 타입 필터
input.vendorType.length > 0
? inArray(qnaView.vendorType, input.vendorType)
: undefined,
// 답변 유무 필터 (뷰에서 이미 계산된 값 사용)
input.hasAnswers === "answered"
? eq(qnaView.hasAnswers, true)
: input.hasAnswers === "unanswered"
? eq(qnaView.hasAnswers, false)
: undefined,
// 내 질문만 보기
input.myQuestions === "true" && currentUserId
? eq(qnaView.author, currentUserId)
: undefined,
// 날짜 범위 필터
);
// 최종 WHERE 절 결합
const finalWhere = and(
advancedWhere,
globalWhere,
qnaSpecificWhere
);
// 정렬 설정
const orderBy = input.sort.length > 0
? input.sort.map((item) =>
item.desc
? desc(qnaView[item.id as keyof typeof qnaView])
: asc(qnaView[item.id as keyof typeof qnaView])
)
: [desc(qnaView.lastActivityAt), desc(qnaView.createdAt)]; // 최근 활동순으로 기본 정렬
// 트랜잭션 내에서 데이터 조회
const { data, total } = await db.transaction(async (tx) => {
const data = await selectQnaList(tx, {
where: finalWhere,
orderBy,
offset,
limit: input.perPage,
});
const total = await countQnaList(tx, finalWhere);
// 내가 답변한 질문들 표시 (로그인한 경우만)
let dataWithMyAnswers = data;
if (currentUserId && data.length > 0) {
const qnaIds = data.map(q => q.id);
const myAnsweredIds = await getMyAnsweredQnaIds(tx, currentUserId, qnaIds);
dataWithMyAnswers = data.map(q => ({
...q,
hasMyAnswer: myAnsweredIds.includes(q.id),
isMyQuestion: currentUserId === q.author,
}));
} else {
dataWithMyAnswers = data.map(q => ({
...q,
hasMyAnswer: false,
isMyQuestion: false,
}));
}
return { data: dataWithMyAnswers, total };
});
const pageCount = Math.ceil(total / input.perPage);
return {
data,
pageCount,
total,
// 메타 정보 추가
meta: {
currentPage: input.page,
perPage: input.perPage,
hasNextPage: input.page < pageCount,
hasPrevPage: input.page > 1,
}
};
} catch (err) {
console.error("Q&A 목록 조회 실패:", err);
return {
data: [],
pageCount: 0,
total: 0,
meta: {
currentPage: 1,
perPage: input.perPage,
hasNextPage: false,
hasPrevPage: false,
}
};
}
},
[JSON.stringify(input)], // 캐싱 키
{
revalidate: 1800, // 30분 캐싱
tags: ["qna", "qna-list"],
}
)();
}
/* ================================================================
2) Q&A 상세 조회
================================================================ */
export async function getQnaById(id: number) {
return unstable_cache(
async () => {
try {
const currentUserId = await getCurrentUserId();
return await db.transaction(async (tx) => {
// 질문 정보 조회 (뷰 사용으로 단순화)
const question = await selectQnaDetail(tx, id);
if (!question) return null;
// 답변 목록 조회 (뷰 사용으로 단순화)
const answers = await selectAnswersByQnaId(tx, id);
// 각 답변의 댓글들 조회 및 계층 구조 생성
const answersWithComments = await Promise.all(
answers.map(async (answer) => {
const comments = await selectCommentsByAnswerId(tx, answer.id);
// 댓글 계층 구조 생성 (뷰에서 이미 계층 정보 제공)
const commentMap = new Map();
const rootComments: any[] = [];
// 먼저 모든 댓글을 Map에 저장
comments.forEach(comment => {
commentMap.set(comment.id, {
...comment,
children: [],
isMyComment: currentUserId === comment.author,
});
});
// 계층 구조 생성
comments.forEach(comment => {
const commentWithChildren = commentMap.get(comment.id);
if (comment.parentCommentId) {
const parent = commentMap.get(comment.parentCommentId);
if (parent) {
parent.children.push(commentWithChildren);
}
} else {
rootComments.push(commentWithChildren);
}
});
return {
...answer,
comments: rootComments,
isMyAnswer: currentUserId === answer.author,
};
})
);
return {
...question,
answers: answersWithComments,
isMyQuestion: currentUserId === question.author,
// 뷰에서 이미 계산된 통계 정보 활용
totalInteractions: question.totalAnswers + question.totalComments,
};
});
} catch (err) {
console.error("Q&A 상세 조회 실패:", err);
return null;
}
},
[`qna-detail-${id}`],
{
revalidate: 1800, // 30분 캐싱
tags: ["qna", `qna-${id}`],
}
)();
}
/* ================================================================
3) Q&A 생성/수정/삭제 (CRUD 액션들)
================================================================ */
/**
* 새로운 Q&A 질문 생성
*/
export async function createQna(input: CreateQnaSchema) {
try {
const userId = await getAuthenticatedUser();
const [newQna] = await db.insert(qna).values({
title: input.title,
content: input.content,
category: input.category,
author: userId,
}).returning();
revalidateTag("qna");
revalidateTag("qna-list");
return {
success: true,
data: newQna,
message: "질문이 성공적으로 등록되었습니다."
};
} catch (err) {
console.error("질문 생성 실패:", err);
return {
success: false,
error: err instanceof Error ? err.message : "질문 등록에 실패했습니다.",
data: null
};
}
}
/**
* Q&A 질문 수정
*/
export async function updateQna(id: number, input: UpdateQnaSchema) {
unstable_noStore();
try {
const userId = await getAuthenticatedUser();
// 권한 확인
const [existing] = await db
.select({
author: qna.author,
title: qna.title
})
.from(qna)
.where(eq(qna.id, id));
if (!existing) {
return { success: false, error: "질문을 찾을 수 없습니다." };
}
if (existing.author !== userId) {
return { success: false, error: "수정 권한이 없습니다." };
}
const [updated] = await db
.update(qna)
.set({
...input,
updatedAt: new Date()
})
.where(eq(qna.id, id))
.returning();
revalidateTag("qna");
revalidateTag("qna-list");
revalidateTag(`qna-${id}`);
return {
success: true,
data: updated,
message: "질문이 성공적으로 수정되었습니다."
};
} catch (err) {
console.error("질문 수정 실패:", err);
return {
success: false,
error: err instanceof Error ? err.message : "질문 수정에 실패했습니다."
};
}
}
/**
* Q&A 질문 삭제 (소프트 삭제)
*/
export async function deleteQna(id: number) {
unstable_noStore();
try {
const userId = await getAuthenticatedUser();
// 권한 확인
const [existing] = await db
.select({
author: qna.author,
title: qna.title
})
.from(qna)
.where(eq(qna.id, id));
if (!existing) {
return { success: false, error: "질문을 찾을 수 없습니다." };
}
if (existing.author !== userId) {
return { success: false, error: "삭제 권한이 없습니다." };
}
await db
.update(qna)
.set({
isDeleted: true,
deletedAt: new Date()
})
.where(eq(qna.id, id));
revalidateTag("qna");
revalidateTag("qna-list");
revalidateTag(`qna-${id}`);
return {
success: true,
message: "질문이 성공적으로 삭제되었습니다."
};
} catch (err) {
console.error("질문 삭제 실패:", err);
return {
success: false,
error: err instanceof Error ? err.message : "질문 삭제에 실패했습니다."
};
}
}
/* ================================================================
4) 답변 관련 CRUD 액션들
================================================================ */
/**
* 답변 생성
*/
export async function createAnswer(input: CreateAnswerSchema) {
unstable_noStore();
try {
const userId = await getAuthenticatedUser();
// 질문 존재 여부 확인
const [questionExists] = await db
.select({ id: qna.id })
.from(qna)
.where(and(
eq(qna.id, input.qnaId),
eq(qna.isDeleted, false)
));
if (!questionExists) {
return { success: false, error: "존재하지 않는 질문입니다." };
}
const [newAnswer] = await db.insert(qnaAnswer).values({
qnaId: input.qnaId,
content: input.content,
author: userId,
}).returning();
revalidateTag("qna");
revalidateTag("qna-list");
revalidateTag(`qna-${input.qnaId}`);
return {
success: true,
data: newAnswer,
message: "답변이 성공적으로 등록되었습니다."
};
} catch (error) {
console.error("답변 생성 실패:", error);
return {
success: false,
error: error instanceof Error ? error.message : "답변 등록에 실패했습니다."
};
}
}
/**
* 답변 수정
*/
export async function updateAnswer(id: number, input: UpdateAnswerSchema) {
unstable_noStore();
try {
const userId = await getAuthenticatedUser();
// 권한 확인
const [existing] = await db
.select({
author: qnaAnswer.author,
qnaId: qnaAnswer.qnaId
})
.from(qnaAnswer)
.where(eq(qnaAnswer.id, id));
if (!existing) {
return { success: false, error: "답변을 찾을 수 없습니다." };
}
if (existing.author !== userId) {
return { success: false, error: "수정 권한이 없습니다." };
}
const [updated] = await db
.update(qnaAnswer)
.set({
content: input.content,
updatedAt: new Date()
})
.where(eq(qnaAnswer.id, id))
.returning();
revalidateTag("qna");
revalidateTag("qna-list");
revalidateTag(`qna-${existing.qnaId}`);
return {
success: true,
data: updated,
message: "답변이 성공적으로 수정되었습니다."
};
} catch (err) {
console.error("답변 수정 실패:", err);
return {
success: false,
error: err instanceof Error ? err.message : "답변 수정에 실패했습니다."
};
}
}
/**
* 답변 삭제
*/
export async function deleteAnswer(id: number) {
unstable_noStore();
try {
const userId = await getAuthenticatedUser();
// 권한 확인
const [existing] = await db
.select({
author: qnaAnswer.author,
qnaId: qnaAnswer.qnaId
})
.from(qnaAnswer)
.where(eq(qnaAnswer.id, id));
if (!existing) {
return { success: false, error: "답변을 찾을 수 없습니다." };
}
if (existing.author !== userId) {
return { success: false, error: "삭제 권한이 없습니다." };
}
// 하드 삭제 (답변은 CASCADE로 댓글도 함께 삭제)
await db.delete(qnaAnswer).where(eq(qnaAnswer.id, id));
revalidateTag("qna");
revalidateTag("qna-list");
revalidateTag(`qna-${existing.qnaId}`);
return {
success: true,
message: "답변이 성공적으로 삭제되었습니다."
};
} catch (err) {
console.error("답변 삭제 실패:", err);
return {
success: false,
error: err instanceof Error ? err.message : "답변 삭제에 실패했습니다."
};
}
}
/* ================================================================
5) 댓글 관련 CRUD 액션들
================================================================ */
/**
* 댓글 생성
*/
export async function createComment(input: CreateCommentSchema) {
unstable_noStore();
try {
const userId = await getAuthenticatedUser();
// 답변 존재 여부 확인
const [answerExists] = await db
.select({
id: qnaAnswer.id,
qnaId: qnaAnswer.qnaId
})
.from(qnaAnswer)
.where(and(
eq(qnaAnswer.id, input.answerId),
eq(qnaAnswer.isDeleted, false)
));
if (!answerExists) {
return { success: false, error: "존재하지 않는 답변입니다." };
}
// 부모 댓글 존재 여부 확인 (대댓글인 경우)
if (input.parentCommentId) {
const [parentExists] = await db
.select({ id: qnaComments.id })
.from(qnaComments)
.where(and(
eq(qnaComments.id, input.parentCommentId),
eq(qnaComments.answerId, input.answerId),
eq(qnaComments.isDeleted, false)
));
if (!parentExists) {
return { success: false, error: "존재하지 않는 부모 댓글입니다." };
}
}
const [newComment] = await db.insert(qnaComments).values({
...input,
author: userId,
}).returning();
revalidateTag("qna");
revalidateTag("qna-list");
revalidateTag(`qna-${answerExists.qnaId}`);
return {
success: true,
data: newComment,
message: "댓글이 성공적으로 등록되었습니다."
};
} catch (error) {
console.error("댓글 작성 실패:", error);
return {
success: false,
error: error instanceof Error ? error.message : "댓글 등록에 실패했습니다."
};
}
}
/**
* 댓글 수정
*/
export async function updateComment(id: number, input: UpdateCommentSchema) {
unstable_noStore();
try {
const userId = await getAuthenticatedUser();
// 권한 확인 및 질문 ID 가져오기
const [existing] = await db
.select({
author: qnaComments.author,
answerId: qnaComments.answerId,
qnaId: qnaAnswer.qnaId
})
.from(qnaComments)
.leftJoin(qnaAnswer, eq(qnaComments.answerId, qnaAnswer.id))
.where(eq(qnaComments.id, id));
if (!existing) {
return { success: false, error: "댓글을 찾을 수 없습니다." };
}
if (existing.author !== userId) {
return { success: false, error: "수정 권한이 없습니다." };
}
const [updated] = await db
.update(qnaComments)
.set({
content: input.content,
updatedAt: new Date()
})
.where(eq(qnaComments.id, id))
.returning();
revalidateTag("qna");
revalidateTag("qna-list");
revalidateTag(`qna-${existing.qnaId}`);
return {
success: true,
data: updated,
message: "댓글이 성공적으로 수정되었습니다."
};
} catch (error) {
console.error("댓글 수정 실패:", error);
return {
success: false,
error: error instanceof Error ? error.message : "댓글 수정에 실패했습니다."
};
}
}
/**
* 댓글 삭제
*/
export async function deleteComment(id: number) {
unstable_noStore();
try {
const userId = await getAuthenticatedUser();
// 권한 확인 및 질문 ID 가져오기
const [existing] = await db
.select({
author: qnaComments.author,
answerId: qnaComments.answerId,
qnaId: qnaAnswer.qnaId
})
.from(qnaComments)
.leftJoin(qnaAnswer, eq(qnaComments.answerId, qnaAnswer.id))
.where(eq(qnaComments.id, id));
if (!existing) {
return { success: false, error: "댓글을 찾을 수 없습니다." };
}
if (existing.author !== userId) {
return { success: false, error: "삭제 권한이 없습니다." };
}
// 하드 삭제 (대댓글도 CASCADE로 함께 삭제)
await db.delete(qnaComments).where(eq(qnaComments.id, id));
revalidateTag("qna");
revalidateTag("qna-list");
revalidateTag(`qna-${existing.qnaId}`);
return {
success: true,
message: "댓글이 성공적으로 삭제되었습니다."
};
} catch (error) {
console.error("댓글 삭제 실패:", error);
return {
success: false,
error: error instanceof Error ? error.message : "댓글 삭제에 실패했습니다."
};
}
}
/* ================================================================
6) 답변별 댓글 목록 조회
================================================================ */
export async function getCommentsByAnswerId(answerId: number) {
return unstable_cache(
async () => {
try {
const currentUserId = await getCurrentUserId();
return await db.transaction(async (tx) => {
const comments = await selectCommentsByAnswerId(tx, answerId);
// 댓글 계층 구조 생성
const commentMap = new Map();
const rootComments: any[] = [];
// 모든 댓글을 Map에 저장
comments.forEach(comment => {
commentMap.set(comment.id, {
...comment,
children: [],
isMyComment: currentUserId === comment.author,
});
});
// 계층 구조 생성
comments.forEach(comment => {
const commentWithChildren = commentMap.get(comment.id);
if (comment.parentCommentId) {
const parent = commentMap.get(comment.parentCommentId);
if (parent) {
parent.children.push(commentWithChildren);
}
} else {
rootComments.push(commentWithChildren);
}
});
return rootComments;
});
} catch (error) {
console.error("댓글 조회 실패:", error);
return [];
}
},
[`comments-${answerId}`],
{
revalidate: 1800, // 30분 캐싱
tags: ["qna", `comments-${answerId}`],
}
)();
}
/* ================================================================
7) 통계 및 메타 정보 조회
================================================================ */
/**
* Q&A 대시보드 통계 조회
*/
export async function getQnaStats() {
return unstable_cache(
async () => {
try {
// 뷰를 사용하여 간단하게 통계 조회
const [stats] = await db
.select({
totalQuestions: count(),
answeredQuestions: sql<number>`COUNT(CASE WHEN ${qnaView.hasAnswers} = true THEN 1 END)`,
unansweredQuestions: sql<number>`COUNT(CASE WHEN ${qnaView.hasAnswers} = false THEN 1 END)`,
popularQuestions: sql<number>`COUNT(CASE WHEN ${qnaView.isPopular} = true THEN 1 END)`,
totalAnswers: sql<number>`SUM(${qnaView.totalAnswers})`,
totalComments: sql<number>`SUM(${qnaView.totalComments})`,
})
.from(qnaView);
// 최근 활동 통계
const [recentStats] = await db
.select({
questionsThisWeek: sql<number>`COUNT(CASE WHEN ${qnaView.createdAt} >= NOW() - INTERVAL '7 days' THEN 1 END)`,
questionsThisMonth: sql<number>`COUNT(CASE WHEN ${qnaView.createdAt} >= NOW() - INTERVAL '30 days' THEN 1 END)`,
activeQuestionsThisWeek: sql<number>`COUNT(CASE WHEN ${qnaView.lastActivityAt} >= NOW() - INTERVAL '7 days' THEN 1 END)`,
})
.from(qnaView);
return {
...stats,
...recentStats,
// 추가 계산 통계
answerRate: stats.totalQuestions > 0
? Math.round((stats.answeredQuestions / stats.totalQuestions) * 100)
: 0,
avgAnswersPerQuestion: stats.totalQuestions > 0
? Math.round((stats.totalAnswers || 0) / stats.totalQuestions * 100) / 100
: 0,
};
} catch (err) {
console.error("Q&A 통계 조회 실패:", err);
return {
totalQuestions: 0,
answeredQuestions: 0,
unansweredQuestions: 0,
popularQuestions: 0,
totalAnswers: 0,
totalComments: 0,
questionsThisWeek: 0,
questionsThisMonth: 0,
activeQuestionsThisWeek: 0,
answerRate: 0,
avgAnswersPerQuestion: 0,
};
}
},
["qna-stats"],
{
revalidate: 3600, // 1시간 캐싱
tags: ["qna", "qna-stats"],
}
)();
}
/**
* 사용자별 Q&A 활동 통계
*/
export async function getMyQnaActivity() {
try {
const userId = await getAuthenticatedUser();
return unstable_cache(
async () => {
const [myStats] = await db
.select({
myQuestions: count(),
myAnsweredQuestions: sql<number>`COUNT(CASE WHEN ${qnaView.hasAnswers} = true THEN 1 END)`,
})
.from(qnaView)
.where(eq(qnaView.author, userId));
const [myAnswers] = await db
.select({
totalAnswers: count(),
})
.from(qnaAnswerView)
.where(eq(qnaAnswerView.author, userId));
const [myComments] = await db
.select({
totalComments: count(),
})
.from(qnaCommentView)
.where(eq(qnaCommentView.author, userId));
return {
...myStats,
...myAnswers,
...myComments,
};
},
[`my-qna-activity-${userId}`],
{
revalidate: 1800, // 30분 캐싱
tags: ["qna", `user-activity-${userId}`],
}
)();
} catch (err) {
return {
myQuestions: 0,
myAnsweredQuestions: 0,
totalAnswers: 0,
totalComments: 0,
};
}
}
|