user report gtound work
This commit is contained in:
1 parent
1be2de05fa
commit
74f0d0dbed
42 files changed
+8127
-5
No files matched your search
@@ -0,0 +1,100 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { auth } from "@clerk/nextjs/server";
|
||||
import { getDatabase } from "@/lib/database";
|
||||
import { ensureUserSynced } from "@/lib/sync-user";
|
||||
import log from "@/lib/logger";
|
||||
|
||||
/**
|
||||
* DELETE /api/trainer-client/[id]
|
||||
* Delete (deactivate) a trainer-client assignment
|
||||
*/
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
const { userId: authUserId } = await auth();
|
||||
if (!authUserId) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const db = await getDatabase();
|
||||
const currentUser = await ensureUserSynced(authUserId, db);
|
||||
|
||||
if (!currentUser) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
// Only admins can delete assignments
|
||||
if (currentUser.role !== "admin" && currentUser.role !== "superAdmin") {
|
||||
return NextResponse.json(
|
||||
{ error: "Only admins can remove trainer-client assignments" },
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
const trainerAssignments = await db.getTrainerClientAssignments(
|
||||
currentUser.id,
|
||||
);
|
||||
const 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);
|
||||
|
||||
if (!foundAssignment) {
|
||||
return NextResponse.json(
|
||||
{ error: "Assignment not found" },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
// Deactivate the assignment
|
||||
await db.deactivateTrainerClientAssignment(id);
|
||||
|
||||
log.info("Trainer-client assignment deactivated", {
|
||||
assignmentId: id,
|
||||
deactivatedBy: currentUser.id,
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "Assignment deactivated successfully",
|
||||
});
|
||||
}
|
||||
|
||||
// Deactivate the assignment
|
||||
const result = await db.deactivateTrainerClientAssignment(id);
|
||||
|
||||
if (!result) {
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to deactivate assignment" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
||||
log.info("Trainer-client assignment deactivated", {
|
||||
assignmentId: id,
|
||||
deactivatedBy: currentUser.id,
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "Assignment deactivated successfully",
|
||||
});
|
||||
} catch (error) {
|
||||
log.error("Failed to delete trainer-client assignment", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { auth } from "@clerk/nextjs/server";
|
||||
import { getDatabase } from "@/lib/database";
|
||||
import { ensureUserSynced } from "@/lib/sync-user";
|
||||
import log from "@/lib/logger";
|
||||
|
||||
/**
|
||||
* GET /api/trainer-client
|
||||
* Get trainer-client assignments
|
||||
*
|
||||
* Query params:
|
||||
* - trainerId: string (optional) - Filter by trainer
|
||||
* - clientId: string (optional) - Filter by client
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { userId: authUserId } = await auth();
|
||||
if (!authUserId) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const db = await getDatabase();
|
||||
const currentUser = await ensureUserSynced(authUserId, db);
|
||||
|
||||
if (!currentUser) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
// Only admins and superadmins can view all assignments
|
||||
if (currentUser.role !== "admin" && currentUser.role !== "superAdmin") {
|
||||
return NextResponse.json(
|
||||
{ error: "Only admins can view trainer-client assignments" },
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const trainerId = searchParams.get("trainerId");
|
||||
const clientId = searchParams.get("clientId");
|
||||
|
||||
let assignments;
|
||||
|
||||
if (trainerId) {
|
||||
assignments = await db.getTrainerClientAssignments(trainerId);
|
||||
// Filter by clientId if provided
|
||||
if (clientId) {
|
||||
assignments = assignments.filter((a) => a.clientId === clientId);
|
||||
}
|
||||
} else {
|
||||
// Get all assignments (for admins, filtered by gym)
|
||||
assignments = await db.getTrainerClientAssignments("");
|
||||
// TODO: Filter by gym once gymId is properly set
|
||||
}
|
||||
|
||||
return NextResponse.json({ assignments });
|
||||
} catch (error) {
|
||||
log.error("Failed to get trainer-client assignments", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/trainer-client
|
||||
* Create a new trainer-client assignment
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const { userId: authUserId } = await auth();
|
||||
if (!authUserId) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const db = await getDatabase();
|
||||
const currentUser = await ensureUserSynced(authUserId, db);
|
||||
|
||||
if (!currentUser) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
// Only admins can create assignments
|
||||
if (currentUser.role !== "admin" && currentUser.role !== "superAdmin") {
|
||||
return NextResponse.json(
|
||||
{ error: "Only admins can assign trainers to clients" },
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { trainerId, clientId } = body;
|
||||
|
||||
if (!trainerId || !clientId) {
|
||||
return NextResponse.json(
|
||||
{ error: "trainerId and clientId are required" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
// Verify trainer exists and has trainer role
|
||||
const trainer = await db.getUserById(trainerId);
|
||||
if (!trainer) {
|
||||
return NextResponse.json({ error: "Trainer not found" }, { status: 404 });
|
||||
}
|
||||
if (trainer.role !== "trainer" && trainer.role !== "admin") {
|
||||
return NextResponse.json(
|
||||
{ error: "User must be a trainer or admin" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
// Verify client exists
|
||||
const client = await db.getUserById(clientId);
|
||||
if (!client) {
|
||||
return NextResponse.json({ error: "Client not found" }, { status: 404 });
|
||||
}
|
||||
if (client.role !== "client") {
|
||||
return NextResponse.json(
|
||||
{ error: "User must be a client" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
// Check if assignment already exists
|
||||
const existingAssignments = await db.getTrainerClientAssignments(trainerId);
|
||||
const existingAssignment = existingAssignments.find(
|
||||
(a) => a.clientId === clientId && a.isActive,
|
||||
);
|
||||
if (existingAssignment) {
|
||||
return NextResponse.json(
|
||||
{ error: "Trainer is already assigned to this client" },
|
||||
{ status: 409 },
|
||||
);
|
||||
}
|
||||
|
||||
// Create the assignment
|
||||
const assignment = await db.createTrainerClientAssignment({
|
||||
trainerId,
|
||||
clientId,
|
||||
assignedAt: new Date(),
|
||||
assignedBy: currentUser.id,
|
||||
isActive: true,
|
||||
});
|
||||
|
||||
log.info("Trainer-client assignment created", {
|
||||
trainerId,
|
||||
clientId,
|
||||
assignedBy: currentUser.id,
|
||||
});
|
||||
|
||||
return NextResponse.json({ assignment }, { status: 201 });
|
||||
} catch (error) {
|
||||
log.error("Failed to create trainer-client assignment", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user