diff options
| author | joonhoekim <26rote@gmail.com> | 2025-03-25 15:55:45 +0900 |
|---|---|---|
| committer | joonhoekim <26rote@gmail.com> | 2025-03-25 15:55:45 +0900 |
| commit | 1a2241c40e10193c5ff7008a7b7b36cc1d855d96 (patch) | |
| tree | 8a5587f10ca55b162d7e3254cb088b323a34c41b /lib/tag-numbering/service.ts | |
initial commit
Diffstat (limited to 'lib/tag-numbering/service.ts')
| -rw-r--r-- | lib/tag-numbering/service.ts | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/lib/tag-numbering/service.ts b/lib/tag-numbering/service.ts new file mode 100644 index 00000000..9b1c1172 --- /dev/null +++ b/lib/tag-numbering/service.ts @@ -0,0 +1,123 @@ +"use server"; // Next.js 서버 액션에서 직접 import하려면 (선택) + +import { revalidateTag, unstable_noStore } from "next/cache"; +import db from "@/db/db"; +import { unstable_cache } from "@/lib/unstable-cache"; +import { GetTagNumberigSchema } from "./validation"; +import { filterColumns } from "@/lib/filter-columns"; +import { TagSubfieldOption, tagSubfieldOptions, ViewTagSubfields, viewTagSubfields } from "@/db/schema/vendorData"; +import { asc, desc, ilike, inArray, and, gte, lte, not, or, eq } from "drizzle-orm"; +import { countTagNumbering, selectTagNumbering } from "./repository"; + +export async function getTagNumbering(input: GetTagNumberigSchema) { + + 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: viewTagSubfields, + filters: input.filters, + joinOperator: input.joinOperator, + }); + + + let globalWhere + if (input.search) { + const s = `%${input.search}%` + globalWhere = or(ilike(viewTagSubfields.tagTypeCode, s), ilike(viewTagSubfields.tagTypeDescription, s) + , ilike(viewTagSubfields.attributesId, s) , ilike(viewTagSubfields.attributesDescription, s), ilike(viewTagSubfields.expression, 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 = + input.sort.length > 0 + ? input.sort.map((item) => + item.desc ? desc(viewTagSubfields[item.id]) : asc(viewTagSubfields[item.id]) + ) + : [asc(viewTagSubfields.createdAt)]; + + // 트랜잭션 내부에서 Repository 호출 + const { data, total } = await db.transaction(async (tx) => { + const data = await selectTagNumbering(tx, { + where, + orderBy, + offset, + limit: input.perPage, + }); + + const total = await countTagNumbering(tx, where); + return { data, total }; + }); + + const pageCount = Math.ceil(total / input.perPage); + + return { data, pageCount }; + } catch (err) { + // 에러 발생 시 디폴트 + return { data: [], pageCount: 0 }; + } + }, + [JSON.stringify(input)], // 캐싱 키 + { + revalidate: 3600, + tags: ["tag-numbering"], // revalidateTag("items") 호출 시 무효화 + } + )(); + } + + + + export const fetchTagSubfieldOptions = (async (attributesId: string): Promise<TagSubfieldOption[]> => { + try { + // (A) findMany -> 스키마 제네릭 누락 에러 발생 → 대신 select().from().where() 사용 + const rows = await db + .select() + .from(tagSubfieldOptions) + .where(eq(tagSubfieldOptions.attributesId, attributesId)) + .orderBy(asc(tagSubfieldOptions.code)) + + // rows는 TagSubfieldOption[] 형태 + return rows + } catch (error) { + console.error("Error fetching tag subfield options:", error) + return [] + } + }) + + export const getTagNumberingRules = (async (tagType: string): Promise<ViewTagSubfields[]> => { + try { + if (!tagType) { + return [] + } + + // 기존 findMany 대신 select().from().where() + orderBy + const rules = await db + .select() + .from(viewTagSubfields) + .where(eq(viewTagSubfields.tagTypeDescription, tagType)) + .orderBy(asc(viewTagSubfields.sortOrder)) + + return rules + } catch (error) { + console.error("Error fetching tag numbering rules:", error) + return [] + } + })
\ No newline at end of file |
