Skip to content

Commit

Permalink
update (note parents): update the search of note ids to respect the o…
Browse files Browse the repository at this point in the history
…rder, add related test to this case
  • Loading branch information
dependentmadani committed Oct 17, 2024
1 parent dd1bcdc commit 92c8569
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/domain/service/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,7 @@ export default class NoteService {
}

/**
* Get note parent structure recursively by note id and user id
* and check if user has access to the parent note.
* Return a sequence of parent notes for the given note id.
* @param noteId - id of the note to get parent structure
* @returns - array of notes that are parent structure of the note
*/
Expand Down
30 changes: 15 additions & 15 deletions src/presentation/http/router/note.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,65 +568,65 @@ describe('Note API', () => {
});
});

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

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

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

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

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

/** Create test note settings */
await global.db.insertNoteSetting({
noteId: childNote.id,
noteId: secondNote.id,
isPublic: true,
});

/** Create note relation between parent and grandParentNote */
await global.db.insertNoteRelation({
parentId: grandParentNote.id,
noteId: parentNote.id,
parentId: firstNote.id,
noteId: thirdNote.id,
});

/** Create test note relation */
await global.db.insertNoteRelation({
parentId: parentNote.id,
noteId: childNote.id,
parentId: thirdNote.id,
noteId: secondNote.id,
});

const response = await global.api?.fakeRequest({
method: 'GET',
headers: {
authorization: `Bearer ${accessToken}`,
},
url: `/note/${childNote.publicId}`,
url: `/note/${secondNote.publicId}`,
});

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

expect(response?.json()).toMatchObject({
parents: [
{
id: grandParentNote.publicId,
content: grandParentNote.content,
id: firstNote.publicId,
content: firstNote.content,
},
{
id: parentNote.publicId,
content: parentNote.content,
id: thirdNote.publicId,
content: thirdNote.content,
},
],
});
Expand Down Expand Up @@ -684,7 +684,7 @@ describe('Note API', () => {
});
});

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

Expand Down
7 changes: 7 additions & 0 deletions src/repository/storage/postgres/orm/sequelize/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,19 @@ export default class NoteSequelizeStorage {
* @param noteIds - list of note ids
*/
public async getNotesByIds(noteIds: NoteInternalId[]): Promise<Note[]> {
if (noteIds.length === 0) {
return [];
}

const notes: Note[] = await this.model.findAll({
where: {
id: {
[Op.in]: noteIds,
},
},
order: [
this.database.literal(`ARRAY_POSITION(ARRAY[${noteIds.map(id => `${id}`).join(',')}], id)`),
],
});

return notes;
Expand Down

0 comments on commit 92c8569

Please sign in to comment.