summaryrefslogtreecommitdiff
path: root/lib/vendor-document-list/enhanced-document-service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/vendor-document-list/enhanced-document-service.ts')
-rw-r--r--lib/vendor-document-list/enhanced-document-service.ts133
1 files changed, 133 insertions, 0 deletions
diff --git a/lib/vendor-document-list/enhanced-document-service.ts b/lib/vendor-document-list/enhanced-document-service.ts
index f2d9c26f..7464b13f 100644
--- a/lib/vendor-document-list/enhanced-document-service.ts
+++ b/lib/vendor-document-list/enhanced-document-service.ts
@@ -1173,6 +1173,139 @@ export async function getDocumentDetails(documentId: number) {
}
}
+ export async function getUserVendorDocumentsAll(
+ userId: number,
+ input: GetVendorShipDcoumentsSchema
+ ) {
+ try {
+
+ const session = await getServerSession(authOptions)
+ if (!session?.user?.id) {
+ throw new Error("인증이 필요합니다.")
+ }
+
+
+
+ const offset = (input.page - 1) * input.perPage
+
+
+
+ // 3. 고급 필터 처리
+ const advancedWhere = filterColumns({
+ table: simplifiedDocumentsView,
+ filters: input.filters || [],
+ joinOperator: input.joinOperator || "and",
+ })
+
+ // 4. 전역 검색 처리
+ let globalWhere
+ if (input.search) {
+ const searchTerm = `%${input.search}%`
+ globalWhere = or(
+ ilike(simplifiedDocumentsView.title, searchTerm),
+ ilike(simplifiedDocumentsView.docNumber, searchTerm),
+ ilike(simplifiedDocumentsView.vendorDocNumber, searchTerm),
+ )
+ }
+
+ // 5. 최종 WHERE 조건 (계약 ID들로 필터링)
+ const finalWhere = and(
+ advancedWhere,
+ globalWhere,
+ )
+
+ // 6. 정렬 처리
+ const orderBy = input.sort && input.sort.length > 0
+ ? input.sort.map((item) =>
+ item.desc
+ ? desc(simplifiedDocumentsView[item.id])
+ : asc(simplifiedDocumentsView[item.id])
+ )
+ : [desc(simplifiedDocumentsView.createdAt)]
+
+ // 7. 트랜잭션 실행
+ const { data, total, drawingKind, vendorInfo } = await db.transaction(async (tx) => {
+ // 데이터 조회
+ const data = await tx
+ .select()
+ .from(simplifiedDocumentsView)
+ .where(finalWhere)
+ .orderBy(...orderBy)
+ .limit(input.perPage)
+ .offset(offset)
+
+ // 총 개수 조회
+ const [{ total }] = await tx
+ .select({
+ total: count()
+ })
+ .from(simplifiedDocumentsView)
+ .where(finalWhere)
+
+ // DrawingKind 분석 (첫 번째 문서의 drawingKind 사용)
+ const drawingKind = data.length > 0 ? data[0].drawingKind : null
+
+ // 벤더 정보 조회
+
+
+ return { data, total, drawingKind }
+ })
+
+ const pageCount = Math.ceil(total / input.perPage)
+
+ return {
+ data,
+ pageCount,
+ total,
+ drawingKind: drawingKind as 'B3' | 'B4' | 'B5' | null,
+ }
+ } catch (err) {
+ console.error("Error fetching user vendor documents:", err)
+ return { data: [], pageCount: 0, total: 0, drawingKind: null }
+ }
+ }
+
+ /**
+ * DrawingKind별 문서 통계 조회
+ */
+ export async function getUserVendorDocumentStatsAll(userId: number) {
+ try {
+
+ const session = await getServerSession(authOptions)
+ if (!session?.user?.id) {
+ throw new Error("인증이 필요합니다.")
+ }
+
+
+ // DrawingKind별 통계 조회
+ const documents = await db
+ .select({
+ drawingKind: simplifiedDocumentsView.drawingKind,
+ })
+ .from(simplifiedDocumentsView)
+
+ // 통계 계산
+ const stats = documents.reduce((acc, doc) => {
+ if (doc.drawingKind) {
+ acc[doc.drawingKind] = (acc[doc.drawingKind] || 0) + 1
+ }
+ return acc
+ }, {} as Record<string, number>)
+
+ // 가장 많은 DrawingKind 찾기
+ const primaryDrawingKind = Object.entries(stats)
+ .sort(([,a], [,b]) => b - a)[0]?.[0] as 'B3' | 'B4' | 'B5' | undefined
+
+ return {
+ stats,
+ totalDocuments: documents.length,
+ primaryDrawingKind: primaryDrawingKind || null
+ }
+ } catch (err) {
+ console.error("Error fetching user vendor document stats:", err)
+ return { stats: {}, totalDocuments: 0, primaryDrawingKind: null }
+ }
+ }
export interface UpdateRevisionInput {