2024-03-17 15:31:15 +01:00
|
|
|
import {
|
|
|
|
NewWeeklyReport,
|
|
|
|
NewUser,
|
|
|
|
User,
|
|
|
|
Project,
|
|
|
|
NewProject,
|
|
|
|
} from "../Types/goTypes";
|
2024-03-13 17:06:26 +01:00
|
|
|
|
2024-03-16 17:42:28 +01:00
|
|
|
// This type of pattern should be hard to misuse
|
2024-03-17 19:18:03 +01:00
|
|
|
export interface APIResponse<T> {
|
2024-03-16 17:42:28 +01:00
|
|
|
success: boolean;
|
|
|
|
message?: string;
|
|
|
|
data?: T;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that all protected routes also require a token
|
2024-03-13 17:06:26 +01:00
|
|
|
// Defines all the methods that an instance of the API must implement
|
|
|
|
interface API {
|
|
|
|
/** Register a new user */
|
2024-03-16 17:42:28 +01:00
|
|
|
registerUser(user: NewUser): Promise<APIResponse<User>>;
|
2024-03-13 17:06:26 +01:00
|
|
|
/** Remove a user */
|
2024-03-16 17:42:28 +01:00
|
|
|
removeUser(username: string, token: string): Promise<APIResponse<User>>;
|
2024-03-17 22:20:34 +01:00
|
|
|
/** Login */
|
|
|
|
login(NewUser: NewUser): Promise<APIResponse<string>>;
|
|
|
|
/** Renew the token */
|
|
|
|
renewToken(token: string): Promise<APIResponse<string>>;
|
2024-03-13 17:52:56 +01:00
|
|
|
/** Create a project */
|
2024-03-16 17:42:28 +01:00
|
|
|
createProject(
|
|
|
|
project: NewProject,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<Project>>;
|
2024-03-18 00:22:32 +01:00
|
|
|
/** Gets all the projects of a user*/
|
|
|
|
getUserProjects(
|
|
|
|
username: string,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<Project[]>>;
|
2024-03-17 12:35:53 +01:00
|
|
|
/** Submit a weekly report */
|
2024-03-17 14:44:25 +01:00
|
|
|
submitWeeklyReport(
|
|
|
|
project: NewWeeklyReport,
|
|
|
|
token: string,
|
2024-03-17 22:35:51 +01:00
|
|
|
): Promise<APIResponse<NewWeeklyReport>>;
|
2024-03-17 22:20:34 +01:00
|
|
|
/**Gets a weekly report*/
|
|
|
|
getWeeklyReport(
|
2024-03-17 22:27:21 +01:00
|
|
|
username: string,
|
2024-03-17 22:20:34 +01:00
|
|
|
projectName: string,
|
2024-03-17 22:27:21 +01:00
|
|
|
week: string,
|
2024-03-17 22:35:51 +01:00
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<NewWeeklyReport>>;
|
2024-03-13 17:06:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Export an instance of the API
|
|
|
|
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 {
|
|
|
|
const response = await fetch("/api/userdelete", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
},
|
|
|
|
body: JSON.stringify(username),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
return { success: false, message: "Failed to remove user" };
|
|
|
|
} else {
|
|
|
|
const data = (await response.json()) as User;
|
|
|
|
return { success: true, data };
|
|
|
|
}
|
|
|
|
} 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-16 17:42:28 +01:00
|
|
|
async createProject(
|
|
|
|
project: NewProject,
|
2024-03-17 14:44:25 +01:00
|
|
|
token: string,
|
2024-03-16 17:42:28 +01:00
|
|
|
): Promise<APIResponse<Project>> {
|
|
|
|
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 {
|
|
|
|
const data = (await response.json()) as Project;
|
|
|
|
return { success: true, data };
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return { success: false, message: "Failed to create project" };
|
|
|
|
}
|
2024-03-13 17:52:56 +01:00
|
|
|
},
|
2024-03-13 20:56:47 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
async getUserProjects(token: string): Promise<APIResponse<Project[]>> {
|
|
|
|
try {
|
|
|
|
const response = await fetch("/api/getUserProjects", {
|
|
|
|
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,
|
|
|
|
message: "Failed to get user projects",
|
|
|
|
});
|
2024-03-17 12:35:53 +01:00
|
|
|
}
|
|
|
|
},
|
2024-03-17 14:44:25 +01:00
|
|
|
|
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-17 22:35:51 +01:00
|
|
|
): Promise<APIResponse<NewWeeklyReport>> {
|
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-17 22:35:51 +01:00
|
|
|
const data = (await response.json()) as NewWeeklyReport;
|
2024-03-17 16:11:03 +01:00
|
|
|
return { success: true, 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-03-17 22:35:51 +01:00
|
|
|
async getWeeklyReport(
|
|
|
|
username: string,
|
|
|
|
projectName: string,
|
|
|
|
week: string,
|
|
|
|
token: string,
|
|
|
|
): Promise<APIResponse<NewWeeklyReport>> {
|
|
|
|
try {
|
|
|
|
const response = await fetch("/api/getWeeklyReport", {
|
|
|
|
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" };
|
|
|
|
} else {
|
|
|
|
const data = (await response.json()) as NewWeeklyReport;
|
|
|
|
return { success: true, data };
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return { success: false, message: "Failed to get weekly report" };
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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-13 17:06:26 +01:00
|
|
|
};
|