Skip to content

Commit

Permalink
Merge pull request #288 from UoaWDCC/UABC-272-user-profile-endpoint
Browse files Browse the repository at this point in the history
Create User Profile Endpoint
  • Loading branch information
dyzhuu authored Oct 20, 2024
2 parents badca2f + 9ca1f8a commit f806a3f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
35 changes: 35 additions & 0 deletions src/app/api/users/[userId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { db } from "@/lib/db";
import { users } from "@/lib/db/schema";
import { userRouteWrapper } from "@/lib/wrappers";
import { getUserFromId } from "@/services/user";
import { updateUserSchema } from "@/lib/validators";

export const GET = userRouteWrapper(
async (_req, { params }: { params: { userId: string } }, currentUser) => {
Expand Down Expand Up @@ -48,3 +49,37 @@ export const DELETE = userRouteWrapper(
return responses.success();
}
);

export const PATCH = userRouteWrapper(
async (req, { params }: { params: { userId: string } }, currentUser) => {

const { userId } = params;

if (currentUser.role !== "admin" && currentUser.id !== userId) {
return responses.forbidden();
}

const body = await req.json();

const { firstName, lastName, playLevel } = updateUserSchema.parse(body);

const [user] = await db
.update(users)
.set({
firstName: firstName,
lastName: lastName,
playLevel: playLevel,
})
.where(eq(users.id, userId))
.returning();

if (!user) {
return responses.notFound({
resourceType: "user",
resourceId: userId,
});
}

return NextResponse.json(user);
}
);
8 changes: 5 additions & 3 deletions src/lib/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
gameSessionExceptions,
gameSessions,
gameSessionSchedules,
playLevelEnum,
semesters,
weekdayEnum,
} from "./db/schema";
Expand Down Expand Up @@ -46,9 +47,10 @@ export const updateSemesterSchema = z.object({
});

export const updateUserSchema = z.object({
firstName: z.string().min(1),
lastName: z.string().min(1),
member: z.boolean(),
firstName: z.string().min(1).optional(),
lastName: z.string().min(1).optional(),
member: z.boolean().optional(),
playLevel: z.enum(playLevelEnum.enumValues).optional(),
});

export const insertGameSessionScheduleSchema = createInsertSchema(
Expand Down

0 comments on commit f806a3f

Please sign in to comment.