summaryrefslogtreecommitdiff
path: root/lib/general-check-list/service.ts
blob: fde756ea4d9fa58cd965f55b5f1d5421d0f2404d (plain)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"use server"; // Next.js 서버 액션에서 직접 import하려면 (선택)

import db from "@/db/db";

import { filterColumns } from "@/lib/filter-columns";

import { asc, desc, ilike, inArray, and, gte, lte, not, or, sql, eq } from "drizzle-orm";
import { generalEvaluations} from "@/db/schema";
import { GetGeneralEvaluationsSchema } from "./validation";
import { selectGeneralCheckLists , countGeneralCheckList} from "./repository";

export async function getGeneralCheckList(input: GetGeneralEvaluationsSchema) {
    try {
        const offset = (input.page - 1) * input.perPage;


        // 고급 필터 처리 - 테이블의 DataTableFilterList에서 오는 필터
        const advancedFilters = input.filters || [];
        const advancedJoinOperator = input.joinOperator || "and";


        // 고급 필터 조건 생성
        let advancedWhere;
        if (advancedFilters.length > 0) {
            advancedWhere = filterColumns({
                table: generalEvaluations,
                filters: advancedFilters,
                joinOperator: advancedJoinOperator,
            });
        }

        // 전역 검색 조건
        let globalWhere;
        if (input.search) {
            const s = `%${input.search}%`;
            globalWhere = or(
                ilike(generalEvaluations.category, s),
                ilike(generalEvaluations.inspectionItem, s),
                ilike(generalEvaluations.remarks, s),
            );
        }

        // 모든 조건 결합
        let whereConditions = [];
        if (advancedWhere) whereConditions.push(advancedWhere);
        if (globalWhere) whereConditions.push(globalWhere);

        // 조건이 있을 때만 and() 사용
        const finalWhere = whereConditions.length > 0
            ? and(...whereConditions)
            : undefined;


        // 정렬 조건 - 안전하게 처리
        const orderBy =
            input.sort && input.sort.length > 0
                ? input.sort.map((item) =>
                    item.desc
                        ? desc(generalEvaluations[item.id])
                        : asc(generalEvaluations[item.id])
                )
                : [desc(generalEvaluations.updatedAt)]


        // 트랜잭션 내부에서 Repository 호출
        const { data, total } = await db.transaction(async (tx) => {
            const data = await selectGeneralCheckLists(tx, {
                where: finalWhere,
                orderBy,
                offset,
                limit: input.perPage,
            });

            const total = await countGeneralCheckList(tx, finalWhere);
            return { data, total };
        });

        const pageCount = Math.ceil(total / input.perPage);

        return { data, pageCount };
    } catch (err) {
        console.error("getRfqs 에러:", err);

        // 에러 세부 정보 더 자세히 로깅
        if (err instanceof Error) {
            console.error("에러 메시지:", err.message);
            console.error("에러 스택:", err.stack);

            if ('code' in err) {
                console.error("SQL 에러 코드:", (err as any).code);
            }
        }

        // 에러 발생 시 디폴트
        return { data: [], pageCount: 0 };
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Types
// ─────────────────────────────────────────────────────────────────────────────
export type GeneralEvaluationInput = {
    category: string;
    inspectionItem: string;
    remarks?: string | null;
    isActive?: boolean;
  };
  
  // ─────────────────────────────────────────────────────────────────────────────
  // Helpers
  // ─────────────────────────────────────────────────────────────────────────────
  async function generateSerialNumber(tx: typeof db, category: string) {
    const prefix = `GE-${category}-`;
  
    // 카테고리 내에서 가장 최근 시리얼 찾아서 +1
    const latest = await tx
      .select({ serialNumber: generalEvaluations.serialNumber })
      .from(generalEvaluations)
      .where(eq(generalEvaluations.category, category))
      .orderBy(desc(generalEvaluations.serialNumber))
      .limit(1);
  
    let nextSeq = 1;
    if (latest.length) {
      const parts = latest[0].serialNumber.split("-");
      const last = parts[parts.length - 1];
      const num = parseInt(last, 10);
      if (!isNaN(num)) nextSeq = num + 1;
    }
  
    const seqStr = nextSeq.toString().padStart(3, "0");
    return `${prefix}${seqStr}`;
  }
  // ─────────────────────────────────────────────────────────────────────────────
  // CRUD Actions
  // ─────────────────────────────────────────────────────────────────────────────
  export async function createGeneralEvaluation(input: GeneralEvaluationInput) {
    return db.transaction(async (tx) => {
      try {
        const serialNumber = await generateSerialNumber(tx, input.category);
  
        const [created] = await tx
          .insert(generalEvaluations)
          .values({
            serialNumber,
            category: input.category,
            inspectionItem: input.inspectionItem,
            remarks: input.remarks ?? null,
            isActive: input.isActive ?? true,
          })
          .returning();
  
        return { success: true, data: created, message: "체크리스트가 추가되었습니다." };
      } catch (err) {
        console.error("createGeneralEvaluation error", err);
        return { success: false, message: "추가 중 오류가 발생했습니다." };
      }
    });
  }
  
  export async function updateGeneralEvaluation(id: number, fields: Partial<GeneralEvaluationInput>) {
    try {
      const [updated] = await db
        .update(generalEvaluations)
        .set({ ...fields, updatedAt: sql`now()` })
        .where(eq(generalEvaluations.id, id))
        .returning();
  
      return { success: true, data: updated, message: "수정되었습니다." };
    } catch (err) {
      console.error("updateGeneralEvaluation error", err);
      return { success: false, message: "수정 중 오류가 발생했습니다." };
    }
  }
  
  export async function deleteGeneralEvaluations(ids: number[]) {
    if (ids.length === 0) return { success: false, message: "삭제할 항목이 없습니다." };
    try {
      await db.delete(generalEvaluations).where(inArray(generalEvaluations.id, ids));
      return { success: true, message: `${ids.length}개의 체크리스트가 삭제되었습니다.` };
    } catch (err) {
      console.error("deleteGeneralEvaluations error", err);
      return { success: false, message: "삭제 중 오류가 발생했습니다." };
    }
  }
  
  // ─────────────────────────────────────────────────────────────────────────────
  // Pagination Search (기존 getGeneralCheckList → getGeneralEvaluations)
  // ─────────────────────────────────────────────────────────────────────────────
  export async function getGeneralEvaluations(input: GetGeneralEvaluationsSchema) {
    const offset = (input.page - 1) * input.perPage;
  
    // 고급 필터 처리
    const advFilters = input.filters ?? [];
    const advOperator = input.joinOperator ?? "and";
    const advWhere = advFilters.length
      ? filterColumns({ table: generalEvaluations, filters: advFilters, joinOperator: advOperator })
      : undefined;
  
    // 전역 검색
    const globalWhere = input.search
      ? or(
          ilike(generalEvaluations.serialNumber, `%${input.search}%`),
          ilike(generalEvaluations.category, `%${input.search}%`),
          ilike(generalEvaluations.inspectionItem, `%${input.search}%`),
          ilike(generalEvaluations.remarks, `%${input.search}%`)
        )
      : undefined;
  
    const whereAll = [advWhere, globalWhere].filter(Boolean);
    const finalWhere = whereAll.length ? and(...whereAll) : undefined;
  
    // 정렬
    const orderBy =
    input.sort && input.sort.length > 0
        ? input.sort.map((item) =>
            item.desc
                ? desc(generalEvaluations[item.id])
                : asc(generalEvaluations[item.id])
        )
        : [desc(generalEvaluations.updatedAt)]

  
    // 트랜잭션
    const { data, total } = await db.transaction(async (tx) => {
      const data = await tx
        .select()
        .from(generalEvaluations)
        .where(finalWhere ?? undefined)
        .orderBy(...orderBy)
        .limit(input.perPage)
        .offset(offset);
  
      const [{ count }] = await tx
        .select({ count: sql<number>`count(*)` })
        .from(generalEvaluations)
        .where(finalWhere ?? undefined);
  
      return { data, total: count };
    });

  
    return { data, pageCount: Math.ceil(total / input.perPage) };
  }