diff options
| author | joonhoekim <26rote@gmail.com> | 2025-03-27 18:50:59 +0900 |
|---|---|---|
| committer | joonhoekim <26rote@gmail.com> | 2025-03-27 18:50:59 +0900 |
| commit | 281a4060cff0396253192f4e852be6770ad97cbd (patch) | |
| tree | dc7f7d6e01469982638be02889fa1bb95fe60d84 /db/seeds_2 | |
| parent | 34bbeb86c1a8d24b5f526710889b5e54d699cfd0 (diff) | |
feat: POA 관련 파일 추가 및 contract 스키마 수정
Diffstat (limited to 'db/seeds_2')
| -rw-r--r-- | db/seeds_2/poaSeed.ts | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/db/seeds_2/poaSeed.ts b/db/seeds_2/poaSeed.ts new file mode 100644 index 00000000..d93cde14 --- /dev/null +++ b/db/seeds_2/poaSeed.ts @@ -0,0 +1,109 @@ +import { faker } from "@faker-js/faker" +import db from "../db" +import { contracts, poa } from "../schema/contract" +import { sql } from "drizzle-orm" + +export async function seedPOA({ count = 10 } = {}) { + try { + console.log(`📝 Inserting POA ${count}`) + + // 기존 POA 데이터 삭제 및 ID 시퀀스 초기화 + await db.delete(poa) + await db.execute(sql`ALTER SEQUENCE poa_id_seq RESTART WITH 1;`) + console.log("✅ 기존 POA 데이터 삭제 및 ID 초기화 완료") + + // 조선업 맥락에 맞는 예시 문구들 + const deliveryTermsExamples = [ + "FOB 부산항", + "CIF 상하이항", + "DAP 울산조선소", + "DDP 거제 옥포조선소", + ] + const deliveryLocations = [ + "부산 영도조선소", + "울산 본사 도크 #3", + "거제 옥포조선소 해양공장", + "목포신항 부두", + ] + const changeReasonExamples = [ + "납품 일정 조정 필요", + "자재 사양 변경", + "선박 설계 변경에 따른 수정", + "추가 부품 요청", + "납품 장소 변경", + "계약 조건 재협상" + ] + + // 1. 기존 계약(PO) 목록 가져오기 + const existingContracts = await db.select().from(contracts) + console.log(`Found ${existingContracts.length} existing contracts`) + + if (existingContracts.length === 0) { + throw new Error("계약(PO) 데이터가 없습니다. 먼저 계약 데이터를 생성해주세요.") + } + + // 2. POA 생성 + for (let i = 0; i < count; i++) { + try { + // 랜덤으로 원본 계약 선택 + const originalContract = faker.helpers.arrayElement(existingContracts) + console.log(`Selected original contract: ${originalContract.contractNo}`) + + // POA 생성 + const totalAmount = faker.number.float({ min: 5000000, max: 500000000 }) + const discount = faker.helpers.maybe(() => faker.number.float({ min: 0, max: 500000 }), { probability: 0.3 }) + const tax = faker.helpers.maybe(() => faker.number.float({ min: 0, max: 1000000 }), { probability: 0.8 }) + const shippingFee = faker.helpers.maybe(() => faker.number.float({ min: 0, max: 300000 }), { probability: 0.5 }) + const netTotal = totalAmount - (discount || 0) + (tax || 0) + (shippingFee || 0) + + const poaData = { + // Form code는 원본과 동일하게 유지 + contractNo: originalContract.contractNo, + originalContractNo: originalContract.contractNo, + projectId: originalContract.projectId, + vendorId: originalContract.vendorId, + originalContractName: originalContract.contractName, + originalStatus: originalContract.status, + + // 변경 가능한 정보들 + deliveryTerms: faker.helpers.arrayElement(deliveryTermsExamples), + deliveryDate: faker.helpers.maybe(() => faker.date.future().toISOString(), { probability: 0.7 }), + deliveryLocation: faker.helpers.arrayElement(deliveryLocations), + currency: "KRW", + totalAmount: totalAmount.toString(), + discount: discount?.toString(), + tax: tax?.toString(), + shippingFee: shippingFee?.toString(), + netTotal: netTotal.toString(), + changeReason: faker.helpers.arrayElement(changeReasonExamples), + approvalStatus: faker.helpers.arrayElement(["PENDING", "APPROVED", "REJECTED"]), + createdAt: new Date(), + updatedAt: new Date(), + } + + console.log("POA data:", poaData) + + await db.insert(poa).values(poaData) + console.log(`Created POA for contract: ${originalContract.contractNo}`) + } catch (error) { + console.error(`Error creating POA ${i + 1}:`, error) + throw error + } + } + + console.log(`✅ Successfully added ${count} new POAs`) + } catch (error) { + console.error("Error in seedPOA:", error) + throw error + } +} + +// 실행 +if (require.main === module) { + seedPOA({ count: 5 }) + .then(() => process.exit(0)) + .catch((error) => { + console.error(error) + process.exit(1) + }) +}
\ No newline at end of file |
