Skip to content

Commit

Permalink
returning 404 when scenario does not exist (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeknovy authored Sep 6, 2023
1 parent 3bf497b commit 43509f7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/server/controllers/item/create-item-async-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 9 additions & 6 deletions src/server/controllers/item/shared/upsert-scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 43509f7

Please sign in to comment.