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 { 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<APIResponse<Project>>} A promise resolving to an API response with the created project. | ||||
|    */ | ||||
|   createProject( | ||||
|     project: NewProject, | ||||
|     token: string, | ||||
|   ): Promise<APIResponse<Project>>; | ||||
|   createProject(project: NewProject, token: string): Promise<APIResponse<void>>; | ||||
| 
 | ||||
|   /** Submits a weekly report | ||||
|    * @param {NewWeeklyReport} weeklyReport The weekly report object. | ||||
|  | @ -148,6 +146,17 @@ interface API { | |||
|     data: StrNameChange, | ||||
|     token: string, | ||||
|   ): 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( | ||||
|     user: NewProjMember, | ||||
|     token: string, | ||||
|  | @ -253,7 +262,7 @@ export const api: API = { | |||
|   async createProject( | ||||
|     project: NewProject, | ||||
|     token: string, | ||||
|   ): Promise<APIResponse<Project>> { | ||||
|   ): Promise<APIResponse<void>> { | ||||
|     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<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( | ||||
|     username: string, | ||||
|     token: string, | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Peter KW
						Peter KW