mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-14 16:44:07 -08:00
⚡ Add tag update endpoint
This commit is contained in:
parent
f7a13b891b
commit
e53c3211d5
|
@ -18,6 +18,9 @@ import {
|
|||
IDeferredPromise,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
UpdateResult
|
||||
} from 'typeorm';
|
||||
|
||||
import * as PCancelable from 'p-cancelable';
|
||||
import { Repository } from 'typeorm';
|
||||
|
@ -75,13 +78,17 @@ export interface IWorkflowBase extends IWorkflowBaseWorkflow {
|
|||
id?: number | string;
|
||||
}
|
||||
|
||||
export interface ITagDb {
|
||||
id?: number | string; // auto-generated so unneeded in POST payload
|
||||
export interface ITagBase {
|
||||
name: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
export interface ITagDb extends ITagBase {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface ITagResponse extends UpdateResult {}
|
||||
|
||||
// Almost identical to editor-ui.Interfaces.ts
|
||||
export interface IWorkflowDb extends IWorkflowBase {
|
||||
|
|
|
@ -108,7 +108,9 @@ import * as querystring from 'querystring';
|
|||
import * as Queue from '../src/Queue';
|
||||
import { OptionsWithUrl } from 'request-promise-native';
|
||||
import { Registry } from 'prom-client';
|
||||
import { ITagDb } from './Interfaces';
|
||||
import { ITagBase, ITagDb, ITagResponse } from './Interfaces';
|
||||
|
||||
import * as TagHelpers from './TagHelpers';
|
||||
|
||||
class App {
|
||||
|
||||
|
@ -727,26 +729,17 @@ class App {
|
|||
|
||||
// Creates a tag
|
||||
this.app.post(`/${this.restEndpoint}/tags`, ResponseHelper.send(async (req: express.Request, res: express.Response): Promise<ITagDb> => {
|
||||
const { tagName } = req.body as { tagName: string };
|
||||
const { name } = req.body;
|
||||
const existingName = await TagHelpers.nameExists(name);
|
||||
|
||||
const alreadyExists = async (tagName: string) => {
|
||||
const findQuery = { where: { name: tagName } } as FindOneOptions;
|
||||
const result = await Db.collections.Tag!.findOne(findQuery);
|
||||
return result !== undefined;
|
||||
};
|
||||
|
||||
const hasInvalidLength = (tagName: string) => !tagName.length || tagName.length > 24;
|
||||
|
||||
if (await alreadyExists(tagName)) {
|
||||
if (existingName) {
|
||||
throw new ResponseHelper.ResponseError('Tag name already exists.', undefined, 400);
|
||||
}
|
||||
|
||||
if (hasInvalidLength(tagName)) {
|
||||
throw new ResponseHelper.ResponseError('Tag name must be 1 to 24 characters long.', undefined, 400);
|
||||
}
|
||||
TagHelpers.validateLength(name);
|
||||
|
||||
const newTag: ITagDb = {
|
||||
name: tagName,
|
||||
const newTag: ITagBase = {
|
||||
name,
|
||||
createdAt: this.getCurrentDate(),
|
||||
updatedAt: this.getCurrentDate(),
|
||||
};
|
||||
|
@ -760,6 +753,26 @@ class App {
|
|||
return true;
|
||||
}));
|
||||
|
||||
// Updates an existing tag
|
||||
this.app.patch(`/${this.restEndpoint}/tags/:id`, ResponseHelper.send(async (req: express.Request, res: express.Response): Promise<ITagResponse> => {
|
||||
const { name } = req.body;
|
||||
const { id } = req.params;
|
||||
const existingId = await TagHelpers.idExists(id);
|
||||
|
||||
if (!existingId) {
|
||||
throw new ResponseHelper.ResponseError(`Tag with the ID "${id}" does not exist.`, undefined, 400);
|
||||
}
|
||||
|
||||
TagHelpers.validateLength(name);
|
||||
|
||||
const updatedTag: Partial<ITagDb> = {
|
||||
name,
|
||||
updatedAt: this.getCurrentDate(),
|
||||
};
|
||||
|
||||
return await Db.collections.Tag!.update(id, updatedTag);
|
||||
}));
|
||||
|
||||
// Returns parameter values which normally get loaded from an external API or
|
||||
// get generated dynamically
|
||||
this.app.get(`/${this.restEndpoint}/node-parameter-options`, ResponseHelper.send(async (req: express.Request, res: express.Response): Promise<INodePropertyOptions[]> => {
|
||||
|
|
22
packages/cli/src/TagHelpers.ts
Normal file
22
packages/cli/src/TagHelpers.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
import { FindOneOptions } from "typeorm";
|
||||
import { Db, ResponseHelper } from ".";
|
||||
|
||||
export async function nameExists(name: string) {
|
||||
const findQuery = { where: { name } } as FindOneOptions;
|
||||
const result = await Db.collections.Tag!.findOne(findQuery);
|
||||
|
||||
return result !== undefined;
|
||||
}
|
||||
|
||||
export async function idExists(id: string) {
|
||||
const findQuery = { where: { id } } as FindOneOptions;
|
||||
const result = await Db.collections.Tag!.findOne(findQuery);
|
||||
|
||||
return result !== undefined;
|
||||
}
|
||||
|
||||
export function validateLength(name: string) {
|
||||
if (name.length < 0 || name.length > 24) {
|
||||
throw new ResponseHelper.ResponseError('Tag name must be 1 to 24 characters long.', undefined, 400);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue