generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolve build issues with server actions
- Move server action to separate file with 'use server' directive - Fix syntax error in page component - Update imports to use new actions file Co-Authored-By: ben <ben@prologe.io>
- Loading branch information
1 parent
d569ef4
commit 11604c7
Showing
3 changed files
with
63 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use server'; | ||
|
||
import { db, UserUsageTable, createEmptyUserUsage } 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'; | ||
|
||
export async function claimTokens() { | ||
try { | ||
// Check if after deadline | ||
const deadline = new Date('2025-01-01'); | ||
if (new Date() > deadline) { | ||
return { error: "This offer has expired" }; | ||
} | ||
|
||
// Get authenticated user | ||
const headersList = headers(); | ||
const request = new NextRequest('https://dummy.url', { | ||
headers: headersList, | ||
}); | ||
|
||
const { userId } = await handleAuthorizationV2(request); | ||
if (!userId) { | ||
return { error: "You must be logged in to claim tokens" }; | ||
} | ||
|
||
// Get or create user usage record | ||
let [usage] = await db.select().from(UserUsageTable).where(eq(UserUsageTable.userId, userId)); | ||
|
||
if (!usage) { | ||
await createEmptyUserUsage(userId); | ||
[usage] = await db.select().from(UserUsageTable).where(eq(UserUsageTable.userId, userId)); | ||
} | ||
|
||
// Check if already claimed | ||
if (usage.maxTokenUsage >= 5_000_000) { | ||
return { error: "You have already claimed this offer" }; | ||
} | ||
|
||
// Increase maxTokenUsage by 5M | ||
const result = await db | ||
.update(UserUsageTable) | ||
.set({ | ||
maxTokenUsage: sql`GREATEST(${UserUsageTable.maxTokenUsage}, 5000000)`, | ||
subscriptionStatus: "active", | ||
paymentStatus: "succeeded" | ||
}) | ||
.where(eq(UserUsageTable.userId, userId)) | ||
.returning({ newMaxTokens: UserUsageTable.maxTokenUsage }); | ||
|
||
revalidatePath('/claim-offer'); | ||
return { | ||
success: true, | ||
maxTokens: result[0].newMaxTokens | ||
}; | ||
} catch (error: any) { | ||
console.error("Error claiming tokens:", error); | ||
return { error: error.message || "Failed to claim tokens" }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters