diff --git a/src/app/api/users/getUserByAppleId/route.ts b/src/app/api/users/getUserByAppleId/route.ts
new file mode 100644
index 0000000..6015cac
--- /dev/null
+++ b/src/app/api/users/getUserByAppleId/route.ts
@@ -0,0 +1,21 @@
+"use server";
+import { NextResponse } from "next/server";
+import type { NextRequest } from "next/server";
+import { getUserByAppleId } from "~/server/functions";
+import { middleware } from "~/middleware";
+
+export const GET = async (request: NextRequest) => {
+ const middlewareResponse = await middleware(request);
+ if (middlewareResponse) return middlewareResponse;
+ try {
+ const appleId = request.nextUrl.searchParams.get("appleId");
+ if (!appleId)
+ return NextResponse.json({ message: "Missing Apple ID" }, { status: 400 });
+ const user = await getUserByAppleId(appleId);
+ if (user) return NextResponse.json(user);
+ else return NextResponse.json({ message: "User not found" }, { status: 404 });
+ } catch (error) {
+ console.error(error);
+ return NextResponse.json({ message: "Error" }, { status: 500 });
+ }
+};
diff --git a/src/app/api/users/updatePushTokenByAppleId/route.ts b/src/app/api/users/updatePushTokenByAppleId/route.ts
new file mode 100644
index 0000000..c09e8e2
--- /dev/null
+++ b/src/app/api/users/updatePushTokenByAppleId/route.ts
@@ -0,0 +1,38 @@
+"use server";
+import { NextResponse } from "next/server";
+import type { NextRequest } from "next/server";
+import { updateUserPushTokenByAppleId } from "~/server/functions";
+import { middleware } from "~/middleware";
+
+type UpdatePushTokenRequest = {
+ appleId: string;
+ pushToken: string;
+}
+
+export async function POST(request: NextRequest) {
+ const middlewareResponse = await middleware(request);
+ if (middlewareResponse) return middlewareResponse;
+ try {
+ const { appleId, pushToken } = await request.json() as UpdatePushTokenRequest;
+ console.log("Received request:", { appleId, pushToken });
+
+ if (!appleId || !pushToken)
+ return NextResponse.json({ message: "Missing required fields" }, { status: 400 });
+
+ console.log("Updating push token for user:", appleId);
+ const result = await updateUserPushTokenByAppleId(appleId, pushToken);
+
+ if (result.success) {
+ console.log("Push token updated successfully");
+ return NextResponse.json({ message: "Push token updated successfully" });
+ } else {
+ throw new Error("Failed to update push token");
+ }
+ } catch (error) {
+ console.error("Error in updatePushToken:", error);
+ if (error instanceof Error) {
+ return NextResponse.json({ message: error.message }, { status: 400 });
+ }
+ return NextResponse.json({ message: "Error updating push token" }, { status: 500 });
+ }
+}
diff --git a/src/app/users/createUser/page.tsx b/src/app/users/createUser/page.tsx
index 3b72a4a..0e330d2 100644
--- a/src/app/users/createUser/page.tsx
+++ b/src/app/users/createUser/page.tsx
@@ -25,8 +25,8 @@ export default function CreateUserPage() {
appleId,
appleEmail,
fullName,
- pfpURL,
- pushToken
+ pushToken,
+ pfpURL
})
});
const data = await response.json() as {message: string}
@@ -76,16 +76,6 @@ export default function CreateUserPage() {
required
/>
-
-
- setPfpURL(e.target.value)}
- className="border p-2 w-full bg-black"
- />
-
+
+
+ setPfpURL(e.target.value)}
+ className="border p-2 w-full bg-black"
+ />
+
diff --git a/src/server/functions.ts b/src/server/functions.ts
index 20c889d..553e823 100755
--- a/src/server/functions.ts
+++ b/src/server/functions.ts
@@ -2,26 +2,15 @@ import 'server-only';
import { db } from '~/server/db';
import * as schema from '~/server/db/schema';
import { eq, and, or, sql } from 'drizzle-orm';
-import jwt from 'jsonwebtoken';
-//import { pgEnum } from 'drizzle-orm/pg-core';
-//import { inArray } from 'drizzle-orm/sql';
// --- Helper Functions --- //
-/**
- * Ensure the user exists based on their userId.
- * Throws an error if the user doesn't exist.
- */
export const ensureUserExists = async (userId: number) => {
try {
const user = await db.select().from(schema.users)
.where(eq(schema.users.id, userId));
- if (user.length === 0) {
- throw new Error('User does not exist');
- }
-
- return user[0]; // Return user for further use if needed
+ return (user.length > 0) ? user[0] : null;
} catch (error) {
if (error instanceof Error) {
throw new Error(`Error checking user: ${error.message}`);
@@ -119,13 +108,26 @@ export const getUserById = async (userId: number) => {
}
};
+export const getUserByAppleId = async (appleId: string) => {
+ try {
+ const user = await db.select().from(schema.users)
+ .where(eq(schema.users.appleId, appleId));
+
+ return (user.length > 0) ? user[0] : null;
+ } catch (error) {
+ if (error instanceof Error) {
+ throw new Error(`Failed to fetch user by appleId: ${error.message}`);
+ } else {
+ throw new Error("Unknown error occurred while fetching user by appleId");
+ }
+ }
+};
+
export const getUserByAppleEmail = async (appleEmail: string) => {
try {
const user = await db.select().from(schema.users)
.where(eq(schema.users.appleEmail, appleEmail));
-
- if (user.length === 0) throw new Error('User not found');
- return user[0];
+ return (user.length > 0) ? user[0] : null;
} catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to fetch user by username: ${error.message}`);
@@ -137,7 +139,7 @@ export const getUserByAppleEmail = async (appleEmail: string) => {
export const createUser = async (
appleId: string, appleEmail: string,
- fullName: string, pfpURL = "", pushToken = ""
+ fullName: string, pushToken: string, pfpURL = ""
) => {
try {
if (!appleId || !appleEmail || !fullName || !pushToken) {
@@ -198,6 +200,22 @@ export const updateUserPushToken = async (userId: number, pushToken: string) =>
}
};
+export const updateUserPushTokenByAppleId = async (appleId: string, pushToken: string) => {
+ try {
+ await db.update(schema.users)
+ .set({ pushToken })
+ .where(eq(schema.users.appleId, appleId));
+
+ return { success: true };
+ } catch (error) {
+ if (error instanceof Error) {
+ throw new Error(`Failed to update profile: ${error.message}`);
+ } else {
+ throw new Error("Unknown error occurred while updating profile");
+ }
+ }
+};
+
// --- Relationship Management Functions --- //
export const createRelationshipRequest = async (requestorId: number, requestedId: number) => {