Skip to content

Commit

Permalink
Expose note publicId as id property (#129)
Browse files Browse the repository at this point in the history
* - added notePublic and noteListPublic
- added logic to wrap Note and NoteList to expose public id before sending to frontend

* - Created noteSettingsPublic
- Changed tests to accept note only with public id

* Now note is returned "id" which is public id

* Reset config file to default

* Added requested changes

* canEdit fix

* noteSettings fix

* Fix tests

* changed tests

* Quick fix note.noteSettings whether note had
settings attached, if not then it should be null

* noteSettings is not part of note now, changed logic and tests accordingly

* Resolving conflicts

* Prevented the redecleration of properties of NotePublicSettings that didnt chage

* - moved changeNoteToPublic to domain/entities/notePublic.ts
- Got rid of public noteSetting

* Used utility function in noteList and removed unneccesary checks

* - Created ResponseSchema to serialize the response and updated the comments

* Defined Fastify schema to speed up the json serialization

* Deleted ResponseSchema.ts and updated ref in response schema

* Fix some build errors

* Fix delete note tests

* Fix post note tests

* Fix get note by hostname tests

* Fix get note by id tests

---------

Co-authored-by: Shakhzod Sharifov <[email protected]>
Co-authored-by: Tanya Fomina <[email protected]>
  • Loading branch information
3 people authored Feb 24, 2024
1 parent fbbd62d commit 1d4d4d2
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 159 deletions.
1 change: 0 additions & 1 deletion app-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ database:

openai:
token: 'token'

11 changes: 11 additions & 0 deletions src/domain/entities/noteList.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { Note } from '@domain/entities/note.js';
import type { NotePublic } from '@domain/entities/notePublic.js';


/**
* Note list entity.
Expand All @@ -7,3 +9,12 @@ import type { Note } from '@domain/entities/note.js';
export type NoteList = {
items: Note[];
};

/**
* Public Note list entity .
* An object with list of public notes
*/
export type NoteListPublic = {
items: NotePublic[];
};

27 changes: 27 additions & 0 deletions src/domain/entities/notePublic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { Note } from '@domain/entities/note';

type NotePublicProperties = 'content' | 'createdAt' | 'updatedAt'| 'creatorId';

export interface NotePublic extends Pick<Note, NotePublicProperties> {
/**
* Expose public id as the "id" property
*/
id: string;
}

/**
*Change Note to NotePublic
*
* @param note - Note data to compose a public note
*/
export function definePublicNote(note: Note): NotePublic {
const notePublic: NotePublic = {
id: note.publicId,
content: note.content,
createdAt: note.createdAt,
updatedAt: note.updatedAt,
creatorId: note.creatorId,
};

return notePublic;
}
2 changes: 1 addition & 1 deletion src/domain/entities/noteSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default interface NoteSettings {
/**
* Custom hostname
*/
customHostname?: string;
customHostname?: string ;

/**
* Is note public for everyone or only for collaborators
Expand Down
119 changes: 44 additions & 75 deletions src/presentation/http/router/note.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,22 @@ import noteTeams from '@tests/test-data/note-teams.json';
describe('Note API', () => {
describe('GET note/resolve-hostname/:hostname ', () => {
test('Returns note with specified hostname', async () => {
const expectedResponse = {
'note': {
'id': 2,
'publicId': 'TJmEb89e0l',
'creatorId': 1,
'content': null,
'createdAt': '2023-10-16T13:49:19.000Z',
'updatedAt': '2023-10-16T13:49:19.000Z',
},
'accessRights': {
'canEdit': false,
},
};

const response = await global.api?.fakeRequest({
method: 'GET',
url: '/note/resolve-hostname/codex.so',
});

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

expect(response?.json()).toStrictEqual(expectedResponse);
expect(response?.json()).toMatchObject({
note: {
id: 'TJmEb89e0l',
content: {},
},
accessRights: {
canEdit: false,
},
});
});

test('Returns 404 when note not found', async () => {
Expand All @@ -47,19 +41,6 @@ describe('Note API', () => {
describe('GET note/:notePublicId ', () => {
test('Returns note by public id with 200 status when note is publicly available', async () => {
const correctID = 'Pq1T9vc23Q';
const expectedResponse = {
'note': {
'id': 3,
'publicId': 'Pq1T9vc23Q',
'creatorId': 1,
'content': null,
'createdAt': '2023-10-16T13:49:19.000Z',
'updatedAt': '2023-10-16T13:49:19.000Z',
},
'accessRights': {
'canEdit': false,
},
};

const response = await global.api?.fakeRequest({
method: 'GET',
Expand All @@ -68,23 +49,18 @@ describe('Note API', () => {

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

expect(response?.json()).toStrictEqual(expectedResponse);
expect(response?.json()).toStrictEqual({
note: {
id: 'Pq1T9vc23Q',
content: {},
},
accessRights: {
canEdit: false,
},
});
});

test('Returns note by public id with 200 status when access is disabled, but user is creator', async () => {
const expectedResponse = {
'note': {
'id': 4,
'publicId': '73NdxFZ4k7',
'creatorId': 1,
'content': null,
'createdAt': '2023-10-16T13:49:19.000Z',
'updatedAt': '2023-10-16T13:49:19.000Z',
},
'accessRights': {
'canEdit': true,
},
};
const userId = 1;
const accessToken = global.auth(userId);

Expand All @@ -104,32 +80,19 @@ describe('Note API', () => {

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

expect(response?.json()).toStrictEqual(expectedResponse);
expect(response?.json()).toStrictEqual({
note: {
id: '73NdxFZ4k7',
content: {},
},
accessRights: {
canEdit: true,
},
});
});

test('Returns note and parent note by note public id with 200 status', async () => {
const correctID = 'f43NU75weU';
const expectedResponse = {
'note': {
'id': 54,
'publicId': 'f43NU75weU',
'creatorId': 2,
'content': null,
'createdAt': '2023-10-16T13:49:19.000Z',
'updatedAt': '2023-10-16T13:49:19.000Z',
},
'parentNote': {
'id': 55,
'publicId': 'Hu8Gsm0sA1',
'creatorId': 2,
'content': null,
'createdAt': '2023-10-16T13:49:19.000Z',
'updatedAt': '2023-10-16T13:49:19.000Z',
},
'accessRights': {
'canEdit': false,
},
};

const response = await global.api?.fakeRequest({
method: 'GET',
Expand All @@ -138,7 +101,19 @@ describe('Note API', () => {

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

expect(response?.json()).toStrictEqual(expectedResponse);
expect(response?.json()).toStrictEqual({
note: {
id: 'f43NU75weU',
content: {},
},
parentNote: {
id: 'Hu8Gsm0sA1',
content: {},
},
accessRights: {
canEdit: false,
},
});
});

test('Returns 403 when the note is not public, the user is not authorized', async () => {
Expand Down Expand Up @@ -377,16 +352,10 @@ describe('Note API', () => {

body = await response?.json();

const expectedParentNote = {
'id': 55,
'publicId': 'Hu8Gsm0sA1',
'creatorId': 2,
'content': null,
'createdAt': '2023-10-16T13:49:19.000Z',
'updatedAt': '2023-10-16T13:49:19.000Z',
};

expect(body.parentNote).toStrictEqual(expectedParentNote);
expect(body.parentNote).toMatchObject({
id: 'Hu8Gsm0sA1',
content: {},
});
});

test.todo('Returns 400 when parentId has incorrect characters and lenght');
Expand Down
Loading

0 comments on commit 1d4d4d2

Please sign in to comment.