From b2d51212de2ea157a2856dd3d4e23ab3b0364f93 Mon Sep 17 00:00:00 2001 From: slaveeks Date: Sat, 21 Oct 2023 21:22:26 +0300 Subject: [PATCH 1/5] chore: renamed note settings enabled to isPublic --- ...note-settings-rename-enable-to-ispublic.sql | 3 +++ src/domain/entities/noteSettings.ts | 2 +- src/domain/service/noteSettings.ts | 6 +++--- src/presentation/http/router/note.ts | 5 ++++- src/presentation/http/router/noteSettings.ts | 4 ++-- .../postgres/orm/sequelize/noteSettings.ts | 18 +++++++++--------- 6 files changed, 22 insertions(+), 16 deletions(-) create mode 100644 migrations/tenant/0006-note-settings-rename-enable-to-ispublic.sql 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..5d37598c --- /dev/null +++ b/migrations/tenant/0006-note-settings-rename-enable-to-ispublic.sql @@ -0,0 +1,3 @@ +ALTER TABLE IF EXISTS public.notes_settings + RENAME COLUMN IF EXISTS 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/noteSettings.ts b/src/presentation/http/router/noteSettings.ts index 7d43a333..fcab7375 100644 --- a/src/presentation/http/router/noteSettings.ts +++ b/src/presentation/http/router/noteSettings.ts @@ -96,7 +96,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 +104,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..aad557aa 100644 --- a/src/repository/storage/postgres/orm/sequelize/noteSettings.ts +++ b/src/repository/storage/postgres/orm/sequelize/noteSettings.ts @@ -29,7 +29,7 @@ export class NoteSettingsModel extends Model, /** * Is note public */ - public declare enabled: CreationOptional; + public declare is_public: CreationOptional; } /** @@ -85,7 +85,7 @@ export default class NoteSettingsSequelizeStorage { type: DataTypes.STRING, allowNull: true, }, - enabled: { + is_public: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true, @@ -121,7 +121,7 @@ export default class NoteSettingsSequelizeStorage { return { id: noteSettings.id, noteId: noteSettings.note_id, - enabled: noteSettings.enabled, + isPublic: noteSettings.is_public, customHostname: noteSettings.custom_hostname, }; } @@ -150,7 +150,7 @@ export default class NoteSettingsSequelizeStorage { id: settings.id, noteId: settings.note_id, customHostname: settings.custom_hostname, - enabled: settings.enabled, + isPublic: settings.is_public, }; } @@ -181,20 +181,20 @@ 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, + is_public: isPublic, }); return { id: settings.id, noteId: settings.note_id, customHostname: settings.custom_hostname, - enabled: settings.enabled, + isPublic: settings.is_public, }; } @@ -215,7 +215,7 @@ export default class NoteSettingsSequelizeStorage { } const values = { - enabled: data.enabled, + enabled: data.isPublic, custom_hostname: data.customHostname, }; @@ -225,7 +225,7 @@ export default class NoteSettingsSequelizeStorage { id: updatedSettings.id, noteId: updatedSettings.note_id, customHostname: updatedSettings.custom_hostname, - enabled: updatedSettings.enabled, + isPublic: updatedSettings.is_public, }; } } From 6162759bb0d3b2a5970c544fe0f9db097765fe63 Mon Sep 17 00:00:00 2001 From: slaveeks Date: Sat, 21 Oct 2023 21:24:07 +0300 Subject: [PATCH 2/5] Removed quotes --- .../tenant/0006-note-settings-rename-enable-to-ispublic.sql | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/migrations/tenant/0006-note-settings-rename-enable-to-ispublic.sql b/migrations/tenant/0006-note-settings-rename-enable-to-ispublic.sql index 5d37598c..9356327a 100644 --- a/migrations/tenant/0006-note-settings-rename-enable-to-ispublic.sql +++ b/migrations/tenant/0006-note-settings-rename-enable-to-ispublic.sql @@ -1,3 +1,2 @@ ALTER TABLE IF EXISTS public.notes_settings - RENAME COLUMN IF EXISTS enabled TO is_public; -``` \ No newline at end of file + RENAME COLUMN IF EXISTS enabled TO is_public; \ No newline at end of file From c37222a542e3958570a92823a30ea79d677b9cfa Mon Sep 17 00:00:00 2001 From: slaveeks Date: Sat, 21 Oct 2023 21:25:30 +0300 Subject: [PATCH 3/5] Renamed migration file --- ...ublic.sql => 0006-note-settings@rename-enable-to-ispublic.sql} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename migrations/tenant/{0006-note-settings-rename-enable-to-ispublic.sql => 0006-note-settings@rename-enable-to-ispublic.sql} (100%) diff --git a/migrations/tenant/0006-note-settings-rename-enable-to-ispublic.sql b/migrations/tenant/0006-note-settings@rename-enable-to-ispublic.sql similarity index 100% rename from migrations/tenant/0006-note-settings-rename-enable-to-ispublic.sql rename to migrations/tenant/0006-note-settings@rename-enable-to-ispublic.sql From fc71bfc59f35628530816694d4bc5d1be06c9581 Mon Sep 17 00:00:00 2001 From: slaveeks Date: Sat, 21 Oct 2023 21:33:02 +0300 Subject: [PATCH 4/5] Removed snake_keys from note-settings storage --- src/presentation/http/router/noteList.ts | 2 +- src/presentation/http/router/noteSettings.ts | 3 +- .../postgres/orm/sequelize/noteSettings.ts | 57 +++++-------------- yarn.lock | 4 +- 4 files changed, 18 insertions(+), 48 deletions(-) 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 fcab7375..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. diff --git a/src/repository/storage/postgres/orm/sequelize/noteSettings.ts b/src/repository/storage/postgres/orm/sequelize/noteSettings.ts index aad557aa..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 is_public: 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, }, - is_public: { + 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, - isPublic: noteSettings.is_public, - 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, - isPublic: settings.is_public, - }; + return settings; } /** @@ -185,17 +173,12 @@ export default class NoteSettingsSequelizeStorage { }: NoteSettingsCreationAttributes ): Promise { const settings = await this.model.create({ - note_id: noteId, - custom_hostname: customHostname, - is_public: isPublic, + noteId, + customHostname, + isPublic, }); - return { - id: settings.id, - noteId: settings.note_id, - customHostname: settings.custom_hostname, - isPublic: settings.is_public, - }; + return settings; } /** @@ -214,18 +197,6 @@ export default class NoteSettingsSequelizeStorage { return null; } - const values = { - enabled: data.isPublic, - custom_hostname: data.customHostname, - }; - - const updatedSettings = await settingsToUpdate.update(values); - - return { - id: updatedSettings.id, - noteId: updatedSettings.note_id, - customHostname: updatedSettings.custom_hostname, - isPublic: updatedSettings.is_public, - }; + 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 From 2bd9176c885da762226c67b3a55c844915b77bb2 Mon Sep 17 00:00:00 2001 From: slaveeks Date: Sat, 21 Oct 2023 21:53:10 +0300 Subject: [PATCH 5/5] fixed migration --- .../tenant/0006-note-settings@rename-enable-to-ispublic.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/migrations/tenant/0006-note-settings@rename-enable-to-ispublic.sql b/migrations/tenant/0006-note-settings@rename-enable-to-ispublic.sql index 9356327a..f4d6b8c6 100644 --- a/migrations/tenant/0006-note-settings@rename-enable-to-ispublic.sql +++ b/migrations/tenant/0006-note-settings@rename-enable-to-ispublic.sql @@ -1,2 +1,2 @@ -ALTER TABLE IF EXISTS public.notes_settings - RENAME COLUMN IF EXISTS enabled TO is_public; \ No newline at end of file +ALTER TABLE IF EXISTS public.note_settings + RENAME COLUMN enabled TO is_public; \ No newline at end of file