diff options
Diffstat (limited to 'lib/form-list/service.ts')
| -rw-r--r-- | lib/form-list/service.ts | 151 |
1 files changed, 72 insertions, 79 deletions
diff --git a/lib/form-list/service.ts b/lib/form-list/service.ts index 9887609f..d49dc5fc 100644 --- a/lib/form-list/service.ts +++ b/lib/form-list/service.ts @@ -5,94 +5,87 @@ import db from "@/db/db"; import { unstable_cache } from "@/lib/unstable-cache"; import { GetFormListsSchema } from "./validation"; import { filterColumns } from "@/lib/filter-columns"; -import { tagTypeClassFormMappings } from "@/db/schema/vendorData"; +import { formListsView } from "@/db/schema/vendorData"; import { asc, desc, ilike, inArray, and, gte, lte, not, or } from "drizzle-orm"; import { countFormLists, selectFormLists } from "./repository"; import { projects } from "@/db/schema"; export async function getFormLists(input: GetFormListsSchema) { + return unstable_cache( + async () => { + try { + const offset = (input.page - 1) * input.perPage; + const advancedTable = true; - return unstable_cache( - async () => { - try { - const offset = (input.page - 1) * input.perPage; - - // const advancedTable = input.flags.includes("advancedTable"); - const advancedTable = true; - - // advancedTable 모드면 filterColumns()로 where 절 구성 - const advancedWhere = filterColumns({ - table: tagTypeClassFormMappings, - filters: input.filters, - joinOperator: input.joinOperator, - }); - - - let globalWhere - if (input.search) { - const s = `%${input.search}%` - globalWhere = or(ilike(tagTypeClassFormMappings.formCode, s), ilike(tagTypeClassFormMappings.formName, s) - , ilike(tagTypeClassFormMappings.tagTypeLabel, s) , ilike(tagTypeClassFormMappings.classLabel, s), - ilike(projects.name, s), - ilike(projects.code, s), - ) - // 필요시 여러 칼럼 OR조건 (status, priority, etc) - } - - const finalWhere = and( - // advancedWhere or your existing conditions - advancedWhere, - globalWhere // and()함수로 결합 or or() 등으로 결합 - ) - - - // 아니면 ilike, inArray, gte 등으로 where 절 구성 - const where = finalWhere - - - const orderBy = + + // advancedTable 모드면 filterColumns()로 where 절 구성 + const advancedWhere = filterColumns({ + table: formListsView, // 뷰 테이블 사용 + filters: input.filters, + joinOperator: input.joinOperator, + }); + + let globalWhere; + if (input.search) { + const s = `%${input.search}%`; + globalWhere = or( + ilike(formListsView.formCode, s), + ilike(formListsView.formName, s), + ilike(formListsView.tagTypeLabel, s), + ilike(formListsView.classLabel, s), + ilike(formListsView.projectName, s), // 뷰 테이블의 projectName 사용 + ilike(formListsView.projectCode, s), // 뷰 테이블의 projectCode 사용 + ); + } + + const finalWhere = and( + advancedWhere, + globalWhere + ); + + const where = finalWhere; + + const orderBy = input.sort.length > 0 ? input.sort.map((item) => { - // 프로젝트 관련 필드 정렬 처리 - if (item.id === 'projectCode') { - return item.desc ? desc(projects.code) : asc(projects.code); - } else if (item.id === 'projectName') { - return item.desc ? desc(projects.name) : asc(projects.name); - } else { - // 기존 필드 정렬 - return item.desc - ? desc(tagTypeClassFormMappings[item.id]) - : asc(tagTypeClassFormMappings[item.id]); - } + // 뷰 테이블은 모든 필드가 포함되어 있으므로 간단하게 처리 + return item.desc + ? desc(formListsView[item.id]) + : asc(formListsView[item.id]); }) - : [asc(tagTypeClassFormMappings.createdAt)]; - // 트랜잭션 내부에서 Repository 호출 - const { data, total } = await db.transaction(async (tx) => { - const data = await selectFormLists(tx, { - where, - orderBy, - offset, - limit: input.perPage, - }); + : [asc(formListsView.createdAt)]; - console.log("dbdata") - - const total = await countFormLists(tx, where); - return { data, total }; + // 트랜잭션 내부에서 Repository 호출 + const { data, total } = await db.transaction(async (tx) => { + // 뷰 테이블을 사용하는 새로운 select 함수 + const data = await selectFormLists(tx, { + where, + orderBy, + offset, + limit: input.perPage, }); - - const pageCount = Math.ceil(total / input.perPage); - - return { data, pageCount }; - } catch (err) { - // 에러 발생 시 디폴트 - return { data: [], pageCount: 0 }; - } - }, - [JSON.stringify(input)], // 캐싱 키 - { - revalidate: 3600, - tags: ["form-lists"], + + + + // 뷰 테이블을 사용하는 새로운 count 함수 + const total = await countFormLists(tx, where); + return { data, total }; + }); + + const pageCount = Math.ceil(total / input.perPage); + + + return { data, pageCount }; + } catch (err) { + console.log(err) + // 에러 발생 시 디폴트 + return { data: [], pageCount: 0 }; } - )(); - }
\ No newline at end of file + }, + [JSON.stringify(input)], // 캐싱 키 + { + revalidate: 3600, + tags: ["form-lists"], + } + )(); +}
\ No newline at end of file |
