Skip to content

Commit

Permalink
Reinstate the usage of any use-any-unit (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgegebbett authored Apr 1, 2024
1 parent 2d3850c commit beff973
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 19 deletions.
19 changes: 18 additions & 1 deletion src/server/api/modules/grocy/procedures/createRecipeInGrocy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
UnignoredIngredient,
} from "~/server/api/modules/grocy/procedures/createRecipeInGrocySchema"
import { grocyFetch } from "~/server/api/modules/grocy/service/client"
import { getGrocyProducts } from "~/server/api/modules/grocy/service/getGrocyProducts"
import { deleteRecipe } from "~/server/api/modules/recipes/service/deleteRecipe"
import { protectedProcedure } from "~/server/api/trpc"
import normalizeUrl from "normalize-url"
Expand All @@ -17,6 +18,8 @@ export const createRecipeInGrocyProcedure = protectedProcedure
.mutation(async ({ input }) => {
let imageFilename: string | undefined = undefined

const grocyProducts = await getGrocyProducts()

if (input.imageUrl) {
const normalised = normalizeUrl(input.imageUrl, {
removeQueryParameters: true,
Expand Down Expand Up @@ -70,17 +73,27 @@ export const createRecipeInGrocyProcedure = protectedProcedure
const recipeId = z.coerce.string().parse(recipeJson.created_object_id)

const filteredIngredients = input.ingredients.filter(
(a): a is UnignoredIngredient => a.ignored === false
(a): a is UnignoredIngredient => !a.ignored
)

for (const ingredient of filteredIngredients) {
logger.info(ingredient, `Creating ingredient [${ingredient.scrapedName}]`)

const grocyProduct = grocyProducts.find(
(a) => a.id === ingredient.productId
)

if (!grocyProduct) continue

const useAnyUnit: "1" | "0" =
ingredient.unitId === grocyProduct.qu_id_stock ? "0" : "1"

const body = {
recipe_id: recipeId,
product_id: ingredient.productId,
amount: ingredient.amount,
qu_id: ingredient.unitId,
only_check_single_unit_in_stock: useAnyUnit,
}

const ingredientResponse = await grocyFetch("/objects/recipes_pos", {
Expand All @@ -91,6 +104,10 @@ export const createRecipeInGrocyProcedure = protectedProcedure

const ingredientJson = await ingredientResponse.json()

if (!ingredientResponse.ok) {
throw new Error(ingredientJson.error_message ?? "An error occurred")
}

logger.info(ingredientJson, "Ingredient created")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const IgnoredIngredientSchema = z.object({

const UnignoredIngredientSchema = z.object({
productId: z.string(),
amount: z.number(),
amount: z.coerce.number(),
unitId: z.string(),
scrapedName: z.string(),
ignored: z.literal(false),
Expand Down
18 changes: 3 additions & 15 deletions src/server/api/modules/grocy/procedures/getGrocyProducts.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import { grocyFetch } from "~/server/api/modules/grocy/service/client"
import { getGrocyProducts } from "~/server/api/modules/grocy/service/getGrocyProducts"
import { protectedProcedure } from "~/server/api/trpc"
import z from "zod"

const grocyProductSchema = z.object({
id: z.coerce.string(),
name: z.string(),
qu_id_stock: z.coerce.string(),
})

export const getGrocyProductsProcedure = protectedProcedure.query(async () => {
const prods = await grocyFetch("/objects/products", { cache: "no-cache" })

const json = await prods.json()

return grocyProductSchema.array().parse(json)
})
export const getGrocyProductsProcedure =
protectedProcedure.query(getGrocyProducts)
18 changes: 18 additions & 0 deletions src/server/api/modules/grocy/service/getGrocyProducts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { grocyFetch } from "~/server/api/modules/grocy/service/client"
import z from "zod"

const grocyProductSchema = z.object({
id: z.coerce.string(),
name: z.string(),
qu_id_stock: z.coerce.string(),
})

type GrocyProduct = z.infer<typeof grocyProductSchema>

export const getGrocyProducts = async (): Promise<GrocyProduct[]> => {
const prods = await grocyFetch("/objects/products", { cache: "no-cache" })

const json = await prods.json()

return grocyProductSchema.array().parse(json)
}
38 changes: 36 additions & 2 deletions src/trpc/react.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
"use client"

import { useState } from "react"
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
import {
MutationCache,
QueryCache,
QueryClient,
QueryClientProvider,
} from "@tanstack/react-query"
import { loggerLink, unstable_httpBatchStreamLink } from "@trpc/client"
import { createTRPCReact } from "@trpc/react-query"
import { type AppRouter } from "~/server/api/root"
import { toast } from "sonner"

import { getUrl, transformer } from "./shared"

Expand All @@ -14,7 +20,35 @@ export function TRPCReactProvider(props: {
children: React.ReactNode
cookies: string
}) {
const [queryClient] = useState(() => new QueryClient())
const [queryClient] = useState(
() =>
new QueryClient({
queryCache: new QueryCache({
onError: (error, query) => {
if (query.state.data !== undefined) {
const title =
(query.meta?.title as string) ?? "Something went wrong"
const description =
(query.meta?.description as string) ??
(error instanceof Error && `${error.message}`)

toast.error(title, { description })
}
},
}),
mutationCache: new MutationCache({
onError: (error, _, __, mutation) => {
const title =
(mutation.meta?.title as string) ?? "Something went wrong"
const description =
(mutation.meta?.description as string) ??
(error instanceof Error && `${error.message}`)

toast.error(title, { description })
},
}),
})
)

const [trpcClient] = useState(() =>
api.createClient({
Expand Down

0 comments on commit beff973

Please sign in to comment.