feat(api): add PATCH endpoint for user profile status and color updates
All checks were successful
Deploy Waveform to VPS / deploy (push) Successful in 1m24s
All checks were successful
Deploy Waveform to VPS / deploy (push) Successful in 1m24s
This commit is contained in:
parent
d2f3d30d20
commit
ca9b35f8dd
1 changed files with 77 additions and 0 deletions
77
app/api/users/profile/route.ts
Normal file
77
app/api/users/profile/route.ts
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
/**
|
||||
* @file app/api/users/profile/route.ts
|
||||
* @description API route handler for updating user profile settings (status, color).
|
||||
*/
|
||||
|
||||
import { auth } from "@/auth";
|
||||
import { db } from "@/db";
|
||||
import { users, UserStatus } from "@/db/schema";
|
||||
import { MEMBER_COLOR_OPTIONS } from "@/lib/constants/member.styles";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
const VALID_STATUSES: UserStatus[] = ["ONLINE", "OFFLINE", "AFK", "DND"];
|
||||
|
||||
/** Handles PATCH requests to update the logged-in user's profile settings. */
|
||||
export async function PATCH(req: Request) {
|
||||
try {
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { status, color } = await req.json();
|
||||
|
||||
const updateData: { status?: UserStatus; color?: string } = {};
|
||||
|
||||
// Validate status if provided
|
||||
if (status !== undefined) {
|
||||
if (!VALID_STATUSES.includes(status)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid status value" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
updateData.status = status;
|
||||
}
|
||||
|
||||
// Validate color if provided
|
||||
if (color !== undefined) {
|
||||
if (!MEMBER_COLOR_OPTIONS.includes(color)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid color option" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
updateData.color = color;
|
||||
}
|
||||
|
||||
// Ensure at least one field is provided for update
|
||||
if (Object.keys(updateData).length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "No valid fields to update" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
// Update user in database
|
||||
const [updatedUser] = await db
|
||||
.update(users)
|
||||
.set(updateData)
|
||||
.where(eq(users.id, session.user.id))
|
||||
.returning();
|
||||
|
||||
if (!updatedUser) {
|
||||
return NextResponse.json({ error: "User not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json(updatedUser);
|
||||
} catch (error) {
|
||||
console.error("API User Settings PATCH error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal Server Error" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue