| 
									
										
										
										
											2021-10-23 16:42:20 -08:00
										 |  |  | import { BaseCRUDAPI } from "../_base"; | 
					
						
							| 
									
										
										
										
											2021-10-04 20:16:37 -08:00
										 |  |  | import { GroupInDB, UserOut } from "~/types/api-types/user"; | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const prefix = "/api"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const routes = { | 
					
						
							| 
									
										
										
										
											2021-09-09 08:51:29 -08:00
										 |  |  |   groups: `${prefix}/admin/groups`, | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  |   groupsSelf: `${prefix}/groups/self`, | 
					
						
							| 
									
										
										
										
											2021-09-01 21:39:40 -08:00
										 |  |  |   categories: `${prefix}/groups/categories`, | 
					
						
							| 
									
										
										
										
											2021-10-04 20:16:37 -08:00
										 |  |  |   members: `${prefix}/groups/members`, | 
					
						
							|  |  |  |   permissions: `${prefix}/groups/permissions`, | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-05 22:05:29 -08:00
										 |  |  |   preferences: `${prefix}/groups/preferences`, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 08:51:29 -08:00
										 |  |  |   invitation: `${prefix}/groups/invitations`, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   groupsId: (id: string | number) => `${prefix}/admin/groups/${id}`, | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-01 21:39:40 -08:00
										 |  |  | interface Category { | 
					
						
							|  |  |  |   id: number; | 
					
						
							|  |  |  |   name: string; | 
					
						
							|  |  |  |   slug: string; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  | export interface CreateGroup { | 
					
						
							|  |  |  |   name: string; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-05 22:05:29 -08: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 08:51:29 -08:00
										 |  |  | export interface CreateInvitation { | 
					
						
							|  |  |  |   uses: number; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export interface Invitation { | 
					
						
							|  |  |  |   group_id: number; | 
					
						
							|  |  |  |   token: string; | 
					
						
							|  |  |  |   uses_left: number; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-04 20:16:37 -08:00
										 |  |  | export interface SetPermissions { | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |   userId: string; | 
					
						
							|  |  |  |   canInvite?: boolean; | 
					
						
							|  |  |  |   canManage?: boolean; | 
					
						
							|  |  |  |   canOrganize?: boolean; | 
					
						
							| 
									
										
										
										
											2021-10-04 20:16:37 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  | export class GroupAPI extends BaseCRUDAPI<GroupInDB, CreateGroup> { | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  |   baseRoute = routes.groups; | 
					
						
							|  |  |  |   itemRoute = routes.groupsId; | 
					
						
							|  |  |  |   /** Returns the Group Data for the Current User | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async getCurrentUserGroup() { | 
					
						
							| 
									
										
										
										
											2021-09-05 22:05:29 -08:00
										 |  |  |     return await this.requests.get<Group>(routes.groupsSelf); | 
					
						
							| 
									
										
										
										
											2021-09-01 21:39:40 -08: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-06 16:28:12 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-09-05 22:05:29 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   async getPreferences() { | 
					
						
							|  |  |  |     return await this.requests.get<Preferences>(routes.preferences); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async setPreferences(payload: UpdatePreferences) { | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01: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-05 22:05:29 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-09-09 08:51:29 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   async createInvitation(payload: CreateInvitation) { | 
					
						
							|  |  |  |     return await this.requests.post<Invitation>(routes.invitation, payload); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-10-04 20:16:37 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   async fetchMembers() { | 
					
						
							|  |  |  |     return await this.requests.get<UserOut[]>(routes.members); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async setMemberPermissions(payload: SetPermissions) { | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01: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-04 20:16:37 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  | } |