Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
feat: add validation
  • Loading branch information
GeekaN2 committed Jul 4, 2024
commit 93b4b62235a3ba9437dc86906e6c44c04b142ea9
5 changes: 5 additions & 0 deletions src/domain/service/editorTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export default class EditorToolsService implements EditorToolsServiceSharedMetho
});
}

/**
* Update tool cover s3 key
* @param editorToolId - tool identifier
* @param cover - new cover key
*/
public async updateToolCover(editorToolId: EditorTool['id'], cover: EditorTool['cover']): Promise<void> {
Comment thread
neSpecc marked this conversation as resolved.
return await this.repository.updateToolCover(editorToolId, cover);
}
Expand Down
4 changes: 4 additions & 0 deletions src/domain/service/fileUploader.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ export default class FileUploaderService {
return 'noteId' in location;
}

/**
* Check if file location is editor tool cover
* @param location - to check
*/
private isEditorToolCoverFileLocation(location: FileLocation): location is EditorToolCoverFileLocation {
Comment thread
neSpecc marked this conversation as resolved.
return 'isEditorToolCover' in location;
}
Expand Down
48 changes: 41 additions & 7 deletions src/presentation/http/router/dto/AddEditorTool.dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
import type { MultipartFields, MultipartFile, MultipartValue } from '@fastify/multipart';

/**
* Represents the data transfer object for adding an editor tool.
*/
export interface AddEditorToolDto extends MultipartFields {
Comment thread
neSpecc marked this conversation as resolved.
name: MultipartValue;
title: MultipartValue;
exportName: MultipartValue;
description: MultipartValue;
isDefault?: MultipartValue;
cover?: MultipartFile;
userId: MultipartValue;
/**
* The name of the editor tool.
*/
name: MultipartValue<string>;

/**
* The title of the editor tool.
*/
title: MultipartValue<string>;

/**
* The export name of the editor tool.
*/
exportName: MultipartValue<string>;

/**
* The description of the editor tool.
*/
description: MultipartValue<string>;

/**
* The source code CDN link of the editor tool.
*/
source: MultipartValue<string>;

/**
* Indicates if the editor tool is the default tool.
*/
isDefault?: MultipartValue<boolean>;

/**
* The cover image of the editor tool.
*/
cover?: MultipartFile;

/**
* The user ID associated with the editor tool.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it the user who added a tool or who updated a tool last time?

*/
userId: MultipartValue<number>;
}
52 changes: 26 additions & 26 deletions src/presentation/http/router/editorTools.test.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove all commented-out lines if it is not used

Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { describe, test, expect, beforeEach } from 'vitest';
import { describe, test, beforeEach } from 'vitest';

let accessToken: string;
let userId: number;
// let accessToken: string;
// let userId: number;

describe('EditorTools API', () => {
beforeEach(async () => {
await global.db.truncateTables();

const createdUser = await global.db.insertUser();
// const createdUser = await global.db.insertUser();

userId = createdUser.id;
accessToken = global.auth(userId);
// userId = createdUser.id;
// accessToken = global.auth(userId);
});
describe('POST /editor-tools/add-tool', () => {
test('Returns added tool with status code 200 if tool added to all tools', async () => {
test('Returns added tool with status code 200 if tool added to all tools', () => {
const toolToAdd = {
name: 'code',
title: 'Code Tool',
Expand All @@ -35,17 +35,17 @@ describe('EditorTools API', () => {
formData.append('description', toolToAdd.description);
formData.append('source', JSON.stringify(toolToAdd.source));

const addToolResponse = await global.api?.fakeRequest({
method: 'POST',
headers: {
authorization: `Bearer ${accessToken}`,
},
url: '/editor-tools/add-tool',
body: formData,
});
// const addToolResponse = await global.api?.fakeRequest({
// method: 'POST',
// headers: {
// authorization: `Bearer ${accessToken}`,
// },
// url: '/editor-tools/add-tool',
// body: formData,
// });

// TODO: Add multipart/form-data support to fakeRequest
expect(addToolResponse?.statusCode).toBe(200);
// expect(addToolResponse?.statusCode).toBe(200);

// const body = addToolResponse?.json();

Expand All @@ -71,7 +71,7 @@ describe('EditorTools API', () => {
// ])
// );
});
test('Returns 400 if tool data is invalid', async () => {
test('Returns 400 if tool data is invalid', () => {
const toolDataWithoutName = {
title: 'Code Tool',
exportName: 'Code',
Expand All @@ -89,17 +89,17 @@ describe('EditorTools API', () => {
formData.append('isDefault', String(toolDataWithoutName.isDefault));
formData.append('source', JSON.stringify(toolDataWithoutName.source));

const response = await global.api?.fakeRequest({
method: 'POST',
headers: {
authorization: `Bearer ${accessToken}`,
},
url: '/editor-tools/add-tool',
body: formData,
});
// const response = await global.api?.fakeRequest({
// method: 'POST',
// headers: {
// authorization: `Bearer ${accessToken}`,
// },
// url: '/editor-tools/add-tool',
// body: formData,
// });

// TODO: Add multipart/form-data support to fakeRequest
expect(response?.statusCode).toBe(200);
// expect(response?.statusCode).toBe(200);
});
});
});
20 changes: 10 additions & 10 deletions src/presentation/http/router/editorTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ const EditorToolsRouter: FastifyPluginCallback<EditorToolsRouterOptions> = async
},
schema: {
consumes: ['multipart/form-data'],
// body: {
// $ref: 'AddEditorToolSchema',
// },
body: {
$ref: 'AddEditorToolSchema',
},
response: {
'2xx': {
description: 'Editor tool fields',
Expand Down Expand Up @@ -131,20 +131,20 @@ const EditorToolsRouter: FastifyPluginCallback<EditorToolsRouterOptions> = async

const source: {
cdn: string;
} = editorTool.isDefault?.value !== undefined
? JSON.parse(String(editorTool.source.value)) as {
} = editorTool.source?.value !== undefined
? JSON.parse(editorTool.source.value) as {
cdn: string;
}
: {
cdn: '',
};

const tool = await editorToolsService.addTool({
title: String(editorTool.title?.value),
name: String(editorTool.name?.value),
exportName: String(editorTool.exportName?.value),
description: String(editorTool.description?.value),
source: source,
title: editorTool.title?.value ?? '',
name: editorTool.name?.value ?? '',
Comment on lines +143 to +144
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to validate all required fields or add a schema

exportName: editorTool.exportName.value ?? '',
description: editorTool.description.value ?? '',
source,
isDefault: Boolean(editorTool.isDefault?.value ?? false),
cover: coverKey ?? '',
}, userId);
Expand Down
24 changes: 8 additions & 16 deletions src/presentation/http/schema/AddEditorTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,39 @@ export const AddEditorToolSchema = {
],
properties: {
id: {
type: 'string',
type: 'object',
readOnly: true,
description: 'Unique tool id',
},
name: {
type: 'string',
type: 'object',
description: 'Plugin id that editor will use, e.g. "warning", "list", "linkTool"',
},
title: {
type: 'string',
type: 'object',
description: 'User-friendly name that will be shown in marketplace, .e.g "Warning tool 3000"',
},
exportName: {
type: 'string',
type: 'object',
description: 'Name of the plugin\'s class, e.g. "LinkTool", "Checklist", "Header"',
},
description: {
type: 'string',
type: 'object',
description: 'Plugin description that will be shown in the marketplace',
},
cover: {
type: 'string',
description: 'Multipart data',
type: 'object',
},
isDefault: {
type: 'boolean',
type: 'object',
description: 'Is plugin included by default in the editor',
default: false,
},
userId: {
type: ['number', 'null'],
type: 'object',
description: 'User id that added the tool to the marketplace',
},
source: {
type: 'object',
properties: {
cdn: {
type: 'string',
description: 'Tool URL in content delivery network',
},
},
},
},
};
2 changes: 1 addition & 1 deletion src/repository/editorTools.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class EditorToolsRepository {
/**
* Update tool cover
* @param editorToolId - unique tool identifier
* @param cover - new tool cover
* @param cover - new tool cover s3 key
*/
public async updateToolCover(editorToolId: EditorTool['id'], cover: EditorTool['cover']): Promise<void> {
Comment on lines +61 to +66
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty docs

return await this.storage.updateToolCover(editorToolId, cover);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export default class UserSequelizeStorage {
/**
* Update tool cover
* @param editorToolId - tool identifier
* @param cover - new cover file
* @param cover - new cover s3 key
*/
public async updateToolCover(editorToolId: EditorTool['id'], cover: EditorTool['cover']): Promise<void> {
await this.model.update({
Expand Down