assigment and reports groundwork
This commit is contained in:
1 parent
74f0d0dbed
commit
06973ccfb2
9 files changed
+299
-49
No files matched your search
@@ -35,22 +35,18 @@ export async function DELETE(
|
||||
|
||||
const { id } = await params;
|
||||
|
||||
// Get all assignments for the trainer to find the one with this ID
|
||||
// Note: We need to find the assignment ID, not the trainer or client ID
|
||||
// For now, we'll deactivate by finding the assignment through the trainer
|
||||
// This is a simplified approach - in production you'd want a direct getById method
|
||||
|
||||
// Try to find the assignment by checking trainer's assignments first
|
||||
const trainerAssignments = await db.getTrainerClientAssignments(
|
||||
currentUser.id,
|
||||
);
|
||||
const assignment = trainerAssignments.find((a) => a.id === id);
|
||||
let assignment = trainerAssignments.find((a) => a.id === id);
|
||||
|
||||
if (!assignment) {
|
||||
// Check if any trainer has this assignment
|
||||
const allAssignments = await db.getTrainerClientAssignments("");
|
||||
const foundAssignment = allAssignments.find((a) => a.id === id);
|
||||
// Check all assignments to find the one with this ID
|
||||
const allAssignments = await db.getAllTrainerClientAssignments();
|
||||
assignment = allAssignments.find((a) => a.id === id);
|
||||
|
||||
if (!foundAssignment) {
|
||||
if (!assignment) {
|
||||
return NextResponse.json(
|
||||
{ error: "Assignment not found" },
|
||||
{ status: 404 },
|
||||
|
||||
@@ -48,8 +48,7 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
} else {
|
||||
// Get all assignments (for admins, filtered by gym)
|
||||
assignments = await db.getTrainerClientAssignments("");
|
||||
// TODO: Filter by gym once gymId is properly set
|
||||
assignments = await db.getAllTrainerClientAssignments();
|
||||
}
|
||||
|
||||
return NextResponse.json({ assignments });
|
||||
|
||||
@@ -45,12 +45,24 @@ export async function GET(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const role = searchParams.get("role");
|
||||
|
||||
log.debug("User API called", {
|
||||
currentUserId: currentUser.id,
|
||||
currentUserRole: currentUser.role,
|
||||
currentUserGymId: currentUser.gymId,
|
||||
});
|
||||
|
||||
// Get target gym based on role
|
||||
const targetGymId =
|
||||
currentUser.role === "superAdmin"
|
||||
? (searchParams.get("gymId") ?? undefined)
|
||||
: (currentUser.gymId ?? undefined);
|
||||
|
||||
log.debug("Target gym calculation", {
|
||||
targetGymId,
|
||||
currentUserRole: currentUser.role,
|
||||
currentUserGymId: currentUser.gymId,
|
||||
});
|
||||
|
||||
// Validate gym access for non-superAdmins
|
||||
if (currentUser.role !== "superAdmin" && !targetGymId) {
|
||||
return forbiddenResponse("No gym assigned");
|
||||
@@ -61,6 +73,12 @@ export async function GET(request: NextRequest) {
|
||||
? await getUsersByGym(targetGymId)
|
||||
: await db.getAllUsers();
|
||||
|
||||
log.debug("Users fetched from database", {
|
||||
targetGymId,
|
||||
totalUsers: Array.isArray(users) ? users.length : 0,
|
||||
sampleGymId: users && users[0] ? (users[0] as any).gymId : null,
|
||||
});
|
||||
|
||||
// Hydrate gymId from raw DB to ensure consistency with writes
|
||||
const rawUserRows = await rawDb.all(sql`SELECT id, gym_id FROM users`);
|
||||
const gymById = new Map<string, string | null>(
|
||||
@@ -90,12 +108,12 @@ export async function GET(request: NextRequest) {
|
||||
log.debug("Applied role filter", {
|
||||
role,
|
||||
usersAfterFilter: Array.isArray(users) ? users.length : 0,
|
||||
sample:
|
||||
sampleUser:
|
||||
users && users[0]
|
||||
? {
|
||||
id: users[0].id,
|
||||
role: users[0].role,
|
||||
gymId: (users as any)[0].gymId,
|
||||
gymId: (users[0] as any).gymId,
|
||||
}
|
||||
: null,
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user