use-exists-function-for-folder-and-improve-error-handling (#5464)

* update: removing try and catch for checkExistalready fn and use exist function folder instead of get folder for folder kind

* update .sqlx
This commit is contained in:
dieriba
2025-03-10 12:38:11 +01:00
committed by GitHub
parent 14d7a0daae
commit ba0cfb199c
4 changed files with 108 additions and 60 deletions
@@ -15,7 +15,7 @@
]
},
"nullable": [
true
null
]
},
"hash": "ddf2eccb78a310ed00c7d8b9c3f05d394a7cbcf0038c72a78add5c7b02ef5927"
+17
View File
@@ -10033,6 +10033,23 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/Folder"
/w/{workspace}/folders/exists/{name}:
get:
summary: exists folder
operationId: existsFolder
tags:
- folder
parameters:
- $ref: "#/components/parameters/WorkspaceId"
- $ref: "#/components/parameters/Name"
responses:
"200":
description: folder exists
content:
application/json:
schema:
type: boolean
/w/{workspace}/folders/getusage/{name}:
get:
+17
View File
@@ -42,6 +42,7 @@ pub fn workspaced_service() -> Router {
.route("/listnames", get(list_foldernames))
.route("/create", post(create_folder))
.route("/get/:name", get(get_folder))
.route("/exists/:name", get(exists_folder))
.route("/update/:name", post(update_folder))
.route("/getusage/:name", get(get_folder_usage))
.route("/delete/:name", delete(delete_folder))
@@ -426,6 +427,22 @@ async fn get_folder(
Ok(Json(folder))
}
async fn exists_folder(
Extension(db): Extension<DB>,
Path((w_id, name)): Path<(String, String)>,
) -> JsonResult<bool> {
let exists = sqlx::query_scalar!(
"SELECT EXISTS(SELECT 1 FROM folder WHERE name = $1 AND workspace_id = $2)",
name,
w_id
)
.fetch_one(&db)
.await?
.unwrap_or(false);
Ok(Json(exists))
}
#[derive(Serialize)]
struct FolderUsage {
pub scripts: i64,
@@ -30,6 +30,7 @@
type AdditionalInformation,
type Kind
} from '$lib/utils_deployable'
import type { TriggerKind } from './triggers'
const dispatch = createEventDispatcher()
@@ -174,67 +175,80 @@
}
async function checkAlreadyExists(kind: Kind, path: string): Promise<boolean> {
let exists = true
try {
if (kind == 'flow') {
exists = await FlowService.existsFlowByPath({
workspace: workspaceToDeployTo!,
path: path
})
} else if (kind == 'script') {
exists = await ScriptService.existsScriptByPath({
workspace: workspaceToDeployTo!,
path: path
})
} else if (kind == 'app') {
exists = await AppService.existsApp({
workspace: workspaceToDeployTo!,
path: path
})
} else if (kind == 'raw_app') {
exists = await RawAppService.existsRawApp({
workspace: workspaceToDeployTo!,
path: path
})
} else if (kind == 'variable') {
exists = await VariableService.existsVariable({
workspace: workspaceToDeployTo!,
path: path
})
} else if (kind == 'resource') {
exists = await ResourceService.existsResource({
workspace: workspaceToDeployTo!,
path: path
})
} else if (kind == 'schedule') {
exists = await ScheduleService.existsSchedule({
workspace: workspaceToDeployTo!,
path: path
})
} else if (kind == 'resource_type') {
exists = await ResourceService.existsResourceType({
workspace: workspaceToDeployTo!,
path: path
})
} else if (kind == 'folder') {
await FolderService.getFolder({
workspace: workspaceToDeployTo!,
name: path
})
} else if (kind === 'trigger') {
if (additionalInformation?.triggers) {
exists = await existsTrigger(
{ workspace: workspaceToDeployTo!, path },
additionalInformation.triggers.kind
)
} else {
throw new Error('Missing triggers kind')
}
let exists: boolean
if (kind == 'flow') {
exists = await FlowService.existsFlowByPath({
workspace: workspaceToDeployTo!,
path: path
})
} else if (kind == 'script') {
exists = await ScriptService.existsScriptByPath({
workspace: workspaceToDeployTo!,
path: path
})
} else if (kind == 'app') {
exists = await AppService.existsApp({
workspace: workspaceToDeployTo!,
path: path
})
} else if (kind == 'raw_app') {
exists = await RawAppService.existsRawApp({
workspace: workspaceToDeployTo!,
path: path
})
} else if (kind == 'variable') {
exists = await VariableService.existsVariable({
workspace: workspaceToDeployTo!,
path: path
})
} else if (kind == 'resource') {
exists = await ResourceService.existsResource({
workspace: workspaceToDeployTo!,
path: path
})
} else if (kind == 'schedule') {
exists = await ScheduleService.existsSchedule({
workspace: workspaceToDeployTo!,
path: path
})
} else if (kind == 'resource_type') {
exists = await ResourceService.existsResourceType({
workspace: workspaceToDeployTo!,
path: path
})
} else if (kind == 'folder') {
exists = await FolderService.existsFolder({
workspace: workspaceToDeployTo!,
name: path
})
} else if (kind === 'trigger') {
const triggersKind: TriggerKind[] = [
'kafka',
'mqtt',
'nats',
'postgres',
'routes',
'schedules',
'sqs',
'websockets'
]
if (
additionalInformation?.triggers &&
triggersKind.includes(additionalInformation.triggers.kind)
) {
exists = await existsTrigger(
{ workspace: workspaceToDeployTo!, path },
additionalInformation.triggers.kind
)
} else {
throw new Error(`Unknown kind ${kind}`)
throw new Error(
`Unexpected triggers kind, expected one of: '${triggersKind.join(', ')}' got: ${
additionalInformation?.triggers?.kind
}`
)
}
} catch (error) {
exists = false
} else {
throw new Error(`Unknown kind ${kind}`)
}
return exists
}