From 0a16346de21736aed13892143a7c150056031bde Mon Sep 17 00:00:00 2001 From: slaveeks Date: Sat, 29 Jun 2024 19:33:43 +0300 Subject: [PATCH 1/4] fix: fixed validation of upload file response type, fixed policy of getting file(added necessary preHandlers) --- src/presentation/http/http-api.ts | 1 + src/presentation/http/router/upload.ts | 37 ++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/presentation/http/http-api.ts b/src/presentation/http/http-api.ts index 7beca6c0..6a5f72cf 100644 --- a/src/presentation/http/http-api.ts +++ b/src/presentation/http/http-api.ts @@ -259,6 +259,7 @@ export default class HttpApi implements Api { fileUploaderService: domainServices.fileUploaderService, noteService: domainServices.noteService, fileSizeLimit: this.config.fileSizeLimit, + noteSettingsService: domainServices.noteSettingsService }); } diff --git a/src/presentation/http/router/upload.ts b/src/presentation/http/router/upload.ts index 34b6e581..64049157 100644 --- a/src/presentation/http/router/upload.ts +++ b/src/presentation/http/router/upload.ts @@ -5,6 +5,9 @@ import type NoteService from '@domain/service/note.js'; import useNoteResolver from '../middlewares/note/useNoteResolver.js'; import type { NoteAttachmentFileLocation } from '@domain/entities/file.js'; import { StatusCodes } from 'http-status-codes'; +import useNoteSettingsResolver from '../middlewares/noteSettings/useNoteSettingsResolver.js'; +import NoteSettingsService from '@domain/service/noteSettings.js'; +import useMemberRoleResolver from '../middlewares/noteSettings/useMemberRoleResolver.js'; /** * Interface for upload router options @@ -20,6 +23,8 @@ interface UploadRouterOptions { */ noteService: NoteService; + noteSettingsService: NoteSettingsService; + /** * Limit for uploaded files size */ @@ -35,6 +40,18 @@ const UploadRouter: FastifyPluginCallback = async (fastify, */ const { noteResolver } = useNoteResolver(opts.noteService); + /** + * Prepare note settings resolver middleware + * It should be used to use note settings in middlewares + */ + const { noteSettingsResolver } = useNoteSettingsResolver(opts.noteSettingsService); + + /** + * Prepare user role resolver middleware + * It should be used to use user role in middlewares + */ + const { memberRoleResolver } = useMemberRoleResolver(opts.noteSettingsService); + await fastify.register(fastifyMultipart, { limits: { fieldSize: opts.fileSizeLimit, @@ -74,14 +91,20 @@ const UploadRouter: FastifyPluginCallback = async (fastify, '2xx': { type: 'object', description: 'File key to get it from the API', - key: { - $ref: 'UploadSchema#/properties/key', - }, + properties: { + key: { + $ref: 'UploadSchema#/properties/key', + }, + } }, }, }, attachValidation: true, - preHandler: [ noteResolver ], + preHandler:[ + noteResolver, + noteSettingsResolver, + memberRoleResolver, + ], }, async (request, reply) => { /** * @todo solve trouble with crashing app, when validations is not passed @@ -138,7 +161,11 @@ const UploadRouter: FastifyPluginCallback = async (fastify, }, }, }, - preHandler: [ noteResolver ], + preHandler:[ + noteResolver, + noteSettingsResolver, + memberRoleResolver, + ], }, async (request, reply) => { const fileLocation: NoteAttachmentFileLocation = { noteId: request.note!.id as number, From 6b2b281e0acbbfefc713aa4dd4eba4f5e154ec33 Mon Sep 17 00:00:00 2001 From: slaveeks Date: Sat, 29 Jun 2024 19:39:22 +0300 Subject: [PATCH 2/4] fix: removed conflict text, added doc for opt --- src/presentation/http/router/upload.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/presentation/http/router/upload.ts b/src/presentation/http/router/upload.ts index 615b7283..bffeda81 100644 --- a/src/presentation/http/router/upload.ts +++ b/src/presentation/http/router/upload.ts @@ -23,6 +23,9 @@ interface UploadRouterOptions { */ noteService: NoteService; + /** + * Note settings service instance + */ noteSettingsService: NoteSettingsService; /** @@ -100,15 +103,11 @@ const UploadRouter: FastifyPluginCallback = async (fastify, }, }, attachValidation: true, -<<<<<<< HEAD preHandler:[ noteResolver, noteSettingsResolver, memberRoleResolver, ], -======= - preHandler: [noteResolver], ->>>>>>> 9615a41936a09f160c2b27571457d4c3dd7f2afa }, async (request, reply) => { /** * @todo solve trouble with crashing app, when validations is not passed From 949cc032026479c826c6ca55f770045e83f52c77 Mon Sep 17 00:00:00 2001 From: slaveeks Date: Sat, 29 Jun 2024 20:48:39 +0300 Subject: [PATCH 3/4] refactor: eslint fixes --- src/presentation/http/http-api.ts | 2 +- src/presentation/http/router/upload.ts | 26 +++++++++++++------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/presentation/http/http-api.ts b/src/presentation/http/http-api.ts index 1e7fbb78..c3df80e6 100644 --- a/src/presentation/http/http-api.ts +++ b/src/presentation/http/http-api.ts @@ -252,7 +252,7 @@ export default class HttpApi implements Api { fileUploaderService: domainServices.fileUploaderService, noteService: domainServices.noteService, fileSizeLimit: this.config.fileSizeLimit, - noteSettingsService: domainServices.noteSettingsService + noteSettingsService: domainServices.noteSettingsService, }); } diff --git a/src/presentation/http/router/upload.ts b/src/presentation/http/router/upload.ts index bffeda81..811eed44 100644 --- a/src/presentation/http/router/upload.ts +++ b/src/presentation/http/router/upload.ts @@ -6,7 +6,7 @@ import useNoteResolver from '../middlewares/note/useNoteResolver.js'; import type { NoteAttachmentFileLocation } from '@domain/entities/file.js'; import { StatusCodes } from 'http-status-codes'; import useNoteSettingsResolver from '../middlewares/noteSettings/useNoteSettingsResolver.js'; -import NoteSettingsService from '@domain/service/noteSettings.js'; +import type NoteSettingsService from '@domain/service/noteSettings.js'; import useMemberRoleResolver from '../middlewares/noteSettings/useMemberRoleResolver.js'; /** @@ -39,7 +39,7 @@ const UploadRouter: FastifyPluginCallback = async (fastify, /** * Prepare note id resolver middleware - * It should be used in routes that accepts note public id + * It should be used in routes that accepts note public id */ const { noteResolver } = useNoteResolver(opts.noteService); @@ -98,12 +98,12 @@ const UploadRouter: FastifyPluginCallback = async (fastify, key: { $ref: 'UploadSchema#/properties/key', }, - } + }, }, }, }, attachValidation: true, - preHandler:[ + preHandler: [ noteResolver, noteSettingsResolver, memberRoleResolver, @@ -162,16 +162,16 @@ const UploadRouter: FastifyPluginCallback = async (fastify, }, }, }, - preHandler:[ - noteResolver, - noteSettingsResolver, - memberRoleResolver, - ], - } + }, + preHandler: [ + noteResolver, + noteSettingsResolver, + memberRoleResolver, + ], }, async (request, reply) => { - const fileLocation: NoteAttachmentFileLocation = { - noteId: request.note!.id as number, - }; + const fileLocation: NoteAttachmentFileLocation = { + noteId: request.note!.id, + }; const fileData = await fileUploaderService.getFileData(request.params.key, fileLocation); From 8cdbc83d06d16d72864624c667fd4e00257b3507 Mon Sep 17 00:00:00 2001 From: slaveeks Date: Sat, 29 Jun 2024 21:01:32 +0300 Subject: [PATCH 4/4] refactor: removed extra space from doc --- src/presentation/http/router/upload.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/presentation/http/router/upload.ts b/src/presentation/http/router/upload.ts index 811eed44..de3de609 100644 --- a/src/presentation/http/router/upload.ts +++ b/src/presentation/http/router/upload.ts @@ -39,7 +39,7 @@ const UploadRouter: FastifyPluginCallback = async (fastify, /** * Prepare note id resolver middleware - * It should be used in routes that accepts note public id + * It should be used in routes that accepts note public id */ const { noteResolver } = useNoteResolver(opts.noteService);