Skip to content

Commit 7bfe0eb

Browse files
update (note parents): modify based on last review
1 parent ed49e60 commit 7bfe0eb

5 files changed

Lines changed: 29 additions & 40 deletions

File tree

src/domain/service/note.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import type User from '@domain/entities/user.js';
99
import type { NoteList } from '@domain/entities/noteList.js';
1010
import type NoteHistoryRepository from '@repository/noteHistory.repository.js';
1111
import type { NoteHistoryMeta, NoteHistoryRecord, NoteHistoryPublic } from '@domain/entities/noteHistory.js';
12-
import { definePublicNote, type NotePublic } from '@domain/entities/notePublic.js';
1312

1413
/**
1514
* Note service
@@ -449,13 +448,10 @@ export default class NoteService {
449448
* @param noteId - id of the note to get parent structure
450449
* @returns - array of notes that are parent structure of the note
451450
*/
452-
public async getNoteParents(noteId: NoteInternalId): Promise<NotePublic[]> {
451+
public async getNoteParents(noteId: NoteInternalId): Promise<Note[]> {
453452
const noteIds: NoteInternalId[] = await this.noteRelationsRepository.getNoteParentsIds(noteId);
454453
const noteParents = await this.noteRepository.getNotesByIds(noteIds);
455-
const noteParentsPublic: NotePublic[] = noteParents.map((note) => {
456-
return definePublicNote(note);
457-
});
458454

459-
return noteParentsPublic;
455+
return noteParents;
460456
}
461457
}

src/presentation/http/router/note.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ describe('Note API', () => {
523523
/** Create test user */
524524
const user = await global.db.insertUser();
525525

526-
/** Create acces token for the user */
526+
/** Create access token for the user */
527527
const accessToken = global.auth(user.id);
528528

529529
/** Create test note - a parent note */
@@ -572,7 +572,7 @@ describe('Note API', () => {
572572
/** Create test user */
573573
const user = await global.db.insertUser();
574574

575-
/** Create acces token for the user */
575+
/** Create access token for the user */
576576
const accessToken = global.auth(user.id);
577577

578578
/** Create test note - a grand parent note */
@@ -632,14 +632,14 @@ describe('Note API', () => {
632632
});
633633
});
634634

635-
test('Returns one parent note when the user is not in the parent note\'s team but shares a note relation', async () => {
635+
test('Returns one parent note when the user is not in the parent note\'s team but has note relation', async () => {
636636
/** Create test user */
637637
const user = await global.db.insertUser();
638638

639639
/** Create another user */
640640
const anotherUser = await global.db.insertUser();
641641

642-
/** Create acces token for the user */
642+
/** Create access token for the user */
643643
const accessToken = global.auth(user.id);
644644

645645
/** Create test note - a parent note */
@@ -684,11 +684,11 @@ describe('Note API', () => {
684684
});
685685
});
686686

687-
test('Returns no note in case where there is no relation exist for the note with status 200', async () => {
687+
test('Returns empty array in case where there is no relation exist for the note with status 200', async () => {
688688
/** Create test user */
689689
const user = await global.db.insertUser();
690690

691-
/** Create acces token for the user */
691+
/** Create access token for the user */
692692
const accessToken = global.auth(user.id);
693693

694694
/** Create test note - a child note */

src/presentation/http/router/note.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,16 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
181181

182182
const noteParentStructure = await noteService.getNoteParents(noteId);
183183

184+
const noteParentsPublic = noteParentStructure.map((notes) => {
185+
return definePublicNote(notes);
186+
});
187+
184188
return reply.send({
185189
note: notePublic,
186190
parentNote: parentNote,
187191
accessRights: { canEdit: canEdit },
188192
tools: noteTools,
189-
parents: noteParentStructure,
193+
parents: noteParentsPublic,
190194
});
191195
});
192196

src/repository/storage/postgres/orm/sequelize/note.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import type { CreationOptional, InferAttributes, InferCreationAttributes, ModelStatic, NonAttribute, Sequelize } from 'sequelize';
2-
import { DataTypes, Model } from 'sequelize';
2+
import { DataTypes, Model, Op } from 'sequelize';
33
import type Orm from '@repository/storage/postgres/orm/sequelize/index.js';
44
import type { Note, NoteCreationAttributes, NoteInternalId, NotePublicId } from '@domain/entities/note.js';
55
import { UserModel } from '@repository/storage/postgres/orm/sequelize/user.js';
66
import type { NoteSettingsModel } from './noteSettings.js';
77
import type { NoteVisitsModel } from './noteVisits.js';
88
import type { NoteHistoryModel } from './noteHistory.js';
9-
import { notEmpty } from '@infrastructure/utils/empty.js';
109

1110
/* eslint-disable @typescript-eslint/naming-convention */
1211

@@ -330,17 +329,13 @@ export default class NoteSequelizeStorage {
330329
* @param noteIds - list of note ids
331330
*/
332331
public async getNotesByIds(noteIds: NoteInternalId[]): Promise<Note[]> {
333-
const notes: Note[] = [];
334-
335-
for (const noteId of noteIds) {
336-
const note: Note | null = await this.model.findOne({
337-
where: { id: noteId },
338-
});
339-
340-
if (notEmpty(note)) {
341-
notes.push(note);
342-
}
343-
}
332+
const notes: Note[] = await this.model.findAll({
333+
where: {
334+
id: {
335+
[Op.in]: noteIds,
336+
},
337+
},
338+
});
344339

345340
return notes;
346341
}

src/repository/storage/postgres/orm/sequelize/noteRelations.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,6 @@ export default class NoteRelationsSequelizeStorage {
217217
* @param noteId - the ID of the note.
218218
*/
219219
public async getNoteParentsIds(noteId: NoteInternalId): Promise<NoteInternalId[]> {
220-
let parentNotes: NoteInternalId[] = [];
221-
222220
// get all note ids via a singe sql query instead of many
223221
const query = `
224222
WITH RECURSIVE note_parents AS (
@@ -233,20 +231,16 @@ export default class NoteRelationsSequelizeStorage {
233231
SELECT np.note_id, np.parent_id
234232
FROM note_parents np;`;
235233

236-
try {
237-
const result = await this.database.query(query, {
238-
replacements: { startNoteId: noteId },
239-
type: QueryTypes.SELECT,
240-
});
234+
const result = await this.database.query(query, {
235+
replacements: { startNoteId: noteId },
236+
type: QueryTypes.SELECT,
237+
});
241238

242-
// eslint-disable-next-line @typescript-eslint/naming-convention
243-
parentNotes = (result as { note_id: number; parent_id: number }[])?.map(note => note.parent_id) ?? [];
239+
// eslint-disable-next-line @typescript-eslint/naming-convention
240+
let noteParents = (result as { note_id: number; parent_id: number }[])?.map(note => note.parent_id) ?? [];
244241

245-
parentNotes.reverse();
246-
} catch {
247-
console.log(`something wrong happened with sql query`);
248-
}
242+
noteParents.reverse();
249243

250-
return parentNotes;
244+
return noteParents;
251245
}
252246
}

0 commit comments

Comments
 (0)