@axova/shared 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONFIGURATION_GUIDE.md +1 -0
- package/README.md +384 -0
- package/SCHEMA_ORGANIZATION.md +209 -0
- package/dist/configs/index.d.ts +85 -0
- package/dist/configs/index.js +555 -0
- package/dist/events/kafka.d.ts +40 -0
- package/dist/events/kafka.js +311 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +41 -0
- package/dist/interfaces/customer-events.d.ts +85 -0
- package/dist/interfaces/customer-events.js +2 -0
- package/dist/interfaces/inventory-events.d.ts +453 -0
- package/dist/interfaces/inventory-events.js +3 -0
- package/dist/interfaces/inventory-types.d.ts +894 -0
- package/dist/interfaces/inventory-types.js +3 -0
- package/dist/interfaces/order-events.d.ts +320 -0
- package/dist/interfaces/order-events.js +3 -0
- package/dist/lib/auditLogger.d.ts +162 -0
- package/dist/lib/auditLogger.js +626 -0
- package/dist/lib/authOrganization.d.ts +24 -0
- package/dist/lib/authOrganization.js +110 -0
- package/dist/lib/db.d.ts +6 -0
- package/dist/lib/db.js +88 -0
- package/dist/middleware/serviceAuth.d.ts +60 -0
- package/dist/middleware/serviceAuth.js +272 -0
- package/dist/middleware/storeOwnership.d.ts +15 -0
- package/dist/middleware/storeOwnership.js +156 -0
- package/dist/middleware/storeValidationMiddleware.d.ts +44 -0
- package/dist/middleware/storeValidationMiddleware.js +180 -0
- package/dist/middleware/userAuth.d.ts +27 -0
- package/dist/middleware/userAuth.js +218 -0
- package/dist/schemas/admin/admin-schema.d.ts +741 -0
- package/dist/schemas/admin/admin-schema.js +111 -0
- package/dist/schemas/ai-moderation/ai-moderation-schema.d.ts +648 -0
- package/dist/schemas/ai-moderation/ai-moderation-schema.js +88 -0
- package/dist/schemas/common/common-schemas.d.ts +436 -0
- package/dist/schemas/common/common-schemas.js +94 -0
- package/dist/schemas/compliance/compliance-schema.d.ts +3388 -0
- package/dist/schemas/compliance/compliance-schema.js +472 -0
- package/dist/schemas/compliance/kyc-schema.d.ts +2642 -0
- package/dist/schemas/compliance/kyc-schema.js +361 -0
- package/dist/schemas/customer/customer-schema.d.ts +2727 -0
- package/dist/schemas/customer/customer-schema.js +399 -0
- package/dist/schemas/index.d.ts +27 -0
- package/dist/schemas/index.js +138 -0
- package/dist/schemas/inventory/inventory-tables.d.ts +9476 -0
- package/dist/schemas/inventory/inventory-tables.js +1470 -0
- package/dist/schemas/inventory/lot-tables.d.ts +3281 -0
- package/dist/schemas/inventory/lot-tables.js +608 -0
- package/dist/schemas/order/order-schema.d.ts +5825 -0
- package/dist/schemas/order/order-schema.js +954 -0
- package/dist/schemas/product/discount-relations.d.ts +15 -0
- package/dist/schemas/product/discount-relations.js +34 -0
- package/dist/schemas/product/discount-schema.d.ts +1975 -0
- package/dist/schemas/product/discount-schema.js +297 -0
- package/dist/schemas/product/product-relations.d.ts +41 -0
- package/dist/schemas/product/product-relations.js +133 -0
- package/dist/schemas/product/product-schema.d.ts +4544 -0
- package/dist/schemas/product/product-schema.js +671 -0
- package/dist/schemas/store/store-audit-schema.d.ts +4135 -0
- package/dist/schemas/store/store-audit-schema.js +556 -0
- package/dist/schemas/store/store-schema.d.ts +3100 -0
- package/dist/schemas/store/store-schema.js +381 -0
- package/dist/schemas/store/store-settings-schema.d.ts +665 -0
- package/dist/schemas/store/store-settings-schema.js +141 -0
- package/dist/schemas/types.d.ts +50 -0
- package/dist/schemas/types.js +3 -0
- package/dist/types/events.d.ts +2396 -0
- package/dist/types/events.js +505 -0
- package/dist/utils/errorHandler.d.ts +12 -0
- package/dist/utils/errorHandler.js +36 -0
- package/dist/utils/subdomain.d.ts +6 -0
- package/dist/utils/subdomain.js +20 -0
- package/nul +8 -0
- package/package.json +43 -0
- package/src/configs/index.ts +654 -0
- package/src/events/kafka.ts +429 -0
- package/src/index.ts +26 -0
- package/src/interfaces/customer-events.ts +106 -0
- package/src/interfaces/inventory-events.ts +545 -0
- package/src/interfaces/inventory-types.ts +1004 -0
- package/src/interfaces/order-events.ts +381 -0
- package/src/lib/auditLogger.ts +1117 -0
- package/src/lib/authOrganization.ts +153 -0
- package/src/lib/db.ts +64 -0
- package/src/middleware/serviceAuth.ts +328 -0
- package/src/middleware/storeOwnership.ts +199 -0
- package/src/middleware/storeValidationMiddleware.ts +247 -0
- package/src/middleware/userAuth.ts +248 -0
- package/src/schemas/admin/admin-schema.ts +208 -0
- package/src/schemas/ai-moderation/ai-moderation-schema.ts +180 -0
- package/src/schemas/common/common-schemas.ts +108 -0
- package/src/schemas/compliance/compliance-schema.ts +927 -0
- package/src/schemas/compliance/kyc-schema.ts +649 -0
- package/src/schemas/customer/customer-schema.ts +576 -0
- package/src/schemas/index.ts +189 -0
- package/src/schemas/inventory/inventory-tables.ts +1927 -0
- package/src/schemas/inventory/lot-tables.ts +799 -0
- package/src/schemas/order/order-schema.ts +1400 -0
- package/src/schemas/product/discount-relations.ts +44 -0
- package/src/schemas/product/discount-schema.ts +464 -0
- package/src/schemas/product/product-relations.ts +187 -0
- package/src/schemas/product/product-schema.ts +955 -0
- package/src/schemas/store/ethiopian_business_api.md.resolved +212 -0
- package/src/schemas/store/store-audit-schema.ts +1257 -0
- package/src/schemas/store/store-schema.ts +661 -0
- package/src/schemas/store/store-settings-schema.ts +231 -0
- package/src/schemas/types.ts +67 -0
- package/src/types/events.ts +646 -0
- package/src/utils/errorHandler.ts +44 -0
- package/src/utils/subdomain.ts +19 -0
- package/tsconfig.json +21 -0
|
@@ -0,0 +1,954 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.orderNotesRelations = exports.orderHistoryRelations = exports.orderDiscountsRelations = exports.orderFulfillmentsRelations = exports.orderPaymentsRelations = exports.orderAddressesRelations = exports.orderItemsRelations = exports.ordersRelations = exports.orderNotes = exports.orderHistory = exports.orderDiscounts = exports.orderFulfillments = exports.orderPayments = exports.orderAddresses = exports.orderItems = exports.orders = exports.refundReasonEnum = exports.paymentMethodEnum = exports.addressTypeEnum = exports.discountTypeEnum = exports.orderPriorityEnum = exports.fulfillmentStatusEnum = exports.paymentStatusEnum = exports.orderStatusEnum = exports.orderTypeEnum = void 0;
|
|
4
|
+
const cuid2_1 = require("@paralleldrive/cuid2");
|
|
5
|
+
const drizzle_orm_1 = require("drizzle-orm");
|
|
6
|
+
const pg_core_1 = require("drizzle-orm/pg-core");
|
|
7
|
+
// =====================================================
|
|
8
|
+
// ENUMS
|
|
9
|
+
// =====================================================
|
|
10
|
+
exports.orderTypeEnum = (0, pg_core_1.pgEnum)("order_type", [
|
|
11
|
+
"ONLINE",
|
|
12
|
+
"POS",
|
|
13
|
+
"PICKUP",
|
|
14
|
+
"DELIVERY",
|
|
15
|
+
"MARKETPLACE",
|
|
16
|
+
"PHONE",
|
|
17
|
+
"WHOLESALE",
|
|
18
|
+
"SUBSCRIPTION",
|
|
19
|
+
"SUBSCRIPTION_RENEWAL",
|
|
20
|
+
]);
|
|
21
|
+
exports.orderStatusEnum = (0, pg_core_1.pgEnum)("order_status", [
|
|
22
|
+
"DRAFT",
|
|
23
|
+
"PENDING",
|
|
24
|
+
"CONFIRMED",
|
|
25
|
+
"PROCESSING",
|
|
26
|
+
"PICKING",
|
|
27
|
+
"PACKED",
|
|
28
|
+
"SHIPPED",
|
|
29
|
+
"OUT_FOR_DELIVERY",
|
|
30
|
+
"DELIVERED",
|
|
31
|
+
"COMPLETED",
|
|
32
|
+
"CANCELLED",
|
|
33
|
+
"REFUNDED",
|
|
34
|
+
"PARTIALLY_REFUNDED",
|
|
35
|
+
"RETURNED",
|
|
36
|
+
"PARTIALLY_RETURNED",
|
|
37
|
+
"DISPUTED",
|
|
38
|
+
"ON_HOLD",
|
|
39
|
+
"FAILED",
|
|
40
|
+
]);
|
|
41
|
+
exports.paymentStatusEnum = (0, pg_core_1.pgEnum)("payment_status", [
|
|
42
|
+
"PENDING",
|
|
43
|
+
"AUTHORIZED",
|
|
44
|
+
"PAID",
|
|
45
|
+
"PARTIALLY_PAID",
|
|
46
|
+
"FAILED",
|
|
47
|
+
"CANCELLED",
|
|
48
|
+
"REFUNDED",
|
|
49
|
+
"PARTIALLY_REFUNDED",
|
|
50
|
+
"VOIDED",
|
|
51
|
+
"CHARGEBACK",
|
|
52
|
+
"DISPUTED",
|
|
53
|
+
]);
|
|
54
|
+
exports.fulfillmentStatusEnum = (0, pg_core_1.pgEnum)("fulfillment_status", [
|
|
55
|
+
"PENDING",
|
|
56
|
+
"ALLOCATED",
|
|
57
|
+
"PICKING",
|
|
58
|
+
"PICKED",
|
|
59
|
+
"PACKED",
|
|
60
|
+
"READY_FOR_PICKUP",
|
|
61
|
+
"SHIPPED",
|
|
62
|
+
"IN_TRANSIT",
|
|
63
|
+
"OUT_FOR_DELIVERY",
|
|
64
|
+
"DELIVERED",
|
|
65
|
+
"ATTEMPTED_DELIVERY",
|
|
66
|
+
"DELIVERY_FAILED",
|
|
67
|
+
"RETURNED_TO_SENDER",
|
|
68
|
+
"CANCELLED",
|
|
69
|
+
"LOST",
|
|
70
|
+
]);
|
|
71
|
+
exports.orderPriorityEnum = (0, pg_core_1.pgEnum)("order_priority", [
|
|
72
|
+
"LOW",
|
|
73
|
+
"NORMAL",
|
|
74
|
+
"HIGH",
|
|
75
|
+
"URGENT",
|
|
76
|
+
"CRITICAL",
|
|
77
|
+
]);
|
|
78
|
+
exports.discountTypeEnum = (0, pg_core_1.pgEnum)("discount_type", [
|
|
79
|
+
"PERCENTAGE",
|
|
80
|
+
"FIXED_AMOUNT",
|
|
81
|
+
"FREE_SHIPPING",
|
|
82
|
+
"BOGO",
|
|
83
|
+
"LOYALTY_POINTS",
|
|
84
|
+
"COUPON",
|
|
85
|
+
"PROMOTIONAL",
|
|
86
|
+
"BULK",
|
|
87
|
+
"EARLY_BIRD",
|
|
88
|
+
]);
|
|
89
|
+
exports.addressTypeEnum = (0, pg_core_1.pgEnum)("address_type", [
|
|
90
|
+
"BILLING",
|
|
91
|
+
"SHIPPING",
|
|
92
|
+
"PICKUP",
|
|
93
|
+
"DELIVERY",
|
|
94
|
+
]);
|
|
95
|
+
exports.paymentMethodEnum = (0, pg_core_1.pgEnum)("payment_method", [
|
|
96
|
+
"CREDIT_CARD",
|
|
97
|
+
"DEBIT_CARD",
|
|
98
|
+
"PAYPAL",
|
|
99
|
+
"APPLE_PAY",
|
|
100
|
+
"GOOGLE_PAY",
|
|
101
|
+
"BANK_TRANSFER",
|
|
102
|
+
"CASH",
|
|
103
|
+
"CHECK",
|
|
104
|
+
"STORE_CREDIT",
|
|
105
|
+
"GIFT_CARD",
|
|
106
|
+
"CRYPTOCURRENCY",
|
|
107
|
+
"BUY_NOW_PAY_LATER",
|
|
108
|
+
"INVOICE",
|
|
109
|
+
"COD", // Cash on Delivery
|
|
110
|
+
]);
|
|
111
|
+
exports.refundReasonEnum = (0, pg_core_1.pgEnum)("refund_reason", [
|
|
112
|
+
"CUSTOMER_REQUEST",
|
|
113
|
+
"DEFECTIVE_PRODUCT",
|
|
114
|
+
"WRONG_ITEM",
|
|
115
|
+
"NOT_AS_DESCRIBED",
|
|
116
|
+
"DAMAGED_IN_SHIPPING",
|
|
117
|
+
"QUALITY_ISSUE",
|
|
118
|
+
"SIZE_ISSUE",
|
|
119
|
+
"CHANGE_OF_MIND",
|
|
120
|
+
"DUPLICATE_ORDER",
|
|
121
|
+
"FRAUD_PREVENTION",
|
|
122
|
+
"BUSINESS_POLICY",
|
|
123
|
+
"GOODWILL",
|
|
124
|
+
]);
|
|
125
|
+
// =====================================================
|
|
126
|
+
// CORE ORDERS TABLE
|
|
127
|
+
// =====================================================
|
|
128
|
+
exports.orders = (0, pg_core_1.pgTable)("orders", {
|
|
129
|
+
id: (0, pg_core_1.text)("id")
|
|
130
|
+
.primaryKey()
|
|
131
|
+
.$defaultFn(() => (0, cuid2_1.createId)()),
|
|
132
|
+
// External References
|
|
133
|
+
storeId: (0, pg_core_1.text)("store_id").notNull(),
|
|
134
|
+
customerId: (0, pg_core_1.text)("customer_id"), // Nullable for guest orders
|
|
135
|
+
userId: (0, pg_core_1.text)("user_id"), // Account holder
|
|
136
|
+
sessionId: (0, pg_core_1.text)("session_id"), // Browser/device session
|
|
137
|
+
// Order Identification
|
|
138
|
+
orderNumber: (0, pg_core_1.varchar)("order_number", { length: 50 }).notNull(),
|
|
139
|
+
displayOrderNumber: (0, pg_core_1.varchar)("display_order_number", { length: 50 }), // Customer-friendly number
|
|
140
|
+
externalOrderId: (0, pg_core_1.text)("external_order_id"), // For marketplace orders
|
|
141
|
+
parentOrderId: (0, pg_core_1.text)("parent_order_id"), // For split orders
|
|
142
|
+
// Order Classification
|
|
143
|
+
orderType: (0, exports.orderTypeEnum)("order_type").notNull(),
|
|
144
|
+
orderSource: (0, pg_core_1.varchar)("order_source", { length: 50 })
|
|
145
|
+
.notNull()
|
|
146
|
+
.$type(),
|
|
147
|
+
marketplaceInfo: (0, pg_core_1.jsonb)("marketplace_info").$type(),
|
|
148
|
+
// Status Management
|
|
149
|
+
status: (0, exports.orderStatusEnum)("status").notNull().default("DRAFT"),
|
|
150
|
+
paymentStatus: (0, exports.paymentStatusEnum)("payment_status")
|
|
151
|
+
.notNull()
|
|
152
|
+
.default("PENDING"),
|
|
153
|
+
fulfillmentStatus: (0, exports.fulfillmentStatusEnum)("fulfillment_status")
|
|
154
|
+
.notNull()
|
|
155
|
+
.default("PENDING"),
|
|
156
|
+
// Priority and Flags
|
|
157
|
+
priority: (0, exports.orderPriorityEnum)("priority").notNull().default("NORMAL"),
|
|
158
|
+
isGuestOrder: (0, pg_core_1.boolean)("is_guest_order").notNull().default(false),
|
|
159
|
+
isTestOrder: (0, pg_core_1.boolean)("is_test_order").notNull().default(false),
|
|
160
|
+
isGift: (0, pg_core_1.boolean)("is_gift").notNull().default(false),
|
|
161
|
+
isPreorder: (0, pg_core_1.boolean)("is_preorder").notNull().default(false),
|
|
162
|
+
isSubscription: (0, pg_core_1.boolean)("is_subscription").notNull().default(false),
|
|
163
|
+
requiresShipping: (0, pg_core_1.boolean)("requires_shipping").notNull().default(true),
|
|
164
|
+
requiresPickup: (0, pg_core_1.boolean)("requires_pickup").notNull().default(false),
|
|
165
|
+
// Financial Information
|
|
166
|
+
currency: (0, pg_core_1.varchar)("currency", { length: 3 }).notNull().default("USD"),
|
|
167
|
+
exchangeRate: (0, pg_core_1.decimal)("exchange_rate", { precision: 10, scale: 6 }).default("1.000000"),
|
|
168
|
+
// Amounts (in minor currency units - cents)
|
|
169
|
+
subtotalAmount: (0, pg_core_1.decimal)("subtotal_amount", { precision: 12, scale: 2 })
|
|
170
|
+
.notNull()
|
|
171
|
+
.default("0.00"),
|
|
172
|
+
taxAmount: (0, pg_core_1.decimal)("tax_amount", { precision: 12, scale: 2 })
|
|
173
|
+
.notNull()
|
|
174
|
+
.default("0.00"),
|
|
175
|
+
shippingAmount: (0, pg_core_1.decimal)("shipping_amount", { precision: 12, scale: 2 })
|
|
176
|
+
.notNull()
|
|
177
|
+
.default("0.00"),
|
|
178
|
+
discountAmount: (0, pg_core_1.decimal)("discount_amount", { precision: 12, scale: 2 })
|
|
179
|
+
.notNull()
|
|
180
|
+
.default("0.00"),
|
|
181
|
+
tipAmount: (0, pg_core_1.decimal)("tip_amount", { precision: 12, scale: 2 })
|
|
182
|
+
.notNull()
|
|
183
|
+
.default("0.00"),
|
|
184
|
+
feeAmount: (0, pg_core_1.decimal)("fee_amount", { precision: 12, scale: 2 })
|
|
185
|
+
.notNull()
|
|
186
|
+
.default("0.00"),
|
|
187
|
+
totalAmount: (0, pg_core_1.decimal)("total_amount", { precision: 12, scale: 2 }).notNull(),
|
|
188
|
+
paidAmount: (0, pg_core_1.decimal)("paid_amount", { precision: 12, scale: 2 })
|
|
189
|
+
.notNull()
|
|
190
|
+
.default("0.00"),
|
|
191
|
+
refundedAmount: (0, pg_core_1.decimal)("refunded_amount", { precision: 12, scale: 2 })
|
|
192
|
+
.notNull()
|
|
193
|
+
.default("0.00"),
|
|
194
|
+
outstandingAmount: (0, pg_core_1.decimal)("outstanding_amount", {
|
|
195
|
+
precision: 12,
|
|
196
|
+
scale: 2,
|
|
197
|
+
})
|
|
198
|
+
.notNull()
|
|
199
|
+
.default("0.00"),
|
|
200
|
+
// Tax Information
|
|
201
|
+
taxRate: (0, pg_core_1.decimal)("tax_rate", { precision: 5, scale: 4 }),
|
|
202
|
+
taxIncluded: (0, pg_core_1.boolean)("tax_included").notNull().default(false),
|
|
203
|
+
taxExempt: (0, pg_core_1.boolean)("tax_exempt").notNull().default(false),
|
|
204
|
+
taxExemptReason: (0, pg_core_1.text)("tax_exempt_reason"),
|
|
205
|
+
// Customer Information (for guest orders)
|
|
206
|
+
guestEmail: (0, pg_core_1.varchar)("guest_email", { length: 255 }),
|
|
207
|
+
guestPhone: (0, pg_core_1.varchar)("guest_phone", { length: 20 }),
|
|
208
|
+
guestName: (0, pg_core_1.varchar)("guest_name", { length: 100 }),
|
|
209
|
+
// Location Information
|
|
210
|
+
storeLocationId: (0, pg_core_1.text)("store_location_id"), // Physical store/warehouse
|
|
211
|
+
posTerminalId: (0, pg_core_1.text)("pos_terminal_id"), // POS terminal for in-store orders
|
|
212
|
+
salesAssociateId: (0, pg_core_1.text)("sales_associate_id"), // Staff who processed the order
|
|
213
|
+
// Timing Information
|
|
214
|
+
requestedDeliveryDate: (0, pg_core_1.timestamp)("requested_delivery_date", {
|
|
215
|
+
withTimezone: true,
|
|
216
|
+
}),
|
|
217
|
+
promisedDeliveryDate: (0, pg_core_1.timestamp)("promised_delivery_date", {
|
|
218
|
+
withTimezone: true,
|
|
219
|
+
}),
|
|
220
|
+
actualDeliveryDate: (0, pg_core_1.timestamp)("actual_delivery_date", {
|
|
221
|
+
withTimezone: true,
|
|
222
|
+
}),
|
|
223
|
+
orderPlacedAt: (0, pg_core_1.timestamp)("order_placed_at", { withTimezone: true }),
|
|
224
|
+
orderConfirmedAt: (0, pg_core_1.timestamp)("order_confirmed_at", { withTimezone: true }),
|
|
225
|
+
orderShippedAt: (0, pg_core_1.timestamp)("order_shipped_at", { withTimezone: true }),
|
|
226
|
+
orderDeliveredAt: (0, pg_core_1.timestamp)("order_delivered_at", { withTimezone: true }),
|
|
227
|
+
orderCompletedAt: (0, pg_core_1.timestamp)("order_completed_at", { withTimezone: true }),
|
|
228
|
+
// Special Instructions and Notes
|
|
229
|
+
customerNotes: (0, pg_core_1.text)("customer_notes"),
|
|
230
|
+
internalNotes: (0, pg_core_1.text)("internal_notes"),
|
|
231
|
+
giftMessage: (0, pg_core_1.text)("gift_message"),
|
|
232
|
+
specialInstructions: (0, pg_core_1.text)("special_instructions"),
|
|
233
|
+
deliveryInstructions: (0, pg_core_1.text)("delivery_instructions"),
|
|
234
|
+
// Marketing and Attribution
|
|
235
|
+
referrerUrl: (0, pg_core_1.text)("referrer_url"),
|
|
236
|
+
utmSource: (0, pg_core_1.varchar)("utm_source", { length: 100 }),
|
|
237
|
+
utmMedium: (0, pg_core_1.varchar)("utm_medium", { length: 100 }),
|
|
238
|
+
utmCampaign: (0, pg_core_1.varchar)("utm_campaign", { length: 100 }),
|
|
239
|
+
affiliateId: (0, pg_core_1.text)("affiliate_id"),
|
|
240
|
+
promoCode: (0, pg_core_1.varchar)("promo_code", { length: 50 }),
|
|
241
|
+
// Technical Information
|
|
242
|
+
userAgent: (0, pg_core_1.text)("user_agent"),
|
|
243
|
+
ipAddress: (0, pg_core_1.varchar)("ip_address", { length: 45 }),
|
|
244
|
+
deviceInfo: (0, pg_core_1.jsonb)("device_info").$type(),
|
|
245
|
+
// Risk and Fraud
|
|
246
|
+
riskScore: (0, pg_core_1.decimal)("risk_score", { precision: 5, scale: 2 }),
|
|
247
|
+
fraudScore: (0, pg_core_1.decimal)("fraud_score", { precision: 5, scale: 2 }),
|
|
248
|
+
isHighRisk: (0, pg_core_1.boolean)("is_high_risk").notNull().default(false),
|
|
249
|
+
requiresVerification: (0, pg_core_1.boolean)("requires_verification")
|
|
250
|
+
.notNull()
|
|
251
|
+
.default(false),
|
|
252
|
+
verifiedAt: (0, pg_core_1.timestamp)("verified_at", { withTimezone: true }),
|
|
253
|
+
// Subscription Information
|
|
254
|
+
subscriptionId: (0, pg_core_1.text)("subscription_id"),
|
|
255
|
+
subscriptionCycle: (0, pg_core_1.integer)("subscription_cycle"),
|
|
256
|
+
isSubscriptionRenewal: (0, pg_core_1.boolean)("is_subscription_renewal")
|
|
257
|
+
.notNull()
|
|
258
|
+
.default(false),
|
|
259
|
+
// Advanced Features
|
|
260
|
+
tags: (0, pg_core_1.jsonb)("tags").$type().default([]),
|
|
261
|
+
customFields: (0, pg_core_1.jsonb)("custom_fields")
|
|
262
|
+
.$type()
|
|
263
|
+
.default({}),
|
|
264
|
+
metadata: (0, pg_core_1.jsonb)("metadata").$type().default({}),
|
|
265
|
+
// Analytics
|
|
266
|
+
conversionValue: (0, pg_core_1.decimal)("conversion_value", { precision: 12, scale: 2 }),
|
|
267
|
+
customerLifetimeValue: (0, pg_core_1.decimal)("customer_lifetime_value", {
|
|
268
|
+
precision: 12,
|
|
269
|
+
scale: 2,
|
|
270
|
+
}),
|
|
271
|
+
profitMargin: (0, pg_core_1.decimal)("profit_margin", { precision: 5, scale: 2 }),
|
|
272
|
+
// Timestamps
|
|
273
|
+
createdAt: (0, pg_core_1.timestamp)("created_at", { withTimezone: true })
|
|
274
|
+
.defaultNow()
|
|
275
|
+
.notNull(),
|
|
276
|
+
updatedAt: (0, pg_core_1.timestamp)("updated_at", { withTimezone: true })
|
|
277
|
+
.defaultNow()
|
|
278
|
+
.notNull(),
|
|
279
|
+
deletedAt: (0, pg_core_1.timestamp)("deleted_at", { withTimezone: true }),
|
|
280
|
+
}, (table) => ({
|
|
281
|
+
// Primary Indexes
|
|
282
|
+
orderNumberIndex: (0, pg_core_1.unique)("idx_orders_order_number").on(table.storeId, table.orderNumber),
|
|
283
|
+
externalOrderIndex: (0, pg_core_1.index)("idx_orders_external_id").on(table.externalOrderId),
|
|
284
|
+
// Customer and User Indexes
|
|
285
|
+
customerIndex: (0, pg_core_1.index)("idx_orders_customer").on(table.customerId),
|
|
286
|
+
userIndex: (0, pg_core_1.index)("idx_orders_user").on(table.userId),
|
|
287
|
+
guestEmailIndex: (0, pg_core_1.index)("idx_orders_guest_email").on(table.guestEmail),
|
|
288
|
+
// Status Indexes
|
|
289
|
+
statusIndex: (0, pg_core_1.index)("idx_orders_status").on(table.status),
|
|
290
|
+
paymentStatusIndex: (0, pg_core_1.index)("idx_orders_payment_status").on(table.paymentStatus),
|
|
291
|
+
fulfillmentStatusIndex: (0, pg_core_1.index)("idx_orders_fulfillment_status").on(table.fulfillmentStatus),
|
|
292
|
+
// Type and Source Indexes
|
|
293
|
+
orderTypeIndex: (0, pg_core_1.index)("idx_orders_type").on(table.orderType),
|
|
294
|
+
orderSourceIndex: (0, pg_core_1.index)("idx_orders_source").on(table.orderSource),
|
|
295
|
+
// Location Indexes
|
|
296
|
+
storeLocationIndex: (0, pg_core_1.index)("idx_orders_store_location").on(table.storeLocationId),
|
|
297
|
+
posTerminalIndex: (0, pg_core_1.index)("idx_orders_pos_terminal").on(table.posTerminalId),
|
|
298
|
+
// Timing Indexes
|
|
299
|
+
placedAtIndex: (0, pg_core_1.index)("idx_orders_placed_at").on(table.orderPlacedAt),
|
|
300
|
+
deliveryDateIndex: (0, pg_core_1.index)("idx_orders_delivery_date").on(table.promisedDeliveryDate),
|
|
301
|
+
createdAtIndex: (0, pg_core_1.index)("idx_orders_created_at").on(table.createdAt),
|
|
302
|
+
// Financial Indexes
|
|
303
|
+
totalAmountIndex: (0, pg_core_1.index)("idx_orders_total_amount").on(table.totalAmount),
|
|
304
|
+
currencyIndex: (0, pg_core_1.index)("idx_orders_currency").on(table.currency),
|
|
305
|
+
// Special Flags Indexes
|
|
306
|
+
priorityIndex: (0, pg_core_1.index)("idx_orders_priority").on(table.priority),
|
|
307
|
+
testOrderIndex: (0, pg_core_1.index)("idx_orders_test").on(table.isTestOrder),
|
|
308
|
+
guestOrderIndex: (0, pg_core_1.index)("idx_orders_guest").on(table.isGuestOrder),
|
|
309
|
+
// Composite Indexes
|
|
310
|
+
storeStatusIndex: (0, pg_core_1.index)("idx_orders_store_status").on(table.storeId, table.status),
|
|
311
|
+
customerStatusIndex: (0, pg_core_1.index)("idx_orders_customer_status").on(table.customerId, table.status),
|
|
312
|
+
typeStatusIndex: (0, pg_core_1.index)("idx_orders_type_status").on(table.orderType, table.status),
|
|
313
|
+
dateRangeIndex: (0, pg_core_1.index)("idx_orders_date_range").on(table.storeId, table.createdAt),
|
|
314
|
+
}));
|
|
315
|
+
// =====================================================
|
|
316
|
+
// ORDER ITEMS TABLE
|
|
317
|
+
// =====================================================
|
|
318
|
+
exports.orderItems = (0, pg_core_1.pgTable)("order_items", {
|
|
319
|
+
id: (0, pg_core_1.text)("id")
|
|
320
|
+
.primaryKey()
|
|
321
|
+
.$defaultFn(() => (0, cuid2_1.createId)()),
|
|
322
|
+
orderId: (0, pg_core_1.text)("order_id")
|
|
323
|
+
.notNull()
|
|
324
|
+
.references(() => exports.orders.id, { onDelete: "cascade" }),
|
|
325
|
+
// Product Information
|
|
326
|
+
productId: (0, pg_core_1.text)("product_id").notNull(),
|
|
327
|
+
variantId: (0, pg_core_1.text)("variant_id"),
|
|
328
|
+
sku: (0, pg_core_1.varchar)("sku", { length: 100 }).notNull(),
|
|
329
|
+
productHandle: (0, pg_core_1.varchar)("product_handle", { length: 255 }),
|
|
330
|
+
// Product Details (snapshot at time of order)
|
|
331
|
+
productTitle: (0, pg_core_1.varchar)("product_title", { length: 255 }).notNull(),
|
|
332
|
+
variantTitle: (0, pg_core_1.varchar)("variant_title", { length: 255 }),
|
|
333
|
+
productType: (0, pg_core_1.varchar)("product_type", { length: 50 }),
|
|
334
|
+
vendor: (0, pg_core_1.varchar)("vendor", { length: 100 }),
|
|
335
|
+
// Quantity and Pricing
|
|
336
|
+
quantity: (0, pg_core_1.integer)("quantity").notNull(),
|
|
337
|
+
quantityShipped: (0, pg_core_1.integer)("quantity_shipped").notNull().default(0),
|
|
338
|
+
quantityReturned: (0, pg_core_1.integer)("quantity_returned").notNull().default(0),
|
|
339
|
+
quantityRefunded: (0, pg_core_1.integer)("quantity_refunded").notNull().default(0),
|
|
340
|
+
unitPrice: (0, pg_core_1.decimal)("unit_price", { precision: 12, scale: 2 }).notNull(),
|
|
341
|
+
compareAtPrice: (0, pg_core_1.decimal)("compare_at_price", { precision: 12, scale: 2 }),
|
|
342
|
+
costPrice: (0, pg_core_1.decimal)("cost_price", { precision: 12, scale: 2 }),
|
|
343
|
+
totalPrice: (0, pg_core_1.decimal)("total_price", { precision: 12, scale: 2 }).notNull(),
|
|
344
|
+
// Discounts and Taxes
|
|
345
|
+
discountAmount: (0, pg_core_1.decimal)("discount_amount", { precision: 12, scale: 2 })
|
|
346
|
+
.notNull()
|
|
347
|
+
.default("0.00"),
|
|
348
|
+
taxAmount: (0, pg_core_1.decimal)("tax_amount", { precision: 12, scale: 2 })
|
|
349
|
+
.notNull()
|
|
350
|
+
.default("0.00"),
|
|
351
|
+
// Physical Properties
|
|
352
|
+
weight: (0, pg_core_1.decimal)("weight", { precision: 8, scale: 3 }),
|
|
353
|
+
requiresShipping: (0, pg_core_1.boolean)("requires_shipping").notNull().default(true),
|
|
354
|
+
isGiftWrap: (0, pg_core_1.boolean)("is_gift_wrap").notNull().default(false),
|
|
355
|
+
giftWrapMessage: (0, pg_core_1.text)("gift_wrap_message"),
|
|
356
|
+
// Customization
|
|
357
|
+
customization: (0, pg_core_1.jsonb)("customization").$type(),
|
|
358
|
+
// Inventory and Fulfillment
|
|
359
|
+
inventoryLocationId: (0, pg_core_1.text)("inventory_location_id"),
|
|
360
|
+
fulfillmentService: (0, pg_core_1.varchar)("fulfillment_service", { length: 50 }).default("MANUAL"),
|
|
361
|
+
trackingRequired: (0, pg_core_1.boolean)("tracking_required").notNull().default(true),
|
|
362
|
+
// Subscription Information
|
|
363
|
+
isSubscriptionItem: (0, pg_core_1.boolean)("is_subscription_item")
|
|
364
|
+
.notNull()
|
|
365
|
+
.default(false),
|
|
366
|
+
subscriptionFrequency: (0, pg_core_1.varchar)("subscription_frequency", { length: 20 }),
|
|
367
|
+
subscriptionEndDate: (0, pg_core_1.timestamp)("subscription_end_date", {
|
|
368
|
+
withTimezone: true,
|
|
369
|
+
}),
|
|
370
|
+
// Product Snapshot (preserve product data at time of order)
|
|
371
|
+
productSnapshot: (0, pg_core_1.jsonb)("product_snapshot").$type(),
|
|
372
|
+
// Line Item Flags
|
|
373
|
+
isDigital: (0, pg_core_1.boolean)("is_digital").notNull().default(false),
|
|
374
|
+
isBackorder: (0, pg_core_1.boolean)("is_backorder").notNull().default(false),
|
|
375
|
+
isPreorder: (0, pg_core_1.boolean)("is_preorder").notNull().default(false),
|
|
376
|
+
// Notes and Instructions
|
|
377
|
+
lineItemNotes: (0, pg_core_1.text)("line_item_notes"),
|
|
378
|
+
internalNotes: (0, pg_core_1.text)("internal_notes"),
|
|
379
|
+
// Analytics
|
|
380
|
+
profitMargin: (0, pg_core_1.decimal)("profit_margin", { precision: 5, scale: 2 }),
|
|
381
|
+
marginAmount: (0, pg_core_1.decimal)("margin_amount", { precision: 12, scale: 2 }),
|
|
382
|
+
// Timestamps
|
|
383
|
+
createdAt: (0, pg_core_1.timestamp)("created_at", { withTimezone: true })
|
|
384
|
+
.defaultNow()
|
|
385
|
+
.notNull(),
|
|
386
|
+
updatedAt: (0, pg_core_1.timestamp)("updated_at", { withTimezone: true })
|
|
387
|
+
.defaultNow()
|
|
388
|
+
.notNull(),
|
|
389
|
+
}, (table) => ({
|
|
390
|
+
// Foreign Key Indexes
|
|
391
|
+
orderIdIndex: (0, pg_core_1.index)("idx_order_items_order_id").on(table.orderId),
|
|
392
|
+
productIdIndex: (0, pg_core_1.index)("idx_order_items_product_id").on(table.productId),
|
|
393
|
+
variantIdIndex: (0, pg_core_1.index)("idx_order_items_variant_id").on(table.variantId),
|
|
394
|
+
skuIndex: (0, pg_core_1.index)("idx_order_items_sku").on(table.sku),
|
|
395
|
+
// Inventory and Fulfillment
|
|
396
|
+
inventoryLocationIndex: (0, pg_core_1.index)("idx_order_items_inventory_location").on(table.inventoryLocationId),
|
|
397
|
+
fulfillmentServiceIndex: (0, pg_core_1.index)("idx_order_items_fulfillment_service").on(table.fulfillmentService),
|
|
398
|
+
// Product Information
|
|
399
|
+
vendorIndex: (0, pg_core_1.index)("idx_order_items_vendor").on(table.vendor),
|
|
400
|
+
productTypeIndex: (0, pg_core_1.index)("idx_order_items_product_type").on(table.productType),
|
|
401
|
+
// Flags
|
|
402
|
+
digitalIndex: (0, pg_core_1.index)("idx_order_items_digital").on(table.isDigital),
|
|
403
|
+
backorderIndex: (0, pg_core_1.index)("idx_order_items_backorder").on(table.isBackorder),
|
|
404
|
+
subscriptionIndex: (0, pg_core_1.index)("idx_order_items_subscription").on(table.isSubscriptionItem),
|
|
405
|
+
// Composite Indexes
|
|
406
|
+
orderProductIndex: (0, pg_core_1.index)("idx_order_items_order_product").on(table.orderId, table.productId),
|
|
407
|
+
orderSkuIndex: (0, pg_core_1.index)("idx_order_items_order_sku").on(table.orderId, table.sku),
|
|
408
|
+
}));
|
|
409
|
+
// =====================================================
|
|
410
|
+
// ORDER ADDRESSES TABLE
|
|
411
|
+
// =====================================================
|
|
412
|
+
exports.orderAddresses = (0, pg_core_1.pgTable)("order_addresses", {
|
|
413
|
+
id: (0, pg_core_1.text)("id")
|
|
414
|
+
.primaryKey()
|
|
415
|
+
.$defaultFn(() => (0, cuid2_1.createId)()),
|
|
416
|
+
orderId: (0, pg_core_1.text)("order_id")
|
|
417
|
+
.notNull()
|
|
418
|
+
.references(() => exports.orders.id, { onDelete: "cascade" }),
|
|
419
|
+
addressType: (0, exports.addressTypeEnum)("address_type").notNull(),
|
|
420
|
+
// Contact Information
|
|
421
|
+
firstName: (0, pg_core_1.varchar)("first_name", { length: 100 }),
|
|
422
|
+
lastName: (0, pg_core_1.varchar)("last_name", { length: 100 }),
|
|
423
|
+
company: (0, pg_core_1.varchar)("company", { length: 100 }),
|
|
424
|
+
email: (0, pg_core_1.varchar)("email", { length: 255 }),
|
|
425
|
+
phone: (0, pg_core_1.varchar)("phone", { length: 20 }),
|
|
426
|
+
// Address Information
|
|
427
|
+
address1: (0, pg_core_1.varchar)("address1", { length: 255 }).notNull(),
|
|
428
|
+
address2: (0, pg_core_1.varchar)("address2", { length: 255 }),
|
|
429
|
+
city: (0, pg_core_1.varchar)("city", { length: 100 }).notNull(),
|
|
430
|
+
province: (0, pg_core_1.varchar)("province", { length: 100 }),
|
|
431
|
+
provinceCode: (0, pg_core_1.varchar)("province_code", { length: 10 }),
|
|
432
|
+
country: (0, pg_core_1.varchar)("country", { length: 100 }).notNull(),
|
|
433
|
+
countryCode: (0, pg_core_1.varchar)("country_code", { length: 2 }).notNull(),
|
|
434
|
+
postalCode: (0, pg_core_1.varchar)("postal_code", { length: 20 }),
|
|
435
|
+
// Geolocation
|
|
436
|
+
latitude: (0, pg_core_1.decimal)("latitude", { precision: 10, scale: 8 }),
|
|
437
|
+
longitude: (0, pg_core_1.decimal)("longitude", { precision: 11, scale: 8 }),
|
|
438
|
+
timezone: (0, pg_core_1.varchar)("timezone", { length: 50 }),
|
|
439
|
+
// Delivery Instructions
|
|
440
|
+
deliveryInstructions: (0, pg_core_1.text)("delivery_instructions"),
|
|
441
|
+
accessCode: (0, pg_core_1.varchar)("access_code", { length: 20 }),
|
|
442
|
+
deliveryPreference: (0, pg_core_1.varchar)("delivery_preference", { length: 50 }).$type(),
|
|
443
|
+
// Validation Flags
|
|
444
|
+
isValidated: (0, pg_core_1.boolean)("is_validated").notNull().default(false),
|
|
445
|
+
isResidential: (0, pg_core_1.boolean)("is_residential").notNull().default(true),
|
|
446
|
+
isPOBox: (0, pg_core_1.boolean)("is_po_box").notNull().default(false),
|
|
447
|
+
isMilitary: (0, pg_core_1.boolean)("is_military").notNull().default(false),
|
|
448
|
+
// Timestamps
|
|
449
|
+
createdAt: (0, pg_core_1.timestamp)("created_at", { withTimezone: true })
|
|
450
|
+
.defaultNow()
|
|
451
|
+
.notNull(),
|
|
452
|
+
updatedAt: (0, pg_core_1.timestamp)("updated_at", { withTimezone: true })
|
|
453
|
+
.defaultNow()
|
|
454
|
+
.notNull(),
|
|
455
|
+
}, (table) => ({
|
|
456
|
+
orderIdIndex: (0, pg_core_1.index)("idx_order_addresses_order_id").on(table.orderId),
|
|
457
|
+
addressTypeIndex: (0, pg_core_1.index)("idx_order_addresses_type").on(table.addressType),
|
|
458
|
+
countryIndex: (0, pg_core_1.index)("idx_order_addresses_country").on(table.countryCode),
|
|
459
|
+
postalCodeIndex: (0, pg_core_1.index)("idx_order_addresses_postal_code").on(table.postalCode),
|
|
460
|
+
geoLocationIndex: (0, pg_core_1.index)("idx_order_addresses_geo").on(table.latitude, table.longitude),
|
|
461
|
+
// Ensure one address per type per order
|
|
462
|
+
uniqueOrderAddressType: (0, pg_core_1.unique)("unq_order_address_type").on(table.orderId, table.addressType),
|
|
463
|
+
}));
|
|
464
|
+
// =====================================================
|
|
465
|
+
// ORDER PAYMENTS TABLE
|
|
466
|
+
// =====================================================
|
|
467
|
+
exports.orderPayments = (0, pg_core_1.pgTable)("order_payments", {
|
|
468
|
+
id: (0, pg_core_1.text)("id")
|
|
469
|
+
.primaryKey()
|
|
470
|
+
.$defaultFn(() => (0, cuid2_1.createId)()),
|
|
471
|
+
orderId: (0, pg_core_1.text)("order_id")
|
|
472
|
+
.notNull()
|
|
473
|
+
.references(() => exports.orders.id, { onDelete: "cascade" }),
|
|
474
|
+
// Payment Identification
|
|
475
|
+
paymentMethod: (0, exports.paymentMethodEnum)("payment_method").notNull(),
|
|
476
|
+
paymentProvider: (0, pg_core_1.varchar)("payment_provider", { length: 50 }),
|
|
477
|
+
transactionId: (0, pg_core_1.varchar)("transaction_id", { length: 100 }),
|
|
478
|
+
externalTransactionId: (0, pg_core_1.varchar)("external_transaction_id", { length: 100 }),
|
|
479
|
+
authorizationCode: (0, pg_core_1.varchar)("authorization_code", { length: 50 }),
|
|
480
|
+
// Payment Status and Type
|
|
481
|
+
status: (0, exports.paymentStatusEnum)("status").notNull().default("PENDING"),
|
|
482
|
+
paymentType: (0, pg_core_1.varchar)("payment_type", { length: 20 })
|
|
483
|
+
.notNull()
|
|
484
|
+
.default("PAYMENT")
|
|
485
|
+
.$type(),
|
|
486
|
+
// Financial Information
|
|
487
|
+
currency: (0, pg_core_1.varchar)("currency", { length: 3 }).notNull(),
|
|
488
|
+
amount: (0, pg_core_1.decimal)("amount", { precision: 12, scale: 2 }).notNull(),
|
|
489
|
+
feeAmount: (0, pg_core_1.decimal)("fee_amount", { precision: 12, scale: 2 })
|
|
490
|
+
.notNull()
|
|
491
|
+
.default("0.00"),
|
|
492
|
+
netAmount: (0, pg_core_1.decimal)("net_amount", { precision: 12, scale: 2 }).notNull(),
|
|
493
|
+
exchangeRate: (0, pg_core_1.decimal)("exchange_rate", { precision: 10, scale: 6 }).default("1.000000"),
|
|
494
|
+
// Card Information (for card payments)
|
|
495
|
+
cardLast4: (0, pg_core_1.varchar)("card_last4", { length: 4 }),
|
|
496
|
+
cardBrand: (0, pg_core_1.varchar)("card_brand", { length: 20 }),
|
|
497
|
+
cardType: (0, pg_core_1.varchar)("card_type", { length: 20 }),
|
|
498
|
+
cardCountry: (0, pg_core_1.varchar)("card_country", { length: 2 }),
|
|
499
|
+
cardFingerprint: (0, pg_core_1.varchar)("card_fingerprint", { length: 100 }),
|
|
500
|
+
// Processing Information
|
|
501
|
+
processorResponse: (0, pg_core_1.jsonb)("processor_response").$type(),
|
|
502
|
+
gatewayResponse: (0, pg_core_1.jsonb)("gateway_response").$type(),
|
|
503
|
+
riskAssessment: (0, pg_core_1.jsonb)("risk_assessment").$type(),
|
|
504
|
+
// Timing Information
|
|
505
|
+
authorizedAt: (0, pg_core_1.timestamp)("authorized_at", { withTimezone: true }),
|
|
506
|
+
capturedAt: (0, pg_core_1.timestamp)("captured_at", { withTimezone: true }),
|
|
507
|
+
settledAt: (0, pg_core_1.timestamp)("settled_at", { withTimezone: true }),
|
|
508
|
+
refundedAt: (0, pg_core_1.timestamp)("refunded_at", { withTimezone: true }),
|
|
509
|
+
voidedAt: (0, pg_core_1.timestamp)("voided_at", { withTimezone: true }),
|
|
510
|
+
// Customer Information
|
|
511
|
+
customerIp: (0, pg_core_1.varchar)("customer_ip", { length: 45 }),
|
|
512
|
+
billingAddressId: (0, pg_core_1.text)("billing_address_id"),
|
|
513
|
+
// Reference Information
|
|
514
|
+
parentPaymentId: (0, pg_core_1.text)("parent_payment_id"), // For refunds
|
|
515
|
+
originalTransactionId: (0, pg_core_1.varchar)("original_transaction_id", { length: 100 }), // For refunds/chargebacks
|
|
516
|
+
// Failure Information
|
|
517
|
+
failureCode: (0, pg_core_1.varchar)("failure_code", { length: 50 }),
|
|
518
|
+
failureMessage: (0, pg_core_1.text)("failure_message"),
|
|
519
|
+
declineCode: (0, pg_core_1.varchar)("decline_code", { length: 50 }),
|
|
520
|
+
// Metadata
|
|
521
|
+
metadata: (0, pg_core_1.jsonb)("metadata").$type().default({}),
|
|
522
|
+
processorData: (0, pg_core_1.jsonb)("processor_data")
|
|
523
|
+
.$type()
|
|
524
|
+
.default({}),
|
|
525
|
+
// Timestamps
|
|
526
|
+
createdAt: (0, pg_core_1.timestamp)("created_at", { withTimezone: true })
|
|
527
|
+
.defaultNow()
|
|
528
|
+
.notNull(),
|
|
529
|
+
updatedAt: (0, pg_core_1.timestamp)("updated_at", { withTimezone: true })
|
|
530
|
+
.defaultNow()
|
|
531
|
+
.notNull(),
|
|
532
|
+
}, (table) => ({
|
|
533
|
+
orderIdIndex: (0, pg_core_1.index)("idx_order_payments_order_id").on(table.orderId),
|
|
534
|
+
statusIndex: (0, pg_core_1.index)("idx_order_payments_status").on(table.status),
|
|
535
|
+
paymentMethodIndex: (0, pg_core_1.index)("idx_order_payments_method").on(table.paymentMethod),
|
|
536
|
+
transactionIdIndex: (0, pg_core_1.index)("idx_order_payments_transaction_id").on(table.transactionId),
|
|
537
|
+
externalTransactionIdIndex: (0, pg_core_1.index)("idx_order_payments_external_transaction_id").on(table.externalTransactionId),
|
|
538
|
+
paymentProviderIndex: (0, pg_core_1.index)("idx_order_payments_provider").on(table.paymentProvider),
|
|
539
|
+
parentPaymentIndex: (0, pg_core_1.index)("idx_order_payments_parent").on(table.parentPaymentId),
|
|
540
|
+
createdAtIndex: (0, pg_core_1.index)("idx_order_payments_created_at").on(table.createdAt),
|
|
541
|
+
// Financial Indexes
|
|
542
|
+
amountIndex: (0, pg_core_1.index)("idx_order_payments_amount").on(table.amount),
|
|
543
|
+
currencyIndex: (0, pg_core_1.index)("idx_order_payments_currency").on(table.currency),
|
|
544
|
+
// Card Information Indexes
|
|
545
|
+
cardFingerprintIndex: (0, pg_core_1.index)("idx_order_payments_card_fingerprint").on(table.cardFingerprint),
|
|
546
|
+
cardBrandIndex: (0, pg_core_1.index)("idx_order_payments_card_brand").on(table.cardBrand),
|
|
547
|
+
// Composite Indexes
|
|
548
|
+
orderStatusIndex: (0, pg_core_1.index)("idx_order_payments_order_status").on(table.orderId, table.status),
|
|
549
|
+
methodStatusIndex: (0, pg_core_1.index)("idx_order_payments_method_status").on(table.paymentMethod, table.status),
|
|
550
|
+
}));
|
|
551
|
+
// =====================================================
|
|
552
|
+
// ORDER FULFILLMENTS TABLE
|
|
553
|
+
// =====================================================
|
|
554
|
+
exports.orderFulfillments = (0, pg_core_1.pgTable)("order_fulfillments", {
|
|
555
|
+
id: (0, pg_core_1.text)("id")
|
|
556
|
+
.primaryKey()
|
|
557
|
+
.$defaultFn(() => (0, cuid2_1.createId)()),
|
|
558
|
+
orderId: (0, pg_core_1.text)("order_id")
|
|
559
|
+
.notNull()
|
|
560
|
+
.references(() => exports.orders.id, { onDelete: "cascade" }),
|
|
561
|
+
// Fulfillment Identification
|
|
562
|
+
fulfillmentNumber: (0, pg_core_1.varchar)("fulfillment_number", { length: 50 }),
|
|
563
|
+
trackingNumber: (0, pg_core_1.varchar)("tracking_number", { length: 100 }),
|
|
564
|
+
trackingUrl: (0, pg_core_1.text)("tracking_url"),
|
|
565
|
+
// Status and Type
|
|
566
|
+
status: (0, exports.fulfillmentStatusEnum)("status").notNull().default("PENDING"),
|
|
567
|
+
fulfillmentType: (0, pg_core_1.varchar)("fulfillment_type", { length: 20 })
|
|
568
|
+
.notNull()
|
|
569
|
+
.$type(),
|
|
570
|
+
// Carrier and Service Information
|
|
571
|
+
shippingCarrier: (0, pg_core_1.varchar)("shipping_carrier", { length: 50 }),
|
|
572
|
+
shippingService: (0, pg_core_1.varchar)("shipping_service", { length: 100 }),
|
|
573
|
+
shippingServiceCode: (0, pg_core_1.varchar)("shipping_service_code", { length: 50 }),
|
|
574
|
+
estimatedDeliveryDate: (0, pg_core_1.timestamp)("estimated_delivery_date", {
|
|
575
|
+
withTimezone: true,
|
|
576
|
+
}),
|
|
577
|
+
guaranteedDeliveryDate: (0, pg_core_1.timestamp)("guaranteed_delivery_date", {
|
|
578
|
+
withTimezone: true,
|
|
579
|
+
}),
|
|
580
|
+
// Location Information
|
|
581
|
+
originLocationId: (0, pg_core_1.text)("origin_location_id"),
|
|
582
|
+
destinationAddressId: (0, pg_core_1.text)("destination_address_id"),
|
|
583
|
+
// Package Information
|
|
584
|
+
packageCount: (0, pg_core_1.integer)("package_count").notNull().default(1),
|
|
585
|
+
totalWeight: (0, pg_core_1.decimal)("total_weight", { precision: 8, scale: 3 }),
|
|
586
|
+
totalVolume: (0, pg_core_1.decimal)("total_volume", { precision: 8, scale: 3 }),
|
|
587
|
+
packageDimensions: (0, pg_core_1.jsonb)("package_dimensions").$type(),
|
|
588
|
+
// Shipping Costs
|
|
589
|
+
shippingCost: (0, pg_core_1.decimal)("shipping_cost", { precision: 12, scale: 2 })
|
|
590
|
+
.notNull()
|
|
591
|
+
.default("0.00"),
|
|
592
|
+
insuranceCost: (0, pg_core_1.decimal)("insurance_cost", { precision: 12, scale: 2 })
|
|
593
|
+
.notNull()
|
|
594
|
+
.default("0.00"),
|
|
595
|
+
handlingCost: (0, pg_core_1.decimal)("handling_cost", { precision: 12, scale: 2 })
|
|
596
|
+
.notNull()
|
|
597
|
+
.default("0.00"),
|
|
598
|
+
// Special Services
|
|
599
|
+
signatureRequired: (0, pg_core_1.boolean)("signature_required").notNull().default(false),
|
|
600
|
+
adultSignatureRequired: (0, pg_core_1.boolean)("adult_signature_required")
|
|
601
|
+
.notNull()
|
|
602
|
+
.default(false),
|
|
603
|
+
insuranceRequired: (0, pg_core_1.boolean)("insurance_required").notNull().default(false),
|
|
604
|
+
saturdayDelivery: (0, pg_core_1.boolean)("saturday_delivery").notNull().default(false),
|
|
605
|
+
// Delivery Information
|
|
606
|
+
deliveryAttempts: (0, pg_core_1.integer)("delivery_attempts").notNull().default(0),
|
|
607
|
+
lastDeliveryAttempt: (0, pg_core_1.timestamp)("last_delivery_attempt", {
|
|
608
|
+
withTimezone: true,
|
|
609
|
+
}),
|
|
610
|
+
deliveryNotes: (0, pg_core_1.text)("delivery_notes"),
|
|
611
|
+
proofOfDelivery: (0, pg_core_1.jsonb)("proof_of_delivery").$type(),
|
|
612
|
+
// Timing Information
|
|
613
|
+
labelCreatedAt: (0, pg_core_1.timestamp)("label_created_at", { withTimezone: true }),
|
|
614
|
+
pickedUpAt: (0, pg_core_1.timestamp)("picked_up_at", { withTimezone: true }),
|
|
615
|
+
inTransitAt: (0, pg_core_1.timestamp)("in_transit_at", { withTimezone: true }),
|
|
616
|
+
outForDeliveryAt: (0, pg_core_1.timestamp)("out_for_delivery_at", { withTimezone: true }),
|
|
617
|
+
deliveredAt: (0, pg_core_1.timestamp)("delivered_at", { withTimezone: true }),
|
|
618
|
+
returnedAt: (0, pg_core_1.timestamp)("returned_at", { withTimezone: true }),
|
|
619
|
+
// Staff Information
|
|
620
|
+
packedBy: (0, pg_core_1.text)("packed_by"),
|
|
621
|
+
shippedBy: (0, pg_core_1.text)("shipped_by"),
|
|
622
|
+
deliveredBy: (0, pg_core_1.text)("delivered_by"),
|
|
623
|
+
// Failure and Return Information
|
|
624
|
+
failureReason: (0, pg_core_1.text)("failure_reason"),
|
|
625
|
+
returnReason: (0, pg_core_1.text)("return_reason"),
|
|
626
|
+
returnTrackingNumber: (0, pg_core_1.varchar)("return_tracking_number", { length: 100 }),
|
|
627
|
+
// Metadata
|
|
628
|
+
carrierData: (0, pg_core_1.jsonb)("carrier_data")
|
|
629
|
+
.$type()
|
|
630
|
+
.default({}),
|
|
631
|
+
trackingEvents: (0, pg_core_1.jsonb)("tracking_events")
|
|
632
|
+
.$type()
|
|
633
|
+
.default([]),
|
|
634
|
+
// Timestamps
|
|
635
|
+
createdAt: (0, pg_core_1.timestamp)("created_at", { withTimezone: true })
|
|
636
|
+
.defaultNow()
|
|
637
|
+
.notNull(),
|
|
638
|
+
updatedAt: (0, pg_core_1.timestamp)("updated_at", { withTimezone: true })
|
|
639
|
+
.defaultNow()
|
|
640
|
+
.notNull(),
|
|
641
|
+
}, (table) => ({
|
|
642
|
+
orderIdIndex: (0, pg_core_1.index)("idx_order_fulfillments_order_id").on(table.orderId),
|
|
643
|
+
statusIndex: (0, pg_core_1.index)("idx_order_fulfillments_status").on(table.status),
|
|
644
|
+
trackingNumberIndex: (0, pg_core_1.index)("idx_order_fulfillments_tracking_number").on(table.trackingNumber),
|
|
645
|
+
carrierIndex: (0, pg_core_1.index)("idx_order_fulfillments_carrier").on(table.shippingCarrier),
|
|
646
|
+
fulfillmentTypeIndex: (0, pg_core_1.index)("idx_order_fulfillments_type").on(table.fulfillmentType),
|
|
647
|
+
originLocationIndex: (0, pg_core_1.index)("idx_order_fulfillments_origin").on(table.originLocationId),
|
|
648
|
+
destinationAddressIndex: (0, pg_core_1.index)("idx_order_fulfillments_destination").on(table.destinationAddressId),
|
|
649
|
+
estimatedDeliveryIndex: (0, pg_core_1.index)("idx_order_fulfillments_estimated_delivery").on(table.estimatedDeliveryDate),
|
|
650
|
+
deliveredAtIndex: (0, pg_core_1.index)("idx_order_fulfillments_delivered_at").on(table.deliveredAt),
|
|
651
|
+
createdAtIndex: (0, pg_core_1.index)("idx_order_fulfillments_created_at").on(table.createdAt),
|
|
652
|
+
// Staff Indexes
|
|
653
|
+
packedByIndex: (0, pg_core_1.index)("idx_order_fulfillments_packed_by").on(table.packedBy),
|
|
654
|
+
shippedByIndex: (0, pg_core_1.index)("idx_order_fulfillments_shipped_by").on(table.shippedBy),
|
|
655
|
+
// Composite Indexes
|
|
656
|
+
orderStatusIndex: (0, pg_core_1.index)("idx_order_fulfillments_order_status").on(table.orderId, table.status),
|
|
657
|
+
carrierStatusIndex: (0, pg_core_1.index)("idx_order_fulfillments_carrier_status").on(table.shippingCarrier, table.status),
|
|
658
|
+
}));
|
|
659
|
+
// =====================================================
|
|
660
|
+
// ORDER DISCOUNTS TABLE
|
|
661
|
+
// =====================================================
|
|
662
|
+
exports.orderDiscounts = (0, pg_core_1.pgTable)("order_discounts", {
|
|
663
|
+
id: (0, pg_core_1.text)("id")
|
|
664
|
+
.primaryKey()
|
|
665
|
+
.$defaultFn(() => (0, cuid2_1.createId)()),
|
|
666
|
+
orderId: (0, pg_core_1.text)("order_id")
|
|
667
|
+
.notNull()
|
|
668
|
+
.references(() => exports.orders.id, { onDelete: "cascade" }),
|
|
669
|
+
// Discount Identification
|
|
670
|
+
discountCode: (0, pg_core_1.varchar)("discount_code", { length: 50 }),
|
|
671
|
+
discountType: (0, exports.discountTypeEnum)("discount_type").notNull(),
|
|
672
|
+
discountSource: (0, pg_core_1.varchar)("discount_source", { length: 50 })
|
|
673
|
+
.notNull()
|
|
674
|
+
.$type(),
|
|
675
|
+
// Discount Details
|
|
676
|
+
title: (0, pg_core_1.varchar)("title", { length: 255 }).notNull(),
|
|
677
|
+
description: (0, pg_core_1.text)("description"),
|
|
678
|
+
// Discount Values
|
|
679
|
+
discountAmount: (0, pg_core_1.decimal)("discount_amount", {
|
|
680
|
+
precision: 12,
|
|
681
|
+
scale: 2,
|
|
682
|
+
}).notNull(),
|
|
683
|
+
discountPercentage: (0, pg_core_1.decimal)("discount_percentage", {
|
|
684
|
+
precision: 5,
|
|
685
|
+
scale: 2,
|
|
686
|
+
}),
|
|
687
|
+
maximumDiscount: (0, pg_core_1.decimal)("maximum_discount", { precision: 12, scale: 2 }),
|
|
688
|
+
// Application Rules
|
|
689
|
+
appliedToOrderTotal: (0, pg_core_1.boolean)("applied_to_order_total")
|
|
690
|
+
.notNull()
|
|
691
|
+
.default(true),
|
|
692
|
+
appliedToShipping: (0, pg_core_1.boolean)("applied_to_shipping").notNull().default(false),
|
|
693
|
+
appliedToTax: (0, pg_core_1.boolean)("applied_to_tax").notNull().default(false),
|
|
694
|
+
// Scope and Conditions
|
|
695
|
+
minimumOrderAmount: (0, pg_core_1.decimal)("minimum_order_amount", {
|
|
696
|
+
precision: 12,
|
|
697
|
+
scale: 2,
|
|
698
|
+
}),
|
|
699
|
+
maximumOrderAmount: (0, pg_core_1.decimal)("maximum_order_amount", {
|
|
700
|
+
precision: 12,
|
|
701
|
+
scale: 2,
|
|
702
|
+
}),
|
|
703
|
+
applicableProductIds: (0, pg_core_1.jsonb)("applicable_product_ids")
|
|
704
|
+
.$type()
|
|
705
|
+
.default([]),
|
|
706
|
+
applicableCategories: (0, pg_core_1.jsonb)("applicable_categories")
|
|
707
|
+
.$type()
|
|
708
|
+
.default([]),
|
|
709
|
+
excludedProductIds: (0, pg_core_1.jsonb)("excluded_product_ids")
|
|
710
|
+
.$type()
|
|
711
|
+
.default([]),
|
|
712
|
+
// Customer Eligibility
|
|
713
|
+
eligibleCustomerTypes: (0, pg_core_1.jsonb)("eligible_customer_types")
|
|
714
|
+
.$type()
|
|
715
|
+
.default([]),
|
|
716
|
+
firstTimeCustomerOnly: (0, pg_core_1.boolean)("first_time_customer_only")
|
|
717
|
+
.notNull()
|
|
718
|
+
.default(false),
|
|
719
|
+
loyaltyPointsUsed: (0, pg_core_1.integer)("loyalty_points_used").default(0),
|
|
720
|
+
// Validation and Usage
|
|
721
|
+
isValid: (0, pg_core_1.boolean)("is_valid").notNull().default(true),
|
|
722
|
+
validationErrors: (0, pg_core_1.jsonb)("validation_errors").$type().default([]),
|
|
723
|
+
usageCount: (0, pg_core_1.integer)("usage_count").notNull().default(1),
|
|
724
|
+
// Campaign Information
|
|
725
|
+
campaignId: (0, pg_core_1.text)("campaign_id"),
|
|
726
|
+
campaignName: (0, pg_core_1.varchar)("campaign_name", { length: 100 }),
|
|
727
|
+
affiliateId: (0, pg_core_1.text)("affiliate_id"),
|
|
728
|
+
// Metadata
|
|
729
|
+
metadata: (0, pg_core_1.jsonb)("metadata").$type().default({}),
|
|
730
|
+
promotionRules: (0, pg_core_1.jsonb)("promotion_rules")
|
|
731
|
+
.$type()
|
|
732
|
+
.default({}),
|
|
733
|
+
// Timestamps
|
|
734
|
+
appliedAt: (0, pg_core_1.timestamp)("applied_at", { withTimezone: true })
|
|
735
|
+
.defaultNow()
|
|
736
|
+
.notNull(),
|
|
737
|
+
createdAt: (0, pg_core_1.timestamp)("created_at", { withTimezone: true })
|
|
738
|
+
.defaultNow()
|
|
739
|
+
.notNull(),
|
|
740
|
+
}, (table) => ({
|
|
741
|
+
orderIdIndex: (0, pg_core_1.index)("idx_order_discounts_order_id").on(table.orderId),
|
|
742
|
+
discountCodeIndex: (0, pg_core_1.index)("idx_order_discounts_code").on(table.discountCode),
|
|
743
|
+
discountTypeIndex: (0, pg_core_1.index)("idx_order_discounts_type").on(table.discountType),
|
|
744
|
+
discountSourceIndex: (0, pg_core_1.index)("idx_order_discounts_source").on(table.discountSource),
|
|
745
|
+
campaignIdIndex: (0, pg_core_1.index)("idx_order_discounts_campaign").on(table.campaignId),
|
|
746
|
+
affiliateIdIndex: (0, pg_core_1.index)("idx_order_discounts_affiliate").on(table.affiliateId),
|
|
747
|
+
appliedAtIndex: (0, pg_core_1.index)("idx_order_discounts_applied_at").on(table.appliedAt),
|
|
748
|
+
// Composite Indexes
|
|
749
|
+
orderDiscountTypeIndex: (0, pg_core_1.index)("idx_order_discounts_order_type").on(table.orderId, table.discountType),
|
|
750
|
+
codeValidIndex: (0, pg_core_1.index)("idx_order_discounts_code_valid").on(table.discountCode, table.isValid),
|
|
751
|
+
}));
|
|
752
|
+
// =====================================================
|
|
753
|
+
// ORDER HISTORY TABLE
|
|
754
|
+
// =====================================================
|
|
755
|
+
exports.orderHistory = (0, pg_core_1.pgTable)("order_history", {
|
|
756
|
+
id: (0, pg_core_1.text)("id")
|
|
757
|
+
.primaryKey()
|
|
758
|
+
.$defaultFn(() => (0, cuid2_1.createId)()),
|
|
759
|
+
orderId: (0, pg_core_1.text)("order_id")
|
|
760
|
+
.notNull()
|
|
761
|
+
.references(() => exports.orders.id, { onDelete: "cascade" }),
|
|
762
|
+
// Status Change Information
|
|
763
|
+
eventType: (0, pg_core_1.varchar)("event_type", { length: 50 })
|
|
764
|
+
.notNull()
|
|
765
|
+
.$type(),
|
|
766
|
+
// Change Details
|
|
767
|
+
fromValue: (0, pg_core_1.text)("from_value"),
|
|
768
|
+
toValue: (0, pg_core_1.text)("to_value"),
|
|
769
|
+
fieldChanged: (0, pg_core_1.varchar)("field_changed", { length: 100 }),
|
|
770
|
+
changeReason: (0, pg_core_1.text)("change_reason"),
|
|
771
|
+
// Actor Information
|
|
772
|
+
actorType: (0, pg_core_1.varchar)("actor_type", { length: 20 })
|
|
773
|
+
.notNull()
|
|
774
|
+
.$type(),
|
|
775
|
+
actorId: (0, pg_core_1.text)("actor_id"),
|
|
776
|
+
actorName: (0, pg_core_1.varchar)("actor_name", { length: 100 }),
|
|
777
|
+
actorRole: (0, pg_core_1.varchar)("actor_role", { length: 50 }),
|
|
778
|
+
// Context Information
|
|
779
|
+
source: (0, pg_core_1.varchar)("source", { length: 50 })
|
|
780
|
+
.notNull()
|
|
781
|
+
.$type(),
|
|
782
|
+
sessionId: (0, pg_core_1.text)("session_id"),
|
|
783
|
+
ipAddress: (0, pg_core_1.varchar)("ip_address", { length: 45 }),
|
|
784
|
+
userAgent: (0, pg_core_1.text)("user_agent"),
|
|
785
|
+
// Detailed Information
|
|
786
|
+
description: (0, pg_core_1.text)("description"),
|
|
787
|
+
internalNotes: (0, pg_core_1.text)("internal_notes"),
|
|
788
|
+
customerVisible: (0, pg_core_1.boolean)("customer_visible").notNull().default(true),
|
|
789
|
+
// Related Data
|
|
790
|
+
relatedEntityType: (0, pg_core_1.varchar)("related_entity_type", { length: 50 }),
|
|
791
|
+
relatedEntityId: (0, pg_core_1.text)("related_entity_id"),
|
|
792
|
+
changeData: (0, pg_core_1.jsonb)("change_data")
|
|
793
|
+
.$type()
|
|
794
|
+
.default({}),
|
|
795
|
+
// Impact Assessment
|
|
796
|
+
businessImpact: (0, pg_core_1.varchar)("business_impact", { length: 20 }).$type(),
|
|
797
|
+
customerImpact: (0, pg_core_1.varchar)("customer_impact", { length: 20 }).$type(),
|
|
798
|
+
// Metadata
|
|
799
|
+
metadata: (0, pg_core_1.jsonb)("metadata").$type().default({}),
|
|
800
|
+
// Timestamps
|
|
801
|
+
occurredAt: (0, pg_core_1.timestamp)("occurred_at", { withTimezone: true })
|
|
802
|
+
.defaultNow()
|
|
803
|
+
.notNull(),
|
|
804
|
+
createdAt: (0, pg_core_1.timestamp)("created_at", { withTimezone: true })
|
|
805
|
+
.defaultNow()
|
|
806
|
+
.notNull(),
|
|
807
|
+
}, (table) => ({
|
|
808
|
+
orderIdIndex: (0, pg_core_1.index)("idx_order_history_order_id").on(table.orderId),
|
|
809
|
+
eventTypeIndex: (0, pg_core_1.index)("idx_order_history_event_type").on(table.eventType),
|
|
810
|
+
actorTypeIndex: (0, pg_core_1.index)("idx_order_history_actor_type").on(table.actorType),
|
|
811
|
+
actorIdIndex: (0, pg_core_1.index)("idx_order_history_actor_id").on(table.actorId),
|
|
812
|
+
sourceIndex: (0, pg_core_1.index)("idx_order_history_source").on(table.source),
|
|
813
|
+
occurredAtIndex: (0, pg_core_1.index)("idx_order_history_occurred_at").on(table.occurredAt),
|
|
814
|
+
customerVisibleIndex: (0, pg_core_1.index)("idx_order_history_customer_visible").on(table.customerVisible),
|
|
815
|
+
// Composite Indexes
|
|
816
|
+
orderEventIndex: (0, pg_core_1.index)("idx_order_history_order_event").on(table.orderId, table.eventType),
|
|
817
|
+
orderDateIndex: (0, pg_core_1.index)("idx_order_history_order_date").on(table.orderId, table.occurredAt),
|
|
818
|
+
actorEventIndex: (0, pg_core_1.index)("idx_order_history_actor_event").on(table.actorId, table.eventType),
|
|
819
|
+
}));
|
|
820
|
+
// =====================================================
|
|
821
|
+
// ORDER NOTES TABLE
|
|
822
|
+
// =====================================================
|
|
823
|
+
exports.orderNotes = (0, pg_core_1.pgTable)("order_notes", {
|
|
824
|
+
id: (0, pg_core_1.text)("id")
|
|
825
|
+
.primaryKey()
|
|
826
|
+
.$defaultFn(() => (0, cuid2_1.createId)()),
|
|
827
|
+
orderId: (0, pg_core_1.text)("order_id")
|
|
828
|
+
.notNull()
|
|
829
|
+
.references(() => exports.orders.id, { onDelete: "cascade" }),
|
|
830
|
+
// Note Classification
|
|
831
|
+
noteType: (0, pg_core_1.varchar)("note_type", { length: 30 })
|
|
832
|
+
.notNull()
|
|
833
|
+
.$type(),
|
|
834
|
+
// Note Content
|
|
835
|
+
title: (0, pg_core_1.varchar)("title", { length: 255 }),
|
|
836
|
+
content: (0, pg_core_1.text)("content").notNull(),
|
|
837
|
+
// Visibility and Priority
|
|
838
|
+
isCustomerVisible: (0, pg_core_1.boolean)("is_customer_visible").notNull().default(false),
|
|
839
|
+
isInternal: (0, pg_core_1.boolean)("is_internal").notNull().default(true),
|
|
840
|
+
priority: (0, pg_core_1.varchar)("priority", { length: 20 })
|
|
841
|
+
.notNull()
|
|
842
|
+
.default("NORMAL")
|
|
843
|
+
.$type(),
|
|
844
|
+
// Actor Information
|
|
845
|
+
createdBy: (0, pg_core_1.text)("created_by").notNull(),
|
|
846
|
+
createdByName: (0, pg_core_1.varchar)("created_by_name", { length: 100 }).notNull(),
|
|
847
|
+
createdByRole: (0, pg_core_1.varchar)("created_by_role", { length: 50 }),
|
|
848
|
+
department: (0, pg_core_1.varchar)("department", { length: 50 }),
|
|
849
|
+
// Follow-up and Actions
|
|
850
|
+
requiresFollowUp: (0, pg_core_1.boolean)("requires_follow_up").notNull().default(false),
|
|
851
|
+
followUpDate: (0, pg_core_1.timestamp)("follow_up_date", { withTimezone: true }),
|
|
852
|
+
followUpAssignedTo: (0, pg_core_1.text)("follow_up_assigned_to"),
|
|
853
|
+
isFollowUpCompleted: (0, pg_core_1.boolean)("is_follow_up_completed")
|
|
854
|
+
.notNull()
|
|
855
|
+
.default(false),
|
|
856
|
+
// Context and References
|
|
857
|
+
relatedEntityType: (0, pg_core_1.varchar)("related_entity_type", { length: 50 }),
|
|
858
|
+
relatedEntityId: (0, pg_core_1.text)("related_entity_id"),
|
|
859
|
+
parentNoteId: (0, pg_core_1.text)("parent_note_id"),
|
|
860
|
+
// Tagging and Categorization
|
|
861
|
+
tags: (0, pg_core_1.jsonb)("tags").$type().default([]),
|
|
862
|
+
category: (0, pg_core_1.varchar)("category", { length: 50 }),
|
|
863
|
+
sentiment: (0, pg_core_1.varchar)("sentiment", { length: 20 }).$type(),
|
|
864
|
+
// Metadata
|
|
865
|
+
metadata: (0, pg_core_1.jsonb)("metadata").$type().default({}),
|
|
866
|
+
// Timestamps
|
|
867
|
+
createdAt: (0, pg_core_1.timestamp)("created_at", { withTimezone: true })
|
|
868
|
+
.defaultNow()
|
|
869
|
+
.notNull(),
|
|
870
|
+
updatedAt: (0, pg_core_1.timestamp)("updated_at", { withTimezone: true })
|
|
871
|
+
.defaultNow()
|
|
872
|
+
.notNull(),
|
|
873
|
+
}, (table) => ({
|
|
874
|
+
orderIdIndex: (0, pg_core_1.index)("idx_order_notes_order_id").on(table.orderId),
|
|
875
|
+
noteTypeIndex: (0, pg_core_1.index)("idx_order_notes_type").on(table.noteType),
|
|
876
|
+
createdByIndex: (0, pg_core_1.index)("idx_order_notes_created_by").on(table.createdBy),
|
|
877
|
+
priorityIndex: (0, pg_core_1.index)("idx_order_notes_priority").on(table.priority),
|
|
878
|
+
customerVisibleIndex: (0, pg_core_1.index)("idx_order_notes_customer_visible").on(table.isCustomerVisible),
|
|
879
|
+
followUpIndex: (0, pg_core_1.index)("idx_order_notes_follow_up").on(table.requiresFollowUp, table.followUpDate),
|
|
880
|
+
parentNoteIndex: (0, pg_core_1.index)("idx_order_notes_parent").on(table.parentNoteId),
|
|
881
|
+
createdAtIndex: (0, pg_core_1.index)("idx_order_notes_created_at").on(table.createdAt),
|
|
882
|
+
// Composite Indexes
|
|
883
|
+
orderNoteTypeIndex: (0, pg_core_1.index)("idx_order_notes_order_type").on(table.orderId, table.noteType),
|
|
884
|
+
orderCreatedIndex: (0, pg_core_1.index)("idx_order_notes_order_created").on(table.orderId, table.createdAt),
|
|
885
|
+
}));
|
|
886
|
+
// =====================================================
|
|
887
|
+
// RELATIONS
|
|
888
|
+
// =====================================================
|
|
889
|
+
exports.ordersRelations = (0, drizzle_orm_1.relations)(exports.orders, ({ many, one }) => ({
|
|
890
|
+
orderItems: many(exports.orderItems),
|
|
891
|
+
orderAddresses: many(exports.orderAddresses),
|
|
892
|
+
orderPayments: many(exports.orderPayments),
|
|
893
|
+
orderFulfillments: many(exports.orderFulfillments),
|
|
894
|
+
orderDiscounts: many(exports.orderDiscounts),
|
|
895
|
+
orderHistory: many(exports.orderHistory),
|
|
896
|
+
orderNotes: many(exports.orderNotes),
|
|
897
|
+
// Parent order relation for split orders
|
|
898
|
+
parentOrder: one(exports.orders, {
|
|
899
|
+
fields: [exports.orders.parentOrderId],
|
|
900
|
+
references: [exports.orders.id],
|
|
901
|
+
}),
|
|
902
|
+
childOrders: many(exports.orders),
|
|
903
|
+
}));
|
|
904
|
+
exports.orderItemsRelations = (0, drizzle_orm_1.relations)(exports.orderItems, ({ one }) => ({
|
|
905
|
+
order: one(exports.orders, {
|
|
906
|
+
fields: [exports.orderItems.orderId],
|
|
907
|
+
references: [exports.orders.id],
|
|
908
|
+
}),
|
|
909
|
+
}));
|
|
910
|
+
exports.orderAddressesRelations = (0, drizzle_orm_1.relations)(exports.orderAddresses, ({ one }) => ({
|
|
911
|
+
order: one(exports.orders, {
|
|
912
|
+
fields: [exports.orderAddresses.orderId],
|
|
913
|
+
references: [exports.orders.id],
|
|
914
|
+
}),
|
|
915
|
+
}));
|
|
916
|
+
exports.orderPaymentsRelations = (0, drizzle_orm_1.relations)(exports.orderPayments, ({ one }) => ({
|
|
917
|
+
order: one(exports.orders, {
|
|
918
|
+
fields: [exports.orderPayments.orderId],
|
|
919
|
+
references: [exports.orders.id],
|
|
920
|
+
}),
|
|
921
|
+
parentPayment: one(exports.orderPayments, {
|
|
922
|
+
fields: [exports.orderPayments.parentPaymentId],
|
|
923
|
+
references: [exports.orderPayments.id],
|
|
924
|
+
}),
|
|
925
|
+
}));
|
|
926
|
+
exports.orderFulfillmentsRelations = (0, drizzle_orm_1.relations)(exports.orderFulfillments, ({ one }) => ({
|
|
927
|
+
order: one(exports.orders, {
|
|
928
|
+
fields: [exports.orderFulfillments.orderId],
|
|
929
|
+
references: [exports.orders.id],
|
|
930
|
+
}),
|
|
931
|
+
}));
|
|
932
|
+
exports.orderDiscountsRelations = (0, drizzle_orm_1.relations)(exports.orderDiscounts, ({ one }) => ({
|
|
933
|
+
order: one(exports.orders, {
|
|
934
|
+
fields: [exports.orderDiscounts.orderId],
|
|
935
|
+
references: [exports.orders.id],
|
|
936
|
+
}),
|
|
937
|
+
}));
|
|
938
|
+
exports.orderHistoryRelations = (0, drizzle_orm_1.relations)(exports.orderHistory, ({ one }) => ({
|
|
939
|
+
order: one(exports.orders, {
|
|
940
|
+
fields: [exports.orderHistory.orderId],
|
|
941
|
+
references: [exports.orders.id],
|
|
942
|
+
}),
|
|
943
|
+
}));
|
|
944
|
+
exports.orderNotesRelations = (0, drizzle_orm_1.relations)(exports.orderNotes, ({ one, many }) => ({
|
|
945
|
+
order: one(exports.orders, {
|
|
946
|
+
fields: [exports.orderNotes.orderId],
|
|
947
|
+
references: [exports.orders.id],
|
|
948
|
+
}),
|
|
949
|
+
parentNote: one(exports.orderNotes, {
|
|
950
|
+
fields: [exports.orderNotes.parentNoteId],
|
|
951
|
+
references: [exports.orderNotes.id],
|
|
952
|
+
}),
|
|
953
|
+
childNotes: many(exports.orderNotes),
|
|
954
|
+
}));
|