feat: refactor skills API and database schema

- Replaced file-based skill category loading with API calls in the GET and POST methods of the skills route.
- Added new `SkillsService` for handling skill category operations.
- Updated SQL initialization script to create `skill_categories`, `skills`, and `skill_links` tables with appropriate relationships.
- Enhanced `ApiClient` with methods for loading skill categories and creating new skills, improving API interaction.
- Introduced error handling for skill category creation and loading processes.
This commit is contained in:
Julien Froidefond
2025-08-21 09:55:35 +02:00
parent 345ff5baa0
commit 72b653de19
10 changed files with 715 additions and 28 deletions

View File

@@ -0,0 +1,55 @@
import { NextResponse } from "next/server";
import { SkillsService } from "@/services";
interface RouteParams {
params: {
categoryId: string;
};
}
export async function GET(request: Request, { params }: RouteParams) {
try {
const skills = await SkillsService.getSkillsByCategory(params.categoryId);
return NextResponse.json(skills);
} catch (error) {
console.error("Error loading skills by category:", error);
return NextResponse.json(
{ error: "Failed to load skills by category" },
{ status: 500 }
);
}
}
export async function POST(request: Request, { params }: RouteParams) {
try {
const skillData: {
id: string;
name: string;
description: string;
icon?: string;
links: string[];
} = await request.json();
// Validate required fields
if (!skillData.id || !skillData.name || !skillData.description) {
return NextResponse.json(
{ error: "Missing required fields: id, name, description" },
{ status: 400 }
);
}
await SkillsService.createSkill({
...skillData,
categoryId: params.categoryId,
links: skillData.links || [],
});
return NextResponse.json({ success: true }, { status: 201 });
} catch (error) {
console.error("Error creating skill:", error);
return NextResponse.json(
{ error: "Failed to create skill" },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,52 @@
import { NextResponse } from "next/server";
import { SkillsService } from "@/services";
import { loadSkillCategoriesFromFiles } from "@/lib/skill-file-loader";
export async function POST() {
try {
console.log("🚀 Starting skills migration via API...");
// Load all skill categories from JSON files
const skillCategories = loadSkillCategoriesFromFiles();
console.log(`📊 Found ${skillCategories.length} categories`);
const totalSkills = skillCategories.reduce(
(sum, cat) => sum + cat.skills.length,
0
);
console.log(`🎯 Total skills to migrate: ${totalSkills}`);
// Bulk insert into database
await SkillsService.bulkInsertSkillsFromJSON(skillCategories);
console.log("✅ Skills migration completed successfully!");
// Verify the migration
const categoriesFromDb = await SkillsService.getSkillCategories();
const totalSkillsInDb = categoriesFromDb.reduce(
(sum, cat) => sum + cat.skills.length,
0
);
return NextResponse.json({
success: true,
message: "Skills migration completed successfully",
stats: {
categoriesMigrated: skillCategories.length,
skillsMigrated: totalSkills,
categoriesInDb: categoriesFromDb.length,
skillsInDb: totalSkillsInDb,
},
});
} catch (error) {
console.error("❌ Migration failed:", error);
return NextResponse.json(
{
error: "Failed to migrate skills",
details: error instanceof Error ? error.message : "Unknown error",
},
{ status: 500 }
);
}
}

View File

@@ -1,35 +1,10 @@
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import fs from "fs"; import { SkillsService } from "@/services";
import path from "path";
import { SkillCategory } from "@/lib/types"; import { SkillCategory } from "@/lib/types";
export async function GET() { export async function GET() {
try { try {
const dataDir = path.join(process.cwd(), "data", "skills"); const skillCategories = await SkillsService.getSkillCategories();
const categories = [
"frontend",
"backend",
"devops",
"mobile",
"data",
"cloud",
"security",
"design",
];
const skillCategories: SkillCategory[] = [];
for (const category of categories) {
const filePath = path.join(dataDir, `${category}.json`);
if (fs.existsSync(filePath)) {
const fileContent = fs.readFileSync(filePath, "utf-8");
const data = JSON.parse(fileContent);
skillCategories.push(data);
}
}
return NextResponse.json(skillCategories); return NextResponse.json(skillCategories);
} catch (error) { } catch (error) {
console.error("Error loading skills:", error); console.error("Error loading skills:", error);
@@ -39,3 +14,27 @@ export async function GET() {
); );
} }
} }
export async function POST(request: Request) {
try {
const categoryData: { id: string; name: string; icon: string } =
await request.json();
// Validate required fields
if (!categoryData.id || !categoryData.name || !categoryData.icon) {
return NextResponse.json(
{ error: "Missing required fields: id, name, icon" },
{ status: 400 }
);
}
await SkillsService.createSkillCategory(categoryData);
return NextResponse.json({ success: true }, { status: 201 });
} catch (error) {
console.error("Error creating skill category:", error);
return NextResponse.json(
{ error: "Failed to create skill category" },
{ status: 500 }
);
}
}

View File

@@ -13,6 +13,15 @@ export async function loadSkillCategories(): Promise<SkillCategory[]> {
} }
} }
/**
* Load skill categories from local files (fallback or development mode)
* This is a client-side safe alternative that still uses the API
* For server-side file loading, use loadSkillCategoriesFromFiles from skill-file-loader
*/
export async function loadSkillCategoriesFromAPI(): Promise<SkillCategory[]> {
return loadSkillCategories();
}
export async function loadTeams(): Promise<Team[]> { export async function loadTeams(): Promise<Team[]> {
try { try {
const response = await fetch("/api/teams"); const response = await fetch("/api/teams");

94
lib/skill-file-loader.ts Normal file
View File

@@ -0,0 +1,94 @@
import fs from "fs";
import path from "path";
import { SkillCategory } from "./types";
/**
* Load all skill categories from JSON files in the data/skills directory
*/
export function loadSkillCategoriesFromFiles(): SkillCategory[] {
const dataDir = path.join(process.cwd(), "data", "skills");
const skillCategories: SkillCategory[] = [];
try {
// Read all JSON files in the skills directory
const files = fs
.readdirSync(dataDir)
.filter((file) => file.endsWith(".json"));
console.log(`📁 Found ${files.length} JSON files: ${files.join(", ")}`);
// Load all JSON files
for (const file of files) {
const filePath = path.join(dataDir, file);
const categoryName = path.basename(file, ".json");
try {
console.log(`📖 Loading ${file}...`);
const fileContent = fs.readFileSync(filePath, "utf-8");
const data = JSON.parse(fileContent);
// Validate that it's a proper SkillCategory
if (data.category && data.icon && Array.isArray(data.skills)) {
skillCategories.push(data);
} else {
console.warn(`⚠️ Invalid skill category format in ${file}`);
}
} catch (error) {
console.error(`❌ Error loading ${file}:`, error);
// Continue with other files instead of failing completely
}
}
console.log(`📊 Successfully loaded ${skillCategories.length} categories`);
return skillCategories;
} catch (error) {
console.error("❌ Error reading skills directory:", error);
return [];
}
}
/**
* Get the list of available skill category files
*/
export function getSkillCategoryFiles(): string[] {
try {
const dataDir = path.join(process.cwd(), "data", "skills");
return fs
.readdirSync(dataDir)
.filter((file) => file.endsWith(".json"))
.map((file) => path.basename(file, ".json"));
} catch (error) {
console.error("❌ Error reading skills directory:", error);
return [];
}
}
/**
* Load a specific skill category by filename
*/
export function loadSkillCategoryFromFile(
categoryName: string
): SkillCategory | null {
try {
const dataDir = path.join(process.cwd(), "data", "skills");
const filePath = path.join(dataDir, `${categoryName}.json`);
if (!fs.existsSync(filePath)) {
console.warn(`⚠️ Skill category file not found: ${categoryName}.json`);
return null;
}
const fileContent = fs.readFileSync(filePath, "utf-8");
const data = JSON.parse(fileContent);
// Validate that it's a proper SkillCategory
if (data.category && data.icon && Array.isArray(data.skills)) {
return data;
} else {
console.warn(`⚠️ Invalid skill category format in ${categoryName}.json`);
return null;
}
} catch (error) {
console.error(`❌ Error loading skill category ${categoryName}:`, error);
return null;
}
}

View File

@@ -10,6 +10,34 @@ CREATE TABLE teams (
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
); );
-- Skill categories table
CREATE TABLE skill_categories (
id VARCHAR(50) PRIMARY KEY,
name VARCHAR(100) NOT NULL,
icon VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Skills table
CREATE TABLE skills (
id VARCHAR(100) PRIMARY KEY,
name VARCHAR(200) NOT NULL,
description TEXT,
icon VARCHAR(100),
category_id VARCHAR(50) REFERENCES skill_categories(id),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Skill links table
CREATE TABLE skill_links (
id SERIAL PRIMARY KEY,
skill_id VARCHAR(100) REFERENCES skills(id) ON DELETE CASCADE,
url TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Users table -- Users table
CREATE TABLE users ( CREATE TABLE users (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
@@ -73,6 +101,8 @@ INSERT INTO teams (id, name, direction) VALUES
-- Indexes for performance -- Indexes for performance
CREATE INDEX idx_teams_direction ON teams(direction); CREATE INDEX idx_teams_direction ON teams(direction);
CREATE INDEX idx_skills_category_id ON skills(category_id);
CREATE INDEX idx_skill_links_skill_id ON skill_links(skill_id);
CREATE INDEX idx_users_team_id ON users(team_id); CREATE INDEX idx_users_team_id ON users(team_id);
CREATE INDEX idx_user_evaluations_user_id ON user_evaluations(user_id); CREATE INDEX idx_user_evaluations_user_id ON user_evaluations(user_id);
CREATE INDEX idx_category_evaluations_user_evaluation_id ON category_evaluations(user_evaluation_id); CREATE INDEX idx_category_evaluations_user_evaluation_id ON category_evaluations(user_evaluation_id);
@@ -92,6 +122,12 @@ $$ language 'plpgsql';
CREATE TRIGGER update_teams_updated_at BEFORE UPDATE ON teams CREATE TRIGGER update_teams_updated_at BEFORE UPDATE ON teams
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column(); FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_skill_categories_updated_at BEFORE UPDATE ON skill_categories
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_skills_updated_at BEFORE UPDATE ON skills
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_users_updated_at BEFORE UPDATE ON users CREATE TRIGGER update_users_updated_at BEFORE UPDATE ON users
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column(); FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

54
scripts/migrate-skills.ts Normal file
View File

@@ -0,0 +1,54 @@
import { SkillsService } from "../services/skills-service";
import { loadSkillCategoriesFromFiles } from "../lib/skill-file-loader";
async function migrateSkillsToDatabase() {
console.log("🚀 Starting skills migration...");
try {
// Load all skill categories from JSON files
const skillCategories = loadSkillCategoriesFromFiles();
console.log(`📊 Found ${skillCategories.length} categories`);
const totalSkills = skillCategories.reduce(
(sum, cat) => sum + cat.skills.length,
0
);
console.log(`🎯 Total skills to migrate: ${totalSkills}`);
// Bulk insert into database
await SkillsService.bulkInsertSkillsFromJSON(skillCategories);
console.log("✅ Skills migration completed successfully!");
// Verify the migration
const categoriesFromDb = await SkillsService.getSkillCategories();
console.log(
`✨ Verification: ${categoriesFromDb.length} categories in database`
);
const totalSkillsInDb = categoriesFromDb.reduce(
(sum, cat) => sum + cat.skills.length,
0
);
console.log(`✨ Verification: ${totalSkillsInDb} skills in database`);
} catch (error) {
console.error("❌ Migration failed:", error);
process.exit(1);
}
}
// Run if called directly
if (require.main === module) {
migrateSkillsToDatabase()
.then(() => {
console.log("🎉 Migration script completed");
process.exit(0);
})
.catch((error) => {
console.error("💥 Migration script failed:", error);
process.exit(1);
});
}
export { migrateSkillsToDatabase };

View File

@@ -1,4 +1,11 @@
import { UserEvaluation, UserProfile, SkillLevel, Team } from "../lib/types"; import {
UserEvaluation,
UserProfile,
SkillLevel,
Team,
SkillCategory,
Skill,
} from "../lib/types";
export class ApiClient { export class ApiClient {
private baseUrl: string; private baseUrl: string;
@@ -321,6 +328,131 @@ export class ApiClient {
return false; return false;
} }
} }
/**
* Charge toutes les catégories de skills
*/
async loadSkillCategories(): Promise<SkillCategory[]> {
try {
const response = await fetch(`${this.baseUrl}/api/skills`);
if (!response.ok) {
throw new Error("Erreur lors du chargement des catégories de skills");
}
return await response.json();
} catch (error) {
console.error(
"Erreur lors du chargement des catégories de skills:",
error
);
return [];
}
}
/**
* Charge les skills d'une catégorie
*/
async loadSkillsByCategory(categoryId: string): Promise<Skill[]> {
try {
const response = await fetch(`${this.baseUrl}/api/skills/${categoryId}`);
if (!response.ok) {
throw new Error("Erreur lors du chargement des skills par catégorie");
}
return await response.json();
} catch (error) {
console.error(
"Erreur lors du chargement des skills par catégorie:",
error
);
return [];
}
}
/**
* Migre les skills depuis JSON vers PostgreSQL
*/
async migrateSkills(): Promise<{
success: boolean;
stats?: any;
error?: string;
}> {
try {
const response = await fetch(`${this.baseUrl}/api/skills/migrate`, {
method: "POST",
});
if (!response.ok) {
throw new Error("Erreur lors de la migration des skills");
}
return await response.json();
} catch (error) {
console.error("Erreur lors de la migration des skills:", error);
return {
success: false,
error: error instanceof Error ? error.message : "Unknown error",
};
}
}
/**
* Crée une nouvelle catégorie de skill
*/
async createSkillCategory(category: {
id: string;
name: string;
icon: string;
}): Promise<boolean> {
try {
const response = await fetch(`${this.baseUrl}/api/skills`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(category),
});
return response.ok;
} catch (error) {
console.error(
"Erreur lors de la création de la catégorie de skill:",
error
);
return false;
}
}
/**
* Crée une nouvelle skill
*/
async createSkill(
categoryId: string,
skill: {
id: string;
name: string;
description: string;
icon?: string;
links: string[];
}
): Promise<boolean> {
try {
const response = await fetch(`${this.baseUrl}/api/skills/${categoryId}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(skill),
});
return response.ok;
} catch (error) {
console.error("Erreur lors de la création de la skill:", error);
return false;
}
}
} }
// Instance singleton // Instance singleton

View File

@@ -11,5 +11,8 @@ export { EvaluationService, evaluationService } from "./evaluation-service";
// Teams services (server-only) // Teams services (server-only)
export { TeamsService } from "./teams-service"; export { TeamsService } from "./teams-service";
// Skills services (server-only)
export { SkillsService } from "./skills-service";
// API client (can be used client-side) // API client (can be used client-side)
export { ApiClient, apiClient } from "./api-client"; export { ApiClient, apiClient } from "./api-client";

253
services/skills-service.ts Normal file
View File

@@ -0,0 +1,253 @@
import { getPool } from "./database";
import { SkillCategory, Skill } from "@/lib/types";
export class SkillsService {
/**
* Get all skill categories with their skills
*/
static async getSkillCategories(): Promise<SkillCategory[]> {
const pool = getPool();
const query = `
SELECT
sc.id as category_id,
sc.name as category_name,
sc.icon as category_icon,
s.id as skill_id,
s.name as skill_name,
s.description as skill_description,
s.icon as skill_icon,
COALESCE(
json_agg(
sl.url ORDER BY sl.id
) FILTER (WHERE sl.url IS NOT NULL),
'[]'::json
) as skill_links
FROM skill_categories sc
LEFT JOIN skills s ON sc.id = s.category_id
LEFT JOIN skill_links sl ON s.id = sl.skill_id
GROUP BY sc.id, sc.name, sc.icon, s.id, s.name, s.description, s.icon
ORDER BY sc.name, s.name
`;
try {
const result = await pool.query(query);
// Group by category
const categoriesMap = new Map<string, SkillCategory>();
for (const row of result.rows) {
const categoryId = row.category_id;
if (!categoriesMap.has(categoryId)) {
categoriesMap.set(categoryId, {
category: row.category_name,
icon: row.category_icon,
skills: [],
});
}
if (row.skill_id) {
const category = categoriesMap.get(categoryId)!;
category.skills.push({
id: row.skill_id,
name: row.skill_name,
description: row.skill_description,
icon: row.skill_icon,
links: row.skill_links,
});
}
}
return Array.from(categoriesMap.values());
} catch (error) {
console.error("Error fetching skill categories:", error);
throw new Error("Failed to fetch skill categories");
}
}
/**
* Get skills by category
*/
static async getSkillsByCategory(categoryId: string): Promise<Skill[]> {
const pool = getPool();
const query = `
SELECT
s.id,
s.name,
s.description,
s.icon,
COALESCE(
json_agg(sl.url ORDER BY sl.id) FILTER (WHERE sl.url IS NOT NULL),
'[]'::json
) as links
FROM skills s
LEFT JOIN skill_links sl ON s.id = sl.skill_id
WHERE s.category_id = $1
GROUP BY s.id, s.name, s.description, s.icon
ORDER BY s.name
`;
try {
const result = await pool.query(query, [categoryId]);
return result.rows.map((row) => ({
id: row.id,
name: row.name,
description: row.description,
icon: row.icon,
links: row.links,
}));
} catch (error) {
console.error("Error fetching skills by category:", error);
throw new Error("Failed to fetch skills by category");
}
}
/**
* Create a new skill category
*/
static async createSkillCategory(category: {
id: string;
name: string;
icon: string;
}): Promise<void> {
const pool = getPool();
const query = `
INSERT INTO skill_categories (id, name, icon)
VALUES ($1, $2, $3)
`;
try {
await pool.query(query, [category.id, category.name, category.icon]);
} catch (error) {
console.error("Error creating skill category:", error);
throw new Error("Failed to create skill category");
}
}
/**
* Create a new skill
*/
static async createSkill(skill: {
id: string;
name: string;
description: string;
icon?: string;
categoryId: string;
links: string[];
}): Promise<void> {
const pool = getPool();
const client = await pool.connect();
try {
await client.query("BEGIN");
// Insert skill
const skillQuery = `
INSERT INTO skills (id, name, description, icon, category_id)
VALUES ($1, $2, $3, $4, $5)
`;
await client.query(skillQuery, [
skill.id,
skill.name,
skill.description,
skill.icon,
skill.categoryId,
]);
// Insert links
if (skill.links.length > 0) {
const linkQuery = `
INSERT INTO skill_links (skill_id, url)
VALUES ($1, $2)
`;
for (const link of skill.links) {
await client.query(linkQuery, [skill.id, link]);
}
}
await client.query("COMMIT");
} catch (error) {
await client.query("ROLLBACK");
console.error("Error creating skill:", error);
throw new Error("Failed to create skill");
} finally {
client.release();
}
}
/**
* Bulk insert skills from JSON data
*/
static async bulkInsertSkillsFromJSON(
skillCategoriesData: SkillCategory[]
): Promise<void> {
const pool = getPool();
const client = await pool.connect();
try {
await client.query("BEGIN");
for (const categoryData of skillCategoriesData) {
const categoryId = categoryData.category.toLowerCase();
// Insert category
const categoryQuery = `
INSERT INTO skill_categories (id, name, icon)
VALUES ($1, $2, $3)
ON CONFLICT (id) DO UPDATE SET
name = EXCLUDED.name,
icon = EXCLUDED.icon
`;
await client.query(categoryQuery, [
categoryId,
categoryData.category,
categoryData.icon,
]);
// Insert skills
for (const skill of categoryData.skills) {
const skillQuery = `
INSERT INTO skills (id, name, description, icon, category_id)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (id) DO UPDATE SET
name = EXCLUDED.name,
description = EXCLUDED.description,
icon = EXCLUDED.icon,
category_id = EXCLUDED.category_id
`;
await client.query(skillQuery, [
skill.id,
skill.name,
skill.description,
skill.icon,
categoryId,
]);
// Delete existing links
await client.query("DELETE FROM skill_links WHERE skill_id = $1", [
skill.id,
]);
// Insert new links
if (skill.links && skill.links.length > 0) {
const linkQuery = `
INSERT INTO skill_links (skill_id, url)
VALUES ($1, $2)
`;
for (const link of skill.links) {
await client.query(linkQuery, [skill.id, link]);
}
}
}
}
await client.query("COMMIT");
} catch (error) {
await client.query("ROLLBACK");
console.error("Error bulk inserting skills:", error);
throw new Error("Failed to bulk insert skills");
} finally {
client.release();
}
}
}