feat: CRUD admin for skills and teams
This commit is contained in:
249
app/api/admin/skills/route.ts
Normal file
249
app/api/admin/skills/route.ts
Normal file
@@ -0,0 +1,249 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getPool } from "@/services/database";
|
||||
import { isUserAuthenticated } from "@/lib/server-auth";
|
||||
|
||||
// GET - Récupérer toutes les skills
|
||||
export async function GET() {
|
||||
try {
|
||||
// Vérifier l'authentification
|
||||
const isAuthenticated = await isUserAuthenticated();
|
||||
if (!isAuthenticated) {
|
||||
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
||||
}
|
||||
|
||||
const pool = getPool();
|
||||
const query = `
|
||||
SELECT
|
||||
s.id,
|
||||
s.name,
|
||||
s.description,
|
||||
s.icon,
|
||||
sc.id as category_id,
|
||||
sc.name as category_name,
|
||||
COUNT(DISTINCT se.id) as usage_count
|
||||
FROM skills s
|
||||
LEFT JOIN skill_categories sc ON s.category_id = sc.id
|
||||
LEFT JOIN skill_evaluations se ON s.id = se.skill_id AND se.is_selected = true
|
||||
GROUP BY s.id, s.name, s.description, s.icon, sc.id, sc.name
|
||||
ORDER BY s.name
|
||||
`;
|
||||
|
||||
const result = await pool.query(query);
|
||||
|
||||
const skills = result.rows.map((row) => ({
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
description: row.description || "",
|
||||
icon: row.icon || "",
|
||||
categoryId: row.category_id,
|
||||
category: row.category_name,
|
||||
usageCount: parseInt(row.usage_count) || 0,
|
||||
}));
|
||||
|
||||
return NextResponse.json(skills);
|
||||
} catch (error) {
|
||||
console.error("Error fetching skills:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la récupération des skills" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// POST - Créer une nouvelle skill
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
// Vérifier l'authentification
|
||||
const isAuthenticated = await isUserAuthenticated();
|
||||
if (!isAuthenticated) {
|
||||
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { name, categoryId, description, icon } = await request.json();
|
||||
|
||||
if (!name || !categoryId) {
|
||||
return NextResponse.json(
|
||||
{ error: "Le nom et la catégorie sont requis" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const pool = getPool();
|
||||
|
||||
// Vérifier si la skill existe déjà
|
||||
const existingSkill = await pool.query(
|
||||
"SELECT id FROM skills WHERE LOWER(name) = LOWER($1)",
|
||||
[name]
|
||||
);
|
||||
|
||||
if (existingSkill.rows.length > 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "Une skill avec ce nom existe déjà" },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
// Créer la nouvelle skill
|
||||
const result = await pool.query(
|
||||
`INSERT INTO skills (name, category_id, description, icon)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id, name, description, icon, category_id`,
|
||||
[name, categoryId, description || "", icon || ""]
|
||||
);
|
||||
|
||||
const newSkill = result.rows[0];
|
||||
|
||||
// Récupérer le nom de la catégorie
|
||||
const categoryResult = await pool.query(
|
||||
"SELECT name FROM skill_categories WHERE id = $1",
|
||||
[newSkill.category_id]
|
||||
);
|
||||
|
||||
const skill = {
|
||||
id: newSkill.id,
|
||||
name: newSkill.name,
|
||||
description: newSkill.description,
|
||||
icon: newSkill.icon,
|
||||
categoryId: newSkill.category_id,
|
||||
category: categoryResult.rows[0]?.name || "Inconnue",
|
||||
usageCount: 0,
|
||||
};
|
||||
|
||||
return NextResponse.json(skill, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error("Error creating skill:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la création de la skill" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// PUT - Mettre à jour une skill
|
||||
export async function PUT(request: NextRequest) {
|
||||
try {
|
||||
// Vérifier l'authentification
|
||||
const isAuthenticated = await isUserAuthenticated();
|
||||
if (!isAuthenticated) {
|
||||
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { id, name, categoryId, description, icon } = await request.json();
|
||||
|
||||
if (!id || !name || !categoryId) {
|
||||
return NextResponse.json(
|
||||
{ error: "L'ID, le nom et la catégorie sont requis" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const pool = getPool();
|
||||
|
||||
// Vérifier si la skill existe
|
||||
const existingSkill = await pool.query(
|
||||
"SELECT id FROM skills WHERE id = $1",
|
||||
[id]
|
||||
);
|
||||
|
||||
if (existingSkill.rows.length === 0) {
|
||||
return NextResponse.json({ error: "Skill non trouvée" }, { status: 404 });
|
||||
}
|
||||
|
||||
// Vérifier si le nom existe déjà (sauf pour cette skill)
|
||||
const duplicateName = await pool.query(
|
||||
"SELECT id FROM skills WHERE LOWER(name) = LOWER($1) AND id != $2",
|
||||
[name, id]
|
||||
);
|
||||
|
||||
if (duplicateName.rows.length > 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "Une skill avec ce nom existe déjà" },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
// Mettre à jour la skill
|
||||
await pool.query(
|
||||
`UPDATE skills
|
||||
SET name = $1, category_id = $2, description = $3, icon = $4
|
||||
WHERE id = $5`,
|
||||
[name, categoryId, description || "", icon || "", id]
|
||||
);
|
||||
|
||||
// Récupérer la skill mise à jour
|
||||
const result = await pool.query(
|
||||
`SELECT s.id, s.name, s.description, s.icon, s.category_id, sc.name as category_name
|
||||
FROM skills s
|
||||
LEFT JOIN skill_categories sc ON s.category_id = sc.id
|
||||
WHERE s.id = $1`,
|
||||
[id]
|
||||
);
|
||||
|
||||
const skill = result.rows[0];
|
||||
|
||||
return NextResponse.json({
|
||||
id: skill.id,
|
||||
name: skill.name,
|
||||
description: skill.description,
|
||||
icon: skill.icon,
|
||||
categoryId: skill.category_id,
|
||||
category: skill.category_name,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error updating skill:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la mise à jour de la skill" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE - Supprimer une skill
|
||||
export async function DELETE(request: NextRequest) {
|
||||
try {
|
||||
// Vérifier l'authentification
|
||||
const isAuthenticated = await isUserAuthenticated();
|
||||
if (!isAuthenticated) {
|
||||
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const id = searchParams.get("id");
|
||||
|
||||
if (!id) {
|
||||
return NextResponse.json(
|
||||
{ error: "L'ID de la skill est requis" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const pool = getPool();
|
||||
|
||||
// Vérifier si la skill est utilisée
|
||||
const usageCheck = await pool.query(
|
||||
`SELECT COUNT(*) as count
|
||||
FROM skill_evaluations se
|
||||
WHERE se.skill_id = $1 AND se.is_selected = true`,
|
||||
[id]
|
||||
);
|
||||
|
||||
const usageCount = parseInt(usageCheck.rows[0].count);
|
||||
if (usageCount > 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "Impossible de supprimer une skill qui est utilisée" },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
// Supprimer la skill
|
||||
await pool.query("DELETE FROM skills WHERE id = $1", [id]);
|
||||
|
||||
return NextResponse.json({ message: "Skill supprimée avec succès" });
|
||||
} catch (error) {
|
||||
console.error("Error deleting skill:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la suppression de la skill" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
269
app/api/admin/teams/route.ts
Normal file
269
app/api/admin/teams/route.ts
Normal file
@@ -0,0 +1,269 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getPool } from "@/services/database";
|
||||
import { isUserAuthenticated } from "@/lib/server-auth";
|
||||
|
||||
// GET - Récupérer toutes les teams
|
||||
export async function GET() {
|
||||
try {
|
||||
// Vérifier l'authentification
|
||||
const isAuthenticated = await isUserAuthenticated();
|
||||
if (!isAuthenticated) {
|
||||
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
||||
}
|
||||
|
||||
const pool = getPool();
|
||||
const query = `
|
||||
SELECT
|
||||
t.id,
|
||||
t.name,
|
||||
t.direction,
|
||||
COUNT(DISTINCT u.uuid_id) as member_count
|
||||
FROM teams t
|
||||
LEFT JOIN users u ON t.id = u.team_id
|
||||
GROUP BY t.id, t.name, t.direction
|
||||
ORDER BY t.direction, t.name
|
||||
`;
|
||||
|
||||
const result = await pool.query(query);
|
||||
|
||||
const teams = result.rows.map((row) => ({
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
direction: row.direction,
|
||||
memberCount: parseInt(row.member_count) || 0,
|
||||
}));
|
||||
|
||||
return NextResponse.json(teams);
|
||||
} catch (error) {
|
||||
console.error("Error fetching teams:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la récupération des teams" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// POST - Créer une nouvelle team
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
// Vérifier l'authentification
|
||||
const isAuthenticated = await isUserAuthenticated();
|
||||
if (!isAuthenticated) {
|
||||
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { name, direction } = await request.json();
|
||||
|
||||
if (!name || !direction) {
|
||||
return NextResponse.json(
|
||||
{ error: "Le nom et la direction sont requis" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const pool = getPool();
|
||||
|
||||
// Vérifier si la team existe déjà
|
||||
const existingTeam = await pool.query(
|
||||
"SELECT id FROM teams WHERE LOWER(name) = LOWER($1)",
|
||||
[name]
|
||||
);
|
||||
|
||||
if (existingTeam.rows.length > 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "Une équipe avec ce nom existe déjà" },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
// Créer la nouvelle team
|
||||
const result = await pool.query(
|
||||
`INSERT INTO teams (name, direction)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id, name, direction`,
|
||||
[name, direction]
|
||||
);
|
||||
|
||||
const newTeam = result.rows[0];
|
||||
|
||||
const team = {
|
||||
id: newTeam.id,
|
||||
name: newTeam.name,
|
||||
direction: newTeam.direction,
|
||||
memberCount: 0,
|
||||
};
|
||||
|
||||
return NextResponse.json(team, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error("Error creating team:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la création de l'équipe" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// PUT - Mettre à jour une team
|
||||
export async function PUT(request: NextRequest) {
|
||||
try {
|
||||
// Vérifier l'authentification
|
||||
const isAuthenticated = await isUserAuthenticated();
|
||||
if (!isAuthenticated) {
|
||||
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { id, name, direction } = await request.json();
|
||||
|
||||
if (!id || !name || !direction) {
|
||||
return NextResponse.json(
|
||||
{ error: "L'ID, le nom et la direction sont requis" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const pool = getPool();
|
||||
|
||||
// Vérifier si la team existe
|
||||
const existingTeam = await pool.query(
|
||||
"SELECT id FROM teams WHERE id = $1",
|
||||
[id]
|
||||
);
|
||||
|
||||
if (existingTeam.rows.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "Équipe non trouvée" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Vérifier si le nom existe déjà (sauf pour cette team)
|
||||
const duplicateName = await pool.query(
|
||||
"SELECT id FROM teams WHERE LOWER(name) = LOWER($1) AND id != $2",
|
||||
[name, id]
|
||||
);
|
||||
|
||||
if (duplicateName.rows.length > 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "Une équipe avec ce nom existe déjà" },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
// Mettre à jour la team
|
||||
await pool.query(
|
||||
`UPDATE teams
|
||||
SET name = $1, direction = $2
|
||||
WHERE id = $3`,
|
||||
[name, direction, id]
|
||||
);
|
||||
|
||||
// Récupérer la team mise à jour
|
||||
const result = await pool.query(
|
||||
`SELECT t.id, t.name, t.direction, COUNT(DISTINCT u.uuid_id) as member_count
|
||||
FROM teams t
|
||||
LEFT JOIN users u ON t.id = u.team_id
|
||||
WHERE t.id = $1
|
||||
GROUP BY t.id, t.name, t.direction`,
|
||||
[id]
|
||||
);
|
||||
|
||||
const team = result.rows[0];
|
||||
|
||||
return NextResponse.json({
|
||||
id: team.id,
|
||||
name: team.name,
|
||||
direction: team.direction,
|
||||
memberCount: parseInt(team.member_count) || 0,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error updating team:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la mise à jour de l'équipe" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE - Supprimer une team ou une direction
|
||||
export async function DELETE(request: NextRequest) {
|
||||
try {
|
||||
// Vérifier l'authentification
|
||||
const isAuthenticated = await isUserAuthenticated();
|
||||
if (!isAuthenticated) {
|
||||
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const id = searchParams.get("id");
|
||||
const direction = searchParams.get("direction");
|
||||
|
||||
if (!id && !direction) {
|
||||
return NextResponse.json(
|
||||
{ error: "L'ID de l'équipe ou la direction est requis" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const pool = getPool();
|
||||
|
||||
if (direction) {
|
||||
// Supprimer une direction entière
|
||||
// Vérifier d'abord si des équipes ont des membres
|
||||
const memberCheck = await pool.query(
|
||||
`SELECT COUNT(*) as count
|
||||
FROM users u
|
||||
JOIN teams t ON u.team_id = t.id
|
||||
WHERE t.direction = $1`,
|
||||
[direction]
|
||||
);
|
||||
|
||||
const memberCount = parseInt(memberCheck.rows[0].count);
|
||||
if (memberCount > 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: `Impossible de supprimer la direction "${direction}" car certaines équipes ont des membres`,
|
||||
},
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
// Supprimer toutes les équipes de la direction
|
||||
await pool.query("DELETE FROM teams WHERE direction = $1", [direction]);
|
||||
|
||||
return NextResponse.json({
|
||||
message: `Direction "${direction}" et toutes ses équipes supprimées avec succès`,
|
||||
});
|
||||
} else {
|
||||
// Supprimer une équipe spécifique
|
||||
// Vérifier si la team a des membres
|
||||
const memberCheck = await pool.query(
|
||||
`SELECT COUNT(*) as count
|
||||
FROM users
|
||||
WHERE team_id = $1`,
|
||||
[id]
|
||||
);
|
||||
|
||||
const memberCount = parseInt(memberCheck.rows[0].count);
|
||||
if (memberCount > 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error:
|
||||
"Impossible de supprimer une équipe qui contient des membres",
|
||||
},
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
// Supprimer la team
|
||||
await pool.query("DELETE FROM teams WHERE id = $1", [id]);
|
||||
|
||||
return NextResponse.json({ message: "Équipe supprimée avec succès" });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error deleting team/direction:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la suppression" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user