From 69dc3a6dd7b4c4e50702148c82054de3d20a7383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Mon, 12 Apr 2021 16:30:47 +0200 Subject: [PATCH] :zap: Add tags to /workflow --- packages/cli/src/Server.ts | 16 ++++++++++++++++ packages/cli/src/TagHelpers.ts | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index a2c38fa75a..7c808f5c31 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -499,6 +499,22 @@ class App { // Save the workflow in DB const result = await Db.collections.Workflow!.save(newWorkflowData); + const { tags } = req.body as { tags: string }; + + if (tags) { + const tagIds = tags.split(',').map(Number); + + for (const tagId of tagIds) { + await TagHelpers.validateId(tagId); + } + + await getConnection().createQueryBuilder() + .insert() + .into('workflows_tags') + .values(tags.split(',').map(tagId => ({ workflowId: result.id, tagId }))) + .execute(); + } + // Convert to response format in which the id is a string (result as IWorkflowBase as IWorkflowResponse).id = result.id.toString(); return result as IWorkflowBase as IWorkflowResponse; diff --git a/packages/cli/src/TagHelpers.ts b/packages/cli/src/TagHelpers.ts index d80dddc461..c0a23a368e 100644 --- a/packages/cli/src/TagHelpers.ts +++ b/packages/cli/src/TagHelpers.ts @@ -2,7 +2,7 @@ import { FindOneOptions, getConnection } from "typeorm"; import { Db, ResponseHelper } from "."; /** - * Validate whether a tag name exists so that it cannot be used for a create/update operation. + * Validate whether a tag name exists so that it cannot be used for a tag create or tag update operation. */ export async function validateName(name: string): Promise | never { const findQuery = { where: { name } } as FindOneOptions; @@ -14,7 +14,7 @@ export async function validateName(name: string): Promise | never { } /** - * Validate whether a tag ID exists so that it can be used for an update operation. + * Validate whether a tag ID exists so that it can be used for a workflow create or tag update operation. */ export async function validateId(id: number): Promise | never { const findQuery = { where: { id } } as FindOneOptions;