2025-08-06 12:04:47 +02:00

47 lines
1.2 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { users, type User } from "../../mockData"; // adjust relative path
type UpdateUserBody = Partial<{
firstName: string;
lastName: string;
email: string;
phone: string;
role: string;
}>;
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params;
let body: unknown;
try {
body = await request.json();
} catch {
return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
}
const { firstName, lastName, email, phone, role } = body as UpdateUserBody;
const userIndex = users.findIndex((u: User) => u.id === id);
if (userIndex === -1) {
return NextResponse.json({ error: "User not found" }, { status: 404 });
}
const existingUser = users[userIndex];
const updatedUser: User = {
...existingUser,
firstName: firstName ?? existingUser.firstName,
lastName: lastName ?? existingUser.lastName,
email: email ?? existingUser.email,
phone: phone ?? existingUser.phone,
authorities: role ? [role] : existingUser.authorities ?? [],
};
users[userIndex] = updatedUser;
return NextResponse.json(updatedUser, { status: 200 });
}