Skip to content

Commit 6d3919a

Browse files
author
Abhishek Jasud
committed
Changes to note test and default childnotes null
1 parent 47a3056 commit 6d3919a

3 files changed

Lines changed: 81 additions & 53 deletions

File tree

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

Lines changed: 68 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { MemberRole } from '@domain/entities/team.js';
22
import { describe, test, expect, beforeEach } from 'vitest';
33
import type User from '@domain/entities/user.js';
4+
import type { Note } from '@domain/entities/note.js';
45

56
describe('Note API', () => {
67
/**
@@ -2284,64 +2285,85 @@ describe('Note API', () => {
22842285
accessToken = global.auth(user.id);
22852286
});
22862287

2287-
test('Get note hierarchy with no parent or child', async () => {
2288-
/**
2289-
* Insert test note, note history record will be inserted automatically
2290-
*/
2291-
const note = await global.db.insertNote({
2292-
creatorId: user.id,
2293-
});
2294-
2295-
const response = await global.api?.fakeRequest({
2296-
method: 'GET',
2297-
headers: {
2298-
authorization: `Bearer ${accessToken}`,
2299-
},
2300-
url: `/note/note-hierarchy/${note.publicId}`,
2301-
});
2302-
2303-
expect(response?.json().notehierarchy.id).toBe(note.publicId);
2304-
expect(response?.json().notehierarchy.childNotes).toHaveLength(0);
2305-
});
2288+
test.each([
2289+
// Test case 1: No parent or child
2290+
{
2291+
description: 'Get note hierarchy with no parent or child',
2292+
setup: async () => {
2293+
const note = await global.db.insertNote({ creatorId: user.id });
23062294

2307-
test('Get note hierarchy with child', async () => {
2308-
/* create test child note */
2309-
const childNote = await global.db.insertNote({
2310-
creatorId: user.id,
2311-
});
2295+
await global.db.insertNoteSetting({
2296+
noteId: note.id,
2297+
isPublic: true,
2298+
});
23122299

2313-
/* create test parent note */
2314-
const parentNote = await global.db.insertNote({
2315-
creatorId: user.id,
2316-
});
2300+
return {
2301+
note: note,
2302+
childNote: null,
2303+
};
2304+
},
23172305

2318-
/* create note settings for child note */
2319-
await global.db.insertNoteSetting({
2320-
noteId: childNote.id,
2321-
isPublic: true,
2322-
});
2306+
expected: (note: Note, childNote: Note | null) => ({
2307+
id: note.publicId,
2308+
content: note.content,
2309+
childNotes: childNote,
2310+
}),
2311+
},
23232312

2324-
/* create test relation */
2325-
await global.db.insertNoteRelation({
2326-
noteId: childNote.id,
2327-
parentId: parentNote.id,
2328-
});
2313+
// Test case 2: With child
2314+
{
2315+
description: 'Get note hierarchy with child',
2316+
setup: async () => {
2317+
const childNote = await global.db.insertNote({ creatorId: user.id });
2318+
const parentNote = await global.db.insertNote({ creatorId: user.id });
2319+
2320+
await global.db.insertNoteSetting({
2321+
noteId: childNote.id,
2322+
isPublic: true,
2323+
});
2324+
await global.db.insertNoteSetting({
2325+
noteId: parentNote.id,
2326+
isPublic: true,
2327+
});
2328+
await global.db.insertNoteRelation({
2329+
noteId: childNote.id,
2330+
parentId: parentNote.id,
2331+
});
2332+
2333+
return {
2334+
note: parentNote,
2335+
childNote: childNote,
2336+
};
2337+
},
2338+
expected: (note: Note, childNote: Note | null) => ({
2339+
id: note.publicId,
2340+
content: note.content,
2341+
childNotes: [
2342+
{
2343+
id: childNote?.publicId,
2344+
content: childNote?.content,
2345+
childNotes: null,
2346+
},
2347+
],
2348+
}),
2349+
},
2350+
])('$description', async ({ setup, expected }) => {
2351+
// Setup the test data
2352+
const { note, childNote } = await setup();
23292353

2354+
// Make the API request
23302355
const response = await global.api?.fakeRequest({
23312356
method: 'GET',
23322357
headers: {
23332358
authorization: `Bearer ${accessToken}`,
23342359
},
2335-
url: `/note/note-hierarchy/${parentNote.publicId}`,
2360+
url: `/note/note-hierarchy/${note.publicId}`,
23362361
});
23372362

2338-
const childNoteObj = {
2339-
id: childNote.publicId,
2340-
content: childNote.content,
2341-
childNotes: [],
2342-
};
2343-
2344-
expect(response?.json().notehierarchy.childNotes[0]).toStrictEqual(childNoteObj);
2363+
// Verify the response
2364+
expect(response?.json().noteHierarchy).toStrictEqual(
2365+
expected(note, childNote)
2366+
);
23452367
});
23462368
});
23472369
});

src/presentation/http/router/note.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -782,12 +782,12 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
782782
notePublicId: NotePublicId;
783783
};
784784
Reply: {
785-
notehierarchy: NoteHierarchy | null;
785+
noteHierarchy: NoteHierarchy | null;
786786
} | ErrorResponse;
787787
}>('/note-hierarchy/:notePublicId', {
788788
config: {
789789
policy: [
790-
'authRequired',
790+
'notePublicOrUserInTeam',
791791
],
792792
},
793793
schema: {
@@ -800,7 +800,7 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
800800
'2xx': {
801801
type: 'object',
802802
properties: {
803-
notehierarchy: {
803+
noteHierarchy: {
804804
$ref: 'NoteHierarchySchema#',
805805
},
806806
},
@@ -809,21 +809,23 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
809809
},
810810
preHandler: [
811811
noteResolver,
812+
noteSettingsResolver,
813+
memberRoleResolver,
812814
],
813815
}, async (request, reply) => {
814-
const noteId = request?.note?.id as number;
816+
const noteId = request.note?.id as number;
815817

816818
/**
817819
* Check if note exists
818820
*/
819821
if (noteId === null) {
820-
return reply.notFound('Note not found');
822+
return reply.notAcceptable('Note not found');
821823
}
822824

823825
const noteHierarchy = await noteService.getNoteHierarchy(noteId);
824826

825827
return reply.send({
826-
notehierarchy: noteHierarchy,
828+
noteHierarchy: noteHierarchy,
827829
});
828830
});
829831

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ export default class NoteSequelizeStorage {
407407
notesMap.set(note.noteid, {
408408
id: note.public_id,
409409
content: note.content,
410-
childNotes: [],
410+
childNotes: null,
411411
});
412412
});
413413

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

421421
if (parent) {
422+
// Initialize childNotes as an array if it's null
423+
if (parent.childNotes === null) {
424+
parent.childNotes = [];
425+
}
422426
parent.childNotes?.push(notesMap.get(note.noteid)!);
423427
}
424428
}

0 commit comments

Comments
 (0)