refactor: SSR on page teams and split getAdminData
This commit is contained in:
@@ -191,36 +191,132 @@ export class AdminService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère toutes les données nécessaires pour l'admin
|
||||
* Récupère les données nécessaires pour la page de gestion des skills
|
||||
*/
|
||||
static async getAdminData(): Promise<{
|
||||
teams: Team[];
|
||||
static async getSkillsPageData(): Promise<{
|
||||
skillCategories: SkillCategory[];
|
||||
teamStats: TeamStats[];
|
||||
directionStats: DirectionStats[];
|
||||
skills: any[];
|
||||
}> {
|
||||
const pool = getPool();
|
||||
|
||||
try {
|
||||
// Récupérer toutes les données en parallèle
|
||||
const [teamsResult, categoriesResult, teamStats, skills] =
|
||||
await Promise.all([
|
||||
pool.query(
|
||||
"SELECT id, name, direction FROM teams ORDER BY direction, name"
|
||||
),
|
||||
pool.query(
|
||||
"SELECT id, name, icon FROM skill_categories ORDER BY name"
|
||||
),
|
||||
AdminService.getTeamsStats(),
|
||||
SkillsService.getAllSkillsWithUsage(),
|
||||
]);
|
||||
const [categoriesResult, skills] = await Promise.all([
|
||||
pool.query("SELECT id, name, icon FROM skill_categories ORDER BY name"),
|
||||
SkillsService.getAllSkillsWithUsage(),
|
||||
]);
|
||||
|
||||
const skillCategories = categoriesResult.rows.map((row) => ({
|
||||
...row,
|
||||
category: row.name,
|
||||
skills: [],
|
||||
}));
|
||||
|
||||
return {
|
||||
skillCategories,
|
||||
skills,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error fetching skills page data:", error);
|
||||
throw new Error("Failed to fetch skills page data");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les données nécessaires pour la page de gestion des utilisateurs
|
||||
*/
|
||||
static async getUsersPageData(): Promise<{
|
||||
teams: Team[];
|
||||
users: any[];
|
||||
}> {
|
||||
const pool = getPool();
|
||||
|
||||
try {
|
||||
const [teamsResult, usersResult] = await Promise.all([
|
||||
pool.query(
|
||||
"SELECT id, name, direction FROM teams ORDER BY direction, name"
|
||||
),
|
||||
pool.query(`
|
||||
SELECT
|
||||
u.uuid_id as uuid,
|
||||
u.first_name as "firstName",
|
||||
u.last_name as "lastName",
|
||||
t.name as "teamName",
|
||||
EXISTS (
|
||||
SELECT 1 FROM user_evaluations ue
|
||||
WHERE ue.user_uuid = u.uuid_id
|
||||
) as "hasEvaluations"
|
||||
FROM users u
|
||||
LEFT JOIN teams t ON u.team_id = t.id
|
||||
ORDER BY u.first_name, u.last_name
|
||||
`),
|
||||
]);
|
||||
|
||||
return {
|
||||
teams: teamsResult.rows,
|
||||
users: usersResult.rows,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error fetching users page data:", error);
|
||||
throw new Error("Failed to fetch users page data");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les données nécessaires pour la page de gestion des équipes
|
||||
*/
|
||||
static async getTeamsPageData(): Promise<{
|
||||
teams: Team[];
|
||||
teamStats: TeamStats[];
|
||||
directionStats: DirectionStats[];
|
||||
}> {
|
||||
const pool = getPool();
|
||||
|
||||
try {
|
||||
const [teamsResult, teamStats] = await Promise.all([
|
||||
pool.query(
|
||||
"SELECT id, name, direction FROM teams ORDER BY direction, name"
|
||||
),
|
||||
AdminService.getTeamsStats(),
|
||||
]);
|
||||
|
||||
const directionStats = AdminService.generateDirectionStats(teamStats);
|
||||
|
||||
return {
|
||||
teams: teamsResult.rows,
|
||||
teamStats,
|
||||
directionStats,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error fetching teams page data:", error);
|
||||
throw new Error("Failed to fetch teams page data");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les données nécessaires pour la page d'accueil admin
|
||||
*/
|
||||
static async getOverviewPageData(): Promise<{
|
||||
teams: Team[];
|
||||
skillCategories: SkillCategory[];
|
||||
teamStats: TeamStats[];
|
||||
directionStats: DirectionStats[];
|
||||
}> {
|
||||
const pool = getPool();
|
||||
|
||||
try {
|
||||
const [teamsResult, categoriesResult, teamStats] = await Promise.all([
|
||||
pool.query(
|
||||
"SELECT id, name, direction FROM teams ORDER BY direction, name"
|
||||
),
|
||||
pool.query("SELECT id, name, icon FROM skill_categories ORDER BY name"),
|
||||
AdminService.getTeamsStats(),
|
||||
]);
|
||||
|
||||
const teams = teamsResult.rows;
|
||||
const skillCategories = categoriesResult.rows.map((row) => ({
|
||||
...row,
|
||||
category: row.name, // Adapter le format
|
||||
skills: [], // Les skills individuelles ne sont pas nécessaires pour l'admin
|
||||
category: row.name,
|
||||
skills: [],
|
||||
}));
|
||||
|
||||
const directionStats = AdminService.generateDirectionStats(teamStats);
|
||||
@@ -230,11 +326,10 @@ export class AdminService {
|
||||
skillCategories,
|
||||
teamStats,
|
||||
directionStats,
|
||||
skills,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error fetching admin data:", error);
|
||||
throw new Error("Failed to fetch admin data");
|
||||
console.error("Error fetching overview page data:", error);
|
||||
throw new Error("Failed to fetch overview page data");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user