diff --git a/src/domain/entities/note.ts b/src/domain/entities/note.ts index 8384b706..fbae28a2 100644 --- a/src/domain/entities/note.ts +++ b/src/domain/entities/note.ts @@ -84,9 +84,9 @@ export interface Note { export type NoteCreationAttributes = Pick; /** - * The return type for note parent structure + * Represents the content of a parent note. */ -export type NoteParentStructure = { +export type NoteParentContent = { /** * Note public id */ diff --git a/src/domain/service/note.ts b/src/domain/service/note.ts index e742101b..e1c45319 100644 --- a/src/domain/service/note.ts +++ b/src/domain/service/note.ts @@ -1,4 +1,4 @@ -import type { Note, NoteInternalId, NoteParentStructure, NotePublicId } from '@domain/entities/note.js'; +import type { Note, NoteInternalId, NoteParentContent, NotePublicId } from '@domain/entities/note.js'; import type NoteRepository from '@repository/note.repository.js'; import type NoteVisitsRepository from '@repository/noteVisits.repository.js'; import { createPublicId } from '@infrastructure/utils/id.js'; @@ -457,7 +457,7 @@ export default class NoteService { * @param userId - id of the user that is requesting the parent structure * @returns - array of notes that are parent structure of the note */ - public async getNoteParentStructure(noteId: NoteInternalId, userId: number): Promise> { + public async getNoteParentStructure(noteId: NoteInternalId, userId: number): Promise> { return await this.teamRepository.getAllNotesParents(noteId, userId); } } diff --git a/src/presentation/http/router/note.ts b/src/presentation/http/router/note.ts index 6de6b09d..cc7e9d9b 100644 --- a/src/presentation/http/router/note.ts +++ b/src/presentation/http/router/note.ts @@ -172,6 +172,10 @@ const NoteRouter: FastifyPluginCallback = (fastify, opts, don */ const canEdit = memberRole === MemberRole.Write; + const noteParentContent = await noteService.getNoteParentStructure(noteId, userId!); + + console.log('noteParentContent', noteParentContent); + return reply.send({ note: notePublic, parentNote: parentNote, diff --git a/src/repository/storage/postgres/orm/sequelize/note.ts b/src/repository/storage/postgres/orm/sequelize/note.ts index 4d97591b..d6a4d1ac 100644 --- a/src/repository/storage/postgres/orm/sequelize/note.ts +++ b/src/repository/storage/postgres/orm/sequelize/note.ts @@ -5,7 +5,6 @@ import type { Note, NoteCreationAttributes, NoteInternalId, NotePublicId } from import { UserModel } from '@repository/storage/postgres/orm/sequelize/user.js'; import type { NoteSettingsModel } from './noteSettings.js'; import type { NoteVisitsModel } from './noteVisits.js'; -import { DomainError } from '@domain/entities/DomainError.js'; import type { NoteHistoryModel } from './noteHistory.js'; /* eslint-disable @typescript-eslint/naming-convention */ @@ -233,11 +232,11 @@ export default class NoteSequelizeStorage { */ public async getNoteListByUserId(userId: number, offset: number, limit: number): Promise { if (this.visitsModel === null) { - throw new DomainError('NoteVisit model should be defined'); + throw new Error('NoteStorage: NoteVisit model should be defined'); } if (!this.settingsModel) { - throw new Error('Note settings model not initialized'); + throw new Error('NoteStorage: Note settings model not initialized'); } const reply = await this.model.findAll({ @@ -293,7 +292,7 @@ export default class NoteSequelizeStorage { */ public async getNoteByHostname(hostname: string): Promise { if (!this.settingsModel) { - throw new Error('Note settings model not initialized'); + throw new Error('NoteStorage: Note settings model not initialized'); } /** diff --git a/src/repository/storage/postgres/orm/sequelize/teams.ts b/src/repository/storage/postgres/orm/sequelize/teams.ts index 3e5bfcf4..687e797d 100644 --- a/src/repository/storage/postgres/orm/sequelize/teams.ts +++ b/src/repository/storage/postgres/orm/sequelize/teams.ts @@ -6,8 +6,7 @@ import type { Team, TeamMemberCreationAttributes, TeamMember } from '@domain/ent import { UserModel } from './user.js'; import { MemberRole } from '@domain/entities/team.js'; import type User from '@domain/entities/user.js'; -import type { NoteInternalId, NoteParentStructure } from '@domain/entities/note.js'; -import { DomainError } from '@domain/entities/DomainError.js'; +import type { NoteInternalId, NoteParentContent } from '@domain/entities/note.js'; import type { NoteRelationsModel } from './noteRelations.js'; /** @@ -33,6 +32,11 @@ export class TeamsModel extends Model, InferCreation * Team member role, show what user can do with note */ public declare role: MemberRole; + + /** + * Note relation content + */ + public declare notes?: NoteModel | null; } /** @@ -202,7 +206,7 @@ export default class TeamsSequelizeStorage { */ public async getTeamMembersWithUserInfoByNoteId(noteId: NoteInternalId): Promise { if (!this.userModel) { - throw new DomainError('User model not initialized'); + throw new Error('TeamStorage: User model not defined'); } return await this.model.findAll({ @@ -219,15 +223,15 @@ export default class TeamsSequelizeStorage { /** * Get note parent structure - * @param noteId : the ID of the note. - * @param userId : The ID of the user. + * @param noteId - the ID of the note. + * @param userId - the ID of the user. */ - public async getAllNoteParents(noteId: NoteInternalId, userId: number): Promise { + public async getAllNoteParents(noteId: NoteInternalId, userId: number): Promise { if (!this.noteModel || !this.noteRelationModel) { - throw new DomainError('Note model or Note relation model not initialized'); + throw new Error(`${this.noteModel !== null ? 'TeamStorage: Note relation model is not defined' : 'TeamStorage: Note model is not defined'}`); } - const parentNotes: NoteParentStructure[] = []; + const parentNotes: NoteParentContent[] = []; let currentNoteId: NoteInternalId | null = noteId; while (currentNoteId != null) { @@ -237,20 +241,18 @@ export default class TeamsSequelizeStorage { noteId: currentNoteId, userId, }, + include: { + model: this.noteModel, + as: this.noteModel.tableName, + required: true, + }, }); - if (teamMember) { - // If the user does not have access, add the note to the array - const note = await this.noteModel.findOne({ - where: { id: currentNoteId }, + if (teamMember && teamMember.notes !== undefined && teamMember.notes !== null) { + parentNotes.push({ + noteId: teamMember.notes.publicId, + content: teamMember.notes.content, }); - - if (note) { - parentNotes.push({ - noteId: note.publicId, - content: note.content, - }); - } } // Retrieve the parent note diff --git a/src/repository/team.repository.ts b/src/repository/team.repository.ts index 0a358fa2..4d7e7809 100644 --- a/src/repository/team.repository.ts +++ b/src/repository/team.repository.ts @@ -1,6 +1,6 @@ import type TeamStorage from '@repository/storage/team.storage.js'; import type { Team, TeamMember, TeamMemberCreationAttributes, MemberRole } from '@domain/entities/team.js'; -import type { NoteInternalId, NoteParentStructure } from '@domain/entities/note.js'; +import type { NoteInternalId, NoteParentContent } from '@domain/entities/note.js'; import type User from '@domain/entities/user.js'; /** @@ -63,7 +63,7 @@ export default class TeamRepository { * @param userId : user id to check access * @returns an array of note parents objects containing public id and content */ - public async getAllNotesParents(noteId: NoteInternalId, userId: number): Promise { + public async getAllNotesParents(noteId: NoteInternalId, userId: number): Promise { return await this.storage.getAllNoteParents(noteId, userId); }