1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
"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<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(basicContractTemplateStatsView)
.where(where || undefined)
.orderBy(...(orderBy || [asc(basicContractTemplateStatsView.lastActivityDate)]))
.offset(offset || 0)
.limit(limit || 50);
}
export async function selectBasicContractsVendor(
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 selectBasicContractsById(
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(basicContractTemplateStatsView)
.where(where || undefined);
return result[0]?.count || 0;
}
export async function countBasicContractsVendor(
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 countBasicContractsById(
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));
}
|