From 5a44ab91f3b2a4d1d081f5de1e2bfa472d40b737 Mon Sep 17 00:00:00 2001 From: evgongora Date: Thu, 6 Feb 2025 12:34:28 -0600 Subject: [PATCH] fix: more pr check fixes --- .github/workflows/pr-check.yml | 4 + frontend/src/app/api/confirm-payment/route.ts | 129 ++++++++++-------- 2 files changed, 75 insertions(+), 58 deletions(-) diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index a0597f6..50df215 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -69,6 +69,8 @@ jobs: working-directory: frontend env: NEXT_TELEMETRY_DISABLED: 1 + JWT_SECRET: ${{ secrets.JWT_SECRET || 'dummy-secret-for-ci' }} + NEXT_PUBLIC_WLD_APP_ID: ${{ secrets.NEXT_PUBLIC_WLD_APP_ID || 'app_staging_0' }} run: pnpm build security: @@ -134,6 +136,8 @@ jobs: env: ANALYZE: true NEXT_TELEMETRY_DISABLED: 1 + JWT_SECRET: ${{ secrets.JWT_SECRET || 'dummy-secret-for-ci' }} + NEXT_PUBLIC_WLD_APP_ID: ${{ secrets.NEXT_PUBLIC_WLD_APP_ID || 'app_staging_0' }} run: | pnpm build diff --git a/frontend/src/app/api/confirm-payment/route.ts b/frontend/src/app/api/confirm-payment/route.ts index 74f9064..4482dd4 100644 --- a/frontend/src/app/api/confirm-payment/route.ts +++ b/frontend/src/app/api/confirm-payment/route.ts @@ -22,13 +22,14 @@ interface TokenPayload extends JWTPayload { address?: string; } -const JWT_SECRET = process.env.JWT_SECRET; -if (!JWT_SECRET) { - throw new Error("JWT_SECRET environment variable is required"); +function getSecret() { + const JWT_SECRET = process.env.JWT_SECRET; + if (!JWT_SECRET) { + throw new Error("JWT_SECRET environment variable is required"); + } + return new TextEncoder().encode(JWT_SECRET); } -const secret = new TextEncoder().encode(JWT_SECRET); - export async function POST(req: NextRequest) { try { const { payload } = (await req.json()) as IRequestPayload; @@ -40,74 +41,86 @@ export async function POST(req: NextRequest) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } - const { payload: tokenPayload } = await jwtVerify(token, secret); - const typedPayload = tokenPayload as TokenPayload; + try { + const { payload: tokenPayload } = await jwtVerify(token, getSecret()); + const typedPayload = tokenPayload as TokenPayload; + + if (!typedPayload.address) { + console.error("No address in token payload"); + return NextResponse.json({ error: "Invalid session" }, { status: 401 }); + } + + const user = await xata.db.Users.filter({ + wallet_address: typedPayload.address, + }).getFirst(); + + if (!user) { + return NextResponse.json({ error: "User not found" }, { status: 404 }); + } + + // Get the latest payment_id + const latestPayment = await xata.db.Payments.sort( + "payment_id", + "desc", + ).getFirst(); + const nextPaymentId = (latestPayment?.payment_id || 0) + 1; + + // Create payment record + await xata.db.Payments.create({ + payment_id: nextPaymentId, + user: user.xata_id, + uuid: payload.transaction_id, + }); - if (!typedPayload.address) { - console.error("No address in token payload"); - return NextResponse.json({ error: "Invalid session" }, { status: 401 }); - } + // Check if user already has an active subscription + if ( + user.subscription && + user.subscription_expires && + new Date(user.subscription_expires) > new Date() + ) { + // Extend the existing subscription + const newExpiryDate = new Date(user.subscription_expires); + newExpiryDate.setDate(newExpiryDate.getDate() + 30); - const user = await xata.db.Users.filter({ - wallet_address: typedPayload.address, - }).getFirst(); + await xata.db.Users.update(user.xata_id, { + subscription_expires: newExpiryDate, + }); - if (!user) { - return NextResponse.json({ error: "User not found" }, { status: 404 }); - } + const response: PaymentResponse = { + success: true, + message: "Subscription extended", + next_payment_date: newExpiryDate.toISOString().split("T")[0], + }; + + return NextResponse.json(response); + } - // Get the latest payment_id - const latestPayment = await xata.db.Payments.sort( - "payment_id", - "desc", - ).getFirst(); - const nextPaymentId = (latestPayment?.payment_id || 0) + 1; - - // Create payment record - await xata.db.Payments.create({ - payment_id: nextPaymentId, - user: user.xata_id, - uuid: payload.transaction_id, - }); - - // Check if user already has an active subscription - if ( - user.subscription && - user.subscription_expires && - new Date(user.subscription_expires) > new Date() - ) { - // Extend the existing subscription - const newExpiryDate = new Date(user.subscription_expires); - newExpiryDate.setDate(newExpiryDate.getDate() + 30); + // Update user's subscription status for new subscription + const subscriptionExpiry = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000); await xata.db.Users.update(user.xata_id, { - subscription_expires: newExpiryDate, + subscription: true, + subscription_expires: subscriptionExpiry, }); const response: PaymentResponse = { success: true, - message: "Subscription extended", - next_payment_date: newExpiryDate.toISOString().split("T")[0], + message: "Subscription activated", + next_payment_date: subscriptionExpiry.toISOString().split("T")[0], }; return NextResponse.json(response); - } + } catch (error) { + console.error("Error confirming payment:", error); - // Update user's subscription status for new subscription - const subscriptionExpiry = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000); - - await xata.db.Users.update(user.xata_id, { - subscription: true, - subscription_expires: subscriptionExpiry, - }); - - const response: PaymentResponse = { - success: true, - message: "Subscription activated", - next_payment_date: subscriptionExpiry.toISOString().split("T")[0], - }; + const response: PaymentResponse = { + success: false, + error: "Failed to confirm payment", + details: error instanceof Error ? error.message : "Unknown error", + }; - return NextResponse.json(response); + return NextResponse.json(response, { status: 500 }); + } } catch (error) { console.error("Error confirming payment:", error);