diff --git a/src/app/[locale]/admin/users/page.tsx b/src/app/[locale]/admin/users/page.tsx index 3f4a419..303ac63 100644 --- a/src/app/[locale]/admin/users/page.tsx +++ b/src/app/[locale]/admin/users/page.tsx @@ -9,24 +9,99 @@ import Link from "next/link"; import type { Locale } from "@/locales/server"; // Create mock users based on toy owners for demonstration -const mockUsers = Array.from(new Set(mockToys.map(toy => toy.ownerId))).map(id => { - const userToy = mockToys.find(toy => toy.ownerId === id); + +// 1. Derive users from toy owners, ensuring 'user1' gets the primary admin email +const usersFromToys = Array.from(new Set(mockToys.map(toy => toy.ownerId))).map(ownerId => { + const userToy = mockToys.find(toy => toy.ownerId === ownerId); + const isUser1FromToys = ownerId === 'user1'; // 'user1' corresponds to Alice, the primary admin + return { - id, - name: userToy?.ownerName || `User ${id}`, - email: `${id}@example.com`, // Simple mock email - role: id === 'user1' || id === 'admin' ? 'Admin' : 'User', // user1 is our admin + id: ownerId, + name: userToy?.ownerName || `User ${ownerId}`, + // If ownerId is 'user1', assign the primary admin email. Otherwise, derive email. + email: isUser1FromToys ? 'user@example.com' : `${ownerId}@example.com`, + // 'user1' is an Admin, others are User by default from toys + role: isUser1FromToys ? 'Admin' : 'User', }; }); -// Add a specific admin user if not already present from mockToys -if (!mockUsers.find(u => u.email === 'admin@example.com')) { - mockUsers.push({id: 'admin-main', name: 'Main Admin', email: 'admin@example.com', role: 'Admin'}); + +// 2. Use a Map to ensure unique users by ID, and to easily update specific users +const userMap = new Map(); + +usersFromToys.forEach(user => { + userMap.set(user.id, user); +}); + +// 3. Ensure primary admin (user@example.com, corresponds to 'user1') is correctly represented +const primaryAdminId = 'user1'; +const primaryAdminEmail = 'user@example.com'; +if (userMap.has(primaryAdminId)) { + const user = userMap.get(primaryAdminId)!; + user.email = primaryAdminEmail; // Ensure correct email + user.name = user.name || 'Alice Wonderland (Admin)'; // Set/ensure name + user.role = 'Admin'; // Ensure role is Admin +} else { + // If 'user1' wasn't an ownerId in mockToys, add Alice/primary admin. + userMap.set(primaryAdminId, { + id: primaryAdminId, + name: 'Alice Wonderland (Admin)', + email: primaryAdminEmail, + role: 'Admin', + }); } -if (!mockUsers.find(u => u.email === 'user@example.com')) { - mockUsers.push({id: 'user1', name: 'Alice Wonderland (User1)', email: 'user@example.com', role: 'Admin'}); + +// 4. Ensure secondary admin (admin@example.com) is present with a unique ID +const secondaryAdminId = 'admin-main'; +const secondaryAdminEmail = 'admin@example.com'; + +// Check if a user with the secondary admin email already exists +let existingSecondaryAdmin = Array.from(userMap.values()).find(u => u.email === secondaryAdminEmail); + +if (existingSecondaryAdmin) { + // If user with this email exists, ensure their role is Admin. + // If their ID is not 'admin-main' and not 'user1' (primary admin), update their ID to 'admin-main' + // if 'admin-main' ID is not already taken by someone else. + existingSecondaryAdmin.role = 'Admin'; + if (existingSecondaryAdmin.id !== primaryAdminId && existingSecondaryAdmin.id !== secondaryAdminId) { + if (!userMap.has(secondaryAdminId) || userMap.get(secondaryAdminId)?.email === secondaryAdminEmail) { + // If 'admin-main' is free or already belongs to this email, consolidate. + if (existingSecondaryAdmin.id !== secondaryAdminId) { // avoid deleting and re-adding if id is already correct + userMap.delete(existingSecondaryAdmin.id); // remove old entry if ID was different + existingSecondaryAdmin.id = secondaryAdminId; // update ID + userMap.set(secondaryAdminId, existingSecondaryAdmin); // re-add with correct ID + } + } + // If 'admin-main' ID is taken by someone else with a different email, it implies a conflict. + // For this mock setup, we'll prioritize the email match. + } else if (existingSecondaryAdmin.id === primaryAdminId && primaryAdminEmail !== secondaryAdminEmail) { + // This means primary admin 'user1' somehow got the secondary admin's email, which is an inconsistency. + // For now, we assume this won't happen with the current logic flow. + } +} else { + // No user with this email exists. Add the secondary admin. + // Ensure the ID 'admin-main' is not already used by someone else with a different email. + if (userMap.has(secondaryAdminId) && userMap.get(secondaryAdminId)!.email !== secondaryAdminEmail) { + // ID 'admin-main' is taken by another user. This is a data conflict. + // For now, we'll log or handle this as an edge case. Let's give a new unique ID. + // console.warn("ID 'admin-main' is taken by a different user. Generating new ID for secondary admin."); + // For simplicity in mock data, we'll assume 'admin-main' is available or correctly assigned if email matches. + // If we strictly need 'admin-main' and it's taken, it's a setup issue. + // Let's assume 'admin-main' will be used if available. + } + userMap.set(secondaryAdminId, { + id: secondaryAdminId, + name: 'Main Admin', + email: secondaryAdminEmail, + role: 'Admin', + }); } +const mockUsers = Array.from(userMap.values()); +// Sort users for consistent display, e.g., by name or role +mockUsers.sort((a, b) => a.name.localeCompare(b.name)); + + export default async function AdminUserManagementPage({ params }: { params: { locale: Locale } }) { const t = await getI18n(); const locale = params.locale; @@ -55,7 +130,7 @@ export default async function AdminUserManagementPage({ params }: { params: { lo {mockUsers.map((user) => ( - + {/* Key is user.id, needs to be unique */} {user.name} {user.email} {user.role}