Files
peakskills/services/admin-management-service.ts
2025-08-22 16:34:13 +02:00

194 lines
4.7 KiB
TypeScript

export interface Skill {
id: string;
name: string;
description: string;
icon: string;
categoryId: string;
category: string;
usageCount: number;
}
export interface Team {
id: string;
name: string;
direction: string;
memberCount: number;
}
export interface TeamMember {
id: string;
firstName: string;
lastName: string;
fullName: string;
joinedAt: string;
}
export class AdminManagementService {
private static baseUrl = "/api/admin";
// Skills Management
static async getSkills(): Promise<Skill[]> {
const response = await fetch(`${this.baseUrl}/skills`);
if (!response.ok) {
throw new Error("Failed to fetch skills");
}
return response.json();
}
static async createSkill(
skillData: Omit<Skill, "id" | "usageCount">
): Promise<Skill> {
const response = await fetch(`${this.baseUrl}/skills`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(skillData),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || "Failed to create skill");
}
return response.json();
}
static async updateSkill(skillData: Skill): Promise<Skill> {
const response = await fetch(`${this.baseUrl}/skills`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(skillData),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || "Failed to update skill");
}
return response.json();
}
static async deleteSkill(skillId: string): Promise<void> {
const response = await fetch(`${this.baseUrl}/skills?id=${skillId}`, {
method: "DELETE",
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || "Failed to delete skill");
}
}
// Teams Management
static async getTeams(): Promise<Team[]> {
const response = await fetch(`${this.baseUrl}/teams`);
if (!response.ok) {
throw new Error("Failed to fetch teams");
}
return response.json();
}
static async createTeam(
teamData: Omit<Team, "id" | "memberCount">
): Promise<Team> {
const response = await fetch(`${this.baseUrl}/teams`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(teamData),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || "Failed to create team");
}
return response.json();
}
static async updateTeam(teamData: Team): Promise<Team> {
const response = await fetch(`${this.baseUrl}/teams`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(teamData),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || "Failed to update team");
}
return response.json();
}
static async deleteTeam(teamId: string): Promise<void> {
const response = await fetch(`${this.baseUrl}/teams?id=${teamId}`, {
method: "DELETE",
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || "Failed to delete team");
}
}
static async deleteDirection(direction: string): Promise<void> {
const response = await fetch(
`${this.baseUrl}/teams?direction=${encodeURIComponent(direction)}`,
{
method: "DELETE",
}
);
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || "Failed to delete direction");
}
}
// Team Members
static async getTeamMembers(teamId: string): Promise<TeamMember[]> {
const response = await fetch(`${this.baseUrl}/teams/${teamId}/members`);
if (!response.ok) {
throw new Error("Failed to fetch team members");
}
return response.json();
}
static async removeTeamMember(
teamId: string,
memberId: string
): Promise<void> {
const response = await fetch(`${this.baseUrl}/teams/${teamId}/members`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ memberId }),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || "Failed to remove team member");
}
}
// User Management
static async deleteUser(userId: string): Promise<void> {
const response = await fetch(`${this.baseUrl}/users/${userId}`, {
method: "DELETE",
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || "Failed to delete user");
}
}
}