Skip to content

Commit f21a37e

Browse files
author
Igor Ramadas
committed
The DELETE recipe endpoint does not have a body, so we should use the existing recipe data instead in this case.
1 parent a941e7b commit f21a37e

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/routes/api/users.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -449,11 +449,19 @@ const routeUserRecipe = async (req: any, res: any) => {
449449
const validated = await auth.requestValidator(req, res)
450450
if (!validated) return
451451

452-
let recipe: RecipeData = req.body
453-
const recipeId = req.params.recipeId || recipe.id
454452
const user: UserData = await users.getById(userId)
455-
const asJson = recipe ? recipe["asJson"] || false : false
453+
if (!user) {
454+
return webserver.renderError(req, res, `User ${userId} not found`, 404)
455+
}
456+
457+
// Get and validate the recipe data.
458+
const recipeId: string = req.params.recipeId || req.body?.id
459+
const recipe: RecipeData = req.body?.title ? req.body : user.recipes[recipeId]
460+
if (!recipe) {
461+
return webserver.renderError(req, res, `Recipe ${recipeId} not found`, 404)
462+
}
456463

464+
const asJson = recipe ? recipe["asJson"] || false : false
457465
if (asJson) {
458466
delete recipe["asJson"]
459467
}
@@ -471,11 +479,6 @@ const routeUserRecipe = async (req: any, res: any) => {
471479
}
472480
}
473481

474-
// User not found?
475-
if (!user) {
476-
return webserver.renderError(req, res, `User ${userId} not found`, 404)
477-
}
478-
479482
// If 2 conditions or less, we don't need to set a value for samePropertyOp, as we only use the default op.
480483
if (recipe.conditions?.length <= 2 && recipe.samePropertyOp) {
481484
delete recipe.samePropertyOp

0 commit comments

Comments
 (0)