From 9b1755c592909fe55b5cd31760d65c8af6ded289 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Wed, 25 Dec 2024 08:22:30 +0000 Subject: [PATCH] feat: add christmas_claims table, fix clerk auth import, remove subscription updates, track claims once Co-Authored-By: ben --- packages/web/app/claim-offer/actions.ts | 38 ++++++++++++++++--------- packages/web/drizzle/schema.ts | 33 +++++++++++++++++++++ 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/packages/web/app/claim-offer/actions.ts b/packages/web/app/claim-offer/actions.ts index f6742ef8..82027314 100644 --- a/packages/web/app/claim-offer/actions.ts +++ b/packages/web/app/claim-offer/actions.ts @@ -1,11 +1,9 @@ 'use server'; -import { db, UserUsageTable, createEmptyUserUsage } from "@/drizzle/schema"; +import { db, UserUsageTable, createEmptyUserUsage, christmasClaims, hasClaimedChristmasTokens } from "@/drizzle/schema"; import { eq, sql } from "drizzle-orm"; import { revalidatePath } from 'next/cache'; -import { handleAuthorizationV2 } from "@/lib/handleAuthorization"; -import { headers } from 'next/headers'; -import { NextRequest } from 'next/server'; +import { auth } from "@clerk/nextjs/server"; export async function claimTokens() { try { @@ -16,12 +14,7 @@ export async function claimTokens() { } // Get authenticated user - const headersList = headers(); - const request = new NextRequest('https://dummy.url', { - headers: headersList, - }); - - const { userId } = await handleAuthorizationV2(request); + const { userId } = auth(); if (!userId) { return { error: "You must be logged in to claim tokens" }; } @@ -35,22 +28,39 @@ export async function claimTokens() { } // Check if already claimed - if (usage.maxTokenUsage >= 5_000_000) { + const hasClaimed = await hasClaimedChristmasTokens(userId); + if (hasClaimed) { return { error: "You have already claimed this offer" }; } + // Record the claim + await db.insert(christmasClaims).values({ userId }); + // Increase maxTokenUsage by 5M const result = await db .update(UserUsageTable) .set({ - maxTokenUsage: sql`GREATEST(${UserUsageTable.maxTokenUsage}, 5000000)`, - subscriptionStatus: "active", - paymentStatus: "succeeded" + maxTokenUsage: sql`COALESCE(${UserUsageTable.maxTokenUsage}, 0) + 5000000` }) .where(eq(UserUsageTable.userId, userId)) .returning({ newMaxTokens: UserUsageTable.maxTokenUsage }); revalidatePath('/claim-offer'); + + // Track successful claim in PostHog + const PostHogClient = (await import("@/lib/posthog")).default; + const posthog = PostHogClient(); + if (posthog) { + await posthog.capture({ + distinctId: userId, + event: 'christmas_tokens_claimed', + properties: { + tokens_amount: 5000000, + new_max_tokens: result[0].newMaxTokens + } + }); + } + return { success: true, maxTokens: result[0].newMaxTokens diff --git a/packages/web/drizzle/schema.ts b/packages/web/drizzle/schema.ts index 91d25ce8..2c845fb4 100644 --- a/packages/web/drizzle/schema.ts +++ b/packages/web/drizzle/schema.ts @@ -40,6 +40,39 @@ export const UserUsageTable = pgTable( } ); +// Table to track one-time Christmas token claims +export const christmasClaims = pgTable( + "christmas_claims", + { + id: serial("id").primaryKey(), + userId: text("user_id").notNull(), + claimedAt: timestamp("claimed_at").defaultNow().notNull(), + }, + (table) => { + return { + uniqueUserIdx: uniqueIndex("unique_christmas_claim_idx").on(table.userId), + }; + } +); + +export type ChristmasClaim = typeof christmasClaims.$inferSelect; + +// Helper function to check if a user has claimed their Christmas tokens +export const hasClaimedChristmasTokens = async (userId: string): Promise => { + try { + const claims = await db + .select() + .from(christmasClaims) + .where(eq(christmasClaims.userId, userId)) + .limit(1); + + return claims.length > 0; + } catch (error) { + console.error("Error checking Christmas token claims:", error); + return false; + } +}; + export const vercelTokens = pgTable('vercel_tokens', { id: serial('id').primaryKey(), userId: text('user_id').notNull(),