summaryrefslogtreecommitdiff
path: root/db/schema/vendorData.ts
blob: 048e5ef88446088d463c6443f21e8d15ae9ef4e6 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import {
    pgTable,
    text,
    varchar,
    timestamp,
    integer,
    numeric,
    date,
    unique,
    serial,
    jsonb,
    uniqueIndex,
  } from "drizzle-orm/pg-core";
  import { contractItems } from "./contract";
  
  export const forms = pgTable(
    "forms",
    {
      id: integer("id").primaryKey().generatedAlwaysAsIdentity(),
      contractItemId: integer("contract_item_id")
        .notNull()
        .references(() => contractItems.id, { onDelete: "cascade" }),
      formCode: varchar("form_code", { length: 100 }).notNull(),
      formName: varchar("form_name", { length: 255 }).notNull(),
      // tagType: varchar("tag_type", { length: 50 }).notNull(),
      // class: varchar("class", { length: 100 }).notNull(),
      createdAt: timestamp("created_at").defaultNow().notNull(),
      updatedAt: timestamp("updated_at").defaultNow().notNull(),
    },
    (table) => {
      return {
        // contractItemId와 formCode의 조합을 유니크하게 설정
        contractItemFormCodeUnique: uniqueIndex(
          "contract_item_form_code_unique"
        ).on(table.contractItemId, table.formCode),
      };
    }
  );
  
  export const rfqAttachments = pgTable("form_templates", {
    id: serial("id").primaryKey(),
    formId: integer("form_id").references(() => forms.id),
    fileName: varchar("file_name", { length: 255 }).notNull(),
    filePath: varchar("file_path", { length: 1024 }).notNull(),
  
    createdAt: timestamp("created_at").defaultNow().notNull(),
    udpatedAt: timestamp("updated_at").defaultNow().notNull(),
  });
  
  export const formMetas = pgTable("form_metas", {
    id: serial("id").primaryKey(),
    formCode: varchar("form_code", { length: 50 }).notNull(),
    formName: varchar("form_name", { length: 255 }).notNull(),
    columns: jsonb("columns").notNull(),
    createdAt: timestamp("created_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
    updatedAt: timestamp("updated_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
  });
  
  export const formEntries = pgTable("form_entries", {
    id: serial("id").primaryKey(),
    formCode: varchar("form_code", { length: 50 }).notNull(),
    data: jsonb("data").notNull(),
    contractItemId: integer("contract_item_id")
      .notNull()
      .references(() => contractItems.id, { onDelete: "cascade" }),
    createdAt: timestamp("created_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
    updatedAt: timestamp("updated_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
  });
  
  // ============ tags (각 계약 아이템에 대한 Tag) ============
  // "어느 계약의 어느 아이템에 대한 태그"임을 나타내려면 contract_items를 참조
  export const tags = pgTable("tags", {
    id: integer("id").primaryKey().generatedAlwaysAsIdentity(),
  
    // 이 Tag가 속한 "계약 내 아이템" (즉 contract_items.id)
    contractItemId: integer("contract_item_id")
      .notNull()
      .references(() => contractItems.id, { onDelete: "cascade" }),
  
    formId: integer("form_id").references(() => forms.id, {
      onDelete: "set null",
    }),
  
    tagNo: varchar("tag_no", { length: 100 }).notNull(),
    tagType: varchar("tag_type", { length: 50 }).notNull(),
    class: varchar("class", { length: 100 }).notNull(),
    description: text("description"),
  
    createdAt: timestamp("created_at").defaultNow().notNull(),
    updatedAt: timestamp("updated_at").defaultNow().notNull(),
  });
  
  export type Tag = typeof tags.$inferSelect;
  export type Form = typeof forms.$inferSelect;
  export type NewTag = typeof tags.$inferInsert;
  
  export const tagTypes = pgTable("tag_types", {
    code: varchar("code", { length: 50 }).primaryKey(),
    description: text("description").notNull(),
  
    createdAt: timestamp("created_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
    updatedAt: timestamp("updated_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
  });
  
  export const tagSubfields = pgTable(
    "tag_subfields",
    {
      id: serial("id").primaryKey(),
  
      // 외래키: tagTypeCode -> tagTypes.code
      tagTypeCode: varchar("tag_type_code", { length: 50 })
        .notNull()
        .references(() => tagTypes.code, { onDelete: "cascade" }),
  
      /**
       * 나머지 필드
       */
      // tagTypeDescription: -> 이제 불필요. tagTypes.description로 join
      attributesId: varchar("attributes_id", { length: 50 }).notNull(),
      attributesDescription: text("attributes_description").notNull(),
  
      expression: text("expression"),
      delimiter: varchar("delimiter", { length: 10 }),
  
      sortOrder: integer("sort_order").default(0).notNull(),
  
      createdAt: timestamp("created_at", { withTimezone: true })
        .defaultNow()
        .notNull(),
      updatedAt: timestamp("updated_at", { withTimezone: true })
        .defaultNow()
        .notNull(),
    },
    (table) => {
      return {
        uniqTagTypeAttribute: unique("uniq_tag_type_attribute").on(
          table.tagTypeCode,
          table.attributesId
        ),
      };
    }
  );
  
  export const tagSubfieldOptions = pgTable("tag_subfield_options", {
    id: serial("id").primaryKey(),
  
    // 어떤 subfield에 속하는 옵션인지
    attributesId: varchar("attributes_id", { length: 50 })
      .notNull()
      .references(() => tagSubfields.attributesId, { onDelete: "cascade" }),
  
    /**
     * 실제 코드 (예: "PM", "AA", "VB", "VAR", "01", "02" ...)
     */
    code: varchar("code", { length: 50 }).notNull(),
  
    /**
     * 사용자에게 보여줄 레이블 (예: "Pump", "Pneumatic Motor", "Ball Valve", ...)
     */
    label: text("label").notNull(),
  
    /**
     * 생성/수정 시각 (선택)
     */
    createdAt: timestamp("created_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
    updatedAt: timestamp("updated_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
  });
  
  export const tagClasses = pgTable("tag_classes", {
    id: integer("id").primaryKey().generatedAlwaysAsIdentity(),
  
    // 기존 code/label
    code: varchar("code", { length: 100 }).notNull(),
    label: text("label").notNull(),
  
    // 새 필드: tagTypeCode -> references tagTypes.code
    tagTypeCode: varchar("tag_type_code", { length: 50 })
      .notNull()
      .references(() => tagTypes.code, { onDelete: "cascade" }),
  
    createdAt: timestamp("created_at").defaultNow().notNull(),
    updatedAt: timestamp("updated_at").defaultNow().notNull(),
  });
  
  export const tagTypeClassFormMappings = pgTable(
    "tag_type_class_form_mappings",
    {
      id: serial("id").primaryKey(),
  
      tagTypeLabel: varchar("tag_type_label", { length: 255 }).notNull(),
      classLabel: varchar("class_label", { length: 255 }).notNull(),
  
      formCode: varchar("form_code", { length: 50 }).notNull(),
      formName: varchar("form_name", { length: 255 }).notNull(),
  
      createdAt: timestamp("created_at", { withTimezone: true })
        .defaultNow()
        .notNull(),
      updatedAt: timestamp("updated_at", { withTimezone: true })
        .defaultNow()
        .notNull(),
    }
  );
  
  export type TagTypeClassFormMappings =
    typeof tagTypeClassFormMappings.$inferSelect;
  export type TagSubfields = typeof tagSubfields.$inferSelect;
  export type TagSubfieldOption = typeof tagSubfieldOptions.$inferSelect;
  export type TagClasses = typeof tagClasses.$inferSelect;
  
  export const viewTagSubfields = pgTable("view_tag_subfields", {
    id: integer("id").primaryKey(),
  
    tagTypeCode: varchar("tag_type_code", { length: 50 }).notNull(),
    tagTypeDescription: text("tag_type_description"),
    attributesId: varchar("attributes_id", { length: 50 }).notNull(),
    attributesDescription: text("attributes_description").notNull(),
  
    expression: text("expression"),
    delimiter: varchar("delimiter", { length: 10 }),
    sortOrder: integer("sort_order").default(0).notNull(),
  
    createdAt: timestamp("created_at", { withTimezone: true }),
    updatedAt: timestamp("updated_at", { withTimezone: true }),
  });
  
  export type ViewTagSubfields = typeof viewTagSubfields.$inferSelect;
  
  export const vendorDataReportTemps = pgTable("vendor_data_report_temps", {
    id: serial("id").primaryKey(),
    contractItemId: integer("contract_item_id")
      .notNull()
      .references(() => contractItems.id, { onDelete: "cascade" }),
    formId: integer("form_id")
      .notNull()
      .references(() => forms.id, { onDelete: "cascade" }),
    fileName: varchar("file_name", { length: 255 }).notNull(),
    filePath: varchar("file_path", { length: 1024 }).notNull(),
    createdAt: timestamp("created_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
    updatedAt: timestamp("updated_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
  });
  
  export type VendorDataReportTemps = typeof vendorDataReportTemps.$inferSelect;