This commit is contained in:
al8763be 2024-03-28 12:08:14 +01:00
commit 6a78e67e7e
71 changed files with 2478 additions and 456 deletions

View file

@ -4,21 +4,39 @@ import {
User,
Project,
NewProject,
UserProjectMember,
WeeklyReport,
} from "../Types/goTypes";
// This type of pattern should be hard to misuse
/**
* Response object returned by API methods.
*/
export interface APIResponse<T> {
/** Indicates whether the API call was successful */
success: boolean;
/** Optional message providing additional information or error description */
message?: string;
/** Optional data returned by the API method */
data?: T;
}
// Note that all protected routes also require a token
// Defines all the methods that an instance of the API must implement
/**
* Interface defining methods that an instance of the API must implement.
*/
interface API {
/** Register a new user */
/**
* Register a new user
* @param {NewUser} user The user object to be registered
* @returns {Promise<APIResponse<User>>} A promise containing the API response with the user data.
*/
registerUser(user: NewUser): Promise<APIResponse<User>>;
/** Remove a user */
/**
* Removes a user.
* @param {string} username The username of the user to be removed.
* @param {string} token The authentication token.
* @returns {Promise<APIResponse<User>>} A promise containing the API response with the removed user data.
*/
removeUser(username: string, token: string): Promise<APIResponse<User>>;
/**
@ -38,32 +56,90 @@ interface API {
* @returns {Promise<APIResponse<string>>} A promise resolving to an API response with a token.
*/
login(NewUser: NewUser): Promise<APIResponse<string>>;
/** Renew the token */
/**
* Renew the token
* @param {string} token The current authentication token.
* @returns {Promise<APIResponse<string>>} A promise resolving to an API response with a renewed token.
*/
renewToken(token: string): Promise<APIResponse<string>>;
/** Create a project */
/** Promote user to admin */
/** Creates a new project.
* @param {NewProject} project The project object containing name and description.
* @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>>;
/** Submit a weekly report */
/** Submits a weekly report
* @param {NewWeeklyReport} weeklyReport The weekly report object.
* @param {string} token The authentication token.
* @returns {Promise<APIResponse<NewWeeklyReport>>} A promise resolving to an API response with the submitted report.
*/
submitWeeklyReport(
project: NewWeeklyReport,
weeklyReport: NewWeeklyReport,
token: string,
): Promise<APIResponse<NewWeeklyReport>>;
/**Gets a weekly report*/
): Promise<APIResponse<string>>;
/** Gets a weekly report for a specific user, project and week
* @param {string} projectName The name of the project.
* @param {string} week The week number.
* @param {string} token The authentication token.
* @returns {Promise<APIResponse<WeeklyReport>>} A promise resolving to an API response with the retrieved report.
*/
getWeeklyReport(
username: string,
projectName: string,
week: string,
token: string,
): Promise<APIResponse<NewWeeklyReport>>;
/** Gets all the projects of a user*/
): Promise<APIResponse<WeeklyReport>>;
/**
* Returns all the weekly reports for a user in a particular project
* The username is derived from the token
* @param {string} projectName The name of the project
* @param {string} token The token of the user
* @returns {APIResponse<WeeklyReport[]>} A list of weekly reports
*/
getWeeklyReportsForUser(
projectName: string,
token: string,
): Promise<APIResponse<WeeklyReport[]>>;
/** Gets all the projects of a user
* @param {string} token - The authentication token.
* @returns {Promise<APIResponse<Project[]>>} A promise containing the API response with the user's projects.
*/
getUserProjects(token: string): Promise<APIResponse<Project[]>>;
/** Gets a project from id*/
/** Gets a project by its id.
* @param {number} id The id of the project to retrieve.
* @returns {Promise<APIResponse<Project>>} A promise resolving to an API response containing the project data.
*/
getProject(id: number): Promise<APIResponse<Project>>;
/** Gets a list of all users.
* @param {string} token The authentication token of the requesting user.
* @returns {Promise<APIResponse<string[]>>} A promise resolving to an API response containing the list of users.
*/
getAllUsers(token: string): Promise<APIResponse<string[]>>;
/** Gets all users in a project from name*/
getAllUsersProject(
projectName: string,
token: string,
): Promise<APIResponse<UserProjectMember[]>>;
removeProject(
projectName: string,
token: string,
): Promise<APIResponse<string>>;
}
// Export an instance of the API
/** An instance of the API */
export const api: API = {
async registerUser(user: NewUser): Promise<APIResponse<User>> {
try {
@ -97,7 +173,7 @@ export const api: API = {
token: string,
): Promise<APIResponse<User>> {
try {
const response = await fetch("/api/userdelete", {
const response = await fetch(`/api/userdelete/${username}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
@ -223,7 +299,7 @@ export const api: API = {
async submitWeeklyReport(
weeklyReport: NewWeeklyReport,
token: string,
): Promise<APIResponse<NewWeeklyReport>> {
): Promise<APIResponse<string>> {
try {
const response = await fetch("/api/submitWeeklyReport", {
method: "POST",
@ -241,8 +317,8 @@ export const api: API = {
};
}
const data = (await response.json()) as NewWeeklyReport;
return { success: true, data };
const data = await response.text();
return { success: true, message: data };
} catch (e) {
return {
success: false,
@ -252,29 +328,62 @@ export const api: API = {
},
async getWeeklyReport(
username: string,
projectName: string,
week: string,
token: string,
): Promise<APIResponse<NewWeeklyReport>> {
): Promise<APIResponse<WeeklyReport>> {
try {
const response = await fetch("/api/getWeeklyReport", {
const response = await fetch(
`/api/getWeeklyReport?projectName=${projectName}&week=${week}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
},
);
if (!response.ok) {
return { success: false, message: "Failed to get weekly report" };
} else {
const data = (await response.json()) as WeeklyReport;
return { success: true, data };
}
} catch (e) {
return { success: false, message: "Failed to get weekly report" };
}
},
async getWeeklyReportsForUser(
projectName: string,
token: string,
): Promise<APIResponse<WeeklyReport[]>> {
try {
const response = await fetch(`/api/getWeeklyReportsUser/${projectName}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
body: JSON.stringify({ username, projectName, week }),
});
if (!response.ok) {
return { success: false, message: "Failed to get weekly report" };
return {
success: false,
message:
"Failed to get weekly reports for project: Response code " +
response.status,
};
} else {
const data = (await response.json()) as NewWeeklyReport;
const data = (await response.json()) as WeeklyReport[];
return { success: true, data };
}
} catch (e) {
return { success: false, message: "Failed to get weekly report" };
return {
success: false,
message: "Failed to get weekly reports for project, unknown error",
};
}
},
@ -299,7 +408,6 @@ export const api: API = {
}
},
// Gets a projet by id, currently untested since we have no javascript-based tests
async getProject(id: number): Promise<APIResponse<Project>> {
try {
const response = await fetch(`/api/project/${id}`, {
@ -324,4 +432,91 @@ export const api: API = {
};
}
},
async getAllUsers(token: string): Promise<APIResponse<string[]>> {
try {
const response = await fetch("/api/users/all", {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
});
if (!response.ok) {
return Promise.resolve({
success: false,
message: "Failed to get users",
});
} else {
const data = (await response.json()) as string[];
return Promise.resolve({ success: true, data });
}
} catch (e) {
return Promise.resolve({
success: false,
message: "API is not ok",
});
}
},
//Gets all users in a project
async getAllUsersProject(
projectName: string,
token: string,
): Promise<APIResponse<UserProjectMember[]>> {
try {
const response = await fetch(`/api/getUsersProject/${projectName}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
});
if (!response.ok) {
return Promise.resolve({
success: false,
message: "Failed to get users",
});
} else {
const data = (await response.json()) as UserProjectMember[];
return Promise.resolve({ success: true, data });
}
} catch (e) {
return Promise.resolve({
success: false,
message: "API is not ok",
});
}
},
async removeProject(
projectName: string,
token: string,
): Promise<APIResponse<string>> {
try {
const response = await fetch(`/api/projectdelete/${projectName}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
});
if (!response.ok) {
return Promise.resolve({
success: false,
message: "Failed to remove project",
});
} else {
const data = await response.text();
return Promise.resolve({ success: true, message: data });
}
} catch (e) {
return Promise.resolve({
success: false,
message: "Failed to remove project",
});
}
},
};