2024-03-31 20:54:00 +02:00
|
|
|
import { NewProjMember } from "../Components/AddMember";
|
2024-04-01 02:02:22 +02:00
|
|
|
import { ProjectRoleChange } from "../Components/ChangeRole";
|
2024-04-02 13:09:28 +02:00
|
|
|
import { projectTimes } from "../Components/GetProjectTimes";
|
2024-03-31 20:54:00 +02:00
|
|
|
import { ProjectMember } from "../Components/GetUsersInProject";
|
2024-03-17 15:31:15 +01:00
|
|
|
import {
|
2024-04-02 15:43:05 +02:00
|
|
|
UpdateWeeklyReport,
|
2024-03-17 15:31:15 +01:00
|
|
|
NewWeeklyReport,
|
|
|
|
NewUser,
|
|
|
|
User,
|
|
|
|
Project,
|
|
|
|
NewProject,
|
2024-03-20 14:00:00 +01:00
|
|
|
WeeklyReport,
|
2024-03-21 01:56:27 +01:00
|
|
|
StrNameChange,
|
2024-03-17 15:31:15 +01:00
|
|
|
} from "../Types/goTypes";
|
2024-03-13 17:06:26 +01:00
|
|
|
|
2024-03-20 20:36:34 +01:00
|
|
|
/**
|
|
|
|
* Response object returned by API methods.
|
|
|
|
*/
|
2024-03-17 19:18:03 +01:00
|
|
|
export interface APIResponse<T> {
|
2024-03-20 20:36:34 +01:00
|
|
|
/** Indicates whether the API call was successful */
|
2024-03-16 17:42:28 +01:00
|
|
|
success: boolean;
|
2024-03-20 20:36:34 +01:00
|
|
|
/** Optional message providing additional information or error description */
|
2024-03-16 17:42:28 +01:00
|
|
|
message?: string;
|
2024-03-20 20:36:34 +01:00
|
|
|
/** Optional data returned by the API method */
|
2024-03-16 17:42:28 +01:00
|
|
|
data?: T;
|
|
|
|
}
|
|
|
|
|
2024-03-20 20:36:34 +01:00
|
|
|
/**
|
|
|
|
* Interface defining methods that an instance of the API must implement.
|
|
|
|
*/
|
2024-03-13 17:06:26 +01:00
|
|
|
interface API {
|
2024-03-20 20:36:34 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-03-16 17:42:28 +01:00
|
|
|
registerUser(user: NewUser): Promise<APIResponse<User>>;
|
2024-03-20 20:36:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-03-16 17:42:28 +01:00
|
|
|
removeUser(username: string, token: string): Promise<APIResponse<User>>;
|
2024-03-20 20:36:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if user is project manager.
|
|
|
|
* @param {string} username The username of the user.
|
|
|
|
* @param {string} projectName The name of the project.
|
|
|
|
* @param {string} token The authentication token.
|
|
|
|
* @returns {Promise<APIResponse<boolean>>} A promise containing the API response indicating if the user is a project manager.
|
|
|
|
*/
|
2024-03-19 20:25:26 +01:00
|
|
|
checkIfProjectManager(
|
|
|
|
projectName: string,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<boolean>>;
|
2024-03-20 20:36:34 +01:00
|
|
|
|
|
|
|
/** Logs in a user with the provided credentials.
|
|
|
|
* @param {NewUser} NewUser The user object containing username and password.
|
|
|
|
* @returns {Promise<APIResponse<string>>} A promise resolving to an API response with a token.
|
|
|
|
*/
|
2024-03-17 22:20:34 +01:00
|
|
|
login(NewUser: NewUser): Promise<APIResponse<string>>;
|
2024-03-20 20:36:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-03-17 22:20:34 +01:00
|
|
|
renewToken(token: string): Promise<APIResponse<string>>;
|
2024-03-20 20:36:34 +01:00
|
|
|
|
2024-03-19 19:45:56 +01:00
|
|
|
/** Promote user to admin */
|
2024-03-20 20:36:34 +01:00
|
|
|
|
|
|
|
/** 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.
|
|
|
|
*/
|
2024-04-01 02:02:22 +02:00
|
|
|
createProject(project: NewProject, token: string): Promise<APIResponse<void>>;
|
2024-03-20 20:36:34 +01:00
|
|
|
|
|
|
|
/** 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.
|
|
|
|
*/
|
2024-03-17 14:44:25 +01:00
|
|
|
submitWeeklyReport(
|
2024-03-20 20:36:34 +01:00
|
|
|
weeklyReport: NewWeeklyReport,
|
2024-03-17 14:44:25 +01:00
|
|
|
token: string,
|
2024-03-21 18:05:41 +01:00
|
|
|
): Promise<APIResponse<string>>;
|
2024-03-20 20:36:34 +01:00
|
|
|
|
2024-04-02 15:43:05 +02:00
|
|
|
/**
|
|
|
|
* Updates a weekly report.
|
|
|
|
* @param {UpdateWeeklyReport} weeklyReport The updated weekly report object.
|
|
|
|
* @param {string} token The authentication token.
|
|
|
|
* @returns {Promise<APIResponse<string>>} A promise containing the API response with the updated report.
|
|
|
|
*/
|
|
|
|
updateWeeklyReport(
|
|
|
|
weeklyReport: UpdateWeeklyReport,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<string>>;
|
|
|
|
|
2024-04-03 17:12:29 +02:00
|
|
|
/** Gets a weekly report for a specific user, project and week.
|
2024-04-03 17:32:07 +02:00
|
|
|
* Keep in mind that the user within the token needs to be PM
|
2024-04-03 17:12:29 +02:00
|
|
|
* of the project to get the report, unless the user is the target user.
|
2024-03-20 20:36:34 +01:00
|
|
|
* @param {string} projectName The name of the project.
|
|
|
|
* @param {string} week The week number.
|
|
|
|
* @param {string} token The authentication token.
|
2024-04-03 17:12:29 +02:00
|
|
|
* @param {string} targetUser The username of the target user. Defaults to token user.
|
2024-03-20 20:36:34 +01:00
|
|
|
* @returns {Promise<APIResponse<WeeklyReport>>} A promise resolving to an API response with the retrieved report.
|
|
|
|
*/
|
2024-03-17 22:20:34 +01:00
|
|
|
getWeeklyReport(
|
|
|
|
projectName: string,
|
2024-03-17 22:27:21 +01:00
|
|
|
week: string,
|
2024-03-17 22:35:51 +01:00
|
|
|
token: string,
|
2024-04-03 17:12:29 +02:00
|
|
|
targetUser?: string,
|
2024-03-20 14:00:00 +01:00
|
|
|
): Promise<APIResponse<WeeklyReport>>;
|
2024-03-20 20:36:34 +01:00
|
|
|
|
2024-03-20 17:10:11 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2024-04-03 17:32:07 +02:00
|
|
|
getAllWeeklyReportsForUser(
|
2024-03-19 19:45:56 +01:00
|
|
|
projectName: string,
|
|
|
|
token: string,
|
2024-04-03 17:32:07 +02:00
|
|
|
targetUser?: string,
|
2024-03-20 14:00:00 +01:00
|
|
|
): Promise<APIResponse<WeeklyReport[]>>;
|
2024-03-20 20:36:34 +01:00
|
|
|
|
|
|
|
/** Gets all the projects of a user
|
2024-03-28 21:31:30 +01:00
|
|
|
* @param {string} username - The authentication token.
|
2024-03-20 20:36:34 +01:00
|
|
|
* @param {string} token - The authentication token.
|
|
|
|
* @returns {Promise<APIResponse<Project[]>>} A promise containing the API response with the user's projects.
|
|
|
|
*/
|
2024-03-28 21:31:30 +01:00
|
|
|
getUserProjects(
|
|
|
|
username: string,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<Project[]>>;
|
2024-03-20 20:36:34 +01:00
|
|
|
|
|
|
|
/** 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.
|
|
|
|
*/
|
2024-03-18 16:42:35 +01:00
|
|
|
getProject(id: number): Promise<APIResponse<Project>>;
|
2024-03-20 20:36:34 +01:00
|
|
|
|
2024-04-02 13:09:28 +02:00
|
|
|
/** Gets a projects reported time
|
|
|
|
* @param {string} projectName The name of the project.
|
|
|
|
* @param {string} token The usertoken.
|
|
|
|
* @returns {Promise<APIResponse<Times>>} A promise resolving to an API response containing the project times.
|
|
|
|
*/
|
|
|
|
getProjectTimes(
|
|
|
|
projectName: string,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<projectTimes>>;
|
|
|
|
|
2024-03-20 20:36:34 +01:00
|
|
|
/** 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.
|
|
|
|
*/
|
2024-03-20 00:10:24 +01:00
|
|
|
getAllUsers(token: string): Promise<APIResponse<string[]>>;
|
2024-03-20 17:02:36 +01:00
|
|
|
/** Gets all users in a project from name*/
|
|
|
|
getAllUsersProject(
|
|
|
|
projectName: string,
|
|
|
|
token: string,
|
2024-03-31 20:54:00 +02:00
|
|
|
): Promise<APIResponse<ProjectMember[]>>;
|
2024-04-02 17:05:46 +02:00
|
|
|
|
2024-04-02 17:11:30 +02:00
|
|
|
/** Gets all unsigned reports in a project.
|
|
|
|
* @param {string} projectName The name of the project.
|
|
|
|
* @param {string} token The authentication token.
|
|
|
|
* @returns {Promise<APIResponse<WeeklyReport[]>>} A promise resolving to an API response containing the list of unsigned reports.
|
|
|
|
*/
|
2024-04-02 17:05:46 +02:00
|
|
|
getUnsignedReportsInProject(
|
|
|
|
projectName: string,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<WeeklyReport[]>>;
|
|
|
|
|
2024-03-21 01:56:27 +01:00
|
|
|
/**
|
|
|
|
* Changes the username of a user in the database.
|
|
|
|
* @param {StrNameChange} data 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.
|
|
|
|
*/
|
|
|
|
changeUserName(
|
|
|
|
data: StrNameChange,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<void>>;
|
2024-04-01 02:02:22 +02:00
|
|
|
/**
|
|
|
|
* 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>>;
|
|
|
|
|
2024-03-21 03:36:57 +01:00
|
|
|
addUserToProject(
|
|
|
|
user: NewProjMember,
|
|
|
|
token: string,
|
2024-03-31 20:54:00 +02:00
|
|
|
): Promise<APIResponse<void>>;
|
2024-03-27 21:18:44 +01:00
|
|
|
|
|
|
|
removeProject(
|
|
|
|
projectName: string,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<string>>;
|
2024-03-29 15:33:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Signs a report. Keep in mind that the user which the token belongs to must be
|
|
|
|
* the project manager of the project the report belongs to.
|
|
|
|
*
|
|
|
|
* @param {number} reportId The id of the report to sign
|
|
|
|
* @param {string} token The authentication token
|
|
|
|
*/
|
2024-03-31 20:54:00 +02:00
|
|
|
signReport(reportId: number, token: string): Promise<APIResponse<string>>;
|
2024-04-03 15:53:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Promotes a user to project manager within a project.
|
|
|
|
*
|
|
|
|
* @param {string} userName The username of the user to promote
|
|
|
|
* @param {string} projectName The name of the project to promote the user in
|
|
|
|
* @returns {Promise<APIResponse<string>} A promise resolving to an API response.
|
|
|
|
*/
|
|
|
|
promoteToPm(
|
|
|
|
userName: string,
|
|
|
|
projectName: string,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<string>>;
|
2024-03-13 17:06:26 +01:00
|
|
|
}
|
|
|
|
|
2024-03-20 20:36:34 +01:00
|
|
|
/** An instance of the API */
|
2024-03-13 17:06:26 +01:00
|
|
|
export const api: API = {
|
2024-03-16 17:42:28 +01:00
|
|
|
async registerUser(user: NewUser): Promise<APIResponse<User>> {
|
|
|
|
try {
|
|
|
|
const response = await fetch("/api/register", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(user),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
2024-03-17 22:48:38 +01:00
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: "Failed to register user: " + response.status,
|
|
|
|
};
|
2024-03-16 17:42:28 +01:00
|
|
|
} else {
|
2024-03-17 22:48:38 +01:00
|
|
|
// const data = (await response.json()) as User; // The API does not currently return the user
|
|
|
|
return { success: true };
|
2024-03-16 17:42:28 +01:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2024-03-17 22:48:38 +01:00
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: "Unknown error while registering user",
|
|
|
|
};
|
2024-03-16 17:42:28 +01:00
|
|
|
}
|
2024-03-13 17:06:26 +01:00
|
|
|
},
|
|
|
|
|
2024-03-16 17:42:28 +01:00
|
|
|
async removeUser(
|
|
|
|
username: string,
|
2024-03-17 14:44:25 +01:00
|
|
|
token: string,
|
2024-03-16 17:42:28 +01:00
|
|
|
): Promise<APIResponse<User>> {
|
|
|
|
try {
|
2024-03-20 00:10:24 +01:00
|
|
|
const response = await fetch(`/api/userdelete/${username}`, {
|
2024-03-28 12:38:12 +01:00
|
|
|
method: "DELETE",
|
2024-03-16 17:42:28 +01:00
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
},
|
|
|
|
body: JSON.stringify(username),
|
|
|
|
});
|
|
|
|
if (!response.ok) {
|
2024-03-28 12:38:12 +01:00
|
|
|
return { success: false, message: "Could not remove user" };
|
2024-03-16 17:42:28 +01:00
|
|
|
} else {
|
2024-03-28 12:38:12 +01:00
|
|
|
return { success: true };
|
2024-03-16 17:42:28 +01:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return { success: false, message: "Failed to remove user" };
|
|
|
|
}
|
2024-03-13 17:06:26 +01:00
|
|
|
},
|
2024-03-13 17:52:56 +01:00
|
|
|
|
2024-03-19 20:29:36 +01:00
|
|
|
async checkIfProjectManager(
|
|
|
|
projectName: string,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<boolean>> {
|
2024-03-19 20:25:26 +01:00
|
|
|
try {
|
2024-03-28 12:06:59 +01:00
|
|
|
const response = await fetch(
|
2024-03-28 12:25:24 +01:00
|
|
|
`/api/checkIfProjectManager/${projectName}`,
|
2024-03-28 12:06:59 +01:00
|
|
|
{
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
},
|
2024-03-19 20:25:26 +01:00
|
|
|
},
|
2024-03-28 12:06:59 +01:00
|
|
|
);
|
2024-03-19 20:25:26 +01:00
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: "Failed to check if project manager",
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
const data = (await response.json()) as boolean;
|
|
|
|
return { success: true, data };
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2024-03-28 12:06:59 +01:00
|
|
|
return { success: false, message: "Failed to check if project manager" };
|
2024-03-19 20:25:26 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-03-16 17:42:28 +01:00
|
|
|
async createProject(
|
|
|
|
project: NewProject,
|
2024-03-17 14:44:25 +01:00
|
|
|
token: string,
|
2024-04-01 02:02:22 +02:00
|
|
|
): Promise<APIResponse<void>> {
|
2024-03-16 17:42:28 +01:00
|
|
|
try {
|
|
|
|
const response = await fetch("/api/project", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
},
|
|
|
|
body: JSON.stringify(project),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
return { success: false, message: "Failed to create project" };
|
|
|
|
} else {
|
2024-04-01 02:02:22 +02:00
|
|
|
return { success: true };
|
2024-03-16 17:42:28 +01:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2024-04-01 02:02:22 +02:00
|
|
|
return { success: false, message: "Failed to create project!" };
|
2024-03-16 17:42:28 +01:00
|
|
|
}
|
2024-03-13 17:52:56 +01:00
|
|
|
},
|
2024-03-13 20:56:47 +01:00
|
|
|
|
2024-03-21 03:36:57 +01:00
|
|
|
async addUserToProject(
|
|
|
|
user: NewProjMember,
|
|
|
|
token: string,
|
2024-03-31 20:54:00 +02:00
|
|
|
): Promise<APIResponse<void>> {
|
2024-03-21 03:36:57 +01:00
|
|
|
try {
|
|
|
|
const response = await fetch("/api/addUserToProject", {
|
2024-03-28 00:57:16 +01:00
|
|
|
method: "PUT",
|
2024-03-21 03:36:57 +01:00
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
},
|
|
|
|
body: JSON.stringify(user),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
return { success: false, message: "Failed to add member" };
|
|
|
|
} else {
|
|
|
|
return { success: true, message: "Added member" };
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return { success: false, message: "Failed to add member" };
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-03-16 17:42:28 +01:00
|
|
|
async renewToken(token: string): Promise<APIResponse<string>> {
|
|
|
|
try {
|
|
|
|
const response = await fetch("/api/loginrenew", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
return { success: false, message: "Failed to renew token" };
|
|
|
|
} else {
|
|
|
|
const data = (await response.json()) as string;
|
|
|
|
return { success: true, data };
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return { success: false, message: "Failed to renew token" };
|
|
|
|
}
|
2024-03-13 20:56:47 +01:00
|
|
|
},
|
2024-03-17 12:35:53 +01:00
|
|
|
|
2024-04-01 02:02:22 +02:00
|
|
|
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" };
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-03-28 21:31:30 +01:00
|
|
|
async getUserProjects(
|
|
|
|
username: string,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<Project[]>> {
|
2024-03-17 12:35:53 +01:00
|
|
|
try {
|
2024-03-28 21:31:30 +01:00
|
|
|
const response = await fetch(`/api/getUserProjects/${username}`, {
|
2024-03-17 12:35:53 +01:00
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
2024-03-17 14:44:25 +01:00
|
|
|
return Promise.resolve({
|
|
|
|
success: false,
|
|
|
|
message: "Failed to get user projects",
|
|
|
|
});
|
2024-03-17 12:35:53 +01:00
|
|
|
} else {
|
|
|
|
const data = (await response.json()) as Project[];
|
|
|
|
return Promise.resolve({ success: true, data });
|
|
|
|
}
|
2024-03-17 14:44:25 +01:00
|
|
|
} catch (e) {
|
|
|
|
return Promise.resolve({
|
|
|
|
success: false,
|
2024-03-19 00:20:08 +01:00
|
|
|
message: "API fucked",
|
2024-03-17 14:44:25 +01:00
|
|
|
});
|
2024-03-17 12:35:53 +01:00
|
|
|
}
|
|
|
|
},
|
2024-03-17 14:44:25 +01:00
|
|
|
|
2024-04-02 13:09:28 +02:00
|
|
|
async getProjectTimes(
|
|
|
|
projectName: string,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<projectTimes>> {
|
|
|
|
try {
|
|
|
|
const response = await fetch(`/api/getProjectTimes/${projectName}`, {
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
return Promise.resolve({
|
|
|
|
success: false,
|
|
|
|
message:
|
|
|
|
"Fetch error: " + response.status + ", failed to get project times",
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const data = (await response.json()) as projectTimes;
|
|
|
|
return Promise.resolve({ success: true, data });
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return Promise.resolve({
|
|
|
|
success: false,
|
|
|
|
message: "API error! Could not get times.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-03-17 14:43:09 +01:00
|
|
|
async submitWeeklyReport(
|
2024-03-17 22:35:51 +01:00
|
|
|
weeklyReport: NewWeeklyReport,
|
2024-03-17 14:44:25 +01:00
|
|
|
token: string,
|
2024-03-21 18:05:41 +01:00
|
|
|
): Promise<APIResponse<string>> {
|
2024-03-17 12:35:53 +01:00
|
|
|
try {
|
2024-03-17 16:11:03 +01:00
|
|
|
const response = await fetch("/api/submitWeeklyReport", {
|
2024-03-17 12:35:53 +01:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
},
|
2024-03-17 22:35:51 +01:00
|
|
|
body: JSON.stringify(weeklyReport),
|
2024-03-17 16:11:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: "Failed to submit weekly report",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-21 18:05:41 +01:00
|
|
|
const data = await response.text();
|
|
|
|
return { success: true, message: data };
|
2024-03-17 12:35:53 +01:00
|
|
|
} catch (e) {
|
2024-03-17 16:11:03 +01:00
|
|
|
return {
|
2024-03-17 14:44:25 +01:00
|
|
|
success: false,
|
|
|
|
message: "Failed to submit weekly report",
|
2024-03-17 16:11:03 +01:00
|
|
|
};
|
2024-03-17 12:35:53 +01:00
|
|
|
}
|
2024-03-17 14:44:25 +01:00
|
|
|
},
|
2024-03-17 14:43:09 +01:00
|
|
|
|
2024-04-02 15:43:05 +02:00
|
|
|
async updateWeeklyReport(
|
|
|
|
weeklyReport: UpdateWeeklyReport,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<string>> {
|
|
|
|
try {
|
|
|
|
const response = await fetch("/api/updateWeeklyReport", {
|
|
|
|
method: "PUT",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
},
|
|
|
|
body: JSON.stringify(weeklyReport),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: "Failed to update weekly report",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await response.text();
|
|
|
|
return { success: true, message: data };
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: "Failed to update weekly report",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-03-17 22:35:51 +01:00
|
|
|
async getWeeklyReport(
|
|
|
|
projectName: string,
|
|
|
|
week: string,
|
|
|
|
token: string,
|
2024-04-03 17:12:29 +02:00
|
|
|
targetUser?: string,
|
2024-03-20 14:00:00 +01:00
|
|
|
): Promise<APIResponse<WeeklyReport>> {
|
2024-03-17 22:35:51 +01:00
|
|
|
try {
|
2024-03-20 22:02:34 +01:00
|
|
|
const response = await fetch(
|
2024-04-03 17:12:29 +02:00
|
|
|
`/api/getWeeklyReport?projectName=${projectName}&week=${week}&targetUser=${targetUser}`,
|
2024-03-20 22:02:34 +01:00
|
|
|
{
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
},
|
2024-03-17 22:35:51 +01:00
|
|
|
},
|
2024-03-20 22:02:34 +01:00
|
|
|
);
|
2024-03-17 22:35:51 +01:00
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
return { success: false, message: "Failed to get weekly report" };
|
|
|
|
} else {
|
2024-03-20 14:00:00 +01:00
|
|
|
const data = (await response.json()) as WeeklyReport;
|
2024-03-17 22:35:51 +01:00
|
|
|
return { success: true, data };
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return { success: false, message: "Failed to get weekly report" };
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-04-03 17:32:07 +02:00
|
|
|
async getAllWeeklyReportsForUser(
|
2024-03-20 14:00:00 +01:00
|
|
|
projectName: string,
|
2024-03-20 14:02:34 +01:00
|
|
|
token: string,
|
2024-04-03 17:32:07 +02:00
|
|
|
targetUser?: string,
|
2024-03-20 14:00:00 +01:00
|
|
|
): Promise<APIResponse<WeeklyReport[]>> {
|
2024-03-19 19:45:56 +01:00
|
|
|
try {
|
2024-04-03 17:32:07 +02:00
|
|
|
const response = await fetch(
|
|
|
|
`/api/getAllWeeklyReports/${projectName}?targetUser=${targetUser}`,
|
|
|
|
{
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
},
|
2024-03-19 19:45:56 +01:00
|
|
|
},
|
2024-04-03 17:32:07 +02:00
|
|
|
);
|
2024-03-19 19:45:56 +01:00
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
return {
|
|
|
|
success: false,
|
2024-03-20 17:19:09 +01:00
|
|
|
message:
|
|
|
|
"Failed to get weekly reports for project: Response code " +
|
|
|
|
response.status,
|
2024-03-19 19:45:56 +01:00
|
|
|
};
|
|
|
|
} else {
|
2024-03-20 14:00:00 +01:00
|
|
|
const data = (await response.json()) as WeeklyReport[];
|
2024-03-19 19:45:56 +01:00
|
|
|
return { success: true, data };
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
|
|
|
success: false,
|
2024-03-20 17:19:09 +01:00
|
|
|
message: "Failed to get weekly reports for project, unknown error",
|
2024-03-19 19:45:56 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-03-17 15:49:34 +01:00
|
|
|
async login(NewUser: NewUser): Promise<APIResponse<string>> {
|
2024-03-17 14:43:09 +01:00
|
|
|
try {
|
|
|
|
const response = await fetch("/api/login", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(NewUser),
|
|
|
|
});
|
|
|
|
|
2024-03-17 14:52:13 +01:00
|
|
|
if (!response.ok) {
|
|
|
|
return { success: false, message: "Failed to login" };
|
|
|
|
} else {
|
2024-03-17 15:49:34 +01:00
|
|
|
const data = (await response.json()) as { token: string }; // Update the type of 'data'
|
|
|
|
return { success: true, data: data.token };
|
2024-03-17 14:43:09 +01:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2024-03-17 15:41:23 +01:00
|
|
|
return Promise.resolve({ success: false, message: "Failed to login" });
|
2024-03-17 12:35:53 +01:00
|
|
|
}
|
2024-03-17 14:52:13 +01:00
|
|
|
},
|
2024-03-18 16:42:35 +01:00
|
|
|
|
|
|
|
async getProject(id: number): Promise<APIResponse<Project>> {
|
|
|
|
try {
|
|
|
|
const response = await fetch(`/api/project/${id}`, {
|
|
|
|
method: "GET",
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: "Failed to get project: Response code " + response.status,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
const data = (await response.json()) as Project;
|
|
|
|
return { success: true, data };
|
|
|
|
}
|
|
|
|
// The code below is garbage but satisfies the linter
|
|
|
|
// This needs fixing, do not copy this pattern
|
|
|
|
} catch (e: unknown) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: "Failed to get project: " + (e as Error).toString(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
2024-03-20 00:10:24 +01:00
|
|
|
|
|
|
|
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",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2024-03-20 17:02:36 +01:00
|
|
|
//Gets all users in a project
|
|
|
|
async getAllUsersProject(
|
|
|
|
projectName: string,
|
|
|
|
token: string,
|
2024-03-31 20:54:00 +02:00
|
|
|
): Promise<APIResponse<ProjectMember[]>> {
|
2024-03-20 17:02:36 +01:00
|
|
|
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 {
|
2024-03-31 20:54:00 +02:00
|
|
|
const data = (await response.json()) as ProjectMember[];
|
2024-03-20 17:02:36 +01:00
|
|
|
return Promise.resolve({ success: true, data });
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return Promise.resolve({
|
|
|
|
success: false,
|
|
|
|
message: "API is not ok",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2024-03-21 01:58:57 +01:00
|
|
|
|
2024-04-02 17:05:46 +02:00
|
|
|
async getUnsignedReportsInProject(
|
|
|
|
projectName: string,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<WeeklyReport[]>> {
|
|
|
|
try {
|
|
|
|
const response = await fetch(`/api/getUnsignedReports/${projectName}`, {
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message:
|
|
|
|
"Failed to get unsigned reports for project: Response code " +
|
|
|
|
response.status,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
const data = (await response.json()) as WeeklyReport[];
|
|
|
|
return { success: true, data };
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: "Failed to get unsigned reports for project, unknown error",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-03-21 01:58:57 +01:00
|
|
|
async changeUserName(
|
|
|
|
data: StrNameChange,
|
2024-03-21 01:56:27 +01:00
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<void>> {
|
2024-03-21 01:58:57 +01:00
|
|
|
try {
|
|
|
|
const response = await fetch("/api/changeUserName", {
|
|
|
|
method: "PUT",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
},
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
return { success: false, message: "Failed to change username" };
|
|
|
|
} else {
|
|
|
|
return { success: true };
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return { success: false, message: "Failed to change username" };
|
|
|
|
}
|
2024-03-21 01:56:27 +01:00
|
|
|
},
|
2024-03-28 16:45:39 +01:00
|
|
|
|
2024-03-27 21:18:44 +01:00
|
|
|
async removeProject(
|
|
|
|
projectName: string,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<string>> {
|
|
|
|
try {
|
2024-04-02 13:43:32 +02:00
|
|
|
const response = await fetch(`/api/removeProject/${projectName}`, {
|
2024-03-27 21:18:44 +01:00
|
|
|
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",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2024-03-29 15:33:20 +01:00
|
|
|
|
|
|
|
async signReport(
|
|
|
|
reportId: number,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<string>> {
|
|
|
|
try {
|
|
|
|
const response = await fetch(`/api/signReport/${reportId}`, {
|
|
|
|
method: "PUT",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
return { success: false, message: "Failed to sign report" };
|
|
|
|
} else {
|
|
|
|
return { success: true, message: "Report signed" };
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return { success: false, message: "Failed to sign report" };
|
|
|
|
}
|
2024-03-31 20:54:00 +02:00
|
|
|
},
|
2024-04-03 15:53:15 +02:00
|
|
|
|
|
|
|
async promoteToPm(
|
|
|
|
userName: string,
|
|
|
|
projectName: string,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<string>> {
|
|
|
|
try {
|
|
|
|
const response = await fetch(
|
|
|
|
`/api/promoteToPm/${projectName}?userName=${userName}`,
|
|
|
|
{
|
|
|
|
method: "PUT",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
if (!response.ok) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: "Failed to promote user to project manager",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: "Failed to promote user to project manager",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return { success: true, message: "User promoted to project manager" };
|
|
|
|
},
|
2024-03-13 17:06:26 +01:00
|
|
|
};
|