@hartl-services/medusa-affiliate 0.3.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.
Files changed (169) hide show
  1. package/.medusa/server/src/admin/index.js +3739 -0
  2. package/.medusa/server/src/admin/index.mjs +3738 -0
  3. package/.medusa/server/src/api/admin/affiliate-applications/[id]/approve/route.js +28 -0
  4. package/.medusa/server/src/api/admin/affiliate-applications/[id]/reject/route.js +19 -0
  5. package/.medusa/server/src/api/admin/affiliate-applications/route.js +23 -0
  6. package/.medusa/server/src/api/admin/affiliate-distribution-reports/query.js +171 -0
  7. package/.medusa/server/src/api/admin/affiliate-distribution-reports/route.js +13 -0
  8. package/.medusa/server/src/api/admin/affiliate-program-settings/route.js +27 -0
  9. package/.medusa/server/src/api/admin/affiliate-reports/[id]/route.js +45 -0
  10. package/.medusa/server/src/api/admin/affiliate-reports/route.js +27 -0
  11. package/.medusa/server/src/api/admin/affiliates/[id]/consignment-reminders/route.js +31 -0
  12. package/.medusa/server/src/api/admin/affiliates/[id]/consignment-transfers/route.js +41 -0
  13. package/.medusa/server/src/api/admin/affiliates/[id]/coupon-codes/[coupon_id]/reconcile/route.js +34 -0
  14. package/.medusa/server/src/api/admin/affiliates/[id]/coupon-codes/[coupon_id]/revoke/route.js +36 -0
  15. package/.medusa/server/src/api/admin/affiliates/[id]/coupon-codes/[coupon_id]/rotate/route.js +40 -0
  16. package/.medusa/server/src/api/admin/affiliates/[id]/coupon-codes/route.js +56 -0
  17. package/.medusa/server/src/api/admin/affiliates/[id]/distribution/route.js +45 -0
  18. package/.medusa/server/src/api/admin/affiliates/[id]/inactive/route.js +20 -0
  19. package/.medusa/server/src/api/admin/affiliates/[id]/route.js +50 -0
  20. package/.medusa/server/src/api/admin/affiliates/[id]/slugs/[slug_id]/route.js +19 -0
  21. package/.medusa/server/src/api/admin/affiliates/[id]/slugs/route.js +26 -0
  22. package/.medusa/server/src/api/admin/affiliates/route.js +43 -0
  23. package/.medusa/server/src/api/admin/consignment-report-response.js +36 -0
  24. package/.medusa/server/src/api/admin/consignment-reports/[id]/approve/route.js +25 -0
  25. package/.medusa/server/src/api/admin/consignment-reports/[id]/correct/route.js +18 -0
  26. package/.medusa/server/src/api/admin/consignment-reports/[id]/reject/route.js +15 -0
  27. package/.medusa/server/src/api/admin/consignment-reports/[id]/route.js +11 -0
  28. package/.medusa/server/src/api/admin/consignment-reports/route.js +27 -0
  29. package/.medusa/server/src/api/admin/customers/[id]/affiliate/route.js +50 -0
  30. package/.medusa/server/src/api/admin/orders/[id]/affiliate-attribution/route.js +30 -0
  31. package/.medusa/server/src/api/affiliate-coupon-http.js +152 -0
  32. package/.medusa/server/src/api/affiliate-referral-cookie.js +34 -0
  33. package/.medusa/server/src/api/affiliate-self-service-http.js +37 -0
  34. package/.medusa/server/src/api/middlewares.js +786 -0
  35. package/.medusa/server/src/api/store/affiliate-referrals/apply-coupon/route.js +47 -0
  36. package/.medusa/server/src/api/store/affiliate-referrals/capture/route.js +76 -0
  37. package/.medusa/server/src/api/store/affiliate-referrals/carts/[cart_id]/route.js +20 -0
  38. package/.medusa/server/src/api/store/affiliates/consignment-inventory/route.js +53 -0
  39. package/.medusa/server/src/api/store/affiliates/consignment-report-response.js +31 -0
  40. package/.medusa/server/src/api/store/affiliates/consignment-reports/[id]/submit/route.js +40 -0
  41. package/.medusa/server/src/api/store/affiliates/consignment-reports/route.js +43 -0
  42. package/.medusa/server/src/api/store/affiliates/coupon-labels/route.js +14 -0
  43. package/.medusa/server/src/api/store/affiliates/me/commissions/route.js +35 -0
  44. package/.medusa/server/src/api/store/affiliates/me/coupon-codes/[coupon_id]/revoke/route.js +36 -0
  45. package/.medusa/server/src/api/store/affiliates/me/coupon-codes/[coupon_id]/rotate/route.js +40 -0
  46. package/.medusa/server/src/api/store/affiliates/me/coupon-codes/route.js +55 -0
  47. package/.medusa/server/src/api/store/affiliates/me/dashboard/route.js +15 -0
  48. package/.medusa/server/src/api/store/affiliates/me/route.js +85 -0
  49. package/.medusa/server/src/api/store/affiliates/me/slugs/[slug_id]/route.js +29 -0
  50. package/.medusa/server/src/api/store/affiliates/me/slugs/route.js +34 -0
  51. package/.medusa/server/src/api/store/affiliates/own-order-discount/route.js +22 -0
  52. package/.medusa/server/src/api/store/affiliates/own-order-promotion/route.js +30 -0
  53. package/.medusa/server/src/api/store/affiliates/register/route.js +27 -0
  54. package/.medusa/server/src/api/store/affiliates/resolve-referral/route.js +34 -0
  55. package/.medusa/server/src/api/validators.js +52 -0
  56. package/.medusa/server/src/api/wire-response.js +10 -0
  57. package/.medusa/server/src/integration/email-events.js +51 -0
  58. package/.medusa/server/src/integrations/inventory-transfer/batch-management-transfer-adapter.js +36 -0
  59. package/.medusa/server/src/integrations/inventory-transfer/native-inventory-transfer-adapter.js +30 -0
  60. package/.medusa/server/src/integrations/inventory-transfer/resolve-inventory-transfer-adapter.js +46 -0
  61. package/.medusa/server/src/integrations/inventory-transfer/types.js +3 -0
  62. package/.medusa/server/src/jobs/affiliate-attribution-expiry.js +20 -0
  63. package/.medusa/server/src/jobs/affiliate-consignment-report-reminders.js +15 -0
  64. package/.medusa/server/src/jobs/affiliate-promotion-freshness.js +24 -0
  65. package/.medusa/server/src/lib/query-string.js +15 -0
  66. package/.medusa/server/src/modules/affiliate/domain/affiliate-attribution-policy.js +81 -0
  67. package/.medusa/server/src/modules/affiliate/domain/affiliate-coupon-policy.js +61 -0
  68. package/.medusa/server/src/modules/affiliate/domain/commission-policy.js +33 -0
  69. package/.medusa/server/src/modules/affiliate/domain/consignment-reminder-rules.js +74 -0
  70. package/.medusa/server/src/modules/affiliate/domain/email-normalization.js +20 -0
  71. package/.medusa/server/src/modules/affiliate/domain/own-order-coupon-code.js +37 -0
  72. package/.medusa/server/src/modules/affiliate/domain/plugin-options.js +40 -0
  73. package/.medusa/server/src/modules/affiliate/index.js +13 -0
  74. package/.medusa/server/src/modules/affiliate/migrations/Migration20260827085154.js +423 -0
  75. package/.medusa/server/src/modules/affiliate/models/affiliate-application.js +38 -0
  76. package/.medusa/server/src/modules/affiliate/models/affiliate-cart-referral.js +65 -0
  77. package/.medusa/server/src/modules/affiliate/models/affiliate-code-redemption.js +41 -0
  78. package/.medusa/server/src/modules/affiliate/models/affiliate-coupon.js +55 -0
  79. package/.medusa/server/src/modules/affiliate/models/affiliate-distribution-settings.js +22 -0
  80. package/.medusa/server/src/modules/affiliate/models/affiliate-order-attribution.js +49 -0
  81. package/.medusa/server/src/modules/affiliate/models/affiliate-program-settings.js +28 -0
  82. package/.medusa/server/src/modules/affiliate/models/affiliate-referral-session.js +34 -0
  83. package/.medusa/server/src/modules/affiliate/models/affiliate-slug.js +27 -0
  84. package/.medusa/server/src/modules/affiliate/models/affiliate.js +49 -0
  85. package/.medusa/server/src/modules/affiliate/models/commission-entry.js +62 -0
  86. package/.medusa/server/src/modules/affiliate/models/consignment-report-line.js +21 -0
  87. package/.medusa/server/src/modules/affiliate/models/consignment-report.js +31 -0
  88. package/.medusa/server/src/modules/affiliate/models/consignment-transfer-line.js +16 -0
  89. package/.medusa/server/src/modules/affiliate/models/consignment-transfer.js +20 -0
  90. package/.medusa/server/src/modules/affiliate/models/customer-affiliate-assignment.js +58 -0
  91. package/.medusa/server/src/modules/affiliate/service.js +2321 -0
  92. package/.medusa/server/src/modules/affiliate/services/affiliate-application-manager.js +171 -0
  93. package/.medusa/server/src/modules/affiliate/services/affiliate-coupon-manager.js +441 -0
  94. package/.medusa/server/src/modules/affiliate/services/affiliate-discount.service.js +55 -0
  95. package/.medusa/server/src/modules/affiliate/services/affiliate-manager.js +242 -0
  96. package/.medusa/server/src/modules/affiliate/services/affiliate-partner-portal.service.js +138 -0
  97. package/.medusa/server/src/modules/affiliate/services/affiliate-report.service.js +436 -0
  98. package/.medusa/server/src/modules/affiliate/services/affiliate-slug-manager.js +163 -0
  99. package/.medusa/server/src/modules/affiliate/services/commission-ledger.service.js +729 -0
  100. package/.medusa/server/src/modules/affiliate/services/customer-affiliate-assignment-manager.js +350 -0
  101. package/.medusa/server/src/modules/affiliate/services/order-attribution-manager.js +1051 -0
  102. package/.medusa/server/src/modules/affiliate/services/referral-lifecycle-manager.js +1420 -0
  103. package/.medusa/server/src/modules/affiliate/services/utils.js +49 -0
  104. package/.medusa/server/src/modules/affiliate/types/internal.js +3 -0
  105. package/.medusa/server/src/paths/affiliate.js +7 -0
  106. package/.medusa/server/src/paths/index.js +18 -0
  107. package/.medusa/server/src/schemas/index.js +18 -0
  108. package/.medusa/server/src/schemas/store.js +24 -0
  109. package/.medusa/server/src/subscribers/affiliate-cart-updated.js +92 -0
  110. package/.medusa/server/src/subscribers/affiliate-order-canceled.js +70 -0
  111. package/.medusa/server/src/subscribers/affiliate-order-placed.js +63 -0
  112. package/.medusa/server/src/subscribers/affiliate-order-return-received.js +67 -0
  113. package/.medusa/server/src/subscribers/affiliate-product-eligibility-updated.js +87 -0
  114. package/.medusa/server/src/types/admin-api.js +3 -0
  115. package/.medusa/server/src/types/enums.js +53 -0
  116. package/.medusa/server/src/types/index.js +32 -0
  117. package/.medusa/server/src/types/public.js +3 -0
  118. package/.medusa/server/src/types/store-api.js +3 -0
  119. package/.medusa/server/src/utils/affiliate-cart-operation-lock.js +316 -0
  120. package/.medusa/server/src/utils/affiliate-promotion-mutation-authorization.js +49 -0
  121. package/.medusa/server/src/utils/cart-promotions.js +9 -0
  122. package/.medusa/server/src/utils/money.js +79 -0
  123. package/.medusa/server/src/utils/promotion-code-availability.js +34 -0
  124. package/.medusa/server/src/utils/registered-email-checkout.js +101 -0
  125. package/.medusa/server/src/utils/resolve-services.js +7 -0
  126. package/.medusa/server/src/utils/settle-cart-promotions.js +44 -0
  127. package/.medusa/server/src/workflows/affiliate/affiliate-coupon-cart.js +215 -0
  128. package/.medusa/server/src/workflows/affiliate/affiliate-coupon-command-authorization.js +20 -0
  129. package/.medusa/server/src/workflows/affiliate/apply-affiliate-coupon.js +174 -0
  130. package/.medusa/server/src/workflows/affiliate/apply-own-order-promotion.js +89 -0
  131. package/.medusa/server/src/workflows/affiliate/approve-consignment-report.js +186 -0
  132. package/.medusa/server/src/workflows/affiliate/correct-approved-consignment-report.js +263 -0
  133. package/.medusa/server/src/workflows/affiliate/create-affiliate-coupon.js +35 -0
  134. package/.medusa/server/src/workflows/affiliate/create-commission-correction-for-cancellation.js +21 -0
  135. package/.medusa/server/src/workflows/affiliate/create-commission-correction-for-refund.js +24 -0
  136. package/.medusa/server/src/workflows/affiliate/create-commission-for-order.js +17 -0
  137. package/.medusa/server/src/workflows/affiliate/deactivate-affiliate.js +89 -0
  138. package/.medusa/server/src/workflows/affiliate/enable-distribution.js +120 -0
  139. package/.medusa/server/src/workflows/affiliate/enrich-affiliate-report.js +75 -0
  140. package/.medusa/server/src/workflows/affiliate/ensure-affiliate-coupon-promotion.js +410 -0
  141. package/.medusa/server/src/workflows/affiliate/ensure-affiliate-own-order-promotion.js +240 -0
  142. package/.medusa/server/src/workflows/affiliate/ensure-default-affiliate-coupon.js +32 -0
  143. package/.medusa/server/src/workflows/affiliate/expire-affiliate-attribution-state.js +131 -0
  144. package/.medusa/server/src/workflows/affiliate/finalize-affiliate-order-attribution.js +82 -0
  145. package/.medusa/server/src/workflows/affiliate/payment-authorization.js +33 -0
  146. package/.medusa/server/src/workflows/affiliate/promotion-eligibility.js +179 -0
  147. package/.medusa/server/src/workflows/affiliate/reconcile-affiliate-cart-referral.js +219 -0
  148. package/.medusa/server/src/workflows/affiliate/reconcile-affiliate-coupon.js +388 -0
  149. package/.medusa/server/src/workflows/affiliate/refresh-affiliate-promotions.js +114 -0
  150. package/.medusa/server/src/workflows/affiliate/resolve-coupon-labels.js +40 -0
  151. package/.medusa/server/src/workflows/affiliate/revoke-affiliate-coupon.js +29 -0
  152. package/.medusa/server/src/workflows/affiliate/rotate-affiliate-coupon.js +36 -0
  153. package/.medusa/server/src/workflows/affiliate/run-consignment-report-reminders.js +58 -0
  154. package/.medusa/server/src/workflows/affiliate/submit-consignment-report.js +279 -0
  155. package/.medusa/server/src/workflows/affiliate/transfer-batch-aware-inventory.js +172 -0
  156. package/.medusa/server/src/workflows/affiliate/transfer-consignment-stock.js +106 -0
  157. package/.medusa/server/src/workflows/affiliate/update-affiliate-coupon-discounts.js +129 -0
  158. package/.medusa/server/src/workflows/affiliate/utils.js +118 -0
  159. package/.medusa/server/src/workflows/hooks/complete-cart-affiliate-attribution.js +57 -0
  160. package/.medusa/server/src/workflows/hooks/complete-cart-order-created-composition.js +111 -0
  161. package/.medusa/server/src/workflows/hooks/complete-cart-own-order-discount.js +75 -0
  162. package/.medusa/server/src/workflows/hooks/enforce-unique-promotion-codes.js +33 -0
  163. package/.medusa/server/src/workflows/hooks/validate-affiliate-customer-code.js +239 -0
  164. package/.medusa/server/src/workflows/hooks/validate-complete-cart-affiliate-referral.js +137 -0
  165. package/LICENSE +21 -0
  166. package/README.md +799 -0
  167. package/dist/integration/email-events.d.ts +20 -0
  168. package/dist/integration/email-events.js +50 -0
  169. package/package.json +84 -0
@@ -0,0 +1,423 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Migration20260827085154 = void 0;
4
+ const migrations_1 = require("@medusajs/framework/mikro-orm/migrations");
5
+ class Migration20260827085154 extends migrations_1.Migration {
6
+ async up() {
7
+ this.addSql(`alter table if exists "affiliate_distribution_settings" drop constraint if exists "affiliate_distribution_settings_channel_unique";`);
8
+ this.addSql(`alter table if exists "affiliate_distribution_settings" drop constraint if exists "affiliate_distribution_settings_location_unique";`);
9
+ this.addSql(`alter table if exists "affiliate_distribution_settings" drop constraint if exists "affiliate_distribution_settings_affiliate_unique";`);
10
+ this.addSql(`alter table if exists "affiliate_coupon" drop constraint if exists "affiliate_coupon_promotion_id_unique";`);
11
+ this.addSql(`alter table if exists "affiliate_code_redemption" drop constraint if exists "affiliate_code_redemption_customer_id_unique";`);
12
+ this.addSql(`create table if not exists "affiliate" ("id" text not null, "display_name" text not null, "customer_id" text null, "status" text not null default 'pending', "commission_rate" real not null default 0, "own_order_discount_rate" real not null default 0, "customer_assignment_mode" text not null default 'lifetime', "assignment_duration_days" integer null, "customer_discount_rate_override" real null, "own_order_promotion_id" text null, "referred_by_affiliate_id" text null, "referrer_commission_rate" real null, "primary_slug_id" text null, "internal_notes" text null, "activated_at" timestamptz null, "inactivated_at" timestamptz null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "affiliate_pkey" primary key ("id"));`);
13
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_customer_id" ON "affiliate" ("customer_id") WHERE deleted_at IS NULL;`);
14
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_status" ON "affiliate" ("status") WHERE deleted_at IS NULL;`);
15
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_referred_by_affiliate_id" ON "affiliate" ("referred_by_affiliate_id") WHERE deleted_at IS NULL;`);
16
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_primary_slug_id" ON "affiliate" ("primary_slug_id") WHERE deleted_at IS NULL;`);
17
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_deleted_at" ON "affiliate" ("deleted_at") WHERE deleted_at IS NULL;`);
18
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_unique_customer" ON "affiliate" ("customer_id") WHERE deleted_at IS NULL AND customer_id IS NOT NULL;`);
19
+ this.addSql(`create table if not exists "affiliate_application" ("id" text not null, "customer_id" text not null, "display_name" text not null, "requested_slug" text null, "status" text not null default 'pending', "message" text null, "rejection_reason" text null, "affiliate_id" text null, "reviewed_by_id" text null, "reviewed_at" timestamptz null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "affiliate_application_pkey" primary key ("id"));`);
20
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_application_customer_id" ON "affiliate_application" ("customer_id") WHERE deleted_at IS NULL;`);
21
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_application_status" ON "affiliate_application" ("status") WHERE deleted_at IS NULL;`);
22
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_application_affiliate_id" ON "affiliate_application" ("affiliate_id") WHERE deleted_at IS NULL;`);
23
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_application_deleted_at" ON "affiliate_application" ("deleted_at") WHERE deleted_at IS NULL;`);
24
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_application_unique_pending_customer" ON "affiliate_application" ("customer_id") WHERE deleted_at IS NULL AND status = 'pending';`);
25
+ this.addSql(`create table if not exists "affiliate_cart_referral" ("id" text not null, "cart_id" text not null, "referral_session_id" text null, "affiliate_id" text not null, "affiliate_slug_id" text null, "source_affiliate_coupon_id" text null, "applied_affiliate_coupon_id" text null, "applied_promotion_id" text null, "source" text not null, "status" text not null default 'active', "source_value_snapshot" text not null, "captured_at" timestamptz not null, "expires_at" timestamptz not null, "replaced_by_referral_id" text null, "consumed_order_id" text null, "invalidated_reason" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "affiliate_cart_referral_pkey" primary key ("id"));`);
26
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_cart_referral_cart_id" ON "affiliate_cart_referral" ("cart_id") WHERE deleted_at IS NULL;`);
27
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_cart_referral_session_id" ON "affiliate_cart_referral" ("referral_session_id") WHERE deleted_at IS NULL;`);
28
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_cart_referral_affiliate_id" ON "affiliate_cart_referral" ("affiliate_id") WHERE deleted_at IS NULL;`);
29
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_cart_referral_affiliate_slug_id" ON "affiliate_cart_referral" ("affiliate_slug_id") WHERE deleted_at IS NULL;`);
30
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_cart_referral_source_coupon_id" ON "affiliate_cart_referral" ("source_affiliate_coupon_id") WHERE deleted_at IS NULL;`);
31
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_cart_referral_applied_coupon_id" ON "affiliate_cart_referral" ("applied_affiliate_coupon_id") WHERE deleted_at IS NULL;`);
32
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_cart_referral_applied_promotion_id" ON "affiliate_cart_referral" ("applied_promotion_id") WHERE deleted_at IS NULL;`);
33
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_cart_referral_source" ON "affiliate_cart_referral" ("source") WHERE deleted_at IS NULL;`);
34
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_cart_referral_status" ON "affiliate_cart_referral" ("status") WHERE deleted_at IS NULL;`);
35
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_cart_referral_captured_at" ON "affiliate_cart_referral" ("captured_at") WHERE deleted_at IS NULL;`);
36
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_cart_referral_expires_at" ON "affiliate_cart_referral" ("expires_at") WHERE deleted_at IS NULL;`);
37
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_cart_referral_deleted_at" ON "affiliate_cart_referral" ("deleted_at") WHERE deleted_at IS NULL;`);
38
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_cart_referral_unique_active_cart" ON "affiliate_cart_referral" ("cart_id") WHERE deleted_at IS NULL AND status = 'active';`);
39
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_cart_referral_unique_pending_coupon_cart" ON "affiliate_cart_referral" ("cart_id") WHERE deleted_at IS NULL AND source = 'coupon' AND status = 'pending_email_validation';`);
40
+ this.addSql(`create table if not exists "affiliate_code_redemption" ("id" text not null, "affiliate_id" text not null, "affiliate_coupon_id" text not null, "promotion_id" text not null, "code_snapshot" text not null, "normalized_email" text not null, "customer_id" text null, "assignment_id" text null, "cart_id" text not null, "order_id" text not null, "redeemed_at" timestamptz not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "affiliate_code_redemption_pkey" primary key ("id"));`);
41
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_code_redemption_affiliate_id" ON "affiliate_code_redemption" ("affiliate_id") WHERE deleted_at IS NULL;`);
42
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_code_redemption_coupon_id" ON "affiliate_code_redemption" ("affiliate_coupon_id") WHERE deleted_at IS NULL;`);
43
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_code_redemption_promotion_id" ON "affiliate_code_redemption" ("promotion_id") WHERE deleted_at IS NULL;`);
44
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_code_redemption_email" ON "affiliate_code_redemption" ("normalized_email");`);
45
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_code_redemption_customer_id" ON "affiliate_code_redemption" ("customer_id") WHERE deleted_at IS NULL;`);
46
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_code_redemption_assignment_id" ON "affiliate_code_redemption" ("assignment_id") WHERE deleted_at IS NULL;`);
47
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_code_redemption_cart_id" ON "affiliate_code_redemption" ("cart_id") WHERE deleted_at IS NULL;`);
48
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_code_redemption_order" ON "affiliate_code_redemption" ("order_id");`);
49
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_code_redemption_redeemed_at" ON "affiliate_code_redemption" ("redeemed_at") WHERE deleted_at IS NULL;`);
50
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_code_redemption_deleted_at" ON "affiliate_code_redemption" ("deleted_at") WHERE deleted_at IS NULL;`);
51
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_code_redemption_customer_id_unique" ON "affiliate_code_redemption" ("customer_id") WHERE customer_id IS NOT NULL;`);
52
+ this.addSql(`create table if not exists "affiliate_coupon" ("id" text not null, "affiliate_id" text not null, "code" text not null, "lifecycle_status" text not null default 'enabled', "provisioning_status" text not null default 'pending', "promotion_id" text null, "promotion_setup_error" text null, "origin" text not null, "creation_idempotency_key" text null, "replaces_coupon_id" text null, "created_by_type" text not null, "created_by_id" text null, "revoked_at" timestamptz null, "revoked_by_type" text null, "revoked_by_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "affiliate_coupon_pkey" primary key ("id"));`);
53
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_coupon_affiliate_id" ON "affiliate_coupon" ("affiliate_id") WHERE deleted_at IS NULL;`);
54
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_coupon_code" ON "affiliate_coupon" ("code");`);
55
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_coupon_lifecycle_status" ON "affiliate_coupon" ("lifecycle_status") WHERE deleted_at IS NULL;`);
56
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_coupon_provisioning_status" ON "affiliate_coupon" ("provisioning_status") WHERE deleted_at IS NULL;`);
57
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_coupon_replaces_coupon_id" ON "affiliate_coupon" ("replaces_coupon_id") WHERE deleted_at IS NULL;`);
58
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_coupon_deleted_at" ON "affiliate_coupon" ("deleted_at") WHERE deleted_at IS NULL;`);
59
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_coupon_promotion_id_unique" ON "affiliate_coupon" ("promotion_id") WHERE promotion_id IS NOT NULL;`);
60
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_coupon_creation_idempotency_key" ON "affiliate_coupon" ("creation_idempotency_key") WHERE creation_idempotency_key IS NOT NULL;`);
61
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_coupon_active_replacement" ON "affiliate_coupon" ("replaces_coupon_id") WHERE replaces_coupon_id IS NOT NULL AND lifecycle_status = 'enabled';`);
62
+ this.addSql(`create table if not exists "affiliate_distribution_settings" ("id" text not null, "affiliate_id" text not null, "stock_location_id" text not null, "internal_sales_channel_id" text not null, "region_id" text not null, "shipping_option_id" text not null, "enabled" boolean not null default true, "own_order_promotion_id" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "affiliate_distribution_settings_pkey" primary key ("id"));`);
63
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_distribution_settings_affiliate_id" ON "affiliate_distribution_settings" ("affiliate_id") WHERE deleted_at IS NULL;`);
64
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_distribution_settings_stock_location_id" ON "affiliate_distribution_settings" ("stock_location_id") WHERE deleted_at IS NULL;`);
65
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_distribution_settings_internal_sales_channel_id" ON "affiliate_distribution_settings" ("internal_sales_channel_id") WHERE deleted_at IS NULL;`);
66
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_distribution_settings_region_id" ON "affiliate_distribution_settings" ("region_id") WHERE deleted_at IS NULL;`);
67
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_distribution_settings_shipping_option_id" ON "affiliate_distribution_settings" ("shipping_option_id") WHERE deleted_at IS NULL;`);
68
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_distribution_settings_enabled" ON "affiliate_distribution_settings" ("enabled") WHERE deleted_at IS NULL;`);
69
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_distribution_settings_deleted_at" ON "affiliate_distribution_settings" ("deleted_at") WHERE deleted_at IS NULL;`);
70
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_distribution_settings_affiliate_unique" ON "affiliate_distribution_settings" ("affiliate_id") WHERE deleted_at IS NULL;`);
71
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_distribution_settings_location_unique" ON "affiliate_distribution_settings" ("stock_location_id") WHERE deleted_at IS NULL;`);
72
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_distribution_settings_channel_unique" ON "affiliate_distribution_settings" ("internal_sales_channel_id") WHERE deleted_at IS NULL;`);
73
+ this.addSql(`create table if not exists "affiliate_order_attribution" ("id" text not null, "order_id" text not null, "cart_id" text not null, "affiliate_id" text not null, "customer_id" text null, "normalized_email" text null, "source" text not null, "customer_affiliate_assignment_id" text null, "affiliate_cart_referral_id" text null, "affiliate_slug_id" text null, "affiliate_code_redemption_id" text null, "promotion_id" text null, "slug_or_code_snapshot" text null, "currency_code" text not null, "customer_assignment_mode" text not null, "assignment_duration_days" integer null, "commission_rate" real not null, "referrer_affiliate_id" text null, "referrer_commission_rate" real null, "policy_version" integer not null, "commission_base_strategy" text not null, "referrer_base_strategy" text not null, "attributed_at" timestamptz not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "affiliate_order_attribution_pkey" primary key ("id"));`);
74
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_order_attribution_order_id" ON "affiliate_order_attribution" ("order_id");`);
75
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_order_attribution_cart_id" ON "affiliate_order_attribution" ("cart_id") WHERE deleted_at IS NULL;`);
76
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_order_attribution_affiliate_id" ON "affiliate_order_attribution" ("affiliate_id") WHERE deleted_at IS NULL;`);
77
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_order_attribution_customer_id" ON "affiliate_order_attribution" ("customer_id") WHERE deleted_at IS NULL;`);
78
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_order_attribution_source" ON "affiliate_order_attribution" ("source") WHERE deleted_at IS NULL;`);
79
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_order_attribution_currency_code" ON "affiliate_order_attribution" ("currency_code") WHERE deleted_at IS NULL;`);
80
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_order_attribution_attributed_at" ON "affiliate_order_attribution" ("attributed_at") WHERE deleted_at IS NULL;`);
81
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_order_attribution_deleted_at" ON "affiliate_order_attribution" ("deleted_at") WHERE deleted_at IS NULL;`);
82
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_order_attribution_reporting" ON "affiliate_order_attribution" ("affiliate_id", "attributed_at", "currency_code") WHERE deleted_at IS NULL;`);
83
+ this.addSql(`create table if not exists "affiliate_program_settings" ("id" text not null, "scope" text not null default 'global', "commission_base" text not null default 'after_discounts', "referrer_base" text not null default 'order_base', "promotion_stacking" text not null default 'allow_stacking', "default_customer_discount_rate" real not null default 5, "referral_overwrite_policy" text not null default 'first_touch', "referral_window_days" integer not null default 30, "policy_version" integer not null default 1, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "affiliate_program_settings_pkey" primary key ("id"));`);
84
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_program_settings_deleted_at" ON "affiliate_program_settings" ("deleted_at") WHERE deleted_at IS NULL;`);
85
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_program_settings_global" ON "affiliate_program_settings" ("scope") WHERE deleted_at IS NULL;`);
86
+ this.addSql(`create table if not exists "affiliate_referral_session" ("id" text not null, "token_hash" text not null, "affiliate_id" text not null, "affiliate_slug_id" text not null, "slug_snapshot" text not null, "status" text not null default 'active', "expires_at" timestamptz not null, "consumed_at" timestamptz null, "invalidated_at" timestamptz null, "replaced_by_session_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "affiliate_referral_session_pkey" primary key ("id"));`);
87
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_referral_session_token_hash" ON "affiliate_referral_session" ("token_hash");`);
88
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_referral_session_affiliate_id" ON "affiliate_referral_session" ("affiliate_id") WHERE deleted_at IS NULL;`);
89
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_referral_session_affiliate_slug_id" ON "affiliate_referral_session" ("affiliate_slug_id") WHERE deleted_at IS NULL;`);
90
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_referral_session_status" ON "affiliate_referral_session" ("status") WHERE deleted_at IS NULL;`);
91
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_referral_session_expires_at" ON "affiliate_referral_session" ("expires_at") WHERE deleted_at IS NULL;`);
92
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_referral_session_replaced_by_session_id" ON "affiliate_referral_session" ("replaced_by_session_id") WHERE deleted_at IS NULL;`);
93
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_referral_session_deleted_at" ON "affiliate_referral_session" ("deleted_at") WHERE deleted_at IS NULL;`);
94
+ this.addSql(`create table if not exists "affiliate_slug" ("id" text not null, "affiliate_id" text not null, "slug" text not null, "active" boolean not null default true, "is_primary" boolean not null default false, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "affiliate_slug_pkey" primary key ("id"));`);
95
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_slug_affiliate_id" ON "affiliate_slug" ("affiliate_id") WHERE deleted_at IS NULL;`);
96
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_slug_unique_slug" ON "affiliate_slug" ("slug");`);
97
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_slug_active" ON "affiliate_slug" ("active") WHERE deleted_at IS NULL;`);
98
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_slug_is_primary" ON "affiliate_slug" ("is_primary") WHERE deleted_at IS NULL;`);
99
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_affiliate_slug_deleted_at" ON "affiliate_slug" ("deleted_at") WHERE deleted_at IS NULL;`);
100
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_affiliate_slug_unique_primary" ON "affiliate_slug" ("affiliate_id") WHERE deleted_at IS NULL AND is_primary = true;`);
101
+ this.addSql(`create table if not exists "commission_entry" ("id" text not null, "affiliate_id" text not null, "customer_id" text null, "order_id" text null, "refund_id" text null, "base_commission_entry_id" text null, "source_affiliate_id" text null, "type" text not null, "base_amount_net" real not null, "policy_version" integer null, "commission_base_strategy" text null, "referrer_base_strategy" text null, "undiscounted_base_amount_net" real null, "discounted_base_amount_net" real null, "discount_amount_net" real null, "direct_commission_amount" real null, "referrer_base_amount_net" real null, "commission_rate" real not null, "commission_amount" real not null, "currency_code" text not null, "occurred_at" timestamptz not null, "idempotency_key" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "commission_entry_pkey" primary key ("id"));`);
102
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_commission_entry_affiliate_id" ON "commission_entry" ("affiliate_id") WHERE deleted_at IS NULL;`);
103
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_commission_entry_customer_id" ON "commission_entry" ("customer_id") WHERE deleted_at IS NULL;`);
104
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_commission_entry_order_id" ON "commission_entry" ("order_id") WHERE deleted_at IS NULL;`);
105
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_commission_entry_refund_id" ON "commission_entry" ("refund_id") WHERE deleted_at IS NULL;`);
106
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_commission_entry_base_commission_entry_id" ON "commission_entry" ("base_commission_entry_id") WHERE deleted_at IS NULL;`);
107
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_commission_entry_source_affiliate_id" ON "commission_entry" ("source_affiliate_id") WHERE deleted_at IS NULL;`);
108
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_commission_entry_type" ON "commission_entry" ("type") WHERE deleted_at IS NULL;`);
109
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_commission_entry_currency_code" ON "commission_entry" ("currency_code") WHERE deleted_at IS NULL;`);
110
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_commission_entry_occurred_at" ON "commission_entry" ("occurred_at") WHERE deleted_at IS NULL;`);
111
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_commission_entry_deleted_at" ON "commission_entry" ("deleted_at") WHERE deleted_at IS NULL;`);
112
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_commission_entry_unique_idempotency_key" ON "commission_entry" ("idempotency_key") WHERE deleted_at IS NULL AND idempotency_key IS NOT NULL;`);
113
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_commission_entry_reporting" ON "commission_entry" ("affiliate_id", "occurred_at", "currency_code") WHERE deleted_at IS NULL;`);
114
+ this.addSql(`create table if not exists "consignment_report" ("id" text not null, "affiliate_id" text not null, "period_start" timestamptz not null, "period_end" timestamptz null, "status" text not null default 'draft', "version" integer not null default 1, "submission_idempotency_key" text null, "approval_idempotency_key" text null, "submitted_by_id" text null, "approved_by_id" text null, "draft_order_id" text null, "order_id" text null, "fulfillment_id" text null, "submitted_at" timestamptz null, "approved_at" timestamptz null, "admin_notes" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "consignment_report_pkey" primary key ("id"));`);
115
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_report_affiliate_id" ON "consignment_report" ("affiliate_id") WHERE deleted_at IS NULL;`);
116
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_report_period_start" ON "consignment_report" ("period_start") WHERE deleted_at IS NULL;`);
117
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_report_period_end" ON "consignment_report" ("period_end") WHERE deleted_at IS NULL;`);
118
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_report_status" ON "consignment_report" ("status") WHERE deleted_at IS NULL;`);
119
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_report_draft_order_id" ON "consignment_report" ("draft_order_id") WHERE deleted_at IS NULL;`);
120
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_report_order_id" ON "consignment_report" ("order_id") WHERE deleted_at IS NULL;`);
121
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_report_fulfillment_id" ON "consignment_report" ("fulfillment_id") WHERE deleted_at IS NULL;`);
122
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_report_deleted_at" ON "consignment_report" ("deleted_at") WHERE deleted_at IS NULL;`);
123
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_consignment_report_active_affiliate_period" ON "consignment_report" ("affiliate_id", "period_start") WHERE deleted_at IS NULL AND status IN ('draft', 'submitted', 'approved');`);
124
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_consignment_report_submission_idempotency_key" ON "consignment_report" ("submission_idempotency_key") WHERE deleted_at IS NULL AND submission_idempotency_key IS NOT NULL;`);
125
+ this.addSql(`create table if not exists "consignment_report_line" ("id" text not null, "consignment_report_id" text not null, "inventory_item_id" text not null, "batch_id" text null, "expected_quantity" integer not null, "counted_quantity" integer not null, "confirmed_sale_quantity" integer not null, "requires_admin_clarification" boolean not null default false, "non_sale_reason" text null, "correction_reason" text null, "unit_price_snapshot" real null, "currency_code" text null, "order_line_item_id" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "consignment_report_line_pkey" primary key ("id"));`);
126
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_report_line_report_id" ON "consignment_report_line" ("consignment_report_id") WHERE deleted_at IS NULL;`);
127
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_report_line_inventory_item_id" ON "consignment_report_line" ("inventory_item_id") WHERE deleted_at IS NULL;`);
128
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_report_line_batch_id" ON "consignment_report_line" ("batch_id") WHERE deleted_at IS NULL;`);
129
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_report_line_order_line_item_id" ON "consignment_report_line" ("order_line_item_id") WHERE deleted_at IS NULL;`);
130
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_report_line_deleted_at" ON "consignment_report_line" ("deleted_at") WHERE deleted_at IS NULL;`);
131
+ this.addSql(`create table if not exists "consignment_transfer" ("id" text not null, "affiliate_id" text not null, "source_location_id" text not null, "destination_location_id" text not null, "direction" text not null, "status" text not null default 'pending', "idempotency_key" text not null, "admin_id" text null, "failure_reason" text null, "completed_at" timestamptz null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "consignment_transfer_pkey" primary key ("id"));`);
132
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_transfer_affiliate_id" ON "consignment_transfer" ("affiliate_id") WHERE deleted_at IS NULL;`);
133
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_transfer_source_location_id" ON "consignment_transfer" ("source_location_id") WHERE deleted_at IS NULL;`);
134
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_transfer_destination_location_id" ON "consignment_transfer" ("destination_location_id") WHERE deleted_at IS NULL;`);
135
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_transfer_direction" ON "consignment_transfer" ("direction") WHERE deleted_at IS NULL;`);
136
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_transfer_status" ON "consignment_transfer" ("status") WHERE deleted_at IS NULL;`);
137
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_transfer_admin_id" ON "consignment_transfer" ("admin_id") WHERE deleted_at IS NULL;`);
138
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_transfer_deleted_at" ON "consignment_transfer" ("deleted_at") WHERE deleted_at IS NULL;`);
139
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_consignment_transfer_idempotency_key" ON "consignment_transfer" ("idempotency_key") WHERE deleted_at IS NULL;`);
140
+ this.addSql(`create table if not exists "consignment_transfer_line" ("id" text not null, "consignment_transfer_id" text not null, "inventory_item_id" text not null, "source_batch_id" text null, "destination_batch_id" text null, "quantity" integer not null, "batch_name_snapshot" text null, "batch_expiry_date_snapshot" timestamptz null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "consignment_transfer_line_pkey" primary key ("id"));`);
141
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_transfer_line_transfer_id" ON "consignment_transfer_line" ("consignment_transfer_id") WHERE deleted_at IS NULL;`);
142
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_transfer_line_inventory_item_id" ON "consignment_transfer_line" ("inventory_item_id") WHERE deleted_at IS NULL;`);
143
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_transfer_line_source_batch_id" ON "consignment_transfer_line" ("source_batch_id") WHERE deleted_at IS NULL;`);
144
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_transfer_line_destination_batch_id" ON "consignment_transfer_line" ("destination_batch_id") WHERE deleted_at IS NULL;`);
145
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_consignment_transfer_line_deleted_at" ON "consignment_transfer_line" ("deleted_at") WHERE deleted_at IS NULL;`);
146
+ this.addSql(`create table if not exists "customer_affiliate_assignment" ("id" text not null, "customer_id" text null, "normalized_email" text null, "affiliate_id" text not null, "source" text not null default 'referral', "active" boolean not null default true, "assigned_at" timestamptz not null, "customer_assignment_mode" text not null default 'lifetime', "assignment_duration_days" integer null, "valid_until" timestamptz null, "ended_at" timestamptz null, "ended_reason" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "customer_affiliate_assignment_pkey" primary key ("id"));`);
147
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_customer_affiliate_assignment_customer_id" ON "customer_affiliate_assignment" ("customer_id") WHERE deleted_at IS NULL;`);
148
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_customer_affiliate_assignment_normalized_email" ON "customer_affiliate_assignment" ("normalized_email") WHERE deleted_at IS NULL;`);
149
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_customer_affiliate_assignment_affiliate_id" ON "customer_affiliate_assignment" ("affiliate_id") WHERE deleted_at IS NULL;`);
150
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_customer_affiliate_assignment_source" ON "customer_affiliate_assignment" ("source") WHERE deleted_at IS NULL;`);
151
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_customer_affiliate_assignment_active" ON "customer_affiliate_assignment" ("active") WHERE deleted_at IS NULL;`);
152
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_customer_affiliate_assigned_at" ON "customer_affiliate_assignment" ("assigned_at") WHERE deleted_at IS NULL;`);
153
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_customer_affiliate_assignment_valid_until" ON "customer_affiliate_assignment" ("valid_until") WHERE deleted_at IS NULL;`);
154
+ this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_customer_affiliate_assignment_deleted_at" ON "customer_affiliate_assignment" ("deleted_at") WHERE deleted_at IS NULL;`);
155
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_customer_affiliate_assignment_unique_active_customer" ON "customer_affiliate_assignment" ("customer_id") WHERE deleted_at IS NULL AND active = true;`);
156
+ this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_customer_affiliate_assignment_active_email" ON "customer_affiliate_assignment" ("normalized_email") WHERE deleted_at IS NULL AND active = true AND normalized_email IS NOT NULL;`);
157
+ const checkConstraints = [
158
+ `alter table "affiliate" add constraint "CHK_affiliate_status"
159
+ check ("status" in ('pending', 'active', 'rejected', 'inactive'));`,
160
+ `alter table "affiliate" add constraint "CHK_affiliate_commission_rate"
161
+ check ("commission_rate" between 0 and 100);`,
162
+ `alter table "affiliate" add constraint "CHK_affiliate_own_order_discount_rate"
163
+ check ("own_order_discount_rate" between 0 and 100);`,
164
+ `alter table "affiliate" add constraint "CHK_affiliate_customer_discount_rate_override"
165
+ check ("customer_discount_rate_override" is null or "customer_discount_rate_override" between 0 and 100);`,
166
+ `alter table "affiliate" add constraint "CHK_affiliate_referrer_commission_rate"
167
+ check ("referrer_commission_rate" is null or "referrer_commission_rate" between 0 and 100);`,
168
+ `alter table "affiliate" add constraint "CHK_affiliate_customer_assignment_mode"
169
+ check ("customer_assignment_mode" in ('lifetime', 'time_limited', 'order_only'));`,
170
+ `alter table "affiliate" add constraint "CHK_affiliate_assignment_duration"
171
+ check (
172
+ ("customer_assignment_mode" = 'time_limited' and "assignment_duration_days" is not null and "assignment_duration_days" > 0)
173
+ or
174
+ ("customer_assignment_mode" in ('lifetime', 'order_only') and "assignment_duration_days" is null)
175
+ );`,
176
+ `alter table "affiliate_coupon" add constraint "CHK_affiliate_coupon_code"
177
+ check ("code" = upper(trim("code")) and "code" ~ '^[A-Z0-9][A-Z0-9-]{2,31}$');`,
178
+ `alter table "affiliate_coupon" add constraint "CHK_affiliate_coupon_lifecycle_status"
179
+ check ("lifecycle_status" in ('enabled', 'revoked'));`,
180
+ `alter table "affiliate_coupon" add constraint "CHK_affiliate_coupon_provisioning_status"
181
+ check ("provisioning_status" in ('pending', 'ready', 'failed'));`,
182
+ `alter table "affiliate_coupon" add constraint "CHK_affiliate_coupon_provisioning_state"
183
+ check (
184
+ ("provisioning_status" = 'pending' and "promotion_setup_error" is null)
185
+ or
186
+ ("provisioning_status" = 'ready' and ("promotion_id" is not null or "lifecycle_status" = 'revoked') and "promotion_setup_error" is null)
187
+ or
188
+ ("provisioning_status" = 'failed' and "promotion_setup_error" is not null)
189
+ );`,
190
+ `alter table "affiliate_coupon" add constraint "CHK_affiliate_coupon_origin"
191
+ check ("origin" in ('default', 'manual', 'rotation'));`,
192
+ `alter table "affiliate_coupon" add constraint "CHK_affiliate_coupon_origin_fields"
193
+ check (
194
+ ("origin" = 'default' and "creation_idempotency_key" is null and "replaces_coupon_id" is null and "created_by_type" = 'system')
195
+ or
196
+ ("origin" = 'manual' and "creation_idempotency_key" is not null and "replaces_coupon_id" is null)
197
+ or
198
+ ("origin" = 'rotation' and "creation_idempotency_key" is not null and "replaces_coupon_id" is not null)
199
+ );`,
200
+ `alter table "affiliate_coupon" add constraint "CHK_affiliate_coupon_created_by_type"
201
+ check ("created_by_type" in ('system', 'admin', 'affiliate'));`,
202
+ `alter table "affiliate_coupon" add constraint "CHK_affiliate_coupon_revoked_by_type"
203
+ check ("revoked_by_type" is null or "revoked_by_type" in ('system', 'admin', 'affiliate'));`,
204
+ `alter table "affiliate_coupon" add constraint "CHK_affiliate_coupon_revocation"
205
+ check (
206
+ ("lifecycle_status" = 'enabled' and "revoked_at" is null and "revoked_by_type" is null and "revoked_by_id" is null)
207
+ or
208
+ ("lifecycle_status" = 'revoked' and "revoked_at" is not null and ("revoked_by_id" is null or "revoked_by_type" is not null))
209
+ );`,
210
+ `alter table "affiliate_application" add constraint "CHK_affiliate_application_status"
211
+ check ("status" in ('pending', 'approved', 'rejected'));`,
212
+ `alter table "affiliate_program_settings" add constraint "CHK_affiliate_program_settings_scope"
213
+ check ("scope" = 'global');`,
214
+ `alter table "affiliate_program_settings" add constraint "CHK_affiliate_program_settings_commission_base"
215
+ check ("commission_base" in ('before_discounts', 'after_discounts'));`,
216
+ `alter table "affiliate_program_settings" add constraint "CHK_affiliate_program_settings_referrer_base"
217
+ check ("referrer_base" in ('order_base', 'after_direct_commission'));`,
218
+ `alter table "affiliate_program_settings" add constraint "CHK_affiliate_program_settings_promotion_stacking"
219
+ check ("promotion_stacking" in ('allow_stacking', 'exclusive'));`,
220
+ `alter table "affiliate_program_settings" add constraint "CHK_affiliate_program_settings_discount_rate"
221
+ check ("default_customer_discount_rate" between 0 and 100);`,
222
+ `alter table "affiliate_program_settings" add constraint "CHK_affiliate_program_settings_referral_overwrite_policy"
223
+ check ("referral_overwrite_policy" in ('first_touch', 'last_touch'));`,
224
+ `alter table "affiliate_program_settings" add constraint "CHK_affiliate_program_settings_referral_window_days"
225
+ check ("referral_window_days" > 0);`,
226
+ `alter table "affiliate_program_settings" add constraint "CHK_affiliate_program_settings_policy_version"
227
+ check ("policy_version" > 0);`,
228
+ `alter table "customer_affiliate_assignment" add constraint "CHK_customer_affiliate_assignment_source"
229
+ check ("source" in ('referral', 'admin', 'system'));`,
230
+ `alter table "customer_affiliate_assignment" add constraint "CHK_customer_affiliate_assignment_mode"
231
+ check ("customer_assignment_mode" in ('lifetime', 'time_limited'));`,
232
+ `alter table "customer_affiliate_assignment" add constraint "CHK_customer_affiliate_assignment_duration"
233
+ check (
234
+ ("customer_assignment_mode" = 'lifetime' and "assignment_duration_days" is null and "valid_until" is null)
235
+ or
236
+ ("customer_assignment_mode" = 'time_limited' and "assignment_duration_days" is not null and "assignment_duration_days" > 0 and "valid_until" is not null)
237
+ );`,
238
+ `alter table "affiliate_referral_session" add constraint "CHK_affiliate_referral_session_status"
239
+ check ("status" in ('active', 'consumed', 'expired', 'invalidated'));`,
240
+ `alter table "affiliate_cart_referral" add constraint "CHK_affiliate_cart_referral_source"
241
+ check ("source" in ('link', 'coupon'));`,
242
+ `alter table "affiliate_cart_referral" add constraint "CHK_affiliate_cart_referral_status"
243
+ check ("status" in (
244
+ 'pending_email_validation', 'active', 'replaced', 'consumed',
245
+ 'expired', 'invalidated', 'ignored_existing_assignment'
246
+ ));`,
247
+ `alter table "affiliate_cart_referral" add constraint "CHK_affiliate_cart_referral_source_binding"
248
+ check (
249
+ ("source" = 'link' and "affiliate_slug_id" is not null and "source_affiliate_coupon_id" is null)
250
+ or
251
+ ("source" = 'coupon' and "referral_session_id" is null and "affiliate_slug_id" is null and "source_affiliate_coupon_id" is not null)
252
+ );`,
253
+ `alter table "affiliate_cart_referral" add constraint "CHK_affiliate_cart_referral_applied_binding"
254
+ check (
255
+ ("applied_affiliate_coupon_id" is null and "applied_promotion_id" is null)
256
+ or
257
+ ("applied_affiliate_coupon_id" is not null and "applied_promotion_id" is not null)
258
+ );`,
259
+ `alter table "affiliate_cart_referral" add constraint "CHK_affiliate_cart_referral_pending_coupon"
260
+ check (
261
+ "status" <> 'pending_email_validation'
262
+ or
263
+ ("source" = 'coupon' and "applied_affiliate_coupon_id" is null and "applied_promotion_id" is null)
264
+ );`,
265
+ `alter table "affiliate_cart_referral" add constraint "CHK_affiliate_cart_referral_active_coupon"
266
+ check (
267
+ "source" <> 'coupon' or "status" <> 'active'
268
+ or (
269
+ "applied_affiliate_coupon_id" is not null
270
+ and "source_affiliate_coupon_id" = "applied_affiliate_coupon_id"
271
+ )
272
+ );`,
273
+ `alter table "affiliate_order_attribution" add constraint "CHK_affiliate_order_attribution_source"
274
+ check ("source" in ('customer_assignment', 'referral_link', 'coupon'));`,
275
+ `alter table "affiliate_order_attribution" add constraint "CHK_affiliate_order_attribution_assignment_mode"
276
+ check ("customer_assignment_mode" in ('lifetime', 'time_limited', 'order_only'));`,
277
+ `alter table "affiliate_order_attribution" add constraint "CHK_affiliate_order_attribution_assignment_duration"
278
+ check (
279
+ ("customer_assignment_mode" = 'time_limited' and "assignment_duration_days" is not null and "assignment_duration_days" > 0)
280
+ or
281
+ ("customer_assignment_mode" in ('lifetime', 'order_only') and "assignment_duration_days" is null)
282
+ );`,
283
+ `alter table "affiliate_order_attribution" add constraint "CHK_affiliate_order_attribution_commission_rate"
284
+ check ("commission_rate" between 0 and 100);`,
285
+ `alter table "affiliate_order_attribution" add constraint "CHK_affiliate_order_attribution_referrer_rate"
286
+ check ("referrer_commission_rate" is null or "referrer_commission_rate" between 0 and 100);`,
287
+ `alter table "affiliate_order_attribution" add constraint "CHK_affiliate_order_attribution_policy_version"
288
+ check ("policy_version" > 0);`,
289
+ `alter table "affiliate_order_attribution" add constraint "CHK_affiliate_order_attribution_commission_base"
290
+ check ("commission_base_strategy" in ('before_discounts', 'after_discounts'));`,
291
+ `alter table "affiliate_order_attribution" add constraint "CHK_affiliate_order_attribution_referrer_base"
292
+ check ("referrer_base_strategy" in ('order_base', 'after_direct_commission'));`,
293
+ `alter table "commission_entry" add constraint "CHK_commission_entry_type"
294
+ check ("type" in (
295
+ 'order_commission', 'refund_correction', 'cancellation_correction',
296
+ 'manual_correction', 'affiliate_referral_bonus',
297
+ 'affiliate_own_order_referrer_bonus'
298
+ ));`,
299
+ `alter table "commission_entry" add constraint "CHK_commission_entry_commission_rate"
300
+ check ("commission_rate" between 0 and 100);`,
301
+ `alter table "commission_entry" add constraint "CHK_commission_entry_commission_base_strategy"
302
+ check ("commission_base_strategy" is null or "commission_base_strategy" in ('before_discounts', 'after_discounts'));`,
303
+ `alter table "commission_entry" add constraint "CHK_commission_entry_referrer_base_strategy"
304
+ check ("referrer_base_strategy" is null or "referrer_base_strategy" in ('order_base', 'after_direct_commission'));`,
305
+ `alter table "consignment_transfer" add constraint "CHK_consignment_transfer_direction"
306
+ check ("direction" in ('outbound', 'return'));`,
307
+ `alter table "consignment_transfer" add constraint "CHK_consignment_transfer_status"
308
+ check ("status" in ('pending', 'completed', 'failed'));`,
309
+ `alter table "consignment_transfer_line" add constraint "CHK_consignment_transfer_line_quantity"
310
+ check ("quantity" > 0);`,
311
+ `alter table "consignment_report" add constraint "CHK_consignment_report_status"
312
+ check ("status" in ('draft', 'submitted', 'approved', 'rejected'));`,
313
+ `alter table "consignment_report" add constraint "CHK_consignment_report_version"
314
+ check ("version" > 0);`,
315
+ `alter table "consignment_report_line" add constraint "CHK_consignment_report_line_non_sale_reason"
316
+ check ("non_sale_reason" is null or "non_sale_reason" in ('damaged', 'expired', 'returned', 'correction', 'requires_admin_clarification'));`,
317
+ ];
318
+ for (const sql of checkConstraints) {
319
+ this.addSql(sql);
320
+ }
321
+ const foreignKeys = [
322
+ `alter table "affiliate" add constraint "FK_affiliate_referred_by_affiliate_id"
323
+ foreign key ("referred_by_affiliate_id") references "affiliate" ("id") on delete restrict;`,
324
+ `alter table "affiliate_coupon" add constraint "FK_affiliate_coupon_affiliate_id"
325
+ foreign key ("affiliate_id") references "affiliate" ("id") on delete restrict;`,
326
+ `alter table "affiliate_coupon" add constraint "FK_affiliate_coupon_replaces_coupon_id"
327
+ foreign key ("replaces_coupon_id") references "affiliate_coupon" ("id") on delete restrict;`,
328
+ `alter table "affiliate_application" add constraint "FK_affiliate_application_affiliate_id"
329
+ foreign key ("affiliate_id") references "affiliate" ("id") on delete restrict;`,
330
+ `alter table "affiliate_slug" add constraint "FK_affiliate_slug_affiliate_id"
331
+ foreign key ("affiliate_id") references "affiliate" ("id") on delete restrict;`,
332
+ `alter table "affiliate" add constraint "FK_affiliate_primary_slug_id"
333
+ foreign key ("primary_slug_id") references "affiliate_slug" ("id") on delete restrict;`,
334
+ `alter table "customer_affiliate_assignment" add constraint "FK_customer_affiliate_assignment_affiliate_id"
335
+ foreign key ("affiliate_id") references "affiliate" ("id") on delete restrict;`,
336
+ `alter table "affiliate_referral_session" add constraint "FK_affiliate_referral_session_affiliate_id"
337
+ foreign key ("affiliate_id") references "affiliate" ("id") on delete restrict;`,
338
+ `alter table "affiliate_referral_session" add constraint "FK_affiliate_referral_session_affiliate_slug_id"
339
+ foreign key ("affiliate_slug_id") references "affiliate_slug" ("id") on delete restrict;`,
340
+ `alter table "affiliate_referral_session" add constraint "FK_affiliate_referral_session_replaced_by_session_id"
341
+ foreign key ("replaced_by_session_id") references "affiliate_referral_session" ("id") on delete set null;`,
342
+ `alter table "affiliate_cart_referral" add constraint "FK_affiliate_cart_referral_session_id"
343
+ foreign key ("referral_session_id") references "affiliate_referral_session" ("id") on delete restrict;`,
344
+ `alter table "affiliate_cart_referral" add constraint "FK_affiliate_cart_referral_affiliate_id"
345
+ foreign key ("affiliate_id") references "affiliate" ("id") on delete restrict;`,
346
+ `alter table "affiliate_cart_referral" add constraint "FK_affiliate_cart_referral_affiliate_slug_id"
347
+ foreign key ("affiliate_slug_id") references "affiliate_slug" ("id") on delete restrict;`,
348
+ `alter table "affiliate_cart_referral" add constraint "FK_affiliate_cart_referral_source_coupon_id"
349
+ foreign key ("source_affiliate_coupon_id") references "affiliate_coupon" ("id") on delete restrict;`,
350
+ `alter table "affiliate_cart_referral" add constraint "FK_affiliate_cart_referral_applied_coupon_id"
351
+ foreign key ("applied_affiliate_coupon_id") references "affiliate_coupon" ("id") on delete restrict;`,
352
+ `alter table "affiliate_cart_referral" add constraint "FK_affiliate_cart_referral_replaced_by_id"
353
+ foreign key ("replaced_by_referral_id") references "affiliate_cart_referral" ("id") on delete restrict;`,
354
+ `alter table "affiliate_code_redemption" add constraint "FK_affiliate_code_redemption_affiliate_id"
355
+ foreign key ("affiliate_id") references "affiliate" ("id") on delete restrict;`,
356
+ `alter table "affiliate_code_redemption" add constraint "FK_affiliate_code_redemption_coupon_id"
357
+ foreign key ("affiliate_coupon_id") references "affiliate_coupon" ("id") on delete restrict;`,
358
+ `alter table "affiliate_code_redemption" add constraint "FK_affiliate_code_redemption_assignment_id"
359
+ foreign key ("assignment_id") references "customer_affiliate_assignment" ("id") on delete restrict;`,
360
+ `alter table "affiliate_order_attribution" add constraint "FK_affiliate_order_attribution_affiliate_id"
361
+ foreign key ("affiliate_id") references "affiliate" ("id") on delete restrict;`,
362
+ `alter table "affiliate_order_attribution" add constraint "FK_affiliate_order_attribution_assignment_id"
363
+ foreign key ("customer_affiliate_assignment_id") references "customer_affiliate_assignment" ("id") on delete restrict;`,
364
+ `alter table "affiliate_order_attribution" add constraint "FK_affiliate_order_attribution_cart_referral_id"
365
+ foreign key ("affiliate_cart_referral_id") references "affiliate_cart_referral" ("id") on delete restrict;`,
366
+ `alter table "affiliate_order_attribution" add constraint "FK_affiliate_order_attribution_slug_id"
367
+ foreign key ("affiliate_slug_id") references "affiliate_slug" ("id") on delete restrict;`,
368
+ `alter table "affiliate_order_attribution" add constraint "FK_affiliate_order_attribution_redemption_id"
369
+ foreign key ("affiliate_code_redemption_id") references "affiliate_code_redemption" ("id") on delete restrict;`,
370
+ `alter table "affiliate_order_attribution" add constraint "FK_affiliate_order_attribution_referrer_id"
371
+ foreign key ("referrer_affiliate_id") references "affiliate" ("id") on delete restrict;`,
372
+ `alter table "commission_entry" add constraint "FK_commission_entry_affiliate_id"
373
+ foreign key ("affiliate_id") references "affiliate" ("id") on delete restrict;`,
374
+ `alter table "commission_entry" add constraint "FK_commission_entry_base_commission_entry_id"
375
+ foreign key ("base_commission_entry_id") references "commission_entry" ("id") on delete restrict;`,
376
+ `alter table "commission_entry" add constraint "FK_commission_entry_source_affiliate_id"
377
+ foreign key ("source_affiliate_id") references "affiliate" ("id") on delete restrict;`,
378
+ `alter table "affiliate_distribution_settings" add constraint "FK_affiliate_distribution_settings_affiliate_id"
379
+ foreign key ("affiliate_id") references "affiliate" ("id") on delete restrict;`,
380
+ `alter table "consignment_transfer" add constraint "FK_consignment_transfer_affiliate_id"
381
+ foreign key ("affiliate_id") references "affiliate" ("id") on delete restrict;`,
382
+ `alter table "consignment_transfer_line" add constraint "FK_consignment_transfer_line_transfer_id"
383
+ foreign key ("consignment_transfer_id") references "consignment_transfer" ("id") on delete restrict;`,
384
+ `alter table "consignment_report" add constraint "FK_consignment_report_affiliate_id"
385
+ foreign key ("affiliate_id") references "affiliate" ("id") on delete restrict;`,
386
+ `alter table "consignment_report_line" add constraint "FK_consignment_report_line_report_id"
387
+ foreign key ("consignment_report_id") references "consignment_report" ("id") on delete restrict;`,
388
+ ];
389
+ for (const sql of foreignKeys) {
390
+ this.addSql(sql);
391
+ }
392
+ this.addSql(`
393
+ insert into "affiliate_program_settings" (
394
+ "id", "scope", "commission_base", "referrer_base",
395
+ "promotion_stacking", "default_customer_discount_rate",
396
+ "referral_overwrite_policy", "referral_window_days", "policy_version"
397
+ ) values (
398
+ 'affprog_global', 'global', 'after_discounts', 'order_base',
399
+ 'allow_stacking', 5, 'first_touch', 30, 1
400
+ );
401
+ `);
402
+ }
403
+ async down() {
404
+ this.addSql(`drop table if exists "affiliate" cascade;`);
405
+ this.addSql(`drop table if exists "affiliate_application" cascade;`);
406
+ this.addSql(`drop table if exists "affiliate_cart_referral" cascade;`);
407
+ this.addSql(`drop table if exists "affiliate_code_redemption" cascade;`);
408
+ this.addSql(`drop table if exists "affiliate_coupon" cascade;`);
409
+ this.addSql(`drop table if exists "affiliate_distribution_settings" cascade;`);
410
+ this.addSql(`drop table if exists "affiliate_order_attribution" cascade;`);
411
+ this.addSql(`drop table if exists "affiliate_program_settings" cascade;`);
412
+ this.addSql(`drop table if exists "affiliate_referral_session" cascade;`);
413
+ this.addSql(`drop table if exists "affiliate_slug" cascade;`);
414
+ this.addSql(`drop table if exists "commission_entry" cascade;`);
415
+ this.addSql(`drop table if exists "consignment_report" cascade;`);
416
+ this.addSql(`drop table if exists "consignment_report_line" cascade;`);
417
+ this.addSql(`drop table if exists "consignment_transfer" cascade;`);
418
+ this.addSql(`drop table if exists "consignment_transfer_line" cascade;`);
419
+ this.addSql(`drop table if exists "customer_affiliate_assignment" cascade;`);
420
+ }
421
+ }
422
+ exports.Migration20260827085154 = Migration20260827085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RQUU5RCxJQUFJLENBQUMsTUFBTSxDQUFDLGtEQUFrRCxDQUFDLENBQUM7UUFFaEUsSUFBSSxDQUFDLE1BQU0sQ0FBQyxvREFBb0QsQ0FBQyxDQUFDO1FBRWxFLElBQUksQ0FBQyxNQUFNLENBQUMseURBQXlELENBQUMsQ0FBQztRQUV2RSxJQUFJLENBQUMsTUFBTSxDQUFDLHNEQUFzRCxDQUFDLENBQUM7UUFFcEUsSUFBSSxDQUFDLE1BQU0sQ0FBQywyREFBMkQsQ0FBQyxDQUFDO1FBRXpFLElBQUksQ0FBQyxNQUFNLENBQ1QsK0RBQStELENBQ2hFLENBQUM7SUFDSixDQUFDO0NBQ0Y7QUFwdkJELDBEQW92QkMifQ==
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("@medusajs/framework/utils");
4
+ const AffiliateApplication = utils_1.model
5
+ .define({
6
+ name: "affiliate_application",
7
+ tableName: "affiliate_application",
8
+ }, {
9
+ id: utils_1.model.id({ prefix: "affapp" }).primaryKey(),
10
+ customer_id: utils_1.model
11
+ .text()
12
+ .index("IDX_affiliate_application_customer_id"),
13
+ display_name: utils_1.model.text().searchable(),
14
+ requested_slug: utils_1.model.text().nullable(),
15
+ status: utils_1.model
16
+ .text()
17
+ .default("pending")
18
+ .index("IDX_affiliate_application_status"),
19
+ message: utils_1.model.text().nullable(),
20
+ rejection_reason: utils_1.model.text().nullable(),
21
+ affiliate_id: utils_1.model
22
+ .text()
23
+ .index("IDX_affiliate_application_affiliate_id")
24
+ .nullable(),
25
+ reviewed_by_id: utils_1.model.text().nullable(),
26
+ reviewed_at: utils_1.model.dateTime().nullable(),
27
+ metadata: utils_1.model.json().nullable(),
28
+ })
29
+ .indexes([
30
+ {
31
+ name: "IDX_affiliate_application_unique_pending_customer",
32
+ on: ["customer_id"],
33
+ unique: true,
34
+ where: "deleted_at IS NULL AND status = 'pending'",
35
+ },
36
+ ]);
37
+ exports.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