"use server"; import { asc, count,inArray ,eq} from "drizzle-orm"; import { basicContractTemplates, basicContractView,basicContractTemplateStatsView, type BasicContractTemplate } from "@/db/schema"; import { PgTransaction } from "drizzle-orm/pg-core"; import db from "@/db/db"; // 템플릿 목록 조회 export async function selectBasicContractTemplates( tx: PgTransaction, options: { where?: any; orderBy?: any[]; offset?: number; limit?: number; } ) { const { where, orderBy, offset, limit } = options; return tx .select() .from(basicContractTemplates) .where(where || undefined) .orderBy(...(orderBy || [asc(basicContractTemplates.createdAt)])) .offset(offset || 0) .limit(limit || 50); } export async function selectBasicContracts( tx: PgTransaction, options: { where?: any; orderBy?: any[]; offset?: number; limit?: number; } ) { const { where, orderBy, offset, limit } = options; return tx .select() .from(basicContractTemplateStatsView) .where(where || undefined) .orderBy(...(orderBy || [asc(basicContractTemplateStatsView.lastActivityDate)])) .offset(offset || 0) .limit(limit || 50); } export async function selectBasicContractsVendor( tx: PgTransaction, options: { where?: any; orderBy?: any[]; offset?: number; limit?: number; } ) { const { where, orderBy, offset, limit } = options; return tx .select() .from(basicContractView) .where(where || undefined) .orderBy(...(orderBy || [asc(basicContractView.createdAt)])) .offset(offset || 0) .limit(limit || 50); } export async function selectBasicContractsById( tx: PgTransaction, options: { where?: any; orderBy?: any[]; offset?: number; limit?: number; } ) { const { where, orderBy, offset, limit } = options; return tx .select() .from(basicContractView) .where(where || undefined) .orderBy(...(orderBy || [asc(basicContractView.createdAt)])) .offset(offset || 0) .limit(limit || 50); } // 템플릿 개수 조회 export async function countBasicContractTemplates( tx: PgTransaction, where?: any ) { const result = await tx .select({ count: count() }) .from(basicContractTemplates) .where(where || undefined); return result[0]?.count || 0; } export async function countBasicContracts( tx: PgTransaction, where?: any ) { const result = await tx .select({ count: count() }) .from(basicContractTemplateStatsView) .where(where || undefined); return result[0]?.count || 0; } export async function countBasicContractsVendor( tx: PgTransaction, where?: any ) { const result = await tx .select({ count: count() }) .from(basicContractView) .where(where || undefined); return result[0]?.count || 0; } export async function countBasicContractsById( tx: PgTransaction, where?: any ) { const result = await tx .select({ count: count() }) .from(basicContractView) .where(where || undefined); return result[0]?.count || 0; } // 템플릿 생성 export async function insertBasicContractTemplate( tx: PgTransaction, data: Omit ) { return tx .insert(basicContractTemplates) .values({ ...data, createdAt: new Date(), updatedAt: new Date(), }) .returning(); } /** * ID로 특정 기본 계약서 템플릿을 조회합니다. * @param tx 데이터베이스 트랜잭션 * @param id 조회할 템플릿 ID (문자열로 받아서 숫자로 변환) * @returns 조회된 템플릿 또는 에러가 있는 경우 null */ export async function getBasicContractTemplateById( tx: PgTransaction, id: number ): Promise<{ data: BasicContractTemplate | null; error: string | null }> { try { const templates = await tx .select() .from(basicContractTemplates) .where(eq(basicContractTemplates.id, id)); if (!templates || templates.length === 0) { return { data: null, error: null }; } return { data: templates[0], error: null }; } catch (error) { console.error(`템플릿 조회 중 오류 발생 (ID: ${id}):`, error); return { data: null, error: error instanceof Error ? error.message : "템플릿 조회 중 오류가 발생했습니다." }; } } /** * 여러 기본 계약서 템플릿을 ID 배열 기반으로 삭제합니다. * @param tx 데이터베이스 트랜잭션 * @param ids 삭제할 템플릿 ID 배열 (문자열로 받아서 숫자로 변환) * @returns 삭제된 템플릿 배열 또는 에러 정보 */ export async function deleteBasicContractTemplates( tx: PgTransaction, ids: number[] ): Promise<{ data: BasicContractTemplate[] | null; error: string | null }> { if (!ids || ids.length === 0) { return { data: [], error: null }; } try { // 삭제될 템플릿 정보를 반환하기 위해 먼저 조회 const templatesBeforeDelete = await tx .select() .from(basicContractTemplates) .where(inArray(basicContractTemplates.id, ids)); // 삭제 실행 const deletedTemplates = await tx .delete(basicContractTemplates) .where(inArray(basicContractTemplates.id, ids)) .returning(); return { data: templatesBeforeDelete.length > 0 ? templatesBeforeDelete : deletedTemplates, error: null }; } catch (error) { console.error("템플릿 삭제 중 오류 발생:", error); return { data: null, error: error instanceof Error ? error.message : "템플릿 삭제 중 오류가 발생했습니다." }; } } export async function findAllTemplates(): Promise { return db.select().from(basicContractTemplates).orderBy(asc(basicContractTemplates.id)); }