Skip to content

Commit 92c8569

Browse files
update (note parents): update the search of note ids to respect the order, add related test to this case
1 parent dd1bcdc commit 92c8569

3 files changed

Lines changed: 23 additions & 17 deletions

File tree

src/domain/service/note.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,7 @@ export default class NoteService {
443443
}
444444

445445
/**
446-
* Get note parent structure recursively by note id and user id
447-
* and check if user has access to the parent note.
446+
* Return a sequence of parent notes for the given note id.
448447
* @param noteId - id of the note to get parent structure
449448
* @returns - array of notes that are parent structure of the note
450449
*/

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -568,65 +568,65 @@ describe('Note API', () => {
568568
});
569569
});
570570

571-
test('Returns two note parents in case when note has two parents', async () => {
571+
test('Returns two note parents in case when the note parents IDs relations are provided in a different order than expected', async () => {
572572
/** Create test user */
573573
const user = await global.db.insertUser();
574574

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

578578
/** Create test note - a grand parent note */
579-
const grandParentNote = await global.db.insertNote({
579+
const firstNote = await global.db.insertNote({
580580
creatorId: user.id,
581581
});
582582

583583
/** Create test note - a parent note */
584-
const parentNote = await global.db.insertNote({
584+
const secondNote = await global.db.insertNote({
585585
creatorId: user.id,
586586
});
587587

588588
/** Create test note - a child note */
589-
const childNote = await global.db.insertNote({
589+
const thirdNote = await global.db.insertNote({
590590
creatorId: user.id,
591591
});
592592

593593
/** Create test note settings */
594594
await global.db.insertNoteSetting({
595-
noteId: childNote.id,
595+
noteId: secondNote.id,
596596
isPublic: true,
597597
});
598598

599599
/** Create note relation between parent and grandParentNote */
600600
await global.db.insertNoteRelation({
601-
parentId: grandParentNote.id,
602-
noteId: parentNote.id,
601+
parentId: firstNote.id,
602+
noteId: thirdNote.id,
603603
});
604604

605605
/** Create test note relation */
606606
await global.db.insertNoteRelation({
607-
parentId: parentNote.id,
608-
noteId: childNote.id,
607+
parentId: thirdNote.id,
608+
noteId: secondNote.id,
609609
});
610610

611611
const response = await global.api?.fakeRequest({
612612
method: 'GET',
613613
headers: {
614614
authorization: `Bearer ${accessToken}`,
615615
},
616-
url: `/note/${childNote.publicId}`,
616+
url: `/note/${secondNote.publicId}`,
617617
});
618618

619619
expect(response?.statusCode).toBe(200);
620620

621621
expect(response?.json()).toMatchObject({
622622
parents: [
623623
{
624-
id: grandParentNote.publicId,
625-
content: grandParentNote.content,
624+
id: firstNote.publicId,
625+
content: firstNote.content,
626626
},
627627
{
628-
id: parentNote.publicId,
629-
content: parentNote.content,
628+
id: thirdNote.publicId,
629+
content: thirdNote.content,
630630
},
631631
],
632632
});
@@ -684,7 +684,7 @@ describe('Note API', () => {
684684
});
685685
});
686686

687-
test('Returns empty array 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', async () => {
688688
/** Create test user */
689689
const user = await global.db.insertUser();
690690

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,19 @@ export default class NoteSequelizeStorage {
329329
* @param noteIds - list of note ids
330330
*/
331331
public async getNotesByIds(noteIds: NoteInternalId[]): Promise<Note[]> {
332+
if (noteIds.length === 0) {
333+
return [];
334+
}
335+
332336
const notes: Note[] = await this.model.findAll({
333337
where: {
334338
id: {
335339
[Op.in]: noteIds,
336340
},
337341
},
342+
order: [
343+
this.database.literal(`ARRAY_POSITION(ARRAY[${noteIds.map(id => `${id}`).join(',')}], id)`),
344+
],
338345
});
339346

340347
return notes;

0 commit comments

Comments
 (0)