diff --git a/package.json b/package.json index 5945ad0..454371b 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,9 @@ "db:studio": "drizzle-kit studio", "dev": "next dev", "lint": "next lint", - "start": "next start" + "start": "next start", + "go": "pnpm dev", + "go:prod": "pnpm build && pnpm start" }, "dependencies": { "@t3-oss/env-nextjs": "^0.10.1", diff --git a/public/uploads/profile-pictures/2_1729280510919.jpg b/public/uploads/profile-pictures/2_1729280510919.jpg new file mode 100644 index 0000000..3ddf9b7 Binary files /dev/null and b/public/uploads/profile-pictures/2_1729280510919.jpg differ diff --git a/src/app/api/users/createUser/route.ts b/src/app/api/users/createUser/route.ts index 38cbff8..760b5ec 100644 --- a/src/app/api/users/createUser/route.ts +++ b/src/app/api/users/createUser/route.ts @@ -8,23 +8,38 @@ import type { User } from '~/server/types'; export const POST = async (request: NextRequest) => { const middlewareResponse = await middleware(request); if (middlewareResponse) return middlewareResponse; + try { - const url = new URL(request.url); - const appleId = url.searchParams.get('appleId'); - const email = url.searchParams.get('email'); - const fullName = url.searchParams.get('fullName'); - const pushToken = url.searchParams.get('pushToken'); - if (!appleId || !email || !fullName || !pushToken) + // Parse the request body + const body = await request.json() as { + appleId: string; + email: string; + fullName: string; + pushToken: string; + }; + const { appleId, email, fullName, pushToken } = body; + + // Validate the required fields + if (!appleId || !email || !fullName || !pushToken) { return NextResponse.json( - { message: 'Missing required parameters' }, { status: 400 } - ); - const newUser: User | null = await createUser(appleId, email, fullName, pushToken); - if (!newUser) { - return NextResponse.json( - { message: 'Error creating user' }, { status: 500 } + { message: 'Missing required parameters' }, + { status: 400 } ); } + + // Create the new user + const newUser: User | undefined = await createUser(appleId, email, fullName, pushToken); + + if (!newUser) { + return NextResponse.json( + { message: 'Error creating user' }, + { status: 500 } + ); + } + + // Return the new user data return NextResponse.json(newUser); + } catch (error) { console.error('Error creating user:', error); if (error instanceof Error) { diff --git a/src/app/api/users/search/route.ts b/src/app/api/users/search/route.ts index d001240..b201218 100644 --- a/src/app/api/users/search/route.ts +++ b/src/app/api/users/search/route.ts @@ -12,6 +12,7 @@ export const GET = async (request: NextRequest) => { const url = new URL(request.url); const userId = Number.parseInt(url.searchParams.get('userId') ?? ''); const searchTerm = url.searchParams.get('searchTerm'); + console.log(userId, searchTerm); if (!userId || !searchTerm || isNaN(userId)) return NextResponse.json( { message: 'Missing userId or searchTerm' }, { status: 400 } diff --git a/src/app/api/users/updatePfp/route.ts b/src/app/api/users/updatePfp/route.ts index 60a3219..3f47b31 100644 --- a/src/app/api/users/updatePfp/route.ts +++ b/src/app/api/users/updatePfp/route.ts @@ -37,6 +37,7 @@ export const POST = async (request: NextRequest) => { // Delete the old pfp file if it exists const oldPfpUrl = await getPfpUrl(userId); + console.log("Old pfp url: ", oldPfpUrl); if (oldPfpUrl) { const oldFilePath = path.join(process.cwd(), 'public', oldPfpUrl); await fs.unlink(oldFilePath).catch((error) => { diff --git a/src/server/functions.ts b/src/server/functions.ts index 1efae5b..008b858 100644 --- a/src/server/functions.ts +++ b/src/server/functions.ts @@ -91,10 +91,11 @@ export const createUser = async ( fullName: string, pushToken: string ) => { try { + console.log(appleId, email, fullName, pushToken); + if (!appleId || !email || !fullName || !pushToken) { throw new Error("Error: All required fields must be filled"); } - // Check if username or email is already taken const existingUser = await db.select().from(schema.users) .where(or(eq(schema.users.appleId, appleId), eq(schema.users.email, email))); @@ -102,15 +103,18 @@ export const createUser = async ( if (existingUser.length > 0) { throw new Error("Username or email is already in use"); } + console.log('right before we add the user'); const newUsers: User[] = await db.insert(schema.users).values({ appleId, email, fullName, pushToken }).returning() as User[]; // return the newly created user + const newUser: User | undefined = newUsers[0]; + if (!newUsers.length || !newUsers[0]?.id) throw new Error("Failed to create new user"); - return newUsers[0]; + return newUser; } catch (error) { if (error instanceof Error) { @@ -144,8 +148,10 @@ export const getPfpUrl = async (userId: number) => { try { const users = await db.select().from(schema.users) .where(eq(schema.users.id, userId)) + console.log(users); const user = users[0] as User; - return (users === undefined) ? user.pfpUrl : null; + if (!user) return null; + return user.pfpUrl; } catch (error) { console.error('Error getting pfp url:', error); }