diff options
| -rw-r--r-- | lib/tbe-last/vendor-tbe-service.ts | 48 |
1 files changed, 43 insertions, 5 deletions
diff --git a/lib/tbe-last/vendor-tbe-service.ts b/lib/tbe-last/vendor-tbe-service.ts index 858a5817..69c0de5f 100644 --- a/lib/tbe-last/vendor-tbe-service.ts +++ b/lib/tbe-last/vendor-tbe-service.ts @@ -4,12 +4,13 @@ import { unstable_cache } from "next/cache" import db from "@/db/db" -import { and, desc, asc, eq, sql, ne } from "drizzle-orm" +import { and, desc, asc, eq, sql, ne, or } from "drizzle-orm" import { tbeLastView, rfqLastTbeSessions } from "@/db/schema" import { rfqPrItems } from "@/db/schema/rfqLast" import { getServerSession } from "next-auth" import { authOptions } from "@/app/api/auth/[...nextauth]/route" import { revalidateTag } from "next/cache" +import { filterColumns } from "@/lib/filter-columns" // ========================================== // 간단한 벤더 Q&A 타입 정의 // ========================================== @@ -42,22 +43,59 @@ export async function getTBEforVendor( const limit = input.perPage ?? 10 // 벤더 필터링 - const vendorWhere =and(eq(tbeLastView.vendorId, vendorId),ne(tbeLastView.sessionStatus, "준비중")) + const vendorWhere = and(eq(tbeLastView.vendorId, vendorId), ne(tbeLastView.sessionStatus, "준비중")) + + // 고급 필터 추가 + const advancedWhere = filterColumns({ + table: tbeLastView, + filters: input.filters ?? [], + joinOperator: input.joinOperator ?? "and", + }); + + // 글로벌 검색 추가 + let globalWhere; + if (input.search) { + const s = `%${input.search}%`; + globalWhere = or( + sql`${tbeLastView.sessionCode} ILIKE ${s}`, + sql`${tbeLastView.rfqCode} ILIKE ${s}`, + sql`${tbeLastView.rfqTitle} ILIKE ${s}`, + sql`${tbeLastView.projectCode} ILIKE ${s}`, + sql`${tbeLastView.projectName} ILIKE ${s}`, + sql`${tbeLastView.packageNo} ILIKE ${s}`, + sql`${tbeLastView.packageName} ILIKE ${s}` + ); + } + + // 최종 WHERE 조건 + const whereConditions = [vendorWhere, advancedWhere]; + if (globalWhere) { + whereConditions.push(globalWhere); + } + const finalWhere = and(...whereConditions); + + // 정렬 처리 + const orderBy = input.sort?.length + ? input.sort.map((s: any) => { + const col = (tbeLastView as any)[s.id]; + return s.desc ? desc(col) : asc(col); + }) + : [desc(tbeLastView.createdAt)]; // 데이터 조회 const [rows, total] = await db.transaction(async (tx) => { const data = await tx .select() .from(tbeLastView) - .where(vendorWhere) - .orderBy(desc(tbeLastView.createdAt)) + .where(finalWhere) + .orderBy(...orderBy) .offset(offset) .limit(limit) const [{ count }] = await tx .select({ count: sql<number>`count(*)`.as("count") }) .from(tbeLastView) - .where(vendorWhere) + .where(finalWhere) return [data, Number(count)] }) |
