"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 { filterColumns } from "@/lib/filter-columns"; import { tagTypeClassFormMappings } from "@/db/schema/vendorData"; import { asc, desc, ilike, inArray, and, gte, lte, not, or, eq } from "drizzle-orm"; import { countProjectLists, selectProjectLists } from "./repository"; import { projects } from "@/db/schema"; import { GetProjectListsSchema } from "./validation"; export async function getProjectLists(input: GetProjectListsSchema) { 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: projects, filters: input.filters, joinOperator: input.joinOperator, }); let globalWhere if (input.search) { const s = `%${input.search}%` globalWhere = or( ilike(projects.name, s), ilike(projects.code, s), ilike(projects.type, 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(projects[item.id]) : asc(projects[item.id]) ) : [asc(projects.createdAt)]; // 트랜잭션 내부에서 Repository 호출 const { data, total } = await db.transaction(async (tx) => { const data = await selectProjectLists(tx, { where, orderBy, offset, limit: input.perPage, }); const total = await countProjectLists(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: ["project-lists"], } )(); } export async function getProjectInfoByProjectCode(projectCode: string) { const projectInfo = await db.select().from(projects).where(eq(projects.code, projectCode)).limit(1); if (!projectInfo || projectInfo.length === 0) { throw new Error(`프로젝트 코드 "${projectCode}"를 찾을 수 없습니다.`); } const projectInfoForAvl = { // 프로젝트코드 projectCode: projectInfo[0].code, // 프로젝트명 projectName: projectInfo[0].name, // 선종 shipType: projectInfo[0].SKND || undefined, // H/T 구분 projectHtDivision: projectInfo[0].type || undefined, }; return projectInfoForAvl; } export async function getAllProjectInfoByProjectCode(projectCode: string) { return await db .select() .from(projects) .where(eq(projects.code, projectCode)) .limit(1); } /** * projectId로 프로젝트 코드를 가져오는 함수 * @param projectId - 프로젝트 ID * @returns 프로젝트 코드 또는 null */ export async function getProjectCode(projectId: number): Promise { try { const project = await db .select({ code: projects.code }) .from(projects) .where(eq(projects.id, projectId)) .limit(1); return project[0]?.code || null; } catch (error) { console.error("Error fetching project code:", error) return null } }