From fee85b9144432d853524cba37dfcde8f8c26ab88 Mon Sep 17 00:00:00 2001 From: GeekaN2 Date: Fri, 20 Oct 2023 19:43:15 +0300 Subject: [PATCH] refactor: rename notesSettings to noteSettings --- .../{notesSettings.ts => noteSettings.ts} | 6 +-- src/domain/service/noteSettings.ts | 18 ++++---- src/index.ts | 1 - src/presentation/http/router/noteSettings.ts | 8 ++-- src/repository/noteSettings.repository.ts | 22 +++++----- .../storage/noteSettings.storage.ts | 2 +- .../storage/postgres/orm/sequelize/note.ts | 8 ++-- .../{notesSettings.ts => noteSettings.ts} | 41 +++++++++---------- src/tests/utils/insert-data.ts | 10 ++--- 9 files changed, 57 insertions(+), 59 deletions(-) rename src/domain/entities/{notesSettings.ts => noteSettings.ts} (74%) rename src/repository/storage/postgres/orm/sequelize/{notesSettings.ts => noteSettings.ts} (75%) diff --git a/src/domain/entities/notesSettings.ts b/src/domain/entities/noteSettings.ts similarity index 74% rename from src/domain/entities/notesSettings.ts rename to src/domain/entities/noteSettings.ts index bf07b2bf..2787d9de 100644 --- a/src/domain/entities/notesSettings.ts +++ b/src/domain/entities/noteSettings.ts @@ -1,7 +1,7 @@ /** * Notes settings entity */ -export default interface NotesSettings { +export default interface NoteSettings { /** * Just unique property identifier */ @@ -26,8 +26,8 @@ export default interface NotesSettings { /** * Notes settings creation attributes, omitting id, because it's generated by database */ -type NotesSettingsCreationAttributes = Omit; +type NoteSettingsCreationAttributes = Omit; export type { - NotesSettingsCreationAttributes + NoteSettingsCreationAttributes }; diff --git a/src/domain/service/noteSettings.ts b/src/domain/service/noteSettings.ts index 44f2bb10..b041376d 100644 --- a/src/domain/service/noteSettings.ts +++ b/src/domain/service/noteSettings.ts @@ -1,5 +1,5 @@ import type { Note, NotePublicId } from '@domain/entities/note.js'; -import type NotesSettings from '@domain/entities/notesSettings.js'; +import type NoteSettings from '@domain/entities/noteSettings.js'; import type NoteSettingsRepository from '@repository/noteSettings.repository.js'; /** @@ -24,9 +24,9 @@ export default class NoteSettingsService { * Gets note settings by public id * * @param id - note public id - * @returns { Promise } note settings + * @returns { Promise } note settings */ - public async getNoteSettingsByPublicId(id: NotePublicId): Promise { + public async getNoteSettingsByPublicId(id: NotePublicId): Promise { /** * @todo get internal id by public id and resolve note settings by the internal id */ @@ -37,9 +37,9 @@ export default class NoteSettingsService { * Gets note settings by note id * * @param id - note id - * @returns { Promise } note + * @returns { Promise } note */ - public async getNoteSettingsByNoteId(id: Note['id']): Promise { + public async getNoteSettingsByNoteId(id: Note['id']): Promise { return await this.repository.getNoteSettingsByNoteId(id); } @@ -48,9 +48,9 @@ export default class NoteSettingsService { * * @param noteId - note id * @param enabled - is note enabled - * @returns { Promise } note settings + * @returns { Promise } note settings */ - public async addNoteSettings(noteId: Note['id'], enabled: boolean = true): Promise { + public async addNoteSettings(noteId: Note['id'], enabled: boolean = true): Promise { return await this.repository.addNoteSettings({ noteId: noteId, enabled: enabled, @@ -62,9 +62,9 @@ export default class NoteSettingsService { * * @param data - note settings data with new values * @param noteId - note public id - * @returns { Promise } updated note settings + * @returns { Promise } updated note settings */ - public async patchNoteSettingsByPublicId(data: Partial, noteId: NotePublicId): Promise { + public async patchNoteSettingsByPublicId(data: Partial, noteId: NotePublicId): Promise { const noteSettings = await this.repository.getNoteSettingsByPublicId(noteId); return await this.repository.patchNoteSettingsByPublicId(data, noteSettings.id); diff --git a/src/index.ts b/src/index.ts index 4606c2dd..efe8d064 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,7 +24,6 @@ const start = async (): Promise => { logger.info('Application launched successfully'); } catch (err) { - console.log('err', err); logger.fatal('Failed to start application ' + err); process.exit(1); } diff --git a/src/presentation/http/router/noteSettings.ts b/src/presentation/http/router/noteSettings.ts index eb808a54..73c34a9c 100644 --- a/src/presentation/http/router/noteSettings.ts +++ b/src/presentation/http/router/noteSettings.ts @@ -1,7 +1,7 @@ import type { FastifyPluginCallback } from 'fastify'; import type NoteSettingsService from '@domain/service/noteSettings.js'; import type { NotePublicId } from '@domain/entities/note.js'; -import type NotesSettings from '@domain/entities/notesSettings.js'; +import type NoteSettings from '@domain/entities/noteSettings.js'; import type { Middlewares } from '@presentation/http/middlewares/index.js'; import notEmpty from '@infrastructure/utils/notEmpty.js'; @@ -50,7 +50,7 @@ const NoteSettingsRouter: FastifyPluginCallback = (fa */ fastify.get<{ Params: GetNoteSettingsByNodeIdOptions, - Reply: NotesSettings + Reply: NoteSettings }>('/:id', async (request, reply) => { const params = request.params; /** @@ -74,9 +74,9 @@ const NoteSettingsRouter: FastifyPluginCallback = (fa * Patch noteSettings by note public id */ fastify.patch<{ - Body: Partial, + Body: Partial, Params: GetNoteSettingsByNodeIdOptions, - Reply: NotesSettings, + Reply: NoteSettings, }>('/:id', { preHandler: [opts.middlewares.authRequired, opts.middlewares.withUser] }, async (request, reply) => { const noteId = request.params.id; diff --git a/src/repository/noteSettings.repository.ts b/src/repository/noteSettings.repository.ts index fbced096..893b7839 100644 --- a/src/repository/noteSettings.repository.ts +++ b/src/repository/noteSettings.repository.ts @@ -1,7 +1,7 @@ import type { Note, NotePublicId } from '@domain/entities/note.js'; -import type NotesSettings from '@domain/entities/notesSettings.js'; +import type NoteSettings from '@domain/entities/noteSettings.js'; import type NoteSettingsStorage from '@repository/storage/noteSettings.storage.js'; -import type { NotesSettingsCreationAttributes } from '@domain/entities/notesSettings.js'; +import type { NoteSettingsCreationAttributes } from '@domain/entities/noteSettings.js'; /** * Repository allows accessing data from business-logic (domain) level @@ -25,9 +25,9 @@ export default class NoteSettingsRepository { * Gets note settings by id * * @param id - note id - * @returns { Promise } - found note + * @returns { Promise } - found note */ - public async getNoteSettingsById(id: NotesSettings['id']): Promise { + public async getNoteSettingsById(id: NoteSettings['id']): Promise { return await this.storage.getNoteSettingsById(id); } @@ -35,9 +35,9 @@ export default class NoteSettingsRepository { * Get note settings by note id * * @param id - note public id - * @returns { Promise } found note settings + * @returns { Promise } found note settings */ - public async getNoteSettingsByPublicId(id: NotePublicId): Promise { + public async getNoteSettingsByPublicId(id: NotePublicId): Promise { /** * @todo get internal id by public id and resolve note settings by the internal id */ @@ -48,9 +48,9 @@ export default class NoteSettingsRepository { * Get note settings by note id * * @param id - note id - * @returns { Promise } found note settings + * @returns { Promise } found note settings */ - public async getNoteSettingsByNoteId(id: Note['id']): Promise { + public async getNoteSettingsByNoteId(id: Note['id']): Promise { return await this.storage.getNoteSettingsByNoteId(id); } @@ -59,7 +59,7 @@ export default class NoteSettingsRepository { * * @param settings - note settings */ - public async addNoteSettings(settings: NotesSettingsCreationAttributes): Promise { + public async addNoteSettings(settings: NoteSettingsCreationAttributes): Promise { return await this.storage.insertNoteSettings(settings); } @@ -68,9 +68,9 @@ export default class NoteSettingsRepository { * * @param data - note settings new values * @param id - note settings id - * @returns { Promise } patched note settings + * @returns { Promise } patched note settings */ - public async patchNoteSettingsByPublicId(data: Partial, id: NotesSettings['id']): Promise { + public async patchNoteSettingsByPublicId(data: Partial, id: NoteSettings['id']): Promise { return await this.storage.patchNoteSettingsByPublicId(data, id); } } diff --git a/src/repository/storage/noteSettings.storage.ts b/src/repository/storage/noteSettings.storage.ts index 7e14af9d..d3941646 100644 --- a/src/repository/storage/noteSettings.storage.ts +++ b/src/repository/storage/noteSettings.storage.ts @@ -1,4 +1,4 @@ -import NoteSettingsSequelizeStorage from './postgres/orm/sequelize/notesSettings.js'; +import NoteSettingsSequelizeStorage from './postgres/orm/sequelize/noteSettings.js'; /** * Current note storage */ diff --git a/src/repository/storage/postgres/orm/sequelize/note.ts b/src/repository/storage/postgres/orm/sequelize/note.ts index 6a4be972..51927235 100644 --- a/src/repository/storage/postgres/orm/sequelize/note.ts +++ b/src/repository/storage/postgres/orm/sequelize/note.ts @@ -3,7 +3,7 @@ import { Model, DataTypes } from 'sequelize'; import type Orm from '@repository/storage/postgres/orm/sequelize/index.js'; import type { Note, NoteInternalId, NotePublicId } from '@domain/entities/note.js'; import type { NoteCreationAttributes } from '@domain/entities/note.js'; -import type { NotesSettingsModel } from '@repository/storage/postgres/orm/sequelize/notesSettings.js'; +import type { NoteSettingsModel } from '@repository/storage/postgres/orm/sequelize/noteSettings.js'; import { UserModel } from '@repository/storage/postgres/orm/sequelize/user.js'; /* eslint-disable @typescript-eslint/naming-convention */ @@ -56,7 +56,7 @@ export default class NoteSequelizeStorage { /** * Notes settings model in database */ - public settingsModel: typeof NotesSettingsModel; + public settingsModel: typeof NoteSettingsModel; /** * Database instance @@ -74,7 +74,7 @@ export default class NoteSequelizeStorage { * @param ormInstance - ORM instance * @param settingsModel - note customization parameters */ - constructor({ connection }: Orm, settingsModel: typeof NotesSettingsModel) { + constructor({ connection }: Orm, settingsModel: typeof NoteSettingsModel) { this.database = connection; /** @@ -112,7 +112,7 @@ export default class NoteSequelizeStorage { */ this.settingsModel = settingsModel; - /** NoteModel and NotesSettingsModel are connected as ONE-TO-ONE */ + /** NoteModel and NoteSettingsModel are connected as ONE-TO-ONE */ this.model.hasOne(this.settingsModel, { foreignKey: 'note_id', as: this.settingsModel.tableName, diff --git a/src/repository/storage/postgres/orm/sequelize/notesSettings.ts b/src/repository/storage/postgres/orm/sequelize/noteSettings.ts similarity index 75% rename from src/repository/storage/postgres/orm/sequelize/notesSettings.ts rename to src/repository/storage/postgres/orm/sequelize/noteSettings.ts index 6540cf73..61039f47 100644 --- a/src/repository/storage/postgres/orm/sequelize/notesSettings.ts +++ b/src/repository/storage/postgres/orm/sequelize/noteSettings.ts @@ -3,34 +3,34 @@ import { Model, DataTypes } from 'sequelize'; import type Orm from '@repository/storage/postgres/orm/sequelize/index.js'; import type { NotePublicId } from '@domain/entities/note.js'; import { NoteModel } from '@repository/storage/postgres/orm/sequelize/note.js'; -import type NotesSettings from '@domain/entities/notesSettings.js'; -import type { NotesSettingsCreationAttributes } from '@domain/entities/notesSettings.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 */ -export class NotesSettingsModel extends Model, InferCreationAttributes> { +export class NoteSettingsModel extends Model, InferCreationAttributes> { /** * Note Settings id */ - public declare id: CreationOptional; + public declare id: CreationOptional; /** * Note ID */ - public declare note_id: NotesSettings['noteId']; + public declare note_id: NoteSettings['noteId']; /** * Custom hostname */ - public declare custom_hostname: CreationOptional; + public declare custom_hostname: CreationOptional; /** * Is note public */ - public declare enabled: CreationOptional; + public declare enabled: CreationOptional; } /** @@ -40,7 +40,7 @@ export default class NoteSettingsSequelizeStorage { /** * Notes settings model in database */ - public model: typeof NotesSettingsModel; + public model: typeof NoteSettingsModel; /** * Database instance @@ -50,7 +50,7 @@ export default class NoteSettingsSequelizeStorage { /** * Settings table name */ - private readonly tableName = 'notes_settings'; + private readonly tableName = 'note_settings'; /** * Constructor for note storage @@ -60,11 +60,10 @@ export default class NoteSettingsSequelizeStorage { constructor({ connection }: Orm) { this.database = connection; - console.log("GIVE ME THE ONNECTION", this.database) /** * Initiate note settings model */ - this.model = NotesSettingsModel.init({ + this.model = NoteSettingsModel.init({ id: { type: DataTypes.INTEGER, autoIncrement: true, @@ -99,9 +98,9 @@ export default class NoteSettingsSequelizeStorage { * Gets note settings by id * * @param id - note id - * @returns { Promise } found note + * @returns { Promise } found note */ - public async getNoteSettingsById(id: NotesSettings['id']): Promise { + public async getNoteSettingsById(id: NoteSettings['id']): Promise { const noteSettings = await this.model.findOne({ where: { id, @@ -127,9 +126,9 @@ export default class NoteSettingsSequelizeStorage { * Get note settings * * @param noteId - note id - * @returns { Promise } - note settings + * @returns { Promise } - note settings */ - public async getNoteSettingsByNoteId(noteId: NotesSettings['noteId']): Promise { + public async getNoteSettingsByNoteId(noteId: NoteSettings['noteId']): Promise { const settings = await this.model.findOne({ where: { note_id: noteId, @@ -155,12 +154,12 @@ export default class NoteSettingsSequelizeStorage { * Get note settings * * @param id - note internal id - * @returns { Promise } - note settings + * @returns { Promise } - note settings * * @deprecated * @todo resolve note setting by internal id */ - public async getNoteSettingsByPublicId(id: NotePublicId): Promise { + public async getNoteSettingsByPublicId(id: NotePublicId): Promise { const settings = await this.model.findOne({ where: { id: id, @@ -186,14 +185,14 @@ export default class NoteSettingsSequelizeStorage { * Insert note settings * * @param options - note settings options - * @returns { Promise } - inserted note settings + * @returns { Promise } - inserted note settings */ public async insertNoteSettings({ noteId, customHostname, enabled, - }: NotesSettingsCreationAttributes - ): Promise { + }: NoteSettingsCreationAttributes + ): Promise { const settings = await this.model.create({ note_id: noteId, custom_hostname: customHostname, @@ -214,7 +213,7 @@ export default class NoteSettingsSequelizeStorage { * @param data - note settings new data * @param id - note settings id */ - public async patchNoteSettingsByPublicId(data: Partial, id: NotesSettings['id']): Promise { + public async patchNoteSettingsByPublicId(data: Partial, id: NoteSettings['id']): Promise { const settingsToUpdate = await this.model.findByPk(id); /** diff --git a/src/tests/utils/insert-data.ts b/src/tests/utils/insert-data.ts index 0535d222..5b41b130 100644 --- a/src/tests/utils/insert-data.ts +++ b/src/tests/utils/insert-data.ts @@ -1,7 +1,7 @@ import type SequelizeOrm from '@repository/storage/postgres/orm/index.js'; import users from '../test-data/users.json'; import notes from '../test-data/notes.json'; -import notesSettings from '../test-data/notes-settings.json'; +import noteSettings from '../test-data/notes-settings.json'; /** * Fills in the database with users data @@ -30,9 +30,9 @@ async function insertNotes(db: SequelizeOrm): Promise { * * @param db - SequelizeOrm instance */ -async function insertNotesSettings(db: SequelizeOrm): Promise { - for (const noteSettings of notesSettings) { - await db.connection.query(`INSERT INTO public.notes_settings (id, "note_id", "custom_hostname", "enabled") VALUES (${noteSettings.id}, '${noteSettings.note_id}', '${noteSettings.custom_hostname}', ${noteSettings.enabled})`); +async function insertNoteSettings(db: SequelizeOrm): Promise { + for (const noteSetting of noteSettings) { + await db.connection.query(`INSERT INTO public.notes_settings (id, "note_id", "custom_hostname", "enabled") VALUES (${noteSetting.id}, '${noteSetting.note_id}', '${noteSetting.custom_hostname}', ${noteSetting.enabled})`); } } @@ -45,6 +45,6 @@ async function insertNotesSettings(db: SequelizeOrm): Promise { export async function insertData(db: SequelizeOrm): Promise { await insertUsers(db); await insertNotes(db); - await insertNotesSettings(db); + await insertNoteSettings(db); }