feat(creators): added referrers, code redemption, campaign tracking, etc

This commit is contained in:
Waleed Latif
2026-02-11 11:54:50 -08:00
parent 2f492cacc1
commit eedf67013c
18 changed files with 11918 additions and 1 deletions

View File

@@ -8,6 +8,11 @@
*/
export const DEFAULT_FREE_CREDITS = 20
/**
* Default bonus credits (in dollars) for referral signups
*/
export const DEFAULT_REFERRAL_BONUS_CREDITS = 10
/**
* Storage limit constants (in GB)
* Can be overridden via environment variables

View File

@@ -0,0 +1,42 @@
CREATE TABLE "referral_attribution" (
"id" text PRIMARY KEY NOT NULL,
"user_id" text NOT NULL,
"organization_id" text,
"campaign_id" text,
"utm_source" text,
"utm_medium" text,
"utm_campaign" text,
"utm_content" text,
"referrer_url" text,
"landing_page" text,
"bonus_credit_amount" numeric DEFAULT '0' NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
CONSTRAINT "referral_attribution_user_id_unique" UNIQUE("user_id")
);
--> statement-breakpoint
CREATE TABLE "referral_campaigns" (
"id" text PRIMARY KEY NOT NULL,
"name" text NOT NULL,
"code" text,
"utm_source" text,
"utm_medium" text,
"utm_campaign" text,
"utm_content" text,
"bonus_credit_amount" numeric NOT NULL,
"is_active" boolean DEFAULT true NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL,
CONSTRAINT "referral_campaigns_code_unique" UNIQUE("code")
);
--> statement-breakpoint
ALTER TABLE "referral_attribution" ADD CONSTRAINT "referral_attribution_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "referral_attribution" ADD CONSTRAINT "referral_attribution_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "referral_attribution" ADD CONSTRAINT "referral_attribution_campaign_id_referral_campaigns_id_fk" FOREIGN KEY ("campaign_id") REFERENCES "public"."referral_campaigns"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "referral_attribution_user_id_idx" ON "referral_attribution" USING btree ("user_id");--> statement-breakpoint
CREATE UNIQUE INDEX "referral_attribution_org_unique_idx" ON "referral_attribution" USING btree ("organization_id") WHERE "referral_attribution"."organization_id" IS NOT NULL;--> statement-breakpoint
CREATE INDEX "referral_attribution_campaign_id_idx" ON "referral_attribution" USING btree ("campaign_id");--> statement-breakpoint
CREATE INDEX "referral_attribution_utm_campaign_idx" ON "referral_attribution" USING btree ("utm_campaign");--> statement-breakpoint
CREATE INDEX "referral_attribution_utm_content_idx" ON "referral_attribution" USING btree ("utm_content");--> statement-breakpoint
CREATE INDEX "referral_attribution_created_at_idx" ON "referral_attribution" USING btree ("created_at");--> statement-breakpoint
CREATE INDEX "referral_campaigns_active_idx" ON "referral_campaigns" USING btree ("is_active");--> statement-breakpoint
CREATE UNIQUE INDEX "referral_campaigns_code_idx" ON "referral_campaigns" USING btree ("code");

File diff suppressed because it is too large Load Diff

View File

@@ -1072,6 +1072,13 @@
"when": 1770410282842,
"tag": "0153_complete_arclight",
"breakpoints": true
},
{
"idx": 154,
"version": "7",
"when": 1770839615891,
"tag": "0154_many_vindicator",
"breakpoints": true
}
]
}

View File

@@ -726,6 +726,62 @@ export const userStats = pgTable('user_stats', {
billingBlockedReason: billingBlockedReasonEnum('billing_blocked_reason'),
})
export const referralCampaigns = pgTable(
'referral_campaigns',
{
id: text('id').primaryKey(),
name: text('name').notNull(),
code: text('code').unique(),
utmSource: text('utm_source'),
utmMedium: text('utm_medium'),
utmCampaign: text('utm_campaign'),
utmContent: text('utm_content'),
bonusCreditAmount: decimal('bonus_credit_amount').notNull(),
isActive: boolean('is_active').notNull().default(true),
createdAt: timestamp('created_at').notNull().defaultNow(),
updatedAt: timestamp('updated_at').notNull().defaultNow(),
},
(table) => ({
activeIdx: index('referral_campaigns_active_idx').on(table.isActive),
codeIdx: uniqueIndex('referral_campaigns_code_idx').on(table.code),
})
)
export const referralAttribution = pgTable(
'referral_attribution',
{
id: text('id').primaryKey(),
userId: text('user_id')
.notNull()
.references(() => user.id, { onDelete: 'cascade' })
.unique(),
organizationId: text('organization_id').references(() => organization.id, {
onDelete: 'set null',
}),
campaignId: text('campaign_id').references(() => referralCampaigns.id, {
onDelete: 'set null',
}),
utmSource: text('utm_source'),
utmMedium: text('utm_medium'),
utmCampaign: text('utm_campaign'),
utmContent: text('utm_content'),
referrerUrl: text('referrer_url'),
landingPage: text('landing_page'),
bonusCreditAmount: decimal('bonus_credit_amount').notNull().default('0'),
createdAt: timestamp('created_at').notNull().defaultNow(),
},
(table) => ({
userIdIdx: index('referral_attribution_user_id_idx').on(table.userId),
orgUniqueIdx: uniqueIndex('referral_attribution_org_unique_idx')
.on(table.organizationId)
.where(sql`${table.organizationId} IS NOT NULL`),
campaignIdIdx: index('referral_attribution_campaign_id_idx').on(table.campaignId),
utmCampaignIdx: index('referral_attribution_utm_campaign_idx').on(table.utmCampaign),
utmContentIdx: index('referral_attribution_utm_content_idx').on(table.utmContent),
createdAtIdx: index('referral_attribution_created_at_idx').on(table.createdAt),
})
)
export const customTools = pgTable(
'custom_tools',
{