Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PATCH /projects/:projectId/folders/:folderId endpoint to allow moving folder (no-changelog) #13574

Open
wants to merge 2 commits into
base: ado-3281-be-update-endpoint-that-return-workflows-and-folders-to-also
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ describe('UpdateFolderDto', () => {
tagIds: [],
},
},
{
name: 'string parentFolderId',
request: {
parentFolderId: 'test',
},
},
])('should validate $name', ({ request }) => {
const result = UpdateFolderDto.safeParse(request);
expect(result.success).toBe(true);
Expand Down Expand Up @@ -50,6 +56,13 @@ describe('UpdateFolderDto', () => {
},
expectedErrorPath: ['tagIds'],
},
{
name: 'non string parentFolderId',
request: {
parentFolderId: 0,
},
expectedErrorPath: ['parentFolderId'],
},
])('should fail validation for $name', ({ request, expectedErrorPath }) => {
const result = UpdateFolderDto.safeParse(request);

Expand Down
4 changes: 2 additions & 2 deletions packages/@n8n/api-types/src/dto/folders/create-folder.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Z } from 'zod-class';

import { folderNameSchema, folderId } from '../../schemas/folder.schema';
import { folderNameSchema, folderIdSchema } from '../../schemas/folder.schema';

export class CreateFolderDto extends Z.class({
name: folderNameSchema,
parentFolderId: folderId.optional(),
parentFolderId: folderIdSchema.optional(),
}) {}
4 changes: 2 additions & 2 deletions packages/@n8n/api-types/src/dto/folders/delete-folder.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Z } from 'zod-class';

import { folderId } from '../../schemas/folder.schema';
import { folderIdSchema } from '../../schemas/folder.schema';

export class DeleteFolderDto extends Z.class({
transferToFolderId: folderId.optional(),
transferToFolderId: folderIdSchema.optional(),
}) {}
3 changes: 2 additions & 1 deletion packages/@n8n/api-types/src/dto/folders/update-folder.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { z } from 'zod';
import { Z } from 'zod-class';

import { folderNameSchema } from '../../schemas/folder.schema';
import { folderNameSchema, folderIdSchema } from '../../schemas/folder.schema';
export class UpdateFolderDto extends Z.class({
name: folderNameSchema.optional(),
tagIds: z.array(z.string().max(24)).optional(),
parentFolderId: folderIdSchema.optional(),
}) {}
2 changes: 1 addition & 1 deletion packages/@n8n/api-types/src/schemas/folder.schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { z } from 'zod';

export const folderNameSchema = z.string().trim().min(1).max(128);
export const folderId = z.string().max(36);
export const folderIdSchema = z.string().max(36);
8 changes: 7 additions & 1 deletion packages/cli/src/controllers/folder.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {
UpdateFolderDto,
} from '@n8n/api-types';
import { Response } from 'express';
import { UserError } from 'n8n-workflow';

import { Post, RestController, ProjectScope, Body, Get, Patch, Delete, Query } from '@/decorators';
import { FolderNotFoundError } from '@/errors/folder-not-found.error';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { InternalServerError } from '@/errors/response-errors/internal-server.error';
import { NotFoundError } from '@/errors/response-errors/not-found.error';
import type { ListQuery } from '@/requests';
Expand Down Expand Up @@ -69,6 +71,8 @@ export class ProjectController {
} catch (e) {
if (e instanceof FolderNotFoundError) {
throw new NotFoundError(e.message);
} else if (e instanceof UserError) {
throw new BadRequestError(e.message);
}
throw new InternalServerError(undefined, e);
}
Expand All @@ -79,7 +83,7 @@ export class ProjectController {
async deleteFolder(
req: AuthenticatedRequest<{ projectId: string; folderId: string }>,
_res: Response,
@Body payload: DeleteFolderDto,
@Query payload: DeleteFolderDto,
) {
const { projectId, folderId } = req.params;

Expand All @@ -88,6 +92,8 @@ export class ProjectController {
} catch (e) {
if (e instanceof FolderNotFoundError) {
throw new NotFoundError(e.message);
} else if (e instanceof UserError) {
throw new BadRequestError(e.message);
}
throw new InternalServerError(undefined, e);
}
Expand Down
26 changes: 25 additions & 1 deletion packages/cli/src/services/folder.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { CreateFolderDto, DeleteFolderDto, UpdateFolderDto } from '@n8n/api
import { Service } from '@n8n/di';
// eslint-disable-next-line n8n-local-rules/misplaced-n8n-typeorm-import
import type { EntityManager } from '@n8n/typeorm';
import { UserError } from 'n8n-workflow';
import { PROJECT_ROOT } from 'n8n-workflow/src/Constants';

import { Folder } from '@/databases/entities/folder';
import { FolderTagMappingRepository } from '@/databases/repositories/folder-tag-mapping.repository';
Expand Down Expand Up @@ -47,14 +49,32 @@ export class FolderService {
return folder;
}

async updateFolder(folderId: string, projectId: string, { name, tagIds }: UpdateFolderDto) {
async updateFolder(
folderId: string,
projectId: string,
{ name, tagIds, parentFolderId }: UpdateFolderDto,
) {
await this.findFolderInProjectOrFail(folderId, projectId);
if (name) {
await this.folderRepository.update({ id: folderId }, { name });
}
if (tagIds) {
await this.folderTagMappingRepository.overwriteTags(folderId, tagIds);
}

if (parentFolderId) {
if (folderId === parentFolderId) {
throw new UserError('Cannot set a folder as its own parent');
}

if (parentFolderId !== PROJECT_ROOT) {
await this.findFolderInProjectOrFail(parentFolderId, projectId);
}
await this.folderRepository.update(
{ id: folderId },
{ parentFolder: parentFolderId !== PROJECT_ROOT ? { id: parentFolderId } : null },
);
}
}

async findFolderInProjectOrFail(folderId: string, projectId: string, em?: EntityManager) {
Expand Down Expand Up @@ -115,6 +135,10 @@ export class FolderService {
return;
}

if (folderId === transferToFolderId) {
throw new UserError('Cannot transfer folder contents to the folder being deleted');
}

await this.findFolderInProjectOrFail(transferToFolderId, projectId);

return await this.folderRepository.manager.transaction(async (tx) => {
Expand Down
Loading