ChangeUserRole API added + bugfix
This commit is contained in:
parent
60fb333090
commit
6fa8135e32
1 changed files with 43 additions and 8 deletions
|
@ -1,4 +1,5 @@
|
||||||
import { NewProjMember } from "../Components/AddMember";
|
import { NewProjMember } from "../Components/AddMember";
|
||||||
|
import { ProjectRoleChange } from "../Components/ChangeRole";
|
||||||
import { ProjectMember } from "../Components/GetUsersInProject";
|
import { ProjectMember } from "../Components/GetUsersInProject";
|
||||||
import {
|
import {
|
||||||
NewWeeklyReport,
|
NewWeeklyReport,
|
||||||
|
@ -73,10 +74,7 @@ interface API {
|
||||||
* @param {string} token The authentication token.
|
* @param {string} token The authentication token.
|
||||||
* @returns {Promise<APIResponse<Project>>} A promise resolving to an API response with the created project.
|
* @returns {Promise<APIResponse<Project>>} A promise resolving to an API response with the created project.
|
||||||
*/
|
*/
|
||||||
createProject(
|
createProject(project: NewProject, token: string): Promise<APIResponse<void>>;
|
||||||
project: NewProject,
|
|
||||||
token: string,
|
|
||||||
): Promise<APIResponse<Project>>;
|
|
||||||
|
|
||||||
/** Submits a weekly report
|
/** Submits a weekly report
|
||||||
* @param {NewWeeklyReport} weeklyReport The weekly report object.
|
* @param {NewWeeklyReport} weeklyReport The weekly report object.
|
||||||
|
@ -148,6 +146,17 @@ interface API {
|
||||||
data: StrNameChange,
|
data: StrNameChange,
|
||||||
token: string,
|
token: string,
|
||||||
): Promise<APIResponse<void>>;
|
): Promise<APIResponse<void>>;
|
||||||
|
/**
|
||||||
|
* 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<APIResponse<void>>} A promise resolving to an API response.
|
||||||
|
*/
|
||||||
|
changeUserRole(
|
||||||
|
roleInfo: ProjectRoleChange,
|
||||||
|
token: string,
|
||||||
|
): Promise<APIResponse<void>>;
|
||||||
|
|
||||||
addUserToProject(
|
addUserToProject(
|
||||||
user: NewProjMember,
|
user: NewProjMember,
|
||||||
token: string,
|
token: string,
|
||||||
|
@ -253,7 +262,7 @@ export const api: API = {
|
||||||
async createProject(
|
async createProject(
|
||||||
project: NewProject,
|
project: NewProject,
|
||||||
token: string,
|
token: string,
|
||||||
): Promise<APIResponse<Project>> {
|
): Promise<APIResponse<void>> {
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/api/project", {
|
const response = await fetch("/api/project", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
@ -267,11 +276,10 @@ export const api: API = {
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
return { success: false, message: "Failed to create project" };
|
return { success: false, message: "Failed to create project" };
|
||||||
} else {
|
} else {
|
||||||
const data = (await response.json()) as Project;
|
return { success: true };
|
||||||
return { success: true, data };
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} 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<APIResponse<void>> {
|
||||||
|
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(
|
async getUserProjects(
|
||||||
username: string,
|
username: string,
|
||||||
token: string,
|
token: string,
|
||||||
|
|
Loading…
Add table
Reference in a new issue