Skip to content

Commit 4ccb075

Browse files
update(note parents): based on last review, still work on sql
1 parent 5a9fad2 commit 4ccb075

3 files changed

Lines changed: 26 additions & 26 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ describe('Note API', () => {
519519
expect(response?.json().message).toStrictEqual(expectedMessage);
520520
});
521521

522-
test('Returns one parents note in case when note has one parents', async () => {
522+
test('Returns one parents note in case when note has one parent', async () => {
523523
/** Create test user */
524524
const user = await global.db.insertUser();
525525

@@ -572,7 +572,7 @@ describe('Note API', () => {
572572
});
573573
});
574574

575-
test('Returns two parents note in case when note has two parents', async () => {
575+
test('Returns two note parents in case when note has two parents', async () => {
576576
/** Create test user */
577577
const user = await global.db.insertUser();
578578

@@ -640,7 +640,7 @@ describe('Note API', () => {
640640
});
641641
});
642642

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

src/repository/noteRelations.repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ export default class NoteRelationsRepository {
7474
* @returns an array of note parents ids
7575
*/
7676
public async getNoteParentsIds(noteId: NoteInternalId): Promise<NoteInternalId[]> {
77-
return await this.storage.getAllNoteParentsIds(noteId);
77+
return await this.storage.getNoteParentsIds(noteId);
7878
}
7979
}

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { CreationOptional, InferAttributes, InferCreationAttributes, ModelStatic, Sequelize } from 'sequelize';
2+
import { QueryTypes } from 'sequelize';
23
import { Op } from 'sequelize';
34
import { NoteModel } from '@repository/storage/postgres/orm/sequelize/note.js';
45
import type Orm from '@repository/storage/postgres/orm/sequelize/index.js';
56
import type { NoteInternalId } from '@domain/entities/note.js';
67
import type { Note } from '@domain/entities/note.js';
78
import { Model, DataTypes } from 'sequelize';
8-
import { isEmpty, notEmpty } from '@infrastructure/utils/empty.js';
99

1010
/**
1111
* Class representing a note relations in database
@@ -216,29 +216,29 @@ export default class NoteRelationsSequelizeStorage {
216216
* where the user has access to.
217217
* @param noteId - the ID of the note.
218218
*/
219-
public async getAllNoteParentsIds(noteId: NoteInternalId): Promise<NoteInternalId[]> {
220-
const parentNotes: NoteInternalId[] = [];
221-
let currentNoteId: number | null = noteId;
219+
public async getNoteParentsIds(noteId: NoteInternalId): Promise<NoteInternalId[]> {
220+
let parentNotes: NoteInternalId[] = [];
222221

223222
// get all note ids via a singe sql query instead of many
224-
while (currentNoteId !== null) {
225-
const noteRelation: NoteRelationsModel | null = await this.model.findOne({
226-
where: {
227-
noteId: currentNoteId,
228-
},
229-
});
230-
231-
if (notEmpty(noteRelation)) {
232-
parentNotes.push(noteRelation.noteId);
233-
}
234-
235-
if (isEmpty(noteRelation)) {
236-
parentNotes.push(currentNoteId);
237-
break;
238-
} else {
239-
currentNoteId = noteRelation.parentId ?? null;
240-
}
241-
}
223+
const query = `
224+
WITH RECURSIVE note_tree AS (
225+
SELECT noteId, parentId
226+
FROM NoteRelations
227+
WHERE noteId = :startNoteId
228+
UNION ALL
229+
SELECT nr.noteId, nr.parentId
230+
FROM NoteRelations nr
231+
INNER JOIN note_tree nt ON nt.parentId = nr.noteId
232+
)
233+
SELECT noteId FROM note_tree;
234+
`;
235+
236+
const result = await this.model.sequelize?.query(query, {
237+
replacements: { startNoteId: noteId },
238+
type: QueryTypes.SELECT,
239+
});
240+
241+
parentNotes = (result as { noteId: number }[])?.map(note => note.noteId) ?? [];
242242

243243
parentNotes.reverse();
244244

0 commit comments

Comments
 (0)