From ef4c533ebacc2cdc97e518f30e9a9350004fcdfb Mon Sep 17 00:00:00 2001 From: dujinkim Date: Mon, 28 Apr 2025 02:13:30 +0000 Subject: ~20250428 작업사항 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/basic-contract/repository.ts | 167 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 lib/basic-contract/repository.ts (limited to 'lib/basic-contract/repository.ts') diff --git a/lib/basic-contract/repository.ts b/lib/basic-contract/repository.ts new file mode 100644 index 00000000..aab70106 --- /dev/null +++ b/lib/basic-contract/repository.ts @@ -0,0 +1,167 @@ +"use server"; + +import { asc, count,inArray ,eq} from "drizzle-orm"; +import { basicContractTemplates, basicContractView, 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(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(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)); +} \ No newline at end of file -- cgit v1.2.3