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
|
"use server";
import db from "@/db/db";
import { incoterms } from "@/db/schema/procurementRFQ";
import { GetIncotermsSchema } from "@/lib/incoterms/validations";
import { filterColumns } from "@/lib/filter-columns";
import { asc, desc, ilike, and, or, count, eq } from "drizzle-orm";
// Incoterms CRUD
export async function getIncoterms(input: GetIncotermsSchema) {
try {
const offset = (input.page - 1) * input.perPage;
// 1. where 절
let advancedWhere;
try {
advancedWhere = filterColumns({
table: incoterms,
filters: input.filters,
joinOperator: input.joinOperator,
});
} catch (whereErr) {
console.error("Error building advanced where:", whereErr);
advancedWhere = undefined;
}
let globalWhere;
if (input.search) {
try {
const s = `%${input.search}%`;
globalWhere = or(
ilike(incoterms.code, s),
ilike(incoterms.description, s)
);
} catch (searchErr) {
console.error("Error building search where:", searchErr);
globalWhere = undefined;
}
}
// 2. where 결합
let finalWhere;
if (advancedWhere && globalWhere) {
finalWhere = and(advancedWhere, globalWhere);
} else {
finalWhere = advancedWhere || globalWhere;
}
// 3. order by
let orderBy;
try {
orderBy =
input.sort.length > 0
? input.sort
.map((item) => {
if (!item || !item.id || typeof item.id !== "string" || !(item.id in incoterms)) return null;
const col = incoterms[item.id as keyof typeof incoterms];
return item.desc ? desc(col) : asc(col);
})
.filter((v): v is Exclude<typeof v, null> => v !== null)
: [asc(incoterms.createdAt)];
} catch (orderErr) {
console.error("Error building order by:", orderErr);
orderBy = [asc(incoterms.createdAt)];
}
// 4. 쿼리 실행
let data = [];
let total = 0;
try {
const queryBuilder = db.select().from(incoterms);
if (finalWhere) {
queryBuilder.where(finalWhere);
}
if (orderBy && orderBy.length > 0) {
queryBuilder.orderBy(...orderBy);
}
if (typeof offset === "number" && !isNaN(offset)) {
queryBuilder.offset(offset);
}
if (typeof input.perPage === "number" && !isNaN(input.perPage)) {
queryBuilder.limit(input.perPage);
}
data = await queryBuilder;
const countBuilder = db
.select({ count: count() })
.from(incoterms);
if (finalWhere) {
countBuilder.where(finalWhere);
}
const countResult = await countBuilder;
total = countResult[0]?.count || 0;
} catch (queryErr) {
console.error("Query execution failed:", queryErr);
throw queryErr;
}
const pageCount = Math.ceil(total / input.perPage);
return { data, pageCount };
} catch (err) {
console.error("Error in getIncoterms:", err);
if (err instanceof Error) {
console.error("Error message:", err.message);
console.error("Error stack:", err.stack);
}
return { data: [], pageCount: 0 };
}
}
export async function createIncoterm(data: Omit<typeof incoterms.$inferInsert, "createdAt">) {
try {
const [created] = await db.insert(incoterms).values(data).returning();
return { data: created };
} catch (err) {
console.error("Error creating incoterm:", err);
return { error: "생성 중 오류가 발생했습니다." };
}
}
export async function updateIncoterm(code: string, data: Partial<typeof incoterms.$inferInsert>) {
try {
const [updated] = await db
.update(incoterms)
.set(data)
.where(eq(incoterms.code, code))
.returning();
return { data: updated };
} catch (err) {
console.error("Error updating incoterm:", err);
return { error: "수정 중 오류가 발생했습니다." };
}
}
export async function deleteIncoterm(code: string) {
try {
await db.delete(incoterms).where(eq(incoterms.code, code));
return { success: true };
} catch (err) {
console.error("Error deleting incoterm:", err);
return { error: "삭제 중 오류가 발생했습니다." };
}
}
|