fix errors

This commit is contained in:
Gabriel Brown 2024-10-18 16:53:03 -05:00
parent 69a6bb656e
commit 439b285126
6 changed files with 41 additions and 16 deletions

View File

@ -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",

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

View File

@ -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) {

View File

@ -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 }

View File

@ -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) => {

View File

@ -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);
}