2021-10-24 00:42:20 +00:00
|
|
|
import { BaseCRUDAPI } from "../_base";
|
2021-10-05 04:16:37 +00:00
|
|
|
import { GroupInDB, UserOut } from "~/types/api-types/user";
|
2021-08-07 00:28:12 +00:00
|
|
|
|
|
|
|
const prefix = "/api";
|
|
|
|
|
|
|
|
const routes = {
|
2021-09-09 16:51:29 +00:00
|
|
|
groups: `${prefix}/admin/groups`,
|
2021-08-07 00:28:12 +00:00
|
|
|
groupsSelf: `${prefix}/groups/self`,
|
2021-09-02 05:39:40 +00:00
|
|
|
categories: `${prefix}/groups/categories`,
|
2021-10-05 04:16:37 +00:00
|
|
|
members: `${prefix}/groups/members`,
|
|
|
|
permissions: `${prefix}/groups/permissions`,
|
2021-08-07 00:28:12 +00:00
|
|
|
|
2021-09-06 06:05:29 +00:00
|
|
|
preferences: `${prefix}/groups/preferences`,
|
|
|
|
|
2021-09-09 16:51:29 +00:00
|
|
|
invitation: `${prefix}/groups/invitations`,
|
|
|
|
|
|
|
|
groupsId: (id: string | number) => `${prefix}/admin/groups/${id}`,
|
2021-08-07 00:28:12 +00:00
|
|
|
};
|
|
|
|
|
2021-09-02 05:39:40 +00:00
|
|
|
interface Category {
|
|
|
|
id: number;
|
|
|
|
name: string;
|
|
|
|
slug: string;
|
|
|
|
}
|
|
|
|
|
2021-08-07 00:28:12 +00:00
|
|
|
export interface CreateGroup {
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
2021-09-06 06:05:29 +00:00
|
|
|
export interface UpdatePreferences {
|
|
|
|
privateGroup: boolean;
|
|
|
|
firstDayOfWeek: number;
|
|
|
|
recipePublic: boolean;
|
|
|
|
recipeShowNutrition: boolean;
|
|
|
|
recipeShowAssets: boolean;
|
|
|
|
recipeLandscapeView: boolean;
|
|
|
|
recipeDisableComments: boolean;
|
|
|
|
recipeDisableAmount: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Preferences extends UpdatePreferences {
|
|
|
|
id: number;
|
|
|
|
group_id: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Group extends CreateGroup {
|
|
|
|
id: number;
|
|
|
|
preferences: Preferences;
|
|
|
|
}
|
|
|
|
|
2021-09-09 16:51:29 +00:00
|
|
|
export interface CreateInvitation {
|
|
|
|
uses: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Invitation {
|
|
|
|
group_id: number;
|
|
|
|
token: string;
|
|
|
|
uses_left: number;
|
|
|
|
}
|
|
|
|
|
2021-10-05 04:16:37 +00:00
|
|
|
export interface SetPermissions {
|
2022-01-09 06:15:23 +00:00
|
|
|
userId: string;
|
|
|
|
canInvite?: boolean;
|
|
|
|
canManage?: boolean;
|
|
|
|
canOrganize?: boolean;
|
2021-10-05 04:16:37 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 23:12:25 +00:00
|
|
|
export class GroupAPI extends BaseCRUDAPI<GroupInDB, CreateGroup> {
|
2021-08-07 00:28:12 +00:00
|
|
|
baseRoute = routes.groups;
|
|
|
|
itemRoute = routes.groupsId;
|
|
|
|
/** Returns the Group Data for the Current User
|
|
|
|
*/
|
|
|
|
async getCurrentUserGroup() {
|
2021-09-06 06:05:29 +00:00
|
|
|
return await this.requests.get<Group>(routes.groupsSelf);
|
2021-09-02 05:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async getCategories() {
|
|
|
|
return await this.requests.get<Category[]>(routes.categories);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setCategories(payload: Category[]) {
|
|
|
|
return await this.requests.put<Category[]>(routes.categories, payload);
|
2021-08-07 00:28:12 +00:00
|
|
|
}
|
2021-09-06 06:05:29 +00:00
|
|
|
|
|
|
|
async getPreferences() {
|
|
|
|
return await this.requests.get<Preferences>(routes.preferences);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setPreferences(payload: UpdatePreferences) {
|
2022-01-09 06:15:23 +00:00
|
|
|
// TODO: This should probably be a patch request, which isn't offered by the API currently
|
|
|
|
return await this.requests.put<Preferences, UpdatePreferences>(routes.preferences, payload);
|
2021-09-06 06:05:29 +00:00
|
|
|
}
|
2021-09-09 16:51:29 +00:00
|
|
|
|
|
|
|
async createInvitation(payload: CreateInvitation) {
|
|
|
|
return await this.requests.post<Invitation>(routes.invitation, payload);
|
|
|
|
}
|
2021-10-05 04:16:37 +00:00
|
|
|
|
|
|
|
async fetchMembers() {
|
|
|
|
return await this.requests.get<UserOut[]>(routes.members);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setMemberPermissions(payload: SetPermissions) {
|
2022-01-09 06:15:23 +00:00
|
|
|
// TODO: This should probably be a patch request, which isn't offered by the API currently
|
|
|
|
return await this.requests.put<Permissions, SetPermissions>(routes.permissions, payload);
|
2021-10-05 04:16:37 +00:00
|
|
|
}
|
2021-08-07 00:28:12 +00:00
|
|
|
}
|