diff --git a/src/domain/service/note.ts b/src/domain/service/note.ts index 42ae2b7b..6ae5a41e 100644 --- a/src/domain/service/note.ts +++ b/src/domain/service/note.ts @@ -9,7 +9,6 @@ import type User from '@domain/entities/user.js'; import type { NoteList } from '@domain/entities/noteList.js'; import type NoteHistoryRepository from '@repository/noteHistory.repository.js'; import type { NoteHistoryMeta, NoteHistoryRecord, NoteHistoryPublic } from '@domain/entities/noteHistory.js'; -import { definePublicNote, type NotePublic } from '@domain/entities/notePublic.js'; /** * Note service @@ -449,13 +448,10 @@ export default class NoteService { * @param noteId - id of the note to get parent structure * @returns - array of notes that are parent structure of the note */ - public async getNoteParents(noteId: NoteInternalId): Promise { + public async getNoteParents(noteId: NoteInternalId): Promise { const noteIds: NoteInternalId[] = await this.noteRelationsRepository.getNoteParentsIds(noteId); const noteParents = await this.noteRepository.getNotesByIds(noteIds); - const noteParentsPublic: NotePublic[] = noteParents.map((note) => { - return definePublicNote(note); - }); - return noteParentsPublic; + return noteParents; } } diff --git a/src/presentation/http/router/note.test.ts b/src/presentation/http/router/note.test.ts index 732accb1..240e27d2 100644 --- a/src/presentation/http/router/note.test.ts +++ b/src/presentation/http/router/note.test.ts @@ -523,7 +523,7 @@ describe('Note API', () => { /** Create test user */ const user = await global.db.insertUser(); - /** Create acces token for the user */ + /** Create access token for the user */ const accessToken = global.auth(user.id); /** Create test note - a parent note */ @@ -572,7 +572,7 @@ describe('Note API', () => { /** Create test user */ const user = await global.db.insertUser(); - /** Create acces token for the user */ + /** Create access token for the user */ const accessToken = global.auth(user.id); /** Create test note - a grand parent note */ @@ -632,14 +632,14 @@ describe('Note API', () => { }); }); - test('Returns one parent note when the user is not in the parent note\'s team but shares a note relation', async () => { + test('Returns one parent note when the user is not in the parent note\'s team but has note relation', async () => { /** Create test user */ const user = await global.db.insertUser(); /** Create another user */ const anotherUser = await global.db.insertUser(); - /** Create acces token for the user */ + /** Create access token for the user */ const accessToken = global.auth(user.id); /** Create test note - a parent note */ @@ -684,11 +684,11 @@ describe('Note API', () => { }); }); - test('Returns no note in case where there is no relation exist for the note with status 200', async () => { + test('Returns empty array in case where there is no relation exist for the note with status 200', async () => { /** Create test user */ const user = await global.db.insertUser(); - /** Create acces token for the user */ + /** Create access token for the user */ const accessToken = global.auth(user.id); /** Create test note - a child note */ diff --git a/src/presentation/http/router/note.ts b/src/presentation/http/router/note.ts index 1d2a715d..3427890b 100644 --- a/src/presentation/http/router/note.ts +++ b/src/presentation/http/router/note.ts @@ -181,12 +181,16 @@ const NoteRouter: FastifyPluginCallback = (fastify, opts, don const noteParentStructure = await noteService.getNoteParents(noteId); + const noteParentsPublic = noteParentStructure.map((notes) => { + return definePublicNote(notes); + }); + return reply.send({ note: notePublic, parentNote: parentNote, accessRights: { canEdit: canEdit }, tools: noteTools, - parents: noteParentStructure, + parents: noteParentsPublic, }); }); diff --git a/src/repository/storage/postgres/orm/sequelize/note.ts b/src/repository/storage/postgres/orm/sequelize/note.ts index d789130e..15b9f8e2 100644 --- a/src/repository/storage/postgres/orm/sequelize/note.ts +++ b/src/repository/storage/postgres/orm/sequelize/note.ts @@ -1,12 +1,11 @@ import type { CreationOptional, InferAttributes, InferCreationAttributes, ModelStatic, NonAttribute, Sequelize } from 'sequelize'; -import { DataTypes, Model } from 'sequelize'; +import { DataTypes, Model, Op } from 'sequelize'; import type Orm from '@repository/storage/postgres/orm/sequelize/index.js'; import type { Note, NoteCreationAttributes, NoteInternalId, NotePublicId } from '@domain/entities/note.js'; import { UserModel } from '@repository/storage/postgres/orm/sequelize/user.js'; import type { NoteSettingsModel } from './noteSettings.js'; import type { NoteVisitsModel } from './noteVisits.js'; import type { NoteHistoryModel } from './noteHistory.js'; -import { notEmpty } from '@infrastructure/utils/empty.js'; /* eslint-disable @typescript-eslint/naming-convention */ @@ -330,17 +329,13 @@ export default class NoteSequelizeStorage { * @param noteIds - list of note ids */ public async getNotesByIds(noteIds: NoteInternalId[]): Promise { - const notes: Note[] = []; - - for (const noteId of noteIds) { - const note: Note | null = await this.model.findOne({ - where: { id: noteId }, - }); - - if (notEmpty(note)) { - notes.push(note); - } - } + const notes: Note[] = await this.model.findAll({ + where: { + id: { + [Op.in]: noteIds, + }, + }, + }); return notes; } diff --git a/src/repository/storage/postgres/orm/sequelize/noteRelations.ts b/src/repository/storage/postgres/orm/sequelize/noteRelations.ts index 8031c9d1..c6ed962d 100644 --- a/src/repository/storage/postgres/orm/sequelize/noteRelations.ts +++ b/src/repository/storage/postgres/orm/sequelize/noteRelations.ts @@ -217,8 +217,6 @@ export default class NoteRelationsSequelizeStorage { * @param noteId - the ID of the note. */ public async getNoteParentsIds(noteId: NoteInternalId): Promise { - let parentNotes: NoteInternalId[] = []; - // get all note ids via a singe sql query instead of many const query = ` WITH RECURSIVE note_parents AS ( @@ -233,20 +231,16 @@ export default class NoteRelationsSequelizeStorage { SELECT np.note_id, np.parent_id FROM note_parents np;`; - try { - const result = await this.database.query(query, { - replacements: { startNoteId: noteId }, - type: QueryTypes.SELECT, - }); + const result = await this.database.query(query, { + replacements: { startNoteId: noteId }, + type: QueryTypes.SELECT, + }); - // eslint-disable-next-line @typescript-eslint/naming-convention - parentNotes = (result as { note_id: number; parent_id: number }[])?.map(note => note.parent_id) ?? []; + // eslint-disable-next-line @typescript-eslint/naming-convention + let noteParents = (result as { note_id: number; parent_id: number }[])?.map(note => note.parent_id) ?? []; - parentNotes.reverse(); - } catch { - console.log(`something wrong happened with sql query`); - } + noteParents.reverse(); - return parentNotes; + return noteParents; } }