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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user