Skip to content

Commit

Permalink
Merge pull request #59 from codex-team/refactor/note/settings
Browse files Browse the repository at this point in the history
Refactor/note/settings
  • Loading branch information
GeekaN2 authored Oct 20, 2023
2 parents 62fb8d2 + fee85b9 commit 94d0e91
Show file tree
Hide file tree
Showing 17 changed files with 550 additions and 4,434 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"test": "vitest",
"test:verbose": "DEBUG=testcontainers* yarn test"
"test:verbose": "DEBUG=testcontainers* yarn test",
"typecheck": "tsc --noEmit -p ."
},
"author": "",
"license": "ISC",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Notes settings entity
*/
export default interface NotesSettings {
export default interface NoteSettings {
/**
* Just unique property identifier
*/
Expand All @@ -26,8 +26,8 @@ export default interface NotesSettings {
/**
* Notes settings creation attributes, omitting id, because it's generated by database
*/
type NotesSettingsCreationAttributes = Omit<NotesSettings, 'id'>;
type NoteSettingsCreationAttributes = Omit<NoteSettings, 'id'>;

export type {
NotesSettingsCreationAttributes
NoteSettingsCreationAttributes
};
8 changes: 8 additions & 0 deletions src/domain/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import NoteService from '@domain/service/note.js';
import NoteSettingsService from './service/noteSettings.js';
import type { Repositories } from '@repository/index.js';
import AuthService from '@domain/service/auth.js';
import type { AppConfig } from '@infrastructure/config/index.js';
Expand All @@ -15,6 +16,11 @@ export interface DomainServices {
*/
noteService: NoteService,

/**
* Note settings service instance
*/
noteSettingsService: NoteSettingsService,

/**
* Auth service instance
*/
Expand All @@ -40,6 +46,7 @@ export interface DomainServices {
*/
export function init(repositories: Repositories, appConfig: AppConfig): DomainServices {
const noteService = new NoteService(repositories.noteRepository);
const noteSettingsService = new NoteSettingsService(repositories.noteSettingsRepository);

const authService = new AuthService(
appConfig.auth.accessSecret,
Expand All @@ -55,6 +62,7 @@ export function init(repositories: Repositories, appConfig: AppConfig): DomainSe

return {
noteService,
noteSettingsService,
userService,
authService,
aiService,
Expand Down
51 changes: 0 additions & 51 deletions src/domain/service/note.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Note, NotePublicId } from '@domain/entities/note.js';
import type NotesSettings from '@domain/entities/notesSettings.js';
import type NoteRepository from '@repository/note.repository.js';
import { createPublicId } from '@infrastructure/utils/id.js';

Expand Down Expand Up @@ -64,29 +63,6 @@ export default class NoteService {
return await this.repository.getNoteByPublicId(id);
}

/**
* Gets note settings by public id
*
* @param id - note public id
* @returns { Promise<NotesSettings | null> } note settings
*/
public async getNoteSettingsByPublicId(id: NotePublicId): Promise<NotesSettings> {
/**
* @todo get internal id by public id and resolve note settings by the internal id
*/
return await this.repository.getNoteSettingsByPublicId(id);
}

/**
* Gets note settings by note id
*
* @param id - note id
* @returns { Promise<NotesSettings | null> } note
*/
public async getNoteSettingsByNoteId(id: Note['id']): Promise<NotesSettings | null> {
return await this.repository.getNoteSettingsByNoteId(id);
}

/**
* Gets note by custom hostname
*
Expand All @@ -96,31 +72,4 @@ export default class NoteService {
public async getNoteByHostname(hostname: string): Promise<Note | null> {
return await this.repository.getNoteByHostname(hostname);
}

/**
* Adds note settings
*
* @param noteId - note id
* @param enabled - is note enabled
* @returns { Promise<NotesSettings> } note settings
*/
public async addNoteSettings(noteId: Note['id'], enabled: boolean = true): Promise<NotesSettings> {
return await this.repository.addNoteSettings({
noteId: noteId,
enabled: enabled,
});
}

/**
* Partially updates note settings
*
* @param data - note settings data with new values
* @param noteId - note public id
* @returns { Promise<NotesSettings> } updated note settings
*/
public async patchNoteSettings(data: Partial<NotesSettings>, noteId: NotePublicId): Promise<NotesSettings | null> {
const noteSettings = await this.repository.getNoteSettingsByPublicId(noteId);

return await this.repository.patchNoteSettings(data, noteSettings.id);
}
}
72 changes: 72 additions & 0 deletions src/domain/service/noteSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type { Note, NotePublicId } from '@domain/entities/note.js';
import type NoteSettings from '@domain/entities/noteSettings.js';
import type NoteSettingsRepository from '@repository/noteSettings.repository.js';

/**
* Service responsible for Note Settings
*/
export default class NoteSettingsService {
/**
* Note Settings repository
*/
public repository: NoteSettingsRepository;

/**
* Note Settings service constructor
*
* @param repository - note repository
*/
constructor(repository: NoteSettingsRepository) {
this.repository = repository;
}

/**
* Gets note settings by public id
*
* @param id - note public id
* @returns { Promise<NoteSettings | null> } note settings
*/
public async getNoteSettingsByPublicId(id: NotePublicId): Promise<NoteSettings> {
/**
* @todo get internal id by public id and resolve note settings by the internal id
*/
return await this.repository.getNoteSettingsByPublicId(id);
}

/**
* Gets note settings by note id
*
* @param id - note id
* @returns { Promise<NoteSettings | null> } note
*/
public async getNoteSettingsByNoteId(id: Note['id']): Promise<NoteSettings | null> {
return await this.repository.getNoteSettingsByNoteId(id);
}

/**
* Adds note settings
*
* @param noteId - note id
* @param enabled - is note enabled
* @returns { Promise<NoteSettings> } note settings
*/
public async addNoteSettings(noteId: Note['id'], enabled: boolean = true): Promise<NoteSettings> {
return await this.repository.addNoteSettings({
noteId: noteId,
enabled: enabled,
});
}

/**
* Partially updates note settings
*
* @param data - note settings data with new values
* @param noteId - note public id
* @returns { Promise<NoteSettings> } updated note settings
*/
public async patchNoteSettingsByPublicId(data: Partial<NoteSettings>, noteId: NotePublicId): Promise<NoteSettings | null> {
const noteSettings = await this.repository.getNoteSettingsByPublicId(noteId);

return await this.repository.patchNoteSettingsByPublicId(data, noteSettings.id);
}
}
9 changes: 9 additions & 0 deletions src/presentation/http/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import AIRouter from '@presentation/http/router/ai.js';
import EditorToolsRouter from './router/editorTools.js';
import { UserSchema } from './schema/User.js';
import type { RequestParams, Response } from '@presentation/api.interface.js';
import NoteSettingsRouter from './router/noteSettings.js';


const appServerLogger = getLogger('appServer');
Expand Down Expand Up @@ -72,6 +73,7 @@ export default class HttpApi implements Api {
this.addDecorators();

const middlewares = initMiddlewares(domainServices);

await this.addApiRoutes(domainServices, middlewares);
}

Expand Down Expand Up @@ -162,6 +164,13 @@ export default class HttpApi implements Api {
await this.server?.register(NoteRouter, {
prefix: '/note',
noteService: domainServices.noteService,
noteSettingsService: domainServices.noteSettingsService,
middlewares: middlewares,
});

await this.server?.register(NoteSettingsRouter, {
prefix: '/note-settings',
noteSettingsService: domainServices.noteSettingsService,
middlewares: middlewares,
});

Expand Down
63 changes: 9 additions & 54 deletions src/presentation/http/router/note.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { FastifyPluginCallback } from 'fastify';
import type NoteService from '@domain/service/note.js';
import type NoteSettingsService from '@domain/service/noteSettings.js';
import { StatusCodes } from 'http-status-codes';
import type { ErrorResponse } from '@presentation/http/types/HttpResponse.js';
import type { Note, NotePublicId } from '@domain/entities/note.js';
import type NotesSettings from '@domain/entities/notesSettings.js';
import type { Middlewares } from '@presentation/http/middlewares/index.js';
import notEmpty from '@infrastructure/utils/notEmpty.js';

/**
* Get note by id options
Expand Down Expand Up @@ -52,6 +51,11 @@ interface NoteRouterOptions {
*/
noteService: NoteService,

/**
* Note Settings service instance
*/
noteSettingsService: NoteSettingsService,

/**
* Middlewares
*/
Expand All @@ -77,6 +81,7 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
* Get note service from options
*/
const noteService = opts.noteService;
const noteSettingsService = opts.noteSettingsService;

/**
* Get note by id
Expand All @@ -100,7 +105,7 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
return fastify.notFound(reply, 'Note not found');
}

const noteSettings = await noteService.getNoteSettingsByNoteId(note.id);
const noteSettings = await noteSettingsService.getNoteSettingsByNoteId(note.id);

if (noteSettings?.enabled === true) {
return reply.send(note);
Expand All @@ -116,56 +121,6 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
});
});

/**
* Get noteSettings by id
*
* @todo move to the NoteSettings Router
*/
fastify.get<{
Params: GetNoteByIdOptions,
Reply: NotesSettings
}>('/:id/settings', async (request, reply) => {
const params = request.params;
/**
* TODO: Validate request params
*/
const { id } = params;

const noteSettings = await noteService.getNoteSettingsByPublicId(id);

/**
* Check if note does not exist
*/
if (!notEmpty(noteSettings)) {
return fastify.notFound(reply, 'Note settings not found');
}

return reply.send(noteSettings);
});

/**
* Patch noteSettings by note public id
*/
fastify.patch<{
Body: Partial<NotesSettings>,
Params: GetNoteByIdOptions,
Reply: NotesSettings,
}>('/:id/settings', { preHandler: [opts.middlewares.authRequired, opts.middlewares.withUser] }, async (request, reply) => {
const noteId = request.params.id;

/**
* TODO: check is user collaborator
*/

const updatedNoteSettings = await noteService.patchNoteSettings(request.body, noteId);

if (updatedNoteSettings === null) {
return fastify.notFound(reply, 'Note settings not found');
}

return reply.send(updatedNoteSettings);
});

/**
* Adds a new note.
* Responses with note public id.
Expand Down Expand Up @@ -194,7 +149,7 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
/**
* @todo use event bus: emit 'note-added' event and subscribe to it in other modules like 'note-settings'
*/
await noteService.addNoteSettings(addedNote.id);
await noteSettingsService.addNoteSettings(addedNote.id);

return reply.send({
id: addedNote.publicId,
Expand Down
Loading

0 comments on commit 94d0e91

Please sign in to comment.