diff --git a/migrations/tenant/0006-note-settings@rename-enable-to-ispublic.sql b/migrations/tenant/0006-note-settings@rename-enable-to-ispublic.sql new file mode 100644 index 00000000..f4d6b8c6 --- /dev/null +++ b/migrations/tenant/0006-note-settings@rename-enable-to-ispublic.sql @@ -0,0 +1,2 @@ +ALTER TABLE IF EXISTS public.note_settings + RENAME COLUMN enabled TO is_public; \ No newline at end of file diff --git a/src/domain/entities/noteSettings.ts b/src/domain/entities/noteSettings.ts index 2787d9de..579a68dd 100644 --- a/src/domain/entities/noteSettings.ts +++ b/src/domain/entities/noteSettings.ts @@ -20,7 +20,7 @@ export default interface NoteSettings { /** * Is note public for everyone or only for collaborators */ - enabled: boolean; + isPublic: boolean; } /** diff --git a/src/domain/service/noteSettings.ts b/src/domain/service/noteSettings.ts index de9d9bd2..a97a1dd4 100644 --- a/src/domain/service/noteSettings.ts +++ b/src/domain/service/noteSettings.ts @@ -33,13 +33,13 @@ export default class NoteSettingsService { * Adds note settings * * @param noteId - note id - * @param enabled - is note enabled + * @param isPublic - is note public * @returns added note settings */ - public async addNoteSettings(noteId: NoteInternalId, enabled: boolean = true): Promise { + public async addNoteSettings(noteId: NoteInternalId, isPublic: boolean = true): Promise { return await this.repository.addNoteSettings({ noteId: noteId, - enabled: enabled, + isPublic: isPublic, }); } diff --git a/src/presentation/http/router/note.ts b/src/presentation/http/router/note.ts index 55b2dd74..502261ba 100644 --- a/src/presentation/http/router/note.ts +++ b/src/presentation/http/router/note.ts @@ -73,7 +73,10 @@ const NoteRouter: FastifyPluginCallback = (fastify, opts, don const noteSettings = await noteSettingsService.getNoteSettingsByNoteId(note.id); - if (noteSettings?.enabled === true) { + /** + * Check if note is public or user is owner + */ + if (noteSettings.isPublic || note.creatorId === request.userId) { return reply.send(note); } diff --git a/src/presentation/http/router/noteList.ts b/src/presentation/http/router/noteList.ts index 76595d57..f880b096 100644 --- a/src/presentation/http/router/noteList.ts +++ b/src/presentation/http/router/noteList.ts @@ -20,7 +20,6 @@ interface NoteListRouterOptions { * @param done - callback */ const NoteListRouter: FastifyPluginCallback = (fastify, opts, done) => { - const noteListService = opts.noteListService; /** @@ -41,4 +40,5 @@ const NoteListRouter: FastifyPluginCallback = (fastify, o done(); }; + export default NoteListRouter; diff --git a/src/presentation/http/router/noteSettings.ts b/src/presentation/http/router/noteSettings.ts index 7d43a333..ae288ec8 100644 --- a/src/presentation/http/router/noteSettings.ts +++ b/src/presentation/http/router/noteSettings.ts @@ -1,11 +1,10 @@ import type { FastifyPluginCallback } from 'fastify'; import type NoteSettingsService from '@domain/service/noteSettings.js'; -import type { NoteInternalId } from '@domain/entities/note.js'; import type NoteSettings from '@domain/entities/noteSettings.js'; import notEmpty from '@infrastructure/utils/notEmpty.js'; import useNoteResolver from '../middlewares/note/useNoteResolver.js'; import type NoteService from '@domain/service/note.js'; -import { NotePublicId } from '@domain/entities/note.js'; +import type { NotePublicId } from '@domain/entities/note.js'; /** * Interface for the note settings router. @@ -96,7 +95,7 @@ const NoteSettingsRouter: FastifyPluginCallback = (fa /** * @todo validate data */ - const { customHostname, enabled } = request.body; + const { customHostname, isPublic } = request.body; /** * TODO: check is user collaborator @@ -104,7 +103,7 @@ const NoteSettingsRouter: FastifyPluginCallback = (fa const updatedNoteSettings = await noteSettingsService.patchNoteSettingsByNoteId(noteId, { customHostname, - enabled, + isPublic, }); if (updatedNoteSettings === null) { diff --git a/src/repository/storage/postgres/orm/sequelize/noteSettings.ts b/src/repository/storage/postgres/orm/sequelize/noteSettings.ts index 7d2900f5..a7b1b970 100644 --- a/src/repository/storage/postgres/orm/sequelize/noteSettings.ts +++ b/src/repository/storage/postgres/orm/sequelize/noteSettings.ts @@ -5,8 +5,6 @@ import { NoteModel } from '@repository/storage/postgres/orm/sequelize/note.js'; import type NoteSettings from '@domain/entities/noteSettings.js'; import type { NoteSettingsCreationAttributes } from '@domain/entities/noteSettings.js'; -/* eslint-disable @typescript-eslint/naming-convention */ - /** * Class representing a notes settings model in database */ @@ -19,17 +17,17 @@ export class NoteSettingsModel extends Model, /** * Note ID */ - public declare note_id: NoteSettings['noteId']; + public declare noteId: NoteSettings['noteId']; /** * Custom hostname */ - public declare custom_hostname: CreationOptional; + public declare customHostname: CreationOptional; /** * Is note public */ - public declare enabled: CreationOptional; + public declare isPublic: CreationOptional; } /** @@ -73,7 +71,7 @@ export default class NoteSettingsSequelizeStorage { autoIncrement: true, primaryKey: true, }, - note_id: { + noteId: { type: DataTypes.INTEGER, allowNull: false, references: { @@ -81,11 +79,11 @@ export default class NoteSettingsSequelizeStorage { key: 'id', }, }, - custom_hostname: { + customHostname: { type: DataTypes.STRING, allowNull: true, }, - enabled: { + isPublic: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true, @@ -118,12 +116,7 @@ export default class NoteSettingsSequelizeStorage { return null; } - return { - id: noteSettings.id, - noteId: noteSettings.note_id, - enabled: noteSettings.enabled, - customHostname: noteSettings.custom_hostname, - }; + return noteSettings; } /** @@ -135,7 +128,7 @@ export default class NoteSettingsSequelizeStorage { public async getNoteSettingsByNoteId(noteId: NoteSettings['noteId']): Promise { const settings = await this.model.findOne({ where: { - note_id: noteId, + noteId: noteId, }, }); @@ -146,12 +139,7 @@ export default class NoteSettingsSequelizeStorage { throw new Error('Note settings not found'); } - return { - id: settings.id, - noteId: settings.note_id, - customHostname: settings.custom_hostname, - enabled: settings.enabled, - }; + return settings; } /** @@ -181,21 +169,16 @@ export default class NoteSettingsSequelizeStorage { public async insertNoteSettings({ noteId, customHostname, - enabled, + isPublic, }: NoteSettingsCreationAttributes ): Promise { const settings = await this.model.create({ - note_id: noteId, - custom_hostname: customHostname, - enabled: enabled, + noteId, + customHostname, + isPublic, }); - return { - id: settings.id, - noteId: settings.note_id, - customHostname: settings.custom_hostname, - enabled: settings.enabled, - }; + return settings; } /** @@ -214,18 +197,6 @@ export default class NoteSettingsSequelizeStorage { return null; } - const values = { - enabled: data.enabled, - custom_hostname: data.customHostname, - }; - - const updatedSettings = await settingsToUpdate.update(values); - - return { - id: updatedSettings.id, - noteId: updatedSettings.note_id, - customHostname: updatedSettings.custom_hostname, - enabled: updatedSettings.enabled, - }; + return await settingsToUpdate.update(data); } } diff --git a/yarn.lock b/yarn.lock index 5d3f8483..a76b8a29 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5999,11 +5999,11 @@ __metadata: "typescript@patch:typescript@^5.0.4#~builtin": version: 5.2.2 - resolution: "typescript@patch:typescript@npm%3A5.2.2#~builtin::version=5.2.2&hash=f3b441" + resolution: "typescript@patch:typescript@npm%3A5.2.2#~builtin::version=5.2.2&hash=ad5954" bin: tsc: bin/tsc tsserver: bin/tsserver - checksum: 0f4da2f15e6f1245e49db15801dbee52f2bbfb267e1c39225afdab5afee1a72839cd86000e65ee9d7e4dfaff12239d28beaf5ee431357fcced15fb08583d72ca + checksum: 07106822b4305de3f22835cbba949a2b35451cad50888759b6818421290ff95d522b38ef7919e70fb381c5fe9c1c643d7dea22c8b31652a717ddbd57b7f4d554 languageName: node linkType: hard