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
|
'use server';
import {
and,
asc,
count,
desc,
eq,
ilike,
or,
sql,
} from 'drizzle-orm';
import db from '@/db/db';
import { approvalLogs } from '@/db/schema/knox/approvals';
import { filterColumns } from '@/lib/filter-columns';
// ---------------------------------------------
// Types
// ---------------------------------------------
export type ApprovalLog = typeof approvalLogs.$inferSelect;
// ---------------------------------------------
// Revalidation helpers (사용하지 않음 - 추후 필요시 추가)
// ---------------------------------------------
// ---------------------------------------------
// List & read helpers
// ---------------------------------------------
interface ListInput {
page: number;
perPage: number;
search?: string;
filters?: Record<string, unknown>[];
joinOperator?: 'and' | 'or';
sort?: Array<{ id: string; desc: boolean }>;
}
export async function getApprovalLogList(input: ListInput) {
const offset = (input.page - 1) * input.perPage;
/* ------------------------------------------------------------------
* WHERE 절 구성
* ----------------------------------------------------------------*/
const advancedWhere = filterColumns({
table: approvalLogs,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
filters: (input.filters ?? []) as any,
joinOperator: (input.joinOperator ?? 'and') as 'and' | 'or',
});
// 전역 검색 (subject, content, emailAddress, userId)
let globalWhere;
if (input.search) {
const s = `%${input.search}%`;
globalWhere = or(
ilike(approvalLogs.subject, s),
ilike(approvalLogs.content, s),
ilike(approvalLogs.emailAddress, s),
ilike(approvalLogs.userId, s),
);
}
let where = eq(approvalLogs.isDeleted, false); // 기본적으로 삭제되지 않은 것만 조회
if (advancedWhere && globalWhere) {
where = and(where, advancedWhere, globalWhere);
} else if (advancedWhere) {
where = and(where, advancedWhere);
} else if (globalWhere) {
where = and(where, globalWhere);
}
/* ------------------------------------------------------------------
* ORDER BY 절 구성
* ----------------------------------------------------------------*/
let orderBy;
try {
orderBy = input.sort && input.sort.length > 0
? input.sort
.map((item) => {
if (!item || !item.id || typeof item.id !== 'string') return null;
if (!(item.id in approvalLogs)) return null;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const col = approvalLogs[item.id];
return item.desc ? desc(col) : asc(col);
})
.filter((v): v is Exclude<typeof v, null> => v !== null)
: [desc(approvalLogs.createdAt)];
} catch {
orderBy = [desc(approvalLogs.createdAt)];
}
/* ------------------------------------------------------------------
* 데이터 조회
* ----------------------------------------------------------------*/
const data = await db
.select()
.from(approvalLogs)
.where(where)
.orderBy(...orderBy)
.limit(input.perPage)
.offset(offset);
const totalResult = await db
.select({ count: count() })
.from(approvalLogs)
.where(where);
const total = totalResult[0]?.count ?? 0;
const pageCount = Math.ceil(total / input.perPage);
return {
data,
pageCount,
};
}
// ----------------------------------------------------
// Distinct categories for filter
// ----------------------------------------------------
export async function getApprovalLogStatuses(): Promise<string[]> {
const rows = await db
.select({ status: approvalLogs.status })
.from(approvalLogs)
.where(eq(approvalLogs.isDeleted, false))
.groupBy(approvalLogs.status)
.orderBy(asc(approvalLogs.status));
return rows.map((r) => r.status).filter((status) => status !== null && status !== undefined && status !== '');
}
// ----------------------------------------------------
// Get distinct user IDs for filter
// ----------------------------------------------------
export async function getApprovalLogUserIds(): Promise<string[]> {
const rows = await db
.select({ userId: approvalLogs.userId })
.from(approvalLogs)
.where(and(eq(approvalLogs.isDeleted, false), sql`${approvalLogs.userId} IS NOT NULL`))
.groupBy(approvalLogs.userId)
.orderBy(asc(approvalLogs.userId));
return rows.map((r) => r.userId!).filter((id) => id !== null && id !== undefined && id !== '');
}
// ----------------------------------------------------
// Server Action for fetching distinct statuses
// ----------------------------------------------------
export async function getApprovalLogStatusesAction() {
try {
const data = await getApprovalLogStatuses()
return { success: true, data }
} catch (error) {
return { success: false, error: error instanceof Error ? error.message : '상태 조회에 실패했습니다.' }
}
}
// ----------------------------------------------------
// Server Action for fetching distinct user IDs
// ----------------------------------------------------
export async function getApprovalLogUserIdsAction() {
try {
const data = await getApprovalLogUserIds()
return { success: true, data }
} catch (error) {
return { success: false, error: error instanceof Error ? error.message : '사용자 조회에 실패했습니다.' }
}
}
// ----------------------------------------------------
// Get single approval log
// ----------------------------------------------------
export async function getApprovalLog(apInfId: string): Promise<ApprovalLog | null> {
const [log] = await db
.select()
.from(approvalLogs)
.where(and(eq(approvalLogs.apInfId, apInfId), eq(approvalLogs.isDeleted, false)))
.limit(1);
return log || null;
}
// ----------------------------------------------------
// Server Action for getting approval log list
// ----------------------------------------------------
export async function getApprovalLogListAction(input: ListInput) {
try {
const data = await getApprovalLogList(input);
return { success: true, data: data.data, pageCount: data.pageCount };
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : '결재 로그 조회에 실패했습니다.',
data: [],
pageCount: 0
};
}
}
|