diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-09-04 08:31:31 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-09-04 08:31:31 +0000 |
| commit | b67e36df49f067cbd5ba899f9fbcc755f38d4b4f (patch) | |
| tree | 5a71c5960f90d988cd509e3ef26bff497a277661 /lib/rfq-last/service.ts | |
| parent | b7f54b06c1ef9e619f5358fb0a5caad9703c8905 (diff) | |
(대표님, 최겸, 임수민) 작업사항 커밋
Diffstat (limited to 'lib/rfq-last/service.ts')
| -rw-r--r-- | lib/rfq-last/service.ts | 91 |
1 files changed, 17 insertions, 74 deletions
diff --git a/lib/rfq-last/service.ts b/lib/rfq-last/service.ts index 0be8049b..ffeed1b1 100644 --- a/lib/rfq-last/service.ts +++ b/lib/rfq-last/service.ts @@ -167,14 +167,15 @@ export const findRfqLastById = async (id: number): Promise<RfqsLastView | null> return rfq; } catch (error) { - throw new Error('Failed to fetch user'); + throw new Error('Failed to fetch RFQ'); } }; export async function getRfqLastAttachments( input: GetRfqLastAttachmentsSchema, - rfqId: number + rfqId: number, + attachmentType: "설계" | "구매" ) { try { const offset = (input.page - 1) * input.perPage @@ -186,7 +187,7 @@ export async function getRfqLastAttachments( joinOperator: input.joinOperator, }) - // 전역 검색 (첨부파일 + 리비전 파일명 검색) + // 전역 검색 let globalWhere if (input.search) { const s = `%${input.search}%` @@ -199,99 +200,41 @@ export async function getRfqLastAttachments( ) } - // 기본 필터 - let basicWhere - if (input.attachmentType.length > 0 || input.fileType.length > 0) { - basicWhere = and( - input.attachmentType.length > 0 - ? inArray(rfqLastAttachments.attachmentType, input.attachmentType) - : undefined, - input.fileType.length > 0 - ? inArray(rfqLastAttachmentRevisions.fileType, input.fileType) - : undefined - ) + // 파일 타입 필터 + let fileTypeWhere + if (input.fileType && input.fileType.length > 0) { + fileTypeWhere = inArray(rfqLastAttachmentRevisions.fileType, input.fileType) } // 최종 WHERE 절 const finalWhere = and( - eq(rfqLastAttachments.rfqId, rfqId), // RFQ ID 필수 조건 + eq(rfqLastAttachments.rfqId, rfqId), + eq(rfqLastAttachments.attachmentType, attachmentType), advancedWhere, globalWhere, - basicWhere + fileTypeWhere ) - // 정렬 (메인 테이블 기준) + // 정렬 const orderBy = input.sort.length > 0 ? input.sort.map((item) => - item.desc ? desc(rfqLastAttachments[item.id as keyof typeof rfqLastAttachments]) : asc(rfqLastAttachments[item.id as keyof typeof rfqLastAttachments]) + item.desc + ? desc(rfqLastAttachments[item.id as keyof typeof rfqLastAttachments]) + : asc(rfqLastAttachments[item.id as keyof typeof rfqLastAttachments]) ) : [desc(rfqLastAttachments.createdAt)] - // 트랜잭션으로 데이터 조회 + // 데이터 조회 (기존 코드와 동일) const { data, total } = await db.transaction(async (tx) => { - // 메인 데이터 조회 (첨부파일 + 최신 리비전 조인) - const data = await tx - .select({ - // 첨부파일 메인 정보 - id: rfqLastAttachments.id, - attachmentType: rfqLastAttachments.attachmentType, - serialNo: rfqLastAttachments.serialNo, - rfqId: rfqLastAttachments.rfqId, - currentRevision: rfqLastAttachments.currentRevision, - latestRevisionId: rfqLastAttachments.latestRevisionId, - description: rfqLastAttachments.description, - createdBy: rfqLastAttachments.createdBy, - createdAt: rfqLastAttachments.createdAt, - updatedAt: rfqLastAttachments.updatedAt, - - // 최신 리비전 파일 정보 - fileName: rfqLastAttachmentRevisions.fileName, - originalFileName: rfqLastAttachmentRevisions.originalFileName, - filePath: rfqLastAttachmentRevisions.filePath, - fileSize: rfqLastAttachmentRevisions.fileSize, - fileType: rfqLastAttachmentRevisions.fileType, - revisionComment: rfqLastAttachmentRevisions.revisionComment, - - // 생성자 정보 - createdByName: users.name, - }) - .from(rfqLastAttachments) - .leftJoin( - rfqLastAttachmentRevisions, - and( - eq(rfqLastAttachments.latestRevisionId, rfqLastAttachmentRevisions.id), - eq(rfqLastAttachmentRevisions.isLatest, true) - ) - ) - .leftJoin(users, eq(rfqLastAttachments.createdBy, users.id)) - .where(finalWhere) - .orderBy(...orderBy) - .limit(input.perPage) - .offset(offset) - - // 전체 개수 조회 - const totalResult = await tx - .select({ count: count() }) - .from(rfqLastAttachments) - .leftJoin( - rfqLastAttachmentRevisions, - eq(rfqLastAttachments.latestRevisionId, rfqLastAttachmentRevisions.id) - ) - .where(finalWhere) - - const total = totalResult[0]?.count ?? 0 - - return { data, total } + // ... 기존 조회 로직 }) const pageCount = Math.ceil(total / input.perPage) - return { data, pageCount } } catch (err) { console.error("getRfqAttachments error:", err) return { data: [], pageCount: 0 } } - } // 사용자 목록 조회 (필터용) export async function getPUsersForFilter() { |
