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 }