| 
									
										
										
										
											2024-09-22 09:59:20 -05:00
										 |  |  | import { BaseCRUDAPIReadOnly } from "../base/base-clients"; | 
					
						
							| 
									
										
										
										
											2024-10-11 19:36:26 -05:00
										 |  |  | import { PaginationData } from "../types/non-generated"; | 
					
						
							|  |  |  | import { QueryValue } from "../base/route"; | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  | import { UserOut } from "~/lib/api/types/user"; | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   HouseholdInDB, | 
					
						
							|  |  |  |   HouseholdStatistics, | 
					
						
							|  |  |  |   ReadHouseholdPreferences, | 
					
						
							|  |  |  |   SetPermissions, | 
					
						
							|  |  |  |   UpdateHouseholdPreferences, | 
					
						
							|  |  |  |   CreateInviteToken, | 
					
						
							|  |  |  |   ReadInviteToken, | 
					
						
							| 
									
										
										
										
											2024-09-22 09:59:20 -05:00
										 |  |  |   HouseholdSummary, | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |   HouseholdRecipeSummary, | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  | } from "~/lib/api/types/household"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const prefix = "/api"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const routes = { | 
					
						
							| 
									
										
										
										
											2024-09-22 09:59:20 -05:00
										 |  |  |   households: `${prefix}/groups/households`, | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  |   householdsSelf: `${prefix}/households/self`, | 
					
						
							|  |  |  |   members: `${prefix}/households/members`, | 
					
						
							|  |  |  |   permissions: `${prefix}/households/permissions`, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   preferences: `${prefix}/households/preferences`, | 
					
						
							|  |  |  |   statistics: `${prefix}/households/statistics`, | 
					
						
							|  |  |  |   invitation: `${prefix}/households/invitations`, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-22 09:59:20 -05:00
										 |  |  |   householdsId: (id: string | number) => `${prefix}/groups/households/${id}`, | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |   householdsSelfRecipesSlug: (recipeSlug: string) =>  `${prefix}/households/self/recipes/${recipeSlug}`, | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-22 09:59:20 -05:00
										 |  |  | export class HouseholdAPI extends BaseCRUDAPIReadOnly<HouseholdSummary> { | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  |   baseRoute = routes.households; | 
					
						
							|  |  |  |   itemRoute = routes.householdsId; | 
					
						
							| 
									
										
										
										
											2024-09-22 09:59:20 -05:00
										 |  |  |   /** Returns the Household Data for the Current User | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  |    */ | 
					
						
							|  |  |  |   async getCurrentUserHousehold() { | 
					
						
							|  |  |  |     return await this.requests.get<HouseholdInDB>(routes.householdsSelf); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |   async getCurrentUserHouseholdRecipe(recipeSlug: string) { | 
					
						
							|  |  |  |     return await this.requests.get<HouseholdRecipeSummary>(routes.householdsSelfRecipesSlug(recipeSlug)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  |   async getPreferences() { | 
					
						
							|  |  |  |     return await this.requests.get<ReadHouseholdPreferences>(routes.preferences); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async setPreferences(payload: UpdateHouseholdPreferences) { | 
					
						
							|  |  |  |     // TODO: This should probably be a patch request, which isn't offered by the API currently
 | 
					
						
							|  |  |  |     return await this.requests.put<ReadHouseholdPreferences, UpdateHouseholdPreferences>(routes.preferences, payload); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async createInvitation(payload: CreateInviteToken) { | 
					
						
							|  |  |  |     return await this.requests.post<ReadInviteToken>(routes.invitation, payload); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-11 19:36:26 -05:00
										 |  |  |   async fetchMembers(page = 1, perPage = -1, params = {} as Record<string, QueryValue>) { | 
					
						
							|  |  |  |     return await this.requests.get<PaginationData<UserOut>>(routes.members, { page, perPage, ...params }); | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async setMemberPermissions(payload: SetPermissions) { | 
					
						
							|  |  |  |     // TODO: This should probably be a patch request, which isn't offered by the API currently
 | 
					
						
							|  |  |  |     return await this.requests.put<UserOut, SetPermissions>(routes.permissions, payload); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async statistics() { | 
					
						
							|  |  |  |     return await this.requests.get<HouseholdStatistics>(routes.statistics); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |