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 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user