diff options
Diffstat (limited to 'lib/basic-contract/repository.ts')
| -rw-r--r-- | lib/basic-contract/repository.ts | 167 |
1 files changed, 167 insertions, 0 deletions
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<any, any, any>,
+ 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<any, any, any>,
+ 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<any, any, any>,
+ 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<any, any, any>,
+ 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<any, any, any>,
+ data: Omit<BasicContractTemplate, "id" | "createdAt" | "updatedAt">
+) {
+ 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<any, any, any>,
+ 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<any, any, any>,
+ 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<BasicContractTemplate[]> {
+ return db.select().from(basicContractTemplates).orderBy(asc(basicContractTemplates.id));
+}
\ No newline at end of file |
