diff options
Diffstat (limited to 'lib/bidding/service.ts')
| -rw-r--r-- | lib/bidding/service.ts | 231 |
1 files changed, 87 insertions, 144 deletions
diff --git a/lib/bidding/service.ts b/lib/bidding/service.ts index 1ae23e81..0064b66f 100644 --- a/lib/bidding/service.ts +++ b/lib/bidding/service.ts @@ -39,6 +39,7 @@ import { import { revalidatePath } from 'next/cache' import { filterColumns } from '@/lib/filter-columns' import { GetBiddingsSchema, CreateBiddingSchema } from './validation' +import { saveFile } from '../file-stroage' @@ -57,7 +58,6 @@ export async function getUserCodeByEmail(email: string): Promise<string | null> return null } } -import { saveFile } from '../file-stroage' // userId를 user.name으로 변환하는 유틸리티 함수 async function getUserNameById(userId: string): Promise<string> { @@ -798,21 +798,47 @@ export async function createBidding(input: CreateBiddingInput, userId: string) { } } - // 담당자 정보 준비 - let bidPicId = input.bidPicId ? parseInt(input.bidPicId.toString()) : null + // 담당자 정보 준비 - bidPicCode로 users 테이블에서 조회 + let bidPicId: number | null = null let bidPicName = input.bidPicName || null - if (!bidPicId && input.bidPicCode) { + if (input.bidPicCode) { try { - const userInfo = await findUserInfoByEKGRP(input.bidPicCode) - if (userInfo) { - bidPicId = userInfo.userId - bidPicName = userInfo.userName + const user = await db + .select({ id: users.id, name: users.name }) + .from(users) + .where(eq(users.userCode, input.bidPicCode)) + .limit(1) + + if (user.length > 0) { + bidPicId = user[0].id + bidPicName = bidPicName || user[0].name } } catch (e) { - console.error('Failed to find user info by EKGRP:', e) + console.error('Failed to find user by userCode:', e) } } + + // // 조달담당자 정보 준비 - supplyPicCode로 users 테이블에서 조회 + // let supplyPicId: number | null = null + // let supplyPicName = input.supplyPicName || null + + // if (input.supplyPicCode) { + // try { + // const user = await db + // .select({ id: users.id, name: users.name }) + // .from(users) + // .where(eq(users.userCode, input.supplyPicCode)) + // .limit(1) + + // if (user.length > 0) { + // supplyPicId = user[0].id + // supplyPicName = supplyPicName || user[0].name + // } + // } catch (e) { + // console.error('Failed to find user by procurementManagerCode:', e) + // } + // } // 1. 입찰 생성 const [newBidding] = await tx @@ -1161,140 +1187,7 @@ export async function createBidding(input: CreateBiddingInput, userId: string) { } } } -// 입찰 수정 -export async function updateBidding(input: UpdateBiddingInput, userId: string) { - try { - const userName = await getUserNameById(userId) - // 존재 여부 확인 - const existing = await db - .select({ id: biddings.id }) - .from(biddings) - .where(eq(biddings.id, input.id)) - .limit(1) - - if (existing.length === 0) { - return { - success: false, - error: '존재하지 않는 입찰입니다.' - } - } - // 입찰번호 중복 체크 (다른 레코드에서) - if (input.biddingNumber) { - const duplicate = await db - .select({ id: biddings.id }) - .from(biddings) - .where(eq(biddings.biddingNumber, input.biddingNumber)) - .limit(1) - - if (duplicate.length > 0 && duplicate[0].id !== input.id) { - return { - success: false, - error: '이미 존재하는 입찰번호입니다.' - } - } - } - - // 날짜 문자열을 Date 객체로 변환 - const parseDate = (dateStr?: string) => { - if (!dateStr) return undefined - try { - return new Date(dateStr) - } catch { - return undefined - } - } - - // 업데이트할 데이터 준비 - const updateData: any = { - updatedAt: new Date(), - updatedBy: userName, - } - - // 정의된 필드들만 업데이트 - if (input.biddingNumber !== undefined) updateData.biddingNumber = input.biddingNumber - if (input.revision !== undefined) updateData.revision = input.revision - if (input.projectName !== undefined) updateData.projectName = input.projectName - if (input.itemName !== undefined) updateData.itemName = input.itemName - if (input.title !== undefined) updateData.title = input.title - if (input.description !== undefined) updateData.description = input.description - if (input.content !== undefined) updateData.content = input.content - - if (input.contractType !== undefined) updateData.contractType = input.contractType - if (input.noticeType !== undefined) updateData.noticeType = input.noticeType - if (input.biddingType !== undefined) updateData.biddingType = input.biddingType - if (input.awardCount !== undefined) updateData.awardCount = input.awardCount - if (input.contractStartDate !== undefined) updateData.contractStartDate = parseDate(input.contractStartDate) - if (input.contractEndDate !== undefined) updateData.contractEndDate = parseDate(input.contractEndDate) - - if (input.submissionStartDate !== undefined) updateData.submissionStartDate = parseDate(input.submissionStartDate) - if (input.submissionEndDate !== undefined) updateData.submissionEndDate = parseDate(input.submissionEndDate) - if (input.evaluationDate !== undefined) updateData.evaluationDate = parseDate(input.evaluationDate) - - if (input.hasSpecificationMeeting !== undefined) updateData.hasSpecificationMeeting = input.hasSpecificationMeeting - if (input.hasPrDocument !== undefined) updateData.hasPrDocument = input.hasPrDocument - if (input.prNumber !== undefined) updateData.prNumber = input.prNumber - - if (input.currency !== undefined) updateData.currency = input.currency - if (input.budget !== undefined) updateData.budget = input.budget ? parseFloat(input.budget) : null - if (input.targetPrice !== undefined) updateData.targetPrice = input.targetPrice ? parseFloat(input.targetPrice) : null - if (input.finalBidPrice !== undefined) updateData.finalBidPrice = input.finalBidPrice ? parseFloat(input.finalBidPrice) : null - - if (input.status !== undefined) updateData.status = input.status - if (input.isPublic !== undefined) updateData.isPublic = input.isPublic - if (input.isUrgent !== undefined) updateData.isUrgent = input.isUrgent - - // 구매조직 - if (input.purchasingOrganization !== undefined) updateData.purchasingOrganization = input.purchasingOrganization - - // 담당자 정보 (user FK) - if (input.bidPicId !== undefined) updateData.bidPicId = input.bidPicId - if (input.bidPicName !== undefined) updateData.bidPicName = input.bidPicName - - // bidPicCode가 있으면 담당자 정보 자동 조회 및 업데이트 - if (input.bidPicCode !== undefined) { - updateData.bidPicCode = input.bidPicCode - // bidPicId가 명시적으로 제공되지 않았고 코드가 있는 경우 자동 조회 - if (!input.bidPicId && input.bidPicCode) { - try { - const userInfo = await findUserInfoByEKGRP(input.bidPicCode) - if (userInfo) { - updateData.bidPicId = userInfo.userId - updateData.bidPicName = userInfo.userName - } - } catch (e) { - console.error('Failed to find user info by EKGRP:', e) - } - } - } - - if (input.supplyPicId !== undefined) updateData.supplyPicId = input.supplyPicId - if (input.supplyPicName !== undefined) updateData.supplyPicName = input.supplyPicName - - if (input.remarks !== undefined) updateData.remarks = input.remarks - - // 입찰 수정 - await db - .update(biddings) - .set(updateData) - .where(eq(biddings.id, input.id)) - - revalidatePath('/evcp/bid') - revalidatePath(`/evcp/bid/${input.id}/info`) - - return { - success: true, - message: '입찰이 성공적으로 수정되었습니다.' - } - - } catch (error) { - console.error('Error updating bidding:', error) - return { - success: false, - error: '입찰 수정 중 오류가 발생했습니다.' - } - } -} // 입찰 삭제 export async function deleteBidding(id: number) { @@ -1904,12 +1797,62 @@ export async function updateBiddingBasicInfo( if (updates.hasPrDocument !== undefined) updateData.hasPrDocument = updates.hasPrDocument if (updates.currency !== undefined) updateData.currency = updates.currency if (updates.purchasingOrganization !== undefined) updateData.purchasingOrganization = updates.purchasingOrganization - if (updates.bidPicName !== undefined) updateData.bidPicName = updates.bidPicName - if (updates.bidPicCode !== undefined) updateData.bidPicCode = updates.bidPicCode if (updates.supplyPicName !== undefined) updateData.supplyPicName = updates.supplyPicName if (updates.supplyPicCode !== undefined) updateData.supplyPicCode = updates.supplyPicCode if (updates.requesterName !== undefined) updateData.requesterName = updates.requesterName if (updates.remarks !== undefined) updateData.remarks = updates.remarks + + // 담당자 정보 - bidPicCode로 users 테이블에서 조회 + if (updates.bidPicCode !== undefined) { + updateData.bidPicCode = updates.bidPicCode + + if (updates.bidPicCode) { + try { + const user = await db + .select({ id: users.id, name: users.name }) + .from(users) + .where(eq(users.userCode, updates.bidPicCode)) + .limit(1) + + if (user.length > 0) { + updateData.bidPicId = user[0].id + updateData.bidPicName = updates.bidPicName || user[0].name + } + } catch (e) { + console.error('Failed to find user by userCode:', e) + } + } + } + + if (updates.bidPicName !== undefined && updates.bidPicCode === undefined) { + updateData.bidPicName = updates.bidPicName + } + + // // 조달담당자 정보 - supplyPicCode로 users 테이블에서 조회 + // if (updates.supplyPicCode !== undefined) { + // updateData.supplyPicCode = updates.supplyPicCode + + // if (updates.supplyPicCode) { + // try { + // const user = await db + // .select({ id: users.id, name: users.name }) + // .from(users) + // .where(eq(users.userCode, updates.supplyPicCode)) + // .limit(1) + + // if (user.length > 0) { + // updateData.supplyPicId = user[0].id + // updateData.supplyPicName = updates.supplyPicName || user[0].name + // } + // } catch (e) { + // console.error('Failed to find user by procurementManagerCode:', e) + // } + // } + // } + + // if (updates.supplyPicName !== undefined && updates.supplyPicCode === undefined) { + // updateData.supplyPicName = updates.supplyPicName + // } // 데이터베이스 업데이트 await db |
