-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuseNoteSettingsResolver.ts
More file actions
40 lines (35 loc) · 1.33 KB
/
useNoteSettingsResolver.ts
File metadata and controls
40 lines (35 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import type { preHandlerHookHandler } from 'fastify';
import { getRequestLogger } from '@infrastructure/logging/index.js';
import type NoteSettingsService from '@domain/service/noteSettings.js';
import type NoteSettings from '@domain/entities/noteSettings.js';
/**
* Add middleware for resolve Note settings and add it to request
* @param noteSettingsService - note settings domain service
*/
export default function useNoteSettingsResolver(noteSettingsService: NoteSettingsService): {
/**
* Resolve Note settings by note and add it to request
*
* Use this middleware as "preHandler" hook with a particular route
*/
noteSettingsResolver: preHandlerHookHandler;
} {
return {
noteSettingsResolver: async function noteSettingsResolver(request, reply) {
const logger = getRequestLogger('middlewares');
let noteSettings: NoteSettings | null;
try {
if (!request.note) {
throw new Error('Note was not resolved');
}
noteSettings = await noteSettingsService.getNoteSettingsByNoteId(request.note.id);
request.noteSettings = noteSettings;
logger.debug('Note settings resolved');
} catch (error) {
logger.error('Can not resolve Note settings by note');
logger.error(error);
await reply.notAcceptable('Note settings not found');
}
},
};
}