fix: update user fields to match schema

- Replace firstName/lastName with name field in users API route
- Update user formatting in UsersTab component
- Add email fallback when name is not available
This commit is contained in:
2024-12-04 14:45:24 -05:00
parent 95b106d9e9
commit 29ce631901
36 changed files with 2700 additions and 167 deletions

View File

@@ -0,0 +1,73 @@
import { eq, and } from "drizzle-orm";
import { db } from "~/db";
import { userRolesTable, usersTable, rolesTable } from "~/db/schema";
import { PERMISSIONS, checkPermissions } from "~/lib/permissions-server";
import { ApiError, createApiResponse } from "~/lib/api-utils";
export async function GET(
request: Request,
context: { params: { id: string } }
) {
try {
const { id } = await context.params;
const studyId = parseInt(id);
if (isNaN(studyId)) {
return ApiError.BadRequest("Invalid study ID");
}
const permissionCheck = await checkPermissions({
studyId,
permission: PERMISSIONS.VIEW_STUDY
});
if (permissionCheck.error) {
return permissionCheck.error;
}
// Get all users in the study with their roles
const studyUsers = await db
.select({
id: usersTable.id,
email: usersTable.email,
name: usersTable.name,
roleId: rolesTable.id,
roleName: rolesTable.name,
})
.from(userRolesTable)
.innerJoin(usersTable, eq(usersTable.id, userRolesTable.userId))
.innerJoin(rolesTable, eq(rolesTable.id, userRolesTable.roleId))
.where(eq(userRolesTable.studyId, studyId));
// Group roles by user
const users = studyUsers.reduce((acc, curr) => {
const existingUser = acc.find(u => u.id === curr.id);
if (!existingUser) {
acc.push({
id: curr.id,
email: curr.email,
name: curr.name,
roles: [{
id: curr.roleId,
name: curr.roleName,
}]
});
} else if (curr.roleName && !existingUser.roles.some(r => r.id === curr.roleId)) {
existingUser.roles.push({
id: curr.roleId,
name: curr.roleName,
});
}
return acc;
}, [] as Array<{
id: string;
email: string;
name: string | null;
roles: Array<{ id: number; name: string }>;
}>);
return createApiResponse(users);
} catch (error) {
return ApiError.ServerError(error);
}
}