Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose note publicId as id property #129

Merged
merged 37 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
3c81a84
- added notePublic and noteListPublic
shvkhzod Nov 9, 2023
cf6d8a1
- Created noteSettingsPublic
shvkhzod Nov 20, 2023
3ee8572
Merge branch 'expose-publicId-only' into chore/get-note-by-id-tests
elizachi Nov 20, 2023
2c1a4c9
Now note is returned "id" which is public id
elizachi Nov 20, 2023
6b375dc
Reset config file to default
shvkhzod Nov 21, 2023
197ebbd
Merge branch 'main' into chore/get-note-by-id-tests
elizachi Nov 22, 2023
411be1e
Merge remote-tracking branch 'origin/chore/get-note-by-id-tests' into…
elizachi Nov 23, 2023
921d266
Added requested changes
shvkhzod Nov 27, 2023
36f7799
Merge branch 'chore/get-note-by-id-tests' of https://github.com/codex…
shvkhzod Nov 27, 2023
2314d76
canEdit fix
shvkhzod Nov 27, 2023
aed654c
Merge branch 'main' into chore/get-note-by-id-tests
elizachi Nov 27, 2023
f0dcad4
noteSettings fix
shvkhzod Nov 27, 2023
d7fadf0
Merge remote-tracking branch 'origin/chore/get-note-by-id-tests' into…
elizachi Nov 27, 2023
2a838b4
Fix tests
elizachi Nov 27, 2023
a789b39
changed tests
shvkhzod Nov 27, 2023
408f512
Merge remote-tracking branch 'origin/chore/get-note-by-id-tests' into…
elizachi Nov 27, 2023
e1c1e83
Quick fix note.noteSettings whether note had
elizachi Nov 27, 2023
cd7a688
Merge branch 'main' into chore/get-note-by-id-tests
elizachi Nov 30, 2023
3da3afc
noteSettings is not part of note now, changed logic and tests accordi…
shvkhzod Dec 7, 2023
f82a281
Merge branch 'chore/get-note-by-id-tests' of https://github.com/codex…
shvkhzod Dec 7, 2023
d13d75e
Resolving conflicts
shvkhzod Dec 13, 2023
d88de31
Merge remote-tracking branch 'origin' into chore/get-note-by-id-tests
shvkhzod Dec 16, 2023
2c6a8ed
Prevented the redecleration of properties of NotePublicSettings that …
shvkhzod Jan 14, 2024
7a0a622
- moved changeNoteToPublic to domain/entities/notePublic.ts
shvkhzod Jan 24, 2024
13787ed
Used utility function in noteList and removed unneccesary checks
shvkhzod Jan 28, 2024
47c4a95
- Created ResponseSchema to serialize the response and updated the co…
shvkhzod Feb 6, 2024
ef597e8
Defined Fastify schema to speed up the json serialization
shvkhzod Feb 9, 2024
1127ab6
Deleted ResponseSchema.ts and updated ref in response schema
shvkhzod Feb 10, 2024
b0c1c5c
Merge branch 'main' into chore/get-note-by-id-tests
elizachi Feb 23, 2024
314ce92
Fix some build errors
elizachi Feb 23, 2024
42c035c
Merge branch 'main' into chore/get-note-by-id-tests
shvkhzod Feb 23, 2024
17c0af0
Merge branch 'main' into chore/get-note-by-id-tests
TatianaFomina Feb 24, 2024
f3e3958
Merge branch 'main' into chore/get-note-by-id-tests
TatianaFomina Feb 24, 2024
2f81cbb
Fix delete note tests
TatianaFomina Feb 24, 2024
61ba92a
Fix post note tests
TatianaFomina Feb 24, 2024
01bd2d1
Fix get note by hostname tests
TatianaFomina Feb 24, 2024
255d612
Fix get note by id tests
TatianaFomina Feb 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -20,7 +20,7 @@ export default interface NoteSettings {
/**
* Custom hostname
*/
customHostname?: string;
customHostname?: string ;
TatianaFomina marked this conversation as resolved.
Show resolved Hide resolved

/**
* Is note public for everyone or only for collaborators
Expand Down
28 changes: 10 additions & 18 deletions src/presentation/http/router/note.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,11 @@ describe('Note API', () => {
const expectedStatus = 200;
const expectedResponse = {
'note': {
'id': 1,
'publicId': 'note_1',
'id': 'note_1',
'creatorId': 1,
'content': null,
'createdAt': '2023-10-16T13:49:19.000Z',
'updatedAt': '2023-10-16T13:49:19.000Z',
'noteSettings': {
'customHostname': 'codex.so',
'isPublic': true,
'id': 1,
'noteId': 1,
'invitationHash': 'Hzh2hy4igf',
},
},
'accessRights': {
'canEdit': false,
Expand Down Expand Up @@ -59,8 +51,7 @@ describe('Note API', () => {
const correctID = 'Pq1T9vc23Q';
const expectedResponse = {
'note': {
'id': 2,
'publicId': 'Pq1T9vc23Q',
'id': 'Pq1T9vc23Q',
'creatorId': 1,
'content': null,
'createdAt': '2023-10-16T13:49:19.000Z',
Expand All @@ -69,6 +60,7 @@ describe('Note API', () => {
'accessRights': {
'canEdit': false,
},

};

const response = await global.api?.fakeRequest({
Expand All @@ -78,15 +70,18 @@ describe('Note API', () => {

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

expect(response?.json()).toStrictEqual(expectedResponse);
const res = response?.json();

expect(res).toStrictEqual(expectedResponse);
});

test('Returns note by public id with 200 status when access is disabled, but user is creator', async () => {
const expectedStatus = 200;
const userId = 1;
const accessToken = global.auth(userId);
const expectedResponse = {
'note': {
'id': 3,
'publicId': '73NdxFZ4k7',
'id': '73NdxFZ4k7',
'creatorId': 1,
'content': null,
'createdAt': '2023-10-16T13:49:19.000Z',
Expand All @@ -95,9 +90,8 @@ describe('Note API', () => {
'accessRights': {
'canEdit': true,
},

};
const userId = 1;
const accessToken = global.auth(userId);

const privateUserNote = notes.find(newNote => {
const settings = noteSettings.find(ns => ns.note_id === newNote.id);
Expand Down Expand Up @@ -251,7 +245,5 @@ describe('Note API', () => {
expect(response?.json().accessRights).toStrictEqual({ canEdit: true });
});
});

test.todo('API should not return internal id and "publicId". It should return only "id" which is public id.');
});
});
86 changes: 71 additions & 15 deletions src/presentation/http/router/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ErrorResponse } from '@presentation/http/types/HttpResponse.js';
import type { Note, NotePublicId } from '@domain/entities/note.js';
import useNoteResolver from '../middlewares/note/useNoteResolver.js';
import useNoteSettingsResolver from '../middlewares/noteSettings/useNoteSettingsResolver.js';
import { type NotePublic, definePublicNote } from '@domain/entities/notePublic.js';

/**
* Interface for the note router.
Expand Down Expand Up @@ -55,9 +56,11 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
notePublicId: NotePublicId;
},
Reply: {
note : Note
accessRights: { canEdit: boolean },
} | ErrorResponse,
note: NotePublic,
accessRights: {
canEdit: boolean,
},
}| ErrorResponse,
}>('/:notePublicId', {
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
config: {
policy: [
Expand All @@ -70,6 +73,24 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
$ref: 'NoteSchema#/properties/id',
},
},
response: {
'2xx': {
type: 'object',
properties: {
note: {
$ref: 'NoteSchema',
},
accessRights: {
type: 'object',
properties: {
canEdit: {
type: 'boolean',
},
},
},
},
},
},
},
preHandler: [
noteResolver,
Expand All @@ -79,19 +100,25 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
const { note } = request;

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

/**
* Wrap note for public use
*/
const notePublic = definePublicNote(note);

/**
* Check if current user is creator of the note
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
*/
const canEdit = note.creatorId === request.userId;
const canEdit = note.creatorId == request.userId;


return reply.send({
note: note,
note: notePublic,
accessRights: { canEdit: canEdit },
});
});
Expand All @@ -110,7 +137,7 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
schema: {
params: {
notePublicId: {
$ref: 'NoteSchema#/properties/id',
$ref: 'NoteSchema',
},
},
},
Expand Down Expand Up @@ -227,28 +254,57 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
hostname: string;
},
Reply: {
note : Note
accessRights: { canEdit: boolean },
} | ErrorResponse,
}>('/resolve-hostname/:hostname', async (request, reply) => {
note: NotePublic,
accessRights: {
canEdit: boolean,
},
}| ErrorResponse,
}>('/resolve-hostname/:hostname', {
schema: {
response: {
'2xx': {
type: 'object',
properties: {
note: {
$ref: 'NoteSchema',
},
accessRights: {
type: 'object',
properties: {
canEdit: {
type: 'boolean',
},
},
},
},

},
},
},
}, async (request, reply) => {
const params = request.params;

const note = await noteService.getNoteByHostname(params.hostname);

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

/**
* Check if current user is creator of the note
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
* Wrapping Note for public use
*/
const notePublic = definePublicNote(note);

/**
* Check if current user is creator of the note
*/
const canEdit = note.creatorId === request.userId;
const canEdit = note.creatorId == request.userId;

return reply.send({
note: note,
note: notePublic,
accessRights: { canEdit: canEdit },
});
});
Expand Down
14 changes: 13 additions & 1 deletion src/presentation/http/router/noteList.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { FastifyPluginCallback } from 'fastify';
import type NoteListService from '@domain/service/noteList.js';
import { definePublicNote, type NotePublic } from '@domain/entities/notePublic.js';
import type { NoteListPublic } from '@domain/entities/noteList';


/**
* Interface for the noteList router.
Expand Down Expand Up @@ -49,8 +52,17 @@ const NoteListRouter: FastifyPluginCallback<NoteListRouterOptions> = (fastify, o
const page = request.query.page;

const noteList = await noteListService.getNoteListByCreatorId(userId, page);
/**
* Wrapping Notelist for public use
*/
const noteListItemsPublic: NotePublic[] = noteList.items.map(definePublicNote);

const noteListPublic: NoteListPublic = {
items: noteListItemsPublic,
};


return reply.send(noteList);
return reply.send(noteListPublic);
});

done();
Expand Down
4 changes: 2 additions & 2 deletions src/repository/storage/postgres/orm/sequelize/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { CreationOptional, InferAttributes, InferCreationAttributes, ModelS
import { DataTypes, Model } from 'sequelize';
import type Orm from '@repository/storage/postgres/orm/sequelize/index.js';
import type { Note, NoteCreationAttributes, NoteInternalId, NotePublicId } from '@domain/entities/note.js';
import type { NoteList } from '@domain/entities/noteList.js';
import type { NoteSettingsModel } from '@repository/storage/postgres/orm/sequelize/noteSettings.js';
import { UserModel } from '@repository/storage/postgres/orm/sequelize/user.js';
import type { NoteSettingsModel } from './noteSettings';
import { NoteList } from '@domain/entities/noteList';

/* eslint-disable @typescript-eslint/naming-convention */

Expand Down