summaryrefslogtreecommitdiff
path: root/lib/compliance/services.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/compliance/services.ts')
-rw-r--r--lib/compliance/services.ts50
1 files changed, 33 insertions, 17 deletions
diff --git a/lib/compliance/services.ts b/lib/compliance/services.ts
index 8dc8e916..6541bdd1 100644
--- a/lib/compliance/services.ts
+++ b/lib/compliance/services.ts
@@ -1,8 +1,10 @@
'use server'
import db from "@/db/db";
-import { eq, desc, count, and, ne, or, ilike, asc, inArray } from "drizzle-orm";
+import { eq, desc, count, and, ne, or, ilike, asc, inArray, type SQL } from "drizzle-orm";
import { revalidatePath } from "next/cache";
+import { filterColumns } from "@/lib/filter-columns";
+import type { Filter, JoinOperator } from "@/types/table";
import {
complianceSurveyTemplates,
complianceQuestions,
@@ -90,35 +92,48 @@ export async function getComplianceSurveyTemplatesWithSorting(sort?: { id: strin
}
}
-// items 서비스와 동일한 구조의 함수 추가
+// items 서비스와 동일한 구조의 함수 추가 (vendor-candidates 패턴 적용)
export async function getComplianceSurveyTemplates(input: {
page: number;
perPage: number;
search?: string;
- filters?: Array<{ id: string; value: string }>;
- joinOperator?: 'and' | 'or';
+ filters?: Filter<typeof complianceSurveyTemplates>[];
+ joinOperator?: JoinOperator;
sort?: { id: string; desc: boolean }[];
}) {
try {
const safePerPage = Math.min(input.perPage, 100);
const offset = (input.page - 1) * safePerPage;
- let whereClause = eq(complianceSurveyTemplates.isActive, true);
+ // 1) Advanced filters
+ const advancedWhere = filterColumns({
+ table: complianceSurveyTemplates,
+ filters: input.filters || [],
+ joinOperator: input.joinOperator || 'and',
+ });
- // 검색 기능
+ // 2) Global search
+ let globalWhere;
if (input.search) {
const searchTerm = `%${input.search}%`;
- whereClause = and(
- eq(complianceSurveyTemplates.isActive, true),
- or(
- ilike(complianceSurveyTemplates.name, searchTerm),
- ilike(complianceSurveyTemplates.description, searchTerm),
- ilike(complianceSurveyTemplates.version, searchTerm)
- )
- )!;
+ globalWhere = or(
+ ilike(complianceSurveyTemplates.name, searchTerm),
+ ilike(complianceSurveyTemplates.description, searchTerm),
+ ilike(complianceSurveyTemplates.version, searchTerm)
+ );
}
- // 정렬 - 안전한 방식으로 처리
+ // 3) Combine finalWhere
+ const whereConditions: (SQL | undefined)[] = [];
+ if (advancedWhere) whereConditions.push(advancedWhere);
+ if (globalWhere) whereConditions.push(globalWhere);
+
+ const validConditions = whereConditions.filter((c): c is SQL => c !== undefined);
+ const finalWhere = validConditions.length > 0
+ ? and(...validConditions)!
+ : undefined; // 필터가 없으면 전체 데이터 조회
+
+ // 4) Sorting
let orderBy = [desc(complianceSurveyTemplates.createdAt)];
if (input.sort && input.sort.length > 0) {
@@ -149,10 +164,11 @@ export async function getComplianceSurveyTemplates(input: {
}
}
+ // 5) Query & count
const templates = await db
.select()
.from(complianceSurveyTemplates)
- .where(whereClause)
+ .where(finalWhere)
.orderBy(...orderBy)
.limit(safePerPage)
.offset(offset);
@@ -160,7 +176,7 @@ export async function getComplianceSurveyTemplates(input: {
const totalCount = await db
.select({ count: count() })
.from(complianceSurveyTemplates)
- .where(whereClause);
+ .where(finalWhere);
const total = totalCount[0]?.count || 0;
const pageCount = Math.ceil(total / safePerPage);