2023-06-09 09:19:40 +00:00
|
|
|
import { APIClient } from "./ApiClient";
|
|
|
|
|
|
|
|
export interface User {
|
|
|
|
id: number;
|
|
|
|
name: string;
|
|
|
|
email: string;
|
|
|
|
time_create: number;
|
|
|
|
time_activate: number;
|
|
|
|
active: boolean;
|
|
|
|
admin: boolean;
|
|
|
|
has_password: boolean;
|
|
|
|
}
|
|
|
|
|
2023-06-14 13:23:23 +00:00
|
|
|
export enum ReplacePasswordResponse {
|
|
|
|
Error,
|
|
|
|
Success,
|
|
|
|
InvalidOldPassword,
|
|
|
|
InvalidNewPassword,
|
|
|
|
TooManyRequests,
|
|
|
|
}
|
|
|
|
|
2023-06-09 09:19:40 +00:00
|
|
|
export class UserApi {
|
|
|
|
/**
|
|
|
|
* Get current user information
|
|
|
|
*/
|
|
|
|
static async GetUserInfo(): Promise<User> {
|
|
|
|
return (
|
|
|
|
await APIClient.exec({
|
|
|
|
uri: "/user/info",
|
|
|
|
method: "GET",
|
|
|
|
})
|
|
|
|
).data;
|
|
|
|
}
|
2023-06-14 12:14:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update user profile
|
|
|
|
*/
|
|
|
|
static async UpdateProfile(name: string): Promise<void> {
|
|
|
|
await APIClient.exec({
|
|
|
|
uri: "/user/update_profile",
|
|
|
|
method: "POST",
|
|
|
|
jsonData: {
|
|
|
|
name: name,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2023-06-14 13:23:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace user password
|
|
|
|
*/
|
|
|
|
static async ReplacePassword(
|
|
|
|
oldPwd: string,
|
|
|
|
newPwd: string
|
|
|
|
): Promise<ReplacePasswordResponse> {
|
|
|
|
const res = await APIClient.exec({
|
|
|
|
uri: "/user/replace_password",
|
|
|
|
method: "POST",
|
|
|
|
jsonData: {
|
|
|
|
old_password: oldPwd,
|
|
|
|
new_password: newPwd,
|
|
|
|
},
|
|
|
|
allowFail: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (res.status >= 200 && res.status < 300)
|
|
|
|
return ReplacePasswordResponse.Success;
|
|
|
|
|
|
|
|
switch (res.status) {
|
|
|
|
case 400:
|
|
|
|
return ReplacePasswordResponse.InvalidNewPassword;
|
|
|
|
|
|
|
|
case 401:
|
|
|
|
return ReplacePasswordResponse.InvalidOldPassword;
|
|
|
|
|
|
|
|
case 429:
|
|
|
|
return ReplacePasswordResponse.TooManyRequests;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return ReplacePasswordResponse.Error;
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 14:12:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Request account deletion
|
|
|
|
*/
|
|
|
|
static async RequestAccountDeletion(): Promise<void> {
|
|
|
|
await APIClient.exec({
|
|
|
|
uri: "/user/request_delete",
|
|
|
|
method: "GET",
|
|
|
|
});
|
|
|
|
}
|
2023-06-09 09:19:40 +00:00
|
|
|
}
|