From 6fa8135e32d2af99443130c97fc829e0c9eb87df Mon Sep 17 00:00:00 2001 From: Peter KW Date: Mon, 1 Apr 2024 02:02:22 +0200 Subject: [PATCH] ChangeUserRole API added + bugfix --- frontend/src/API/API.ts | 51 ++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index f0b024b..748c64b 100644 --- a/frontend/src/API/API.ts +++ b/frontend/src/API/API.ts @@ -1,4 +1,5 @@ import { NewProjMember } from "../Components/AddMember"; +import { ProjectRoleChange } from "../Components/ChangeRole"; import { ProjectMember } from "../Components/GetUsersInProject"; import { NewWeeklyReport, @@ -73,10 +74,7 @@ interface API { * @param {string} token The authentication token. * @returns {Promise>} A promise resolving to an API response with the created project. */ - createProject( - project: NewProject, - token: string, - ): Promise>; + createProject(project: NewProject, token: string): Promise>; /** Submits a weekly report * @param {NewWeeklyReport} weeklyReport The weekly report object. @@ -148,6 +146,17 @@ interface API { data: StrNameChange, token: string, ): Promise>; + /** + * Changes the role of a user in the database. + * @param {RoleChange} roleInfo The object containing the previous and new username. + * @param {string} token The authentication token. + * @returns {Promise>} A promise resolving to an API response. + */ + changeUserRole( + roleInfo: ProjectRoleChange, + token: string, + ): Promise>; + addUserToProject( user: NewProjMember, token: string, @@ -253,7 +262,7 @@ export const api: API = { async createProject( project: NewProject, token: string, - ): Promise> { + ): Promise> { try { const response = await fetch("/api/project", { method: "POST", @@ -267,11 +276,10 @@ export const api: API = { if (!response.ok) { return { success: false, message: "Failed to create project" }; } else { - const data = (await response.json()) as Project; - return { success: true, data }; + return { success: true }; } } catch (e) { - return { success: false, message: "Failed to create project" }; + return { success: false, message: "Failed to create project!" }; } }, @@ -320,6 +328,33 @@ export const api: API = { } }, + async changeUserRole( + roleInfo: ProjectRoleChange, + token: string, + ): Promise> { + try { + const response = await fetch("/api/ProjectRoleChange", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer " + token, + }, + body: JSON.stringify(roleInfo), + }); + + if (!response.ok) { + if (response.status === 403) { + return { success: false, message: "Cannot change your own role" }; + } + return { success: false, message: "Could not change role" }; + } else { + return { success: true }; + } + } catch (e) { + return { success: false, message: "Could not change role" }; + } + }, + async getUserProjects( username: string, token: string,