summaryrefslogtreecommitdiff
path: root/db/seeds/seedIssueStage.ts
blob: 117e2a9930eb5add200bc96d70f8222efd6bacff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
 * seedIssueStages.ts
 * - issueStages 테이블에 테스트 데이터 삽입
 */

import db from "@/db/db"
import { faker } from "@faker-js/faker"
import { documents, issueStages } from "../schema/vendorDocu"

// 하나의 문서당 스테이지 개수 범위
const STAGE_MIN = 1
const STAGE_MAX = 3

async function seedIssueStages() {
  try {
    console.log("Seeding issue stages started...")
    
    // 먼저 documents 테이블에서 모든 문서 ID 가져오기
    const allDocs = await db.select({ id: documents.id }).from(documents)
    console.log(`Found ${allDocs.length} documents to create stages for.`)
    
    if (allDocs.length === 0) {
      console.warn("No documents found. Run seedDocuments.ts first.")
      return
    }
    
    // 각 문서에 대해 스테이지 생성 및 삽입
    let totalStagesCreated = 0
    
    for (const doc of allDocs) {
      const stageCount = faker.number.int({ min: STAGE_MIN, max: STAGE_MAX })
      const stagesData = []
      
      for (let j = 0; j < stageCount; j++) {
        stagesData.push({
          documentId: doc.id,
          stageName: faker.helpers.arrayElement([
            "Issued for Review",
            "IFC",
            "AFC",
            "As-Built",
          ]),
          planDate: faker.helpers.maybe(() => faker.date.future({ years: 0.5 }), {
            probability: 0.5,
          }),
          actualDate: faker.helpers.maybe(() => faker.date.future({ years: 0.5 }), {
            probability: 0.5,
          })
        })
      }
      
      // 한 번에 많은 데이터를 삽입하지 않고 문서별로 삽입
      try {
        await db.insert(issueStages).values(stagesData)
        totalStagesCreated += stagesData.length
        
        // 진행 상황 로깅 (50개마다)
        if (totalStagesCreated % 50 === 0) {
          console.log(`Created ${totalStagesCreated} stages so far...`)
        }
      } catch (error) {
        console.error(`Error inserting stages for document ${doc.id}:`, error)
      }
    }

    console.log("Seeding issue stages completed successfully.")
  } catch (err) {
    console.error("Seeding issue stages error:", err)
    process.exit(1)
  }
}

// 스크립트 직접 실행 시
seedIssueStages()
  .then(() => process.exit(0))
  .catch(() => process.exit(1))