summaryrefslogtreecommitdiff
path: root/lib/items/service.ts
blob: c841efad5cf48617abf2779523520cbfedd5a4c5 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// src/lib/items/service.ts
"use server"; // Next.js 서버 액션에서 직접 import하려면 (선택)

import { revalidateTag, unstable_noStore } from "next/cache";
import  db  from "@/db/db";
import { customAlphabet } from "nanoid";

import { filterColumns } from "@/lib/filter-columns";
import { unstable_cache } from "@/lib/unstable-cache";
import { getErrorMessage } from "@/lib/handle-error";

import { asc, desc, ilike, inArray, and, gte, lte, not, or ,eq} from "drizzle-orm";
import { CreateItemSchema, GetItemsSchema, UpdateItemSchema } from "./validations";
import { Item, items } from "@/db/schema/items";
import { countItems, deleteItemById, deleteItemsByIds, findAllItems, insertItem, selectItems, updateItem } from "./repository";


/* -----------------------------------------------------
   1) 조회 관련 
----------------------------------------------------- */

/**
 * 복잡한 조건으로 Item 목록을 조회 (+ pagination) 하고,
 * 총 개수에 따라 pageCount를 계산해서 리턴.
 * Next.js의 unstable_cache를 사용해 일정 시간 캐시.
 */
export async function getItems(input: GetItemsSchema) {
  const safePerPage = Math.min(input.perPage, 100);
  
  return unstable_cache(
    async () => {
      try {
        const offset = (input.page - 1) * safePerPage;
        
        const advancedWhere = filterColumns({
          table: items,
          filters: input.filters,
          joinOperator: input.joinOperator,
        });

        let globalWhere;
        if (input.search) {
          const s = `%${input.search}%`;
          globalWhere = or(
            ilike(items.itemLevel, s),
            ilike(items.itemCode, s),
            ilike(items.itemName, s),
            ilike(items.smCode, s),
            ilike(items.packageCode, s),
            ilike(items.description, s),
            ilike(items.changeDate, s)
          );
        }

        const finalWhere = and(advancedWhere, globalWhere);

        const orderBy = input.sort.length > 0
          ? input.sort.map((item) =>
              item.desc ? desc(items[item.id]) : asc(items[item.id])
            )
          : [asc(items.createdAt)];

        const { data, total } = await db.transaction(async (tx) => {
          const data = await selectItems(tx, {
            where: finalWhere,
            orderBy,
            offset,
            limit: safePerPage,
          });

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

        console.log(data)

        const pageCount = Math.ceil(total / safePerPage);
        return { data, pageCount };
      } catch (err) {
        console.error(err);
        return { data: [], pageCount: 0 };
      }
    },
    [JSON.stringify({...input, perPage: safePerPage})],
    {
      revalidate: 3600,
      tags: ["items"],
    }
  )();
}

export interface GetItemsInfiniteInput extends Omit<GetItemsSchema, 'page' | 'perPage'> {
  cursor?: string;
  limit?: number;
}

// 무한 스크롤 결과 타입
export interface GetItemsInfiniteResult {
  data: any[];
  hasNextPage: boolean;
  nextCursor: string | null;
  total?: number | null;
}

export async function getItemsInfinite(input: GetItemsInfiniteInput): Promise<GetItemsInfiniteResult> {
  return unstable_cache(
    async () => {
      try {
        // 페이지 크기 제한 (기존과 동일한 방식)
        const safeLimit = Math.min(input.limit || 50, 100);

        // 고급 필터링 (기존과 완전 동일)
        const advancedWhere = filterColumns({
          table: items,
          filters: input.filters,
          joinOperator: input.joinOperator,
        });

        // 전역 검색 (기존과 완전 동일)
        let globalWhere;
        if (input.search) {
          const s = `%${input.search}%`;
          globalWhere = or(
            ilike(items.itemLevel, s),
            ilike(items.itemCode, s),
            ilike(items.itemName, s),
            ilike(items.description, s),
            ilike(items.parentItemCode, s),
            ilike(items.unitOfMeasure, s),
            ilike(items.steelType, s),
            ilike(items.gradeMaterial, s),
            ilike(items.baseUnitOfMeasure, s),
            ilike(items.changeDate, s)
          );
        }

        // 커서 기반 페이지네이션 조건 추가
        let cursorWhere;
        if (input.cursor) {
          cursorWhere = gt(items.id, input.cursor);
        }

        // 모든 조건 결합
        const finalWhere = and(advancedWhere, globalWhere, cursorWhere);

        // 정렬 (기존과 동일하지만 id 정렬 보장)
        let orderBy = input.sort.length > 0
          ? input.sort.map((item) =>
              item.desc ? desc(items[item.id]) : asc(items[item.id])
            )
          : [asc(items.createdAt)];

        // 무한 스크롤에서는 id 정렬이 필수 (커서 기반 페이지네이션용)
        const hasIdSort = orderBy.some(sort => {
          const column = sort.constructor.name.includes('desc') 
            ? sort.column 
            : sort.column;
          return column === items.id;
        });

        if (!hasIdSort) {
          orderBy.push(asc(items.id));
        }

        // 트랜잭션으로 데이터 조회 (기존과 동일한 패턴)
        const { data, total } = await db.transaction(async (tx) => {
          // limit + 1로 다음 페이지 존재 여부 확인
          const data = await selectItems(tx, {
            where: finalWhere,
            orderBy,
            limit: safeLimit + 1,
          });

          // 첫 페이지에서만 전체 개수 계산 (성능 최적화)
          let total = null;
          if (!input.cursor) {
            // 커서 조건 제외하고 전체 개수 계산
            const countWhere = and(advancedWhere, globalWhere);
            total = await countItems(tx, countWhere);
          }

          return { data, total };
        });

        // 다음 페이지 존재 여부 및 커서 설정
        const hasNextPage = data.length > safeLimit;
        const resultItems = hasNextPage ? data.slice(0, safeLimit) : data;
        const nextCursor = hasNextPage && resultItems.length > 0
          ? resultItems[resultItems.length - 1].id
          : null;

        return {
          data: resultItems,
          hasNextPage,
          nextCursor,
          total,
        };

      } catch (err) {
        console.error('getItemsInfinite error:', err);
        return {
          data: [],
          hasNextPage: false,
          nextCursor: null,
          total: 0,
        };
      }
    },
    [JSON.stringify({ ...input, limit: Math.min(input.limit || 50, 100) })],
    {
      revalidate: 3600,
      tags: ["items"],
    }
  )();
}

// 통합된 Items 조회 함수 (모드별 자동 분기)
export async function getItemsUnified(input: GetItemsSchema & { mode?: 'pagination' | 'infinite'; cursor?: string }): Promise<any> {
  // perPage 기반 모드 자동 결정
  const isInfiniteMode = input.perPage >= 1_000_000;
  
  if (isInfiniteMode || input.mode === 'infinite') {
    // 무한 스크롤 모드
    return getItemsInfinite({
      ...input,
      limit: 50, // 실제로는 50개씩 로드
      cursor: input.cursor,
    });
  } else {
    // 기존 페이지네이션 모드
    return getItems(input);
  }
}


/* -----------------------------------------------------
   2) 생성(Create) 
----------------------------------------------------- */
export interface ItemCreateData {
  itemCode: string
  itemName: string
  description: string | null
  parentItemCode?: string | null
  itemLevel?: number | null
  deleteFlag?: string | null
  unitOfMeasure?: string | null
  steelType?: string | null
  gradeMaterial?: string | null
  changeDate?: string | null
  baseUnitOfMeasure?: string | null
}


/**
 * Item 생성 후, (가장 오래된 Item 1개) 삭제로
 * 전체 Item 개수를 고정
 */
export async function createItem(input: ItemCreateData) {
  unstable_noStore() // Next.js 서버 액션 캐싱 방지
  
  try {
    if (!input.itemCode || !input.itemName) {
      return { 
        success: false, 
        message: "아이템 코드와 아이템 명은 필수입니다",
        data: null, 
        error: "필수 필드 누락" 
      }
    }

    // result 변수에 명시적으로 타입과 초기값 할당
    let result: any[] = []
    
    // 트랜잭션 결과를 result에 할당
    result = await db.transaction(async (tx) => {
      // 기존 아이템 확인 (itemCode는 unique)
      const existingItem = await tx.query.items.findFirst({
        where: eq(items.itemCode, input.itemCode),
      })

      let txResult
      if (existingItem) {
        // 기존 아이템 업데이트
        txResult = await updateItem(tx, existingItem.id, {
          itemName: input.itemName,
          description: input.description,
          parentItemCode: input.parentItemCode,
          itemLevel: input.itemLevel,
          deleteFlag: input.deleteFlag,
          unitOfMeasure: input.unitOfMeasure,
          steelType: input.steelType,
          gradeMaterial: input.gradeMaterial,
          changeDate: input.changeDate,
          baseUnitOfMeasure: input.baseUnitOfMeasure,
        })
      } else {
        // 새 아이템 생성
        txResult = await insertItem(tx, {
          itemCode: input.itemCode,
          itemName: input.itemName,
          description: input.description,
          parentItemCode: input.parentItemCode,
          itemLevel: input.itemLevel,
          deleteFlag: input.deleteFlag,
          unitOfMeasure: input.unitOfMeasure,
          steelType: input.steelType,
          gradeMaterial: input.gradeMaterial,
          changeDate: input.changeDate,
          baseUnitOfMeasure: input.baseUnitOfMeasure,
        })
      }
      
      return txResult
    })

    // 캐시 무효화
    revalidateTag("items")

    return {
      success: true,
      data: result[0] || null,
      error: null
    }
  } catch (err) {
    console.error("아이템 생성/업데이트 오류:", err)
    
    // 중복 키 오류 처리
    if (err instanceof Error && err.message.includes("unique constraint")) {
      return {
        success: false,
        message: "이미 존재하는 아이템 코드입니다",
        data: null,
        error: "중복 키 오류"
      }
    }

    return {
      success: false,
      message: getErrorMessage(err),
      data: null,
      error: getErrorMessage(err)
    }
  }
}

/* -----------------------------------------------------
   3) 업데이트
----------------------------------------------------- */

/** 단건 업데이트 */
export async function modifyItem(input: UpdateItemSchema & { id: number }) {
  unstable_noStore();
  try {
    await db.transaction(async (tx) => {
      await updateItem(tx, input.id, {
        itemCode: input.itemCode,
        itemName: input.itemName,
        description: input.description,
        parentItemCode: input.parentItemCode,
        itemLevel: input.itemLevel,
        deleteFlag: input.deleteFlag,
        unitOfMeasure: input.unitOfMeasure,
        steelType: input.steelType,
        gradeMaterial: input.gradeMaterial,
        changeDate: input.changeDate,
        baseUnitOfMeasure: input.baseUnitOfMeasure,
      });
    });

    revalidateTag("items");
    return { data: null, error: null };
  } catch (err) {
    return { data: null, error: getErrorMessage(err) };
  }
}



/** 단건 삭제  */
export async function removeItem(input: { id: number }) {
  unstable_noStore();
  try {
    await db.transaction(async (tx) => {
      // 삭제
      await deleteItemById(tx, input.id);
      // 바로 새 Item 생성
    });

    revalidateTag("items");

    return { data: null, error: null };
  } catch (err) {
    return { data: null, error: getErrorMessage(err) };
  }
}

/** 복수 삭제  */
export async function removeItems(input: { ids: number[] }) {
  unstable_noStore();
  try {
    await db.transaction(async (tx) => {
      // 삭제
      await deleteItemsByIds(tx, input.ids);
    });

    revalidateTag("items");

    return { data: null, error: null };
  } catch (err) {
    return { data: null, error: getErrorMessage(err) };
  }
}

export async function getAllItems(): Promise<Item[]> {
  try {
    return await findAllItems(); 
  } catch (err) {
    throw new Error("Failed to get items");
  }
}