diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-07-03 02:48:24 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-07-03 02:48:24 +0000 |
| commit | e4b2bef735e6aab6a5ecae9a017c5c618a6d3a4b (patch) | |
| tree | 827f1524fd78ee9ea6adddf4cef3d6e44937d6da /lib/tech-vendors/repository.ts | |
| parent | 6f22fc9ebc8d175041aa18cf0986592e57d03f63 (diff) | |
(최겸) 기술영업 벤더 아이템 조회 아이콘 기능 추가
Diffstat (limited to 'lib/tech-vendors/repository.ts')
| -rw-r--r-- | lib/tech-vendors/repository.ts | 63 |
1 files changed, 62 insertions, 1 deletions
diff --git a/lib/tech-vendors/repository.ts b/lib/tech-vendors/repository.ts index 72c01a1c..d3c6671c 100644 --- a/lib/tech-vendors/repository.ts +++ b/lib/tech-vendors/repository.ts @@ -2,8 +2,9 @@ import { eq, inArray, count, desc } from "drizzle-orm"; import db from '@/db/db'; -import { sql, SQL } from "drizzle-orm"; +import { SQL } from "drizzle-orm"; import { techVendors, techVendorContacts, techVendorPossibleItems, techVendorItemsView, type TechVendor, type TechVendorContact, type TechVendorItem, type TechVendorWithAttachments, techVendorAttachments } from "@/db/schema/techVendors"; +import { itemShipbuilding, itemOffshoreTop, itemOffshoreHull } from "@/db/schema/items"; export type NewTechVendorContact = typeof techVendorContacts.$inferInsert export type NewTechVendorItem = typeof techVendorPossibleItems.$inferInsert @@ -79,10 +80,14 @@ export async function selectTechVendorsWithAttachments( .from(techVendorAttachments) .where(eq(techVendorAttachments.vendorId, vendor.id)); + // 벤더의 worktype 조회 + const workTypes = await getVendorWorkTypes(tx, vendor.id, vendor.techVendorType); + return { ...vendor, hasAttachments: attachments.length > 0, attachmentsList: attachments, + workTypes: workTypes.join(', '), // 콤마로 구분해서 저장 } as TechVendorWithAttachments; }) ); @@ -326,3 +331,59 @@ export async function insertTechVendorItem( .returning(); } +// 벤더의 worktype 조회 +export async function getVendorWorkTypes( + tx: any, + vendorId: number, + vendorType: string +): Promise<string[]> { + try { + // 벤더의 possible items 조회 + const possibleItems = await tx + .select({ itemCode: techVendorPossibleItems.itemCode }) + .from(techVendorPossibleItems) + .where(eq(techVendorPossibleItems.vendorId, vendorId)); + + if (!possibleItems.length) { + return []; + } + + const itemCodes = possibleItems.map((item: { itemCode: string }) => item.itemCode); + const workTypes: string[] = []; + + // 벤더 타입에 따라 해당하는 아이템 테이블에서 worktype 조회 + if (vendorType.includes('조선')) { + const shipWorkTypes = await tx + .select({ workType: itemShipbuilding.workType }) + .from(itemShipbuilding) + .where(inArray(itemShipbuilding.itemCode, itemCodes)); + + workTypes.push(...shipWorkTypes.map((item: { workType: string | null }) => item.workType).filter(Boolean)); + } + + if (vendorType.includes('해양TOP')) { + const topWorkTypes = await tx + .select({ workType: itemOffshoreTop.workType }) + .from(itemOffshoreTop) + .where(inArray(itemOffshoreTop.itemCode, itemCodes)); + + workTypes.push(...topWorkTypes.map((item: { workType: string | null }) => item.workType).filter(Boolean)); + } + + if (vendorType.includes('해양HULL')) { + const hullWorkTypes = await tx + .select({ workType: itemOffshoreHull.workType }) + .from(itemOffshoreHull) + .where(inArray(itemOffshoreHull.itemCode, itemCodes)); + + workTypes.push(...hullWorkTypes.map((item: { workType: string | null }) => item.workType).filter(Boolean)); + } + + // 중복 제거 후 반환 + const uniqueWorkTypes = [...new Set(workTypes)]; + + return uniqueWorkTypes; + } catch (error) { + return []; + } +} |
