Skip to content

Commit

Permalink
feat: add christmas_claims table, fix clerk auth import, remove subsc…
Browse files Browse the repository at this point in the history
…ription updates, track claims once

Co-Authored-By: ben <ben@prologe.io>
  • Loading branch information
devin-ai-integration[bot] and benjaminshafii committed Dec 25, 2024
1 parent 11604c7 commit 9b1755c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
38 changes: 24 additions & 14 deletions packages/web/app/claim-offer/actions.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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" };
}
Expand All @@ -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
Expand Down
33 changes: 33 additions & 0 deletions packages/web/drizzle/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> => {
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(),
Expand Down

0 comments on commit 9b1755c

Please sign in to comment.