From 43509f7de008245bf4240ec5a80e6887fb9b8447 Mon Sep 17 00:00:00 2001 From: Ludek Novy <13610612+ludeknovy@users.noreply.github.com> Date: Wed, 6 Sep 2023 19:26:38 +0200 Subject: [PATCH] returning 404 when scenario does not exist (#253) --- .../item/create-item-async-controller.ts | 5 ++++- .../controllers/item/shared/upsert-scenario.ts | 15 +++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/server/controllers/item/create-item-async-controller.ts b/src/server/controllers/item/create-item-async-controller.ts index 5312c59c..72194e89 100644 --- a/src/server/controllers/item/create-item-async-controller.ts +++ b/src/server/controllers/item/create-item-async-controller.ts @@ -15,7 +15,10 @@ export const createItemAsyncController = async (req: IGetUserAuthInfoRequest, re logger.info(`Creating new item for scenario: ${scenarioName}`) try { - await upsertScenario(projectName, scenarioName) + const scenarioExists = await upsertScenario(projectName, scenarioName) + if (!scenarioExists) { + return res.status(StatusCode.NotFound).json({ message: "scenario not found" }) + } const item = await db.one(createNewItem( scenarioName, diff --git a/src/server/controllers/item/shared/upsert-scenario.ts b/src/server/controllers/item/shared/upsert-scenario.ts index 94c4cc97..ce0475c7 100644 --- a/src/server/controllers/item/shared/upsert-scenario.ts +++ b/src/server/controllers/item/shared/upsert-scenario.ts @@ -4,13 +4,16 @@ import { createNewScenario, getScenario } from "../../../queries/scenario" import { logger } from "../../../../logger" export const upsertScenario = async (projectName, scenarioName) => { + const scenario = await db.oneOrNone(getScenario(projectName, scenarioName)) + if (scenario) { + // scenario already exists + return true + } const project = await db.one(getProject(projectName)) if (project.upsertScenario) { - const scenario = await db.oneOrNone(getScenario(projectName, scenarioName)) - if (!scenario) { - logger.info(`Upserting scenario "${scenarioName}" into project "${projectName}"`) - - await db.query(createNewScenario(projectName, scenarioName)) - } + logger.info(`Creating new scenario "${scenarioName}" into project "${projectName}"`) + await db.query(createNewScenario(projectName, scenarioName)) + return true } + return false }