summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordujinkim <dujin.kim@dtsolution.co.kr>2025-10-16 01:00:47 +0000
committerdujinkim <dujin.kim@dtsolution.co.kr>2025-10-16 01:00:47 +0000
commit5f29ee456fddfb85d1798094cf3e612f4b7647d8 (patch)
treeb947a0acb97d5fd2913651a6e3d5ce96ef0ef94a
parentb54f6f03150dd78d86db62201b6386bf14b72394 (diff)
(임수민) 필터, 정렬, 검색 수정
-rw-r--r--lib/tbe-last/vendor-tbe-service.ts48
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)]
})