| 1 | import { relations } from "drizzle-orm"; |
| 2 | import { |
| 3 | boolean, |
| 4 | foreignKey, |
| 5 | index, |
| 6 | int, |
| 7 | json, |
| 8 | mysqlEnum, |
| 9 | mysqlTable, |
| 10 | text, |
| 11 | timestamp, |
| 12 | unique, |
| 13 | varchar, |
| 14 | } from "drizzle-orm/mysql-core"; |
| 15 | import { user } |
| 16 | |
| 17 | export const billingCategories = |
| 18 | id: varchar("id |
| 19 | name: varchar("name |
| 20 | description: text("description |
| 21 | slug: varchar("slug |
| 22 | icon: text("icon |
| 23 | coverImage: text("cover_image |
| 24 | isActive: boolean("is_active |
| 25 | sortOrder: int("sort_order |
| 26 | createdAt: timestamp("created_at |
| 27 | updatedAt: timestamp("updated_at |
| 28 | .defaultNow() |
| 29 | .$onUpdate(() => |
| 30 | .notNull(), |
| 31 | }); |
| 32 | |
| 33 | export const billingProducts = |
| 34 | "billing_products", |
| 35 | { |
| 36 | id: varchar("id |
| 37 | categoryId: varchar("category_id |
| 38 | () => billingCategories. |
| 39 | { onDelete: "set null" |
| 40 | ), |
| 41 | name: varchar("name |
| 42 | description: text("description |
| 43 | slug: varchar("slug |
| 44 | icon: text("icon |
| 45 | coverImage: text("cover_image |
| 46 | inheritIcon: boolean("inherit_icon |
| 47 | inheritCoverImage: boolean("inherit_cover_image |
| 48 | isFeatured: boolean("is_featured |
| 49 | isActive: boolean("is_active |
| 50 | sortOrder: int("sort_order |
| 51 | createdAt: timestamp("created_at |
| 52 | updatedAt: timestamp("updated_at |
| 53 | .defaultNow() |
| 54 | .$onUpdate(() => |
| 55 | .notNull(), |
| 56 | }, |
| 57 | (table) => |
| 58 | ); |
| 59 | |
| 60 | export const billingPaymentGateways = |
| 61 | id: varchar("id |
| 62 | name: varchar("name |
| 63 | provider: varchar("provider |
| 64 | config: json("config |
| 65 | isActive: boolean("is_active |
| 66 | isDefault: boolean("is_default |
| 67 | sortOrder: int("sort_order |
| 68 | createdAt: timestamp("created_at |
| 69 | updatedAt: timestamp("updated_at |
| 70 | .defaultNow() |
| 71 | .$onUpdate(() => |
| 72 | .notNull(), |
| 73 | }); |
| 74 | |
| 75 | export const billingPlans = |
| 76 | "billing_plans", |
| 77 | { |
| 78 | id: varchar("id |
| 79 | productId: varchar("product_id |
| 80 | () => billingProducts. |
| 81 | { onDelete: "set null" |
| 82 | ), |
| 83 | name: varchar("name |
| 84 | description: text("description |
| 85 | currency: varchar("currency |
| 86 | resourceLimits: json("resource_limits |
| 87 | isActive: boolean("is_active |
| 88 | isPublic: boolean("is_public |
| 89 | sortOrder: int("sort_order |
| 90 | providerPlanId: varchar("provider_plan_id |
| 91 | createdAt: timestamp("created_at |
| 92 | updatedAt: timestamp("updated_at |
| 93 | .defaultNow() |
| 94 | .$onUpdate(() => |
| 95 | .notNull(), |
| 96 | }, |
| 97 | (table) => |
| 98 | ); |
| 99 | |
| 100 | export const billingPlanPrices = |
| 101 | "billing_plan_prices", |
| 102 | { |
| 103 | id: varchar("id |
| 104 | planId: varchar("plan_id |
| 105 | .notNull() |
| 106 | .references(() => |
| 107 | duration: mysqlEnum("duration |
| 108 | "7day", |
| 109 | "1month", |
| 110 | "3months", |
| 111 | "6months", |
| 112 | "1year", |
| 113 | ]).notNull(), |
| 114 | priceCents: int("price_cents |
| 115 | isActive: boolean("is_active |
| 116 | providerPriceId: varchar("provider_price_id |
| 117 | createdAt: timestamp("created_at |
| 118 | updatedAt: timestamp("updated_at |
| 119 | .defaultNow() |
| 120 | .$onUpdate(() => |
| 121 | .notNull(), |
| 122 | }, |
| 123 | (table) => |
| 124 | index("billing_plan_prices_planId_idx"). |
| 125 | unique("billing_plan_prices_planId_duration_unique"). |
| 126 | table.planId, |
| 127 | table.duration, |
| 128 | ), |
| 129 | ], |
| 130 | ); |
| 131 | |
| 132 | export const billingSubscriptions = |
| 133 | "billing_subscriptions", |
| 134 | { |
| 135 | id: varchar("id |
| 136 | userId: varchar("user_id |
| 137 | .notNull() |
| 138 | .references(() => |
| 139 | planId: varchar("plan_id |
| 140 | .notNull() |
| 141 | .references(() => |
| 142 | gatewayId: varchar("gateway_id |
| 143 | () => billingPaymentGateways. |
| 144 | ), |
| 145 | priceId: varchar("price_id |
| 146 | .notNull() |
| 147 | .references(() => |
| 148 | status: mysqlEnum("status |
| 149 | "active", |
| 150 | "trialing", |
| 151 | "past_due", |
| 152 | "canceled", |
| 153 | "unpaid", |
| 154 | "paused", |
| 155 | ]) |
| 156 | .notNull() |
| 157 | .default("active") |
| 158 | currency: varchar("currency |
| 159 | currentPeriodStart: timestamp("current_period_start |
| 160 | currentPeriodEnd: timestamp("current_period_end |
| 161 | trialStart: timestamp("trial_start |
| 162 | trialEnd: timestamp("trial_end |
| 163 | cancelAtPeriodEnd: boolean("cancel_at_period_end |
| 164 | canceledAt: timestamp("canceled_at |
| 165 | providerSubscriptionId: varchar("provider_subscription_id |
| 166 | length: 255, |
| 167 | }), |
| 168 | providerCustomerId: varchar("provider_customer_id |
| 169 | createdAt: timestamp("created_at |
| 170 | updatedAt: timestamp("updated_at |
| 171 | .defaultNow() |
| 172 | .$onUpdate(() => |
| 173 | .notNull(), |
| 174 | }, |
| 175 | (table) => |
| 176 | index("billing_subscriptions_userId_idx"). |
| 177 | index("billing_subscriptions_planId_idx"). |
| 178 | index("billing_subscriptions_providerSubscriptionId_idx"). |
| 179 | table.providerSubscriptionId, |
| 180 | ), |
| 181 | ], |
| 182 | ); |
| 183 | |
| 184 | export const billingInvoices = |
| 185 | "billing_invoices", |
| 186 | { |
| 187 | id: varchar("id |
| 188 | userId: varchar("user_id |
| 189 | .notNull() |
| 190 | .references(() => |
| 191 | subscriptionId: varchar("subscription_id |
| 192 | () => billingSubscriptions. |
| 193 | ), |
| 194 | gatewayId: varchar("gateway_id |
| 195 | () => billingPaymentGateways. |
| 196 | ), |
| 197 | invoiceNumber: varchar("invoice_number |
| 198 | status: mysqlEnum("status |
| 199 | "draft", |
| 200 | "open", |
| 201 | "paid", |
| 202 | "uncollectible", |
| 203 | "void", |
| 204 | ]) |
| 205 | .notNull() |
| 206 | .default("draft") |
| 207 | currency: varchar("currency |
| 208 | subtotalCents: int("subtotal_cents |
| 209 | taxCents: int("tax_cents |
| 210 | discountCents: int("discount_cents |
| 211 | totalCents: int("total_cents |
| 212 | amountPaidCents: int("amount_paid_cents |
| 213 | amountDueCents: int("amount_due_cents |
| 214 | vatNumber: varchar("vat_number |
| 215 | vatRate: int("vat_rate |
| 216 | billingAddress: json("billing_address |
| 217 | dueDate: timestamp("due_date |
| 218 | paidAt: timestamp("paid_at |
| 219 | notes: text("notes |
| 220 | providerInvoiceId: varchar("provider_invoice_id |
| 221 | createdAt: timestamp("created_at |
| 222 | updatedAt: timestamp("updated_at |
| 223 | .defaultNow() |
| 224 | .$onUpdate(() => |
| 225 | .notNull(), |
| 226 | }, |
| 227 | (table) => |
| 228 | unique("billing_invoices_invoiceNumber_unique"). |
| 229 | index("billing_invoices_userId_idx"). |
| 230 | index("billing_invoices_subscriptionId_idx"). |
| 231 | index("billing_invoices_providerInvoiceId_idx"). |
| 232 | ], |
| 233 | ); |
| 234 | |
| 235 | export const billingInvoiceItems = |
| 236 | "billing_invoice_items", |
| 237 | { |
| 238 | id: varchar("id |
| 239 | invoiceId: varchar("invoice_id |
| 240 | .notNull() |
| 241 | .references(() => |
| 242 | description: varchar("description |
| 243 | quantity: int("quantity |
| 244 | unitAmountCents: int("unit_amount_cents |
| 245 | totalCents: int("total_cents |
| 246 | currency: varchar("currency |
| 247 | periodStart: timestamp("period_start |
| 248 | periodEnd: timestamp("period_end |
| 249 | createdAt: timestamp("created_at |
| 250 | updatedAt: timestamp("updated_at |
| 251 | .defaultNow() |
| 252 | .$onUpdate(() => |
| 253 | .notNull(), |
| 254 | }, |
| 255 | (table) => |
| 256 | ); |
| 257 | |
| 258 | export const billingPaymentMethods = |
| 259 | "billing_payment_methods", |
| 260 | { |
| 261 | id: varchar("id |
| 262 | userId: varchar("user_id |
| 263 | .notNull() |
| 264 | .references(() => |
| 265 | gatewayId: varchar("gateway_id |
| 266 | type: mysqlEnum("type |
| 267 | "card", |
| 268 | "sepa_debit", |
| 269 | "paypal", |
| 270 | "bank_transfer", |
| 271 | "other", |
| 272 | ]) |
| 273 | .notNull() |
| 274 | .default("card") |
| 275 | cardBrand: varchar("card_brand |
| 276 | cardLast4: varchar("card_last4 |
| 277 | cardExpMonth: int("card_exp_month |
| 278 | cardExpYear: int("card_exp_year |
| 279 | isDefault: boolean("is_default |
| 280 | providerPaymentMethodId: varchar("provider_payment_method_id |
| 281 | length: 255, |
| 282 | }).notNull(), |
| 283 | providerCustomerId: varchar("provider_customer_id |
| 284 | createdAt: timestamp("created_at |
| 285 | updatedAt: timestamp("updated_at |
| 286 | .defaultNow() |
| 287 | .$onUpdate(() => |
| 288 | .notNull(), |
| 289 | }, |
| 290 | (table) => |
| 291 | index("billing_payment_methods_userId_idx"). |
| 292 | index("billing_payment_methods_providerPaymentMethodId_idx"). |
| 293 | table.providerPaymentMethodId, |
| 294 | ), |
| 295 | foreignKey({ |
| 296 | name: "bpm_gateway_id_fk", |
| 297 | columns: [table.gatewayId |
| 298 | foreignColumns: [billingPaymentGateways.id |
| 299 | }), |
| 300 | ], |
| 301 | ); |
| 302 | |
| 303 | export const billingTransactions = |
| 304 | "billing_transactions", |
| 305 | { |
| 306 | id: varchar("id |
| 307 | userId: varchar("user_id |
| 308 | .notNull() |
| 309 | .references(() => |
| 310 | invoiceId: varchar("invoice_id |
| 311 | () => billingInvoices. |
| 312 | ), |
| 313 | paymentMethodId: varchar("payment_method_id |
| 314 | gatewayId: varchar("gateway_id |
| 315 | () => billingPaymentGateways. |
| 316 | ), |
| 317 | type: mysqlEnum("type |
| 318 | .notNull() |
| 319 | .default("payment") |
| 320 | status: mysqlEnum("status |
| 321 | "pending", |
| 322 | "succeeded", |
| 323 | "failed", |
| 324 | "canceled", |
| 325 | "refunded", |
| 326 | ]) |
| 327 | .notNull() |
| 328 | .default("pending") |
| 329 | amountCents: int("amount_cents |
| 330 | currency: varchar("currency |
| 331 | description: text("description |
| 332 | failureMessage: text("failure_message |
| 333 | providerTransactionId: varchar("provider_transaction_id |
| 334 | providerChargeId: varchar("provider_charge_id |
| 335 | createdAt: timestamp("created_at |
| 336 | updatedAt: timestamp("updated_at |
| 337 | .defaultNow() |
| 338 | .$onUpdate(() => |
| 339 | .notNull(), |
| 340 | }, |
| 341 | (table) => |
| 342 | index("billing_transactions_userId_idx"). |
| 343 | index("billing_transactions_invoiceId_idx"). |
| 344 | unique("billing_transactions_providerTransactionId_unique"). |
| 345 | table.providerTransactionId, |
| 346 | ), |
| 347 | foreignKey({ |
| 348 | name: "bt_payment_method_id_fk", |
| 349 | columns: [table.paymentMethodId |
| 350 | foreignColumns: [billingPaymentMethods.id |
| 351 | }), |
| 352 | ], |
| 353 | ); |
| 354 | |
| 355 | export const billingWallet = |
| 356 | id: varchar("id |
| 357 | userId: varchar("user_id |
| 358 | .notNull() |
| 359 | .unique() |
| 360 | .references(() => |
| 361 | balanceCents: int("balance_cents |
| 362 | currency: varchar("currency |
| 363 | autoTopUpEnabled: boolean("auto_top_up_enabled |
| 364 | autoTopUpThresholdCents: int("auto_top_up_threshold_cents |
| 365 | .notNull() |
| 366 | .default(0), |
| 367 | autoTopUpAmountCents: int("auto_top_up_amount_cents |
| 368 | createdAt: timestamp("created_at |
| 369 | updatedAt: timestamp("updated_at |
| 370 | .defaultNow() |
| 371 | .$onUpdate(() => |
| 372 | .notNull(), |
| 373 | }); |
| 374 | |
| 375 | export const billingWalletTransactions = |
| 376 | "billing_wallet_transactions", |
| 377 | { |
| 378 | id: varchar("id |
| 379 | userId: varchar("user_id |
| 380 | .notNull() |
| 381 | .references(() => |
| 382 | invoiceId: varchar("invoice_id |
| 383 | () => billingInvoices. |
| 384 | ), |
| 385 | amountCents: int("amount_cents |
| 386 | balanceAfterCents: int("balance_after_cents |
| 387 | currency: varchar("currency |
| 388 | type: mysqlEnum("type |
| 389 | "topup", |
| 390 | "refund", |
| 391 | "charge", |
| 392 | "adjustment", |
| 393 | "expired", |
| 394 | ]).notNull(), |
| 395 | description: text("description |
| 396 | createdAt: timestamp("created_at |
| 397 | updatedAt: timestamp("updated_at |
| 398 | .defaultNow() |
| 399 | .$onUpdate(() => |
| 400 | .notNull(), |
| 401 | }, |
| 402 | (table) => |
| 403 | index("billing_wallet_transactions_userId_idx"). |
| 404 | index("billing_wallet_transactions_invoiceId_idx"). |
| 405 | ], |
| 406 | ); |
| 407 | |
| 408 | export const billingCoupons = |
| 409 | "billing_coupons", |
| 410 | { |
| 411 | id: varchar("id |
| 412 | code: varchar("code |
| 413 | description: text("description |
| 414 | discountType: mysqlEnum("discount_type |
| 415 | .notNull() |
| 416 | .default("percentage") |
| 417 | discountValue: int("discount_value |
| 418 | currency: varchar("currency |
| 419 | maxRedemptions: int("max_redemptions |
| 420 | timesRedeemed: int("times_redeemed |
| 421 | redeemBy: timestamp("redeem_by |
| 422 | isActive: boolean("is_active |
| 423 | planId: varchar("plan_id |
| 424 | () => billingPlans. |
| 425 | ), |
| 426 | providerCouponId: varchar("provider_coupon_id |
| 427 | createdAt: timestamp("created_at |
| 428 | updatedAt: timestamp("updated_at |
| 429 | .defaultNow() |
| 430 | .$onUpdate(() => |
| 431 | .notNull(), |
| 432 | }, |
| 433 | (table) => |
| 434 | unique("billing_coupons_code_unique"). |
| 435 | index("billing_coupons_planId_idx"). |
| 436 | ], |
| 437 | ); |
| 438 | |
| 439 | export const billingCouponRedemptions = |
| 440 | "billing_coupon_redemptions", |
| 441 | { |
| 442 | id: varchar("id |
| 443 | couponId: varchar("coupon_id |
| 444 | .notNull() |
| 445 | .references(() => |
| 446 | userId: varchar("user_id |
| 447 | .notNull() |
| 448 | .references(() => |
| 449 | invoiceId: varchar("invoice_id |
| 450 | () => billingInvoices. |
| 451 | ), |
| 452 | discountAmountCents: int("discount_amount_cents |
| 453 | currency: varchar("currency |
| 454 | redeemedAt: timestamp("redeemed_at |
| 455 | createdAt: timestamp("created_at |
| 456 | updatedAt: timestamp("updated_at |
| 457 | .defaultNow() |
| 458 | .$onUpdate(() => |
| 459 | .notNull(), |
| 460 | }, |
| 461 | (table) => |
| 462 | unique("billing_coupon_redemptions_couponId_userId_unique"). |
| 463 | table.couponId, |
| 464 | table.userId, |
| 465 | ), |
| 466 | index("billing_coupon_redemptions_couponId_idx"). |
| 467 | index("billing_coupon_redemptions_userId_idx"). |
| 468 | index("billing_coupon_redemptions_invoiceId_idx"). |
| 469 | ], |
| 470 | ); |
| 471 | |
| 472 | export const billingCategoriesRelations = |
| 473 | billingCategories, |
| 474 | ({ many }) |
| 475 | products: many(billingProducts |
| 476 | }), |
| 477 | ); |
| 478 | |
| 479 | export const billingProductsRelations = |
| 480 | billingProducts, |
| 481 | ({ one, many |
| 482 | category: one(billingCategories |
| 483 | fields: [billingProducts.categoryId |
| 484 | references: [billingCategories.id |
| 485 | }), |
| 486 | plans: many(billingPlans |
| 487 | }), |
| 488 | ); |
| 489 | |
| 490 | export const billingPaymentGatewaysRelations = |
| 491 | billingPaymentGateways, |
| 492 | ({ many }) |
| 493 | subscriptions: many(billingSubscriptions |
| 494 | invoices: many(billingInvoices |
| 495 | paymentMethods: many(billingPaymentMethods |
| 496 | transactions: many(billingTransactions |
| 497 | }), |
| 498 | ); |
| 499 | |
| 500 | export const billingPlansRelations = |
| 501 | billingPlans, |
| 502 | ({ one, many |
| 503 | product: one(billingProducts |
| 504 | fields: [billingPlans.productId |
| 505 | references: [billingProducts.id |
| 506 | }), |
| 507 | prices: many(billingPlanPrices |
| 508 | subscriptions: many(billingSubscriptions |
| 509 | coupons: many(billingCoupons |
| 510 | }), |
| 511 | ); |
| 512 | |
| 513 | export const billingPlanPricesRelations = |
| 514 | billingPlanPrices, |
| 515 | ({ one, many |
| 516 | plan: one(billingPlans |
| 517 | fields: [billingPlanPrices.planId |
| 518 | references: [billingPlans.id |
| 519 | }), |
| 520 | subscriptions: many(billingSubscriptions |
| 521 | }), |
| 522 | ); |
| 523 | |
| 524 | export const billingSubscriptionsRelations = |
| 525 | billingSubscriptions, |
| 526 | ({ one, many |
| 527 | user: one(user |
| 528 | fields: [billingSubscriptions.userId |
| 529 | references: [user.id |
| 530 | }), |
| 531 | plan: one(billingPlans |
| 532 | fields: [billingSubscriptions.planId |
| 533 | references: [billingPlans.id |
| 534 | }), |
| 535 | price: one(billingPlanPrices |
| 536 | fields: [billingSubscriptions.priceId |
| 537 | references: [billingPlanPrices.id |
| 538 | }), |
| 539 | gateway: one(billingPaymentGateways |
| 540 | fields: [billingSubscriptions.gatewayId |
| 541 | references: [billingPaymentGateways.id |
| 542 | }), |
| 543 | invoices: many(billingInvoices |
| 544 | }), |
| 545 | ); |
| 546 | |
| 547 | export const billingInvoicesRelations = |
| 548 | billingInvoices, |
| 549 | ({ one, many |
| 550 | user: one(user |
| 551 | fields: [billingInvoices.userId |
| 552 | references: [user.id |
| 553 | }), |
| 554 | subscription: one(billingSubscriptions |
| 555 | fields: [billingInvoices.subscriptionId |
| 556 | references: [billingSubscriptions.id |
| 557 | }), |
| 558 | gateway: one(billingPaymentGateways |
| 559 | fields: [billingInvoices.gatewayId |
| 560 | references: [billingPaymentGateways.id |
| 561 | }), |
| 562 | items: many(billingInvoiceItems |
| 563 | transactions: many(billingTransactions |
| 564 | walletTransactions: many(billingWalletTransactions |
| 565 | couponRedemptions: many(billingCouponRedemptions |
| 566 | }), |
| 567 | ); |
| 568 | |
| 569 | export const billingInvoiceItemsRelations = |
| 570 | billingInvoiceItems, |
| 571 | ({ one }) |
| 572 | invoice: one(billingInvoices |
| 573 | fields: [billingInvoiceItems.invoiceId |
| 574 | references: [billingInvoices.id |
| 575 | }), |
| 576 | }), |
| 577 | ); |
| 578 | |
| 579 | export const billingPaymentMethodsRelations = |
| 580 | billingPaymentMethods, |
| 581 | ({ one, many |
| 582 | user: one(user |
| 583 | fields: [billingPaymentMethods.userId |
| 584 | references: [user.id |
| 585 | }), |
| 586 | gateway: one(billingPaymentGateways |
| 587 | fields: [billingPaymentMethods.gatewayId |
| 588 | references: [billingPaymentGateways.id |
| 589 | }), |
| 590 | transactions: many(billingTransactions |
| 591 | }), |
| 592 | ); |
| 593 | |
| 594 | export const billingTransactionsRelations = |
| 595 | billingTransactions, |
| 596 | ({ one }) |
| 597 | user: one(user |
| 598 | fields: [billingTransactions.userId |
| 599 | references: [user.id |
| 600 | }), |
| 601 | invoice: one(billingInvoices |
| 602 | fields: [billingTransactions.invoiceId |
| 603 | references: [billingInvoices.id |
| 604 | }), |
| 605 | paymentMethod: one(billingPaymentMethods |
| 606 | fields: [billingTransactions.paymentMethodId |
| 607 | references: [billingPaymentMethods.id |
| 608 | }), |
| 609 | gateway: one(billingPaymentGateways |
| 610 | fields: [billingTransactions.gatewayId |
| 611 | references: [billingPaymentGateways.id |
| 612 | }), |
| 613 | }), |
| 614 | ); |
| 615 | |
| 616 | export const billingWalletRelations = |
| 617 | user: one(user |
| 618 | fields: [billingWallet.userId |
| 619 | references: [user.id |
| 620 | }), |
| 621 | })); |
| 622 | |
| 623 | export const billingWalletTransactionsRelations = |
| 624 | billingWalletTransactions, |
| 625 | ({ one }) |
| 626 | user: one(user |
| 627 | fields: [billingWalletTransactions.userId |
| 628 | references: [user.id |
| 629 | }), |
| 630 | invoice: one(billingInvoices |
| 631 | fields: [billingWalletTransactions.invoiceId |
| 632 | references: [billingInvoices.id |
| 633 | }), |
| 634 | }), |
| 635 | ); |
| 636 | |
| 637 | export const billingCouponsRelations = |
| 638 | billingCoupons, |
| 639 | ({ one, many |
| 640 | plan: one(billingPlans |
| 641 | fields: [billingCoupons.planId |
| 642 | references: [billingPlans.id |
| 643 | }), |
| 644 | redemptions: many(billingCouponRedemptions |
| 645 | }), |
| 646 | ); |
| 647 | |
| 648 | export const billingReferralCodes = |
| 649 | id: varchar("id |
| 650 | userId: varchar("user_id |
| 651 | code: varchar("code |
| 652 | usageCount: int("usage_count |
| 653 | isActive: boolean("is_active |
| 654 | createdAt: timestamp("created_at |
| 655 | updatedAt: timestamp("updated_at |
| 656 | }); |
| 657 | |
| 658 | export const billingReferralRedemptions = |
| 659 | "billing_referral_redemptions", |
| 660 | { |
| 661 | id: varchar("id |
| 662 | codeId: varchar("code_id |
| 663 | refereeId: varchar("referee_id |
| 664 | referrerId: varchar("referrer_id |
| 665 | firstTopupAt: timestamp("first_topup_at |
| 666 | createdAt: timestamp("created_at |
| 667 | updatedAt: timestamp("updated_at |
| 668 | }, |
| 669 | (table) => |
| 670 | index("brr_code_id_idx"). |
| 671 | index("brr_referrer_id_idx"). |
| 672 | foreignKey({ name: |
| 673 | foreignKey({ name: |
| 674 | foreignKey({ name: |
| 675 | ], |
| 676 | ); |
| 677 | |
| 678 | export const billingReferralCodesRelations = |
| 679 | user: one(user |
| 680 | redemptions: many(billingReferralRedemptions |
| 681 | })); |
| 682 | |
| 683 | export const billingReferralRedemptionsRelations = |
| 684 | code: one(billingReferralCodes |
| 685 | referee: one(user |
| 686 | referrer: one(user |
| 687 | })); |
| 688 | |
| 689 | export const billingCouponRedemptionsRelations = |
| 690 | billingCouponRedemptions, |
| 691 | ({ one }) |
| 692 | coupon: one(billingCoupons |
| 693 | fields: [billingCouponRedemptions.couponId |
| 694 | references: [billingCoupons.id |
| 695 | }), |
| 696 | user: one(user |
| 697 | fields: [billingCouponRedemptions.userId |
| 698 | references: [user.id |
| 699 | }), |
| 700 | invoice: one(billingInvoices |
| 701 | fields: [billingCouponRedemptions.invoiceId |
| 702 | references: [billingInvoices.id |
| 703 | }), |
| 704 | }), |
| 705 | ); |
| 706 | |
| 707 | export const billingUserRelations = |
| 708 | billingSubscriptions: many(billingSubscriptions |
| 709 | billingWallet: one(billingWallet |
| 710 | fields: [user.id |
| 711 | references: [billingWallet.userId |
| 712 | }), |
| 713 | billingInvoices: many(billingInvoices |
| 714 | billingPaymentMethods: many(billingPaymentMethods |
| 715 | billingTransactions: many(billingTransactions |
| 716 | billingWalletTransactions: many(billingWalletTransactions |
| 717 | billingCouponRedemptions: many(billingCouponRedemptions |
| 718 | })); |