Skip to content

Commit

Permalink
Changes to note test and default childnotes null
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek Jasud committed Feb 16, 2025
1 parent 47a3056 commit 6d3919a
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 53 deletions.
114 changes: 68 additions & 46 deletions src/presentation/http/router/note.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MemberRole } from '@domain/entities/team.js';
import { describe, test, expect, beforeEach } from 'vitest';
import type User from '@domain/entities/user.js';
import type { Note } from '@domain/entities/note.js';

describe('Note API', () => {
/**
Expand Down Expand Up @@ -2284,64 +2285,85 @@ describe('Note API', () => {
accessToken = global.auth(user.id);
});

test('Get note hierarchy with no parent or child', async () => {
/**
* Insert test note, note history record will be inserted automatically
*/
const note = await global.db.insertNote({
creatorId: user.id,
});

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

expect(response?.json().notehierarchy.id).toBe(note.publicId);
expect(response?.json().notehierarchy.childNotes).toHaveLength(0);
});
test.each([
// Test case 1: No parent or child
{
description: 'Get note hierarchy with no parent or child',
setup: async () => {
const note = await global.db.insertNote({ creatorId: user.id });

test('Get note hierarchy with child', async () => {
/* create test child note */
const childNote = await global.db.insertNote({
creatorId: user.id,
});
await global.db.insertNoteSetting({
noteId: note.id,
isPublic: true,
});

/* create test parent note */
const parentNote = await global.db.insertNote({
creatorId: user.id,
});
return {
note: note,
childNote: null,
};
},

/* create note settings for child note */
await global.db.insertNoteSetting({
noteId: childNote.id,
isPublic: true,
});
expected: (note: Note, childNote: Note | null) => ({
id: note.publicId,
content: note.content,
childNotes: childNote,
}),
},

/* create test relation */
await global.db.insertNoteRelation({
noteId: childNote.id,
parentId: parentNote.id,
});
// Test case 2: With child
{
description: 'Get note hierarchy with child',
setup: async () => {
const childNote = await global.db.insertNote({ creatorId: user.id });
const parentNote = await global.db.insertNote({ creatorId: user.id });

await global.db.insertNoteSetting({
noteId: childNote.id,
isPublic: true,
});
await global.db.insertNoteSetting({
noteId: parentNote.id,
isPublic: true,
});
await global.db.insertNoteRelation({
noteId: childNote.id,
parentId: parentNote.id,
});

return {
note: parentNote,
childNote: childNote,
};
},
expected: (note: Note, childNote: Note | null) => ({
id: note.publicId,
content: note.content,
childNotes: [
{
id: childNote?.publicId,
content: childNote?.content,
childNotes: null,
},
],
}),
},
])('$description', async ({ setup, expected }) => {
// Setup the test data
const { note, childNote } = await setup();

// Make the API request
const response = await global.api?.fakeRequest({
method: 'GET',
headers: {
authorization: `Bearer ${accessToken}`,
},
url: `/note/note-hierarchy/${parentNote.publicId}`,
url: `/note/note-hierarchy/${note.publicId}`,
});

const childNoteObj = {
id: childNote.publicId,
content: childNote.content,
childNotes: [],
};

expect(response?.json().notehierarchy.childNotes[0]).toStrictEqual(childNoteObj);
// Verify the response
expect(response?.json().noteHierarchy).toStrictEqual(
expected(note, childNote)
);
});
});
});
14 changes: 8 additions & 6 deletions src/presentation/http/router/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,12 +782,12 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
notePublicId: NotePublicId;
};
Reply: {
notehierarchy: NoteHierarchy | null;
noteHierarchy: NoteHierarchy | null;
} | ErrorResponse;
}>('/note-hierarchy/:notePublicId', {
config: {
policy: [
'authRequired',
'notePublicOrUserInTeam',
],
},
schema: {
Expand All @@ -800,7 +800,7 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
'2xx': {
type: 'object',
properties: {
notehierarchy: {
noteHierarchy: {
$ref: 'NoteHierarchySchema#',
},
},
Expand All @@ -809,21 +809,23 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
},
preHandler: [
noteResolver,
noteSettingsResolver,
memberRoleResolver,
],
}, async (request, reply) => {
const noteId = request?.note?.id as number;
const noteId = request.note?.id as number;

/**
* Check if note exists
*/
if (noteId === null) {
return reply.notFound('Note not found');
return reply.notAcceptable('Note not found');
}

const noteHierarchy = await noteService.getNoteHierarchy(noteId);

return reply.send({
notehierarchy: noteHierarchy,
noteHierarchy: noteHierarchy,
});
});

Expand Down
6 changes: 5 additions & 1 deletion src/repository/storage/postgres/orm/sequelize/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ export default class NoteSequelizeStorage {
notesMap.set(note.noteid, {
id: note.public_id,
content: note.content,
childNotes: [],
childNotes: null,
});
});

Expand All @@ -419,6 +419,10 @@ export default class NoteSequelizeStorage {
const parent = notesMap.get(note.parent_id);

if (parent) {
// Initialize childNotes as an array if it's null
if (parent.childNotes === null) {
parent.childNotes = [];
}
parent.childNotes?.push(notesMap.get(note.noteid)!);
}
}
Expand Down

0 comments on commit 6d3919a

Please sign in to comment.