diff options
Diffstat (limited to 'lib/tech-vendors/service.ts')
| -rw-r--r-- | lib/tech-vendors/service.ts | 101 |
1 files changed, 67 insertions, 34 deletions
diff --git a/lib/tech-vendors/service.ts b/lib/tech-vendors/service.ts index 15e7331b..71d47e05 100644 --- a/lib/tech-vendors/service.ts +++ b/lib/tech-vendors/service.ts @@ -846,13 +846,15 @@ export async function getItemsByVendorType(vendorType: string, itemCode: string) })); return { data: result, error: null }; - } catch (error) { + } catch (err) { + console.error("Error fetching items by vendor type:", err); return { data: [], error: "Failed to fetch items" }; } } /** * 벤더의 possible_items를 조회하고 해당 아이템 코드로 각 타입별 테이블을 조회 + * 벤더 타입이 콤마로 구분된 경우 (예: "조선,해양TOP,해양HULL") 모든 타입의 아이템을 조회 */ export async function getVendorItemsByType(vendorId: number, vendorType: string) { try { @@ -865,47 +867,61 @@ export async function getVendorItemsByType(vendorId: number, vendorType: string) }) const itemCodes = possibleItems.map(item => item.itemCode) + + if (itemCodes.length === 0) { + return { data: [] } + } - // 벤더 타입에 따라 해당하는 테이블에서 아이템 조회 - switch (vendorType) { - case "조선": - const shipbuildingItems = await db.query.itemShipbuilding.findMany({ - where: inArray(itemShipbuilding.itemCode, itemCodes) - }) - return { - data: shipbuildingItems.map(item => ({ + // 벤더 타입을 콤마로 분리 + const vendorTypes = vendorType.split(',').map(type => type.trim()) + const allItems: Array<Record<string, any> & { techVendorType: "조선" | "해양TOP" | "해양HULL" }> = [] + + // 각 벤더 타입에 따라 해당하는 테이블에서 아이템 조회 + for (const singleType of vendorTypes) { + switch (singleType) { + case "조선": + const shipbuildingItems = await db.query.itemShipbuilding.findMany({ + where: inArray(itemShipbuilding.itemCode, itemCodes) + }) + allItems.push(...shipbuildingItems.map(item => ({ ...item, - techVendorType: "조선" - })) - } + techVendorType: "조선" as const + }))) + break - case "해양TOP": - const offshoreTopItems = await db.query.itemOffshoreTop.findMany({ - where: inArray(itemOffshoreTop.itemCode, itemCodes) - }) - return { - data: offshoreTopItems.map(item => ({ + case "해양TOP": + const offshoreTopItems = await db.query.itemOffshoreTop.findMany({ + where: inArray(itemOffshoreTop.itemCode, itemCodes) + }) + allItems.push(...offshoreTopItems.map(item => ({ ...item, - techVendorType: "해양TOP" - })) - } + techVendorType: "해양TOP" as const + }))) + break - case "해양HULL": - const offshoreHullItems = await db.query.itemOffshoreHull.findMany({ - where: inArray(itemOffshoreHull.itemCode, itemCodes) - }) - return { - data: offshoreHullItems.map(item => ({ + case "해양HULL": + const offshoreHullItems = await db.query.itemOffshoreHull.findMany({ + where: inArray(itemOffshoreHull.itemCode, itemCodes) + }) + allItems.push(...offshoreHullItems.map(item => ({ ...item, - techVendorType: "해양HULL" - })) - } + techVendorType: "해양HULL" as const + }))) + break - default: - throw new Error(`Unsupported vendor type: ${vendorType}`) + default: + console.warn(`Unknown vendor type: ${singleType}`) + break + } } - } catch (error) { - throw error + + // 중복 허용 - 모든 아이템을 그대로 반환 + return { + data: allItems.sort((a, b) => a.itemCode.localeCompare(b.itemCode)) + } + } catch (err) { + console.error("Error getting vendor items by type:", err) + return { data: [] } } } @@ -1645,3 +1661,20 @@ export async function addTechVendor(input: { } } +/** + * 벤더의 possible items 개수 조회 + */ +export async function getTechVendorPossibleItemsCount(vendorId: number): Promise<number> { + try { + const result = await db + .select({ count: sql<number>`count(*)`.as("count") }) + .from(techVendorPossibleItems) + .where(eq(techVendorPossibleItems.vendorId, vendorId)); + + return result[0]?.count || 0; + } catch (err) { + console.error("Error getting tech vendor possible items count:", err); + return 0; + } +} + |
