Skip to content

Commit

Permalink
fix(api): rename table name & minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jsun969 committed Feb 16, 2024
1 parent 796d8b9 commit bfa2e0f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 38 deletions.
41 changes: 18 additions & 23 deletions src/app/api/get-user-info/route.ts
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 5 additions & 5 deletions src/app/api/payment/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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}`, {
Expand Down
20 changes: 10 additions & 10 deletions src/app/api/verify-membership-payment/route.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 });
Expand All @@ -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 });
Expand Down

0 comments on commit bfa2e0f

Please sign in to comment.