diff --git a/app/api/dashboard/admin/mockData.ts b/app/api/dashboard/admin/mockData.ts index 2771499..194e386 100644 --- a/app/api/dashboard/admin/mockData.ts +++ b/app/api/dashboard/admin/mockData.ts @@ -1,4 +1,32 @@ -export const users = [ +// mockData.ts +export type User = { + merchantId: number; + id: string; + name?: string; + username?: string; + firstName?: string; + lastName?: string; + email?: string; + phone?: string; + jobTitle?: string; + enabled?: boolean; + authorities?: string[]; + allowedMerchantIds?: number[]; + created?: string; + disabledBy?: string | null; + disabledDate?: string | null; + disabledReason?: string | null; + incidentNotes?: boolean; + lastLogin?: string; + lastMandatoryUpdated?: string; + marketingNewsletter?: boolean; + releaseNotes?: boolean; + requiredActions?: string[]; + twoFactorCondition?: string; + twoFactorCredentials?: unknown[]; // narrow if you know the shape +}; + +export const users: User[] = [ { merchantId: 100987998, id: "bc6a8a55-13bc-4538-8255-cd0cec3bb4e9", diff --git a/app/api/dashboard/admin/users/[id]/route.ts b/app/api/dashboard/admin/users/[id]/route.ts index abf2c5d..5544bcd 100644 --- a/app/api/dashboard/admin/users/[id]/route.ts +++ b/app/api/dashboard/admin/users/[id]/route.ts @@ -1,34 +1,46 @@ import { NextRequest, NextResponse } from "next/server"; -import { users } from "../../mockData"; +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: { id: string } } + { params }: { params: Promise<{ id: string }> } ) { - const { id } = params; + const { id } = await params; - if (!id) { - return NextResponse.json({ error: "User ID is required" }, { status: 400 }); + let body: unknown; + try { + body = await request.json(); + } catch { + return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 }); } - const body = await request.json(); - const { firstName, lastName, email, phone, role } = body; + const { firstName, lastName, email, phone, role } = body as UpdateUserBody; - // Find the user by id - const userIndex = users.findIndex((u) => u.id === id); + const userIndex = users.findIndex((u: User) => u.id === id); if (userIndex === -1) { return NextResponse.json({ error: "User not found" }, { status: 404 }); } - // Update the user fields - users[userIndex] = { - ...users[userIndex], - firstName: firstName ?? users[userIndex].firstName, - lastName: lastName ?? users[userIndex].lastName, - email: email ?? users[userIndex].email, - phone: phone ?? users[userIndex].phone, - authorities: role ? [role] : users[userIndex].authorities, + 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 ?? [], }; - return NextResponse.json(users[userIndex], { status: 200 }); + users[userIndex] = updatedUser; + + return NextResponse.json(updatedUser, { status: 200 }); } diff --git a/app/features/UserRoles/EditUser/EditUser.tsx b/app/features/UserRoles/EditUser/EditUser.tsx index 1d8973d..5c86810 100644 --- a/app/features/UserRoles/EditUser/EditUser.tsx +++ b/app/features/UserRoles/EditUser/EditUser.tsx @@ -1,7 +1,7 @@ import React from "react"; import { useRouter } from "next/navigation"; import { IEditUserForm, EditUserField } from "../User.interfaces"; -import { addUser, editUser } from "@/services/roles.services"; +import { editUser } from "@/services/roles.services"; import { IUser } from "../../Pages/Admin/Users/interfaces"; import "./EditUser.scss";