diff --git a/src/app/api/get-user-info/route.ts b/src/app/api/get-user-info/route.ts index 28fe335c..feab9895 100644 --- a/src/app/api/get-user-info/route.ts +++ b/src/app/api/get-user-info/route.ts @@ -1,35 +1,30 @@ import { db } from '@/db'; -import { members } from '@/db/schema'; +import { memberTable } from '@/db/schema'; import { currentUser } from '@clerk/nextjs'; import { eq } from 'drizzle-orm'; -export async function GET(request: Request) { +export async function GET() { const user = await currentUser(); if (!user) { return new Response(null, { status: 401 }); } - try { - const userData = await db - .select({ - firstName: members.firstName, - lastName: members.lastName, - ageBracket: members.ageBracket, - gender: members.gender, - studentType: members.studentType, - studentStatus: members.studentStatus, - studentId: members.studentId, - }) - .from(members) - .where(eq(members.clerkId, user.id)); + const userData = await db + .select({ + firstName: memberTable.firstName, + lastName: memberTable.lastName, + ageBracket: memberTable.ageBracket, + gender: memberTable.gender, + studentType: memberTable.studentType, + studentStatus: memberTable.studentStatus, + studentId: memberTable.studentId, + }) + .from(memberTable) + .where(eq(memberTable.clerkId, user.id)); - if (!userData) { - return new Response(JSON.stringify({}), { status: 404 }); - } - - return new Response(JSON.stringify(userData), { status: 200 }); - } catch (error) { - console.error('Error fetching user data:', error); - return new Response(null, { status: 500 }); + if (!userData) { + return new Response(null, { status: 404 }); } + + return Response.json(userData); } diff --git a/src/app/api/payment/route.ts b/src/app/api/payment/route.ts index 2f3f2c02..f94c8e4e 100644 --- a/src/app/api/payment/route.ts +++ b/src/app/api/payment/route.ts @@ -6,14 +6,14 @@ */ import { PRODUCTS } from '@/data/products'; import { db } from '@/db'; -import { members } from '@/db/schema'; +import { memberTable } from '@/db/schema'; import { env } from '@/env.mjs'; import { redisClient } from '@/lib/redis'; import { squareClient } from '@/lib/square'; import { currentUser } from '@clerk/nextjs'; import { eq } from 'drizzle-orm'; import type { NextRequest } from 'next/server'; -import type { CreatePaymentLinkRequest, OrderLineItem } from 'square'; +import type { CreatePaymentLinkRequest } from 'square'; import { ApiError } from 'square'; import { z } from 'zod'; @@ -73,10 +73,10 @@ export async function POST(request: Request) { // Add user ID and payment ID to Redis cache const [{ id: userId }] = await db .select({ - id: members.id, + id: memberTable.id, }) - .from(members) - .where(eq(members.clerkId, user.id)); + .from(memberTable) + .where(eq(memberTable.clerkId, user.id)); const paymentId = resp.result.paymentLink?.id ?? ''; const createdAt = resp.result.paymentLink?.createdAt ?? ''; await redisClient.hSet(`payment:membership:${userId}`, { diff --git a/src/app/api/verify-membership-payment/route.ts b/src/app/api/verify-membership-payment/route.ts index c79311c4..e05ccaef 100644 --- a/src/app/api/verify-membership-payment/route.ts +++ b/src/app/api/verify-membership-payment/route.ts @@ -1,5 +1,5 @@ import { db } from '@/db'; -import { members } from '@/db/schema'; +import { memberTable } from '@/db/schema'; import { redisClient } from '@/lib/redis'; import { squareClient } from '@/lib/square'; import { currentUser } from '@clerk/nextjs'; @@ -41,23 +41,23 @@ export async function PUT(request: Request) { // Get user's membership expiry date from the database const [{ membershipExpiresAt }] = await db .select({ - membershipExpiresAt: members.membershipExpiresAt, + membershipExpiresAt: memberTable.membershipExpiresAt, }) - .from(members) - .where(eq(members.clerkId, user.id)); + .from(memberTable) + .where(eq(memberTable.clerkId, user.id)); // If membership expiry date exists, return the existing date if (membershipExpiresAt) { - return new Response(JSON.stringify({ membershipExpiresAt }), { status: 200 }); + return Response.json({ membershipExpiresAt }); } // Get payment ID from Redis cache const [{ id: userId }] = await db .select({ - id: members.id, + id: memberTable.id, }) - .from(members) - .where(eq(members.clerkId, user.id)); + .from(memberTable) + .where(eq(memberTable.clerkId, user.id)); const paymentId = await redisClient.hGet(`payment:membership:${userId}`, 'paymentId'); if (!paymentId) { return new Response('Membership payment for the user does not exist', { status: 404 }); @@ -72,9 +72,9 @@ export async function PUT(request: Request) { const now = new Date(); const expiryDate = new Date(now.setFullYear(now.getFullYear() + 1)); await db - .update(members) + .update(memberTable) .set({ membershipExpiresAt: expiryDate }) - .where(eq(members.id, userId)); + .where(eq(memberTable.id, userId)); } catch (e) { if (e instanceof ApiError) { return new Response(JSON.stringify(e.errors), { status: e.statusCode });