diff options
Diffstat (limited to 'lib/vendor-evaluation-submit/service.ts')
| -rw-r--r-- | lib/vendor-evaluation-submit/service.ts | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/lib/vendor-evaluation-submit/service.ts b/lib/vendor-evaluation-submit/service.ts index 7be18fb8..3a31b380 100644 --- a/lib/vendor-evaluation-submit/service.ts +++ b/lib/vendor-evaluation-submit/service.ts @@ -17,7 +17,8 @@ import { EsgEvaluationResponse, esgEvaluations, esgAnswerOptions, - esgEvaluationItems + esgEvaluationItems, + periodicEvaluations } from "@/db/schema"; import { and, asc, desc, eq, ilike, or, SQL, count , sql, avg} from "drizzle-orm"; import { filterColumns } from "@/lib/filter-columns"; @@ -84,6 +85,27 @@ export async function getEvaluationSubmissions(input: GetEvaluationsSubmitSchema // 데이터 조회 const { data, total } = await db.transaction(async (tx) => { + + const totalGeneralItemsResult = await tx + .select({ count: count() }) + .from(generalEvaluations) + .where(eq(generalEvaluations.isActive, true)); + + const totalGeneralItemsCount = totalGeneralItemsResult[0]?.count || 0; + + const totalEsgItemsResult = await tx + .select({ count: count() }) + .from(esgEvaluationItems) + .innerJoin(esgEvaluations, eq(esgEvaluationItems.esgEvaluationId, esgEvaluations.id)) + .where( + and( + eq(esgEvaluations.isActive, true), + eq(esgEvaluationItems.isActive, true) + ) + ); + + const totalEGSItemsCount = totalEsgItemsResult[0]?.count || 0; + // 메인 데이터 조회 const data = await tx .select({ @@ -98,9 +120,9 @@ export async function getEvaluationSubmissions(input: GetEvaluationsSubmitSchema reviewedBy: evaluationSubmissions.reviewedBy, reviewComments: evaluationSubmissions.reviewComments, averageEsgScore: evaluationSubmissions.averageEsgScore, - totalGeneralItems: evaluationSubmissions.totalGeneralItems, + completedGeneralItems: evaluationSubmissions.completedGeneralItems, - totalEsgItems: evaluationSubmissions.totalEsgItems, + completedEsgItems: evaluationSubmissions.completedEsgItems, isActive: evaluationSubmissions.isActive, createdAt: evaluationSubmissions.createdAt, @@ -161,6 +183,8 @@ export async function getEvaluationSubmissions(input: GetEvaluationsSubmitSchema return { ...submission, + totalGeneralItems: totalGeneralItemsCount , + totalEsgItems: totalEGSItemsCount, _count: { generalResponses: generalCount, esgResponses: esgCount, @@ -420,6 +444,18 @@ export async function updateEvaluationSubmissionStatus( .where(eq(evaluationSubmissions.id, submissionId)) .returning(); + // newStatus === 'submitted'일 때 periodicEvaluations 테이블도 업데이트 + if (newStatus === 'submitted' && updatedSubmission) { + await tx + .update(periodicEvaluations) + .set({ + documentsSubmitted: true, + submissionDate: new Date(), + updatedAt: new Date(), + }) + .where(eq(periodicEvaluations.id, updatedSubmission.periodicEvaluationId)); + } + return updatedSubmission; }); } |
