2023-06-27 15:13:12 +00:00
|
|
|
import { APIClient } from "./ApiClient";
|
2023-08-16 10:17:04 +00:00
|
|
|
import { Couple } from "./CoupleApi";
|
2023-08-16 08:14:18 +00:00
|
|
|
import { Member } from "./MemberApi";
|
2023-06-27 15:13:12 +00:00
|
|
|
|
2023-07-07 16:57:43 +00:00
|
|
|
interface FamilyAPI {
|
2023-07-07 16:41:00 +00:00
|
|
|
user_id: number;
|
|
|
|
family_id: number;
|
|
|
|
name: string;
|
|
|
|
time_create: number;
|
|
|
|
is_admin: boolean;
|
|
|
|
invitation_code: string;
|
|
|
|
count_members: number;
|
|
|
|
count_admins: number;
|
|
|
|
}
|
2023-06-27 15:13:12 +00:00
|
|
|
|
2023-07-07 16:57:43 +00:00
|
|
|
export class Family implements FamilyAPI {
|
|
|
|
user_id: number;
|
|
|
|
family_id: number;
|
|
|
|
name: string;
|
|
|
|
time_create: number;
|
|
|
|
is_admin: boolean;
|
|
|
|
invitation_code: string;
|
|
|
|
count_members: number;
|
|
|
|
count_admins: number;
|
|
|
|
|
|
|
|
constructor(f: FamilyAPI) {
|
|
|
|
this.user_id = f.user_id;
|
|
|
|
this.family_id = f.family_id;
|
|
|
|
this.name = f.name;
|
|
|
|
this.time_create = f.time_create;
|
|
|
|
this.is_admin = f.is_admin;
|
|
|
|
this.invitation_code = f.invitation_code;
|
|
|
|
this.count_members = f.count_members;
|
|
|
|
this.count_admins = f.count_admins;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the current user user can leave a family
|
|
|
|
*/
|
|
|
|
get CanLeave(): boolean {
|
|
|
|
return !this.is_admin || this.count_admins > 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get application URL for family
|
|
|
|
*/
|
|
|
|
URL(uri?: string): string {
|
|
|
|
return `/family/${this.family_id}/${uri ?? ""}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get base family URL
|
|
|
|
*/
|
|
|
|
get BaseURL(): string {
|
|
|
|
return this.URL("");
|
|
|
|
}
|
2023-08-16 08:14:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get application URL for member page
|
|
|
|
*/
|
|
|
|
memberURL(member: Member, edit?: boolean): string {
|
|
|
|
return (
|
|
|
|
`/family/${this.family_id}/member/${member.id}` + (edit ? "/edit" : "")
|
|
|
|
);
|
|
|
|
}
|
2023-08-16 10:17:04 +00:00
|
|
|
|
2023-08-22 09:41:25 +00:00
|
|
|
/**
|
|
|
|
* Get family tree URL for member
|
|
|
|
*/
|
|
|
|
familyTreeURL(member: Member | number): string {
|
|
|
|
return `/family/${this.family_id}/tree/${
|
|
|
|
typeof member === "number" ? member : member.id
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
|
2023-08-16 10:17:04 +00:00
|
|
|
/**
|
|
|
|
* Get application URL for couple page
|
|
|
|
*/
|
|
|
|
coupleURL(member: Couple, edit?: boolean): string {
|
|
|
|
return (
|
|
|
|
`/family/${this.family_id}/couple/${member.id}` + (edit ? "/edit" : "")
|
|
|
|
);
|
|
|
|
}
|
2023-07-07 16:57:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 16:52:49 +00:00
|
|
|
export enum JoinFamilyResult {
|
|
|
|
TooManyRequests,
|
|
|
|
InvalidCode,
|
|
|
|
AlreadyMember,
|
|
|
|
Error,
|
|
|
|
Success,
|
|
|
|
}
|
|
|
|
|
2023-07-09 15:02:43 +00:00
|
|
|
export interface FamilyUser {
|
|
|
|
user_id: number;
|
|
|
|
family_id: number;
|
|
|
|
time_create: number;
|
|
|
|
is_admin: boolean;
|
|
|
|
user_name: string;
|
|
|
|
user_mail: string;
|
|
|
|
}
|
|
|
|
|
2023-06-27 15:13:12 +00:00
|
|
|
export class FamilyApi {
|
|
|
|
/**
|
|
|
|
* Create a new family
|
|
|
|
*/
|
|
|
|
static async CreateFamily(name: string): Promise<void> {
|
|
|
|
await APIClient.exec({
|
|
|
|
method: "POST",
|
|
|
|
uri: "/family/create",
|
|
|
|
jsonData: { name: name },
|
|
|
|
});
|
|
|
|
}
|
2023-06-27 16:52:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Join an existing family
|
|
|
|
*/
|
|
|
|
static async JoinFamily(code: string): Promise<JoinFamilyResult> {
|
|
|
|
const res = await APIClient.exec({
|
|
|
|
method: "POST",
|
|
|
|
uri: "/family/join",
|
|
|
|
allowFail: true,
|
|
|
|
jsonData: { code: code },
|
|
|
|
});
|
|
|
|
|
|
|
|
if (res.status >= 200 && res.status < 300) return JoinFamilyResult.Success;
|
|
|
|
|
|
|
|
switch (res.status) {
|
|
|
|
case 429:
|
|
|
|
return JoinFamilyResult.TooManyRequests;
|
|
|
|
case 404:
|
|
|
|
return JoinFamilyResult.InvalidCode;
|
|
|
|
case 409:
|
|
|
|
return JoinFamilyResult.AlreadyMember;
|
|
|
|
default:
|
|
|
|
return JoinFamilyResult.Error;
|
|
|
|
}
|
|
|
|
}
|
2023-07-04 17:05:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the list of families
|
|
|
|
*/
|
2023-07-07 16:41:00 +00:00
|
|
|
static async GetList(): Promise<Family[]> {
|
|
|
|
return (
|
|
|
|
await APIClient.exec({
|
|
|
|
method: "GET",
|
|
|
|
uri: "/family/list",
|
|
|
|
})
|
2023-07-07 16:57:43 +00:00
|
|
|
).data.map((f: FamilyAPI) => new Family(f));
|
2023-07-07 16:41:00 +00:00
|
|
|
}
|
|
|
|
|
2023-07-08 09:59:55 +00:00
|
|
|
/**
|
|
|
|
* Get information about a single family
|
|
|
|
*/
|
|
|
|
static async GetSingle(id: number): Promise<Family> {
|
|
|
|
const res = await APIClient.exec({
|
|
|
|
method: "GET",
|
|
|
|
uri: `/family/${id}`,
|
|
|
|
});
|
|
|
|
|
|
|
|
return new Family(res.data);
|
|
|
|
}
|
|
|
|
|
2023-07-07 16:41:00 +00:00
|
|
|
/**
|
|
|
|
* Attempt to leave a family
|
|
|
|
*/
|
|
|
|
static async LeaveFamily(id: number): Promise<void> {
|
|
|
|
await APIClient.exec({
|
|
|
|
method: "POST",
|
|
|
|
uri: `/family/${id}/leave`,
|
|
|
|
});
|
|
|
|
}
|
2023-07-08 14:31:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renew a family invitation code
|
|
|
|
*/
|
|
|
|
static async RenewInvitationCode(id: number): Promise<void> {
|
|
|
|
await APIClient.exec({
|
|
|
|
method: "POST",
|
|
|
|
uri: `/family/${id}/renew_invitation_code`,
|
|
|
|
});
|
|
|
|
}
|
2023-07-09 15:02:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the users of a family
|
|
|
|
*/
|
|
|
|
static async GetUsersList(id: number): Promise<FamilyUser[]> {
|
|
|
|
return (
|
|
|
|
await APIClient.exec({
|
|
|
|
method: "GET",
|
|
|
|
uri: `/family/${id}/users`,
|
|
|
|
})
|
|
|
|
).data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a user of the family
|
|
|
|
*/
|
|
|
|
static async UpdateUser(user: FamilyUser): Promise<void> {
|
|
|
|
await APIClient.exec({
|
|
|
|
method: "PATCH",
|
|
|
|
uri: `/family/${user.family_id}/user/${user.user_id}`,
|
|
|
|
jsonData: {
|
|
|
|
is_admin: user.is_admin,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2023-07-09 15:25:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a member from the family
|
|
|
|
*/
|
|
|
|
static async RemoveUser(user: FamilyUser): Promise<void> {
|
|
|
|
await APIClient.exec({
|
|
|
|
method: "DELETE",
|
|
|
|
uri: `/family/${user.family_id}/user/${user.user_id}`,
|
|
|
|
});
|
|
|
|
}
|
2023-07-12 15:44:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a family settings
|
|
|
|
*/
|
|
|
|
static async UpdateFamily(settings: {
|
|
|
|
id: number;
|
|
|
|
name: string;
|
|
|
|
}): Promise<void> {
|
|
|
|
await APIClient.exec({
|
|
|
|
method: "PATCH",
|
|
|
|
uri: `/family/${settings.id}`,
|
|
|
|
jsonData: {
|
|
|
|
name: settings.name,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2023-07-12 15:58:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a family
|
|
|
|
*/
|
|
|
|
static async DeleteFamily(family: Family) {
|
|
|
|
await APIClient.exec({
|
|
|
|
method: "DELETE",
|
|
|
|
uri: `/family/${family.family_id}`,
|
|
|
|
});
|
|
|
|
}
|
2023-07-07 16:41:00 +00:00
|
|
|
}
|