Skip to content

Commit

Permalink
Merge pull request #185 from codex-team/fix/return-note-public-id
Browse files Browse the repository at this point in the history
Return note public in note settings
  • Loading branch information
kloV148 authored Feb 23, 2024
2 parents b508291 + b54b3c0 commit 0dd5192
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
21 changes: 21 additions & 0 deletions src/domain/entities/noteSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ export default interface NoteSettings {
team?: Team;
}

/**
* Attributes of public note settings
*/
type NoteSettingsPublicProperties = 'customHostname' | 'isPublic' | 'invitationHash' | 'team' ;

export interface NoteSettingsPublic extends Pick<NoteSettings, NoteSettingsPublicProperties> {}

/**
* Define note settings for public usage
*
* @param noteSettings - note settings data
*/
export function definePublicNoteSettings(noteSettings: NoteSettings): NoteSettingsPublic {
return {
customHostname: noteSettings.customHostname,
isPublic: noteSettings.isPublic,
invitationHash: noteSettings.invitationHash,
team: noteSettings.team,
};
}

/**
* Notes settings creation attributes, omitting id, because it's generated by database
*/
Expand Down
6 changes: 1 addition & 5 deletions src/presentation/http/router/noteSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ describe('NoteSettings API', () => {

const expectedNoteSettings = {
'customHostname': 'codex.so',
'id': 54,
'invitationHash': 'FfAwyaR80C',
'isPublic': true,
'noteId': 54,
'team': [],
};

Expand Down Expand Up @@ -45,7 +43,7 @@ describe('NoteSettings API', () => {
'email': 'a@a.com',
'id': 1,
'name': 'Test user 1',
'photo': null,
'photo': '',
},
},
]);
Expand Down Expand Up @@ -210,8 +208,6 @@ describe('NoteSettings API', () => {
});

const updatedNoteSettings = {
'id': 54,
'noteId': 54,
'customHostname': 'codex.so',
'isPublic': false,
'invitationHash': 'FfAwyaR80C',
Expand Down
25 changes: 20 additions & 5 deletions src/presentation/http/router/noteSettings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { FastifyPluginCallback } from 'fastify';
import type NoteSettingsService from '@domain/service/noteSettings.js';
import type NoteSettings from '@domain/entities/noteSettings.js';
import type { InvitationHash } from '@domain/entities/noteSettings.js';
import { definePublicNoteSettings } from '@domain/entities/noteSettings.js';
import type { InvitationHash, NoteSettingsPublic } from '@domain/entities/noteSettings.js';
import useNoteResolver from '../middlewares/note/useNoteResolver.js';
import type NoteService from '@domain/service/note.js';
import useNoteSettingsResolver from '../middlewares/noteSettings/useNoteSettingsResolver.js';
Expand Down Expand Up @@ -57,7 +58,7 @@ const NoteSettingsRouter: FastifyPluginCallback<NoteSettingsRouterOptions> = (fa
Params: {
notePublicId: NotePublicId;
},
Reply: NoteSettings,
Reply: NoteSettingsPublic,
}>('/:notePublicId', {
config: {
policy: [
Expand All @@ -70,6 +71,11 @@ const NoteSettingsRouter: FastifyPluginCallback<NoteSettingsRouterOptions> = (fa
$ref: 'NoteSchema#/properties/id',
},
},
response: {
'2xx': {
$ref: 'NoteSettingsSchema',
},
},
},
preHandler: [
noteResolver,
Expand All @@ -80,7 +86,9 @@ const NoteSettingsRouter: FastifyPluginCallback<NoteSettingsRouterOptions> = (fa

const noteSettings = await noteSettingsService.getNoteSettingsByNoteId(noteId);

return reply.send(noteSettings);
const noteSettingsPublic = definePublicNoteSettings(noteSettings);

return reply.send(noteSettingsPublic);
});

/**
Expand Down Expand Up @@ -132,7 +140,7 @@ const NoteSettingsRouter: FastifyPluginCallback<NoteSettingsRouterOptions> = (fa
Params: {
notePublicId: NotePublicId;
},
Reply: NoteSettings,
Reply: NoteSettingsPublic,
}>('/:notePublicId', {
config: {
policy: [
Expand All @@ -146,6 +154,11 @@ const NoteSettingsRouter: FastifyPluginCallback<NoteSettingsRouterOptions> = (fa
$ref: 'NoteSchema#/properties/id',
},
},
response: {
'2xx': {
$ref: 'NoteSettingsSchema',
},
},
},
preHandler: [
noteResolver,
Expand All @@ -171,7 +184,9 @@ const NoteSettingsRouter: FastifyPluginCallback<NoteSettingsRouterOptions> = (fa
return reply.notFound('Note settings not found');
}

return reply.send(updatedNoteSettings);
const noteSettingsPublic = definePublicNoteSettings(updatedNoteSettings);

return reply.send(noteSettingsPublic);
});

/**
Expand Down
29 changes: 17 additions & 12 deletions src/presentation/http/schema/NoteSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ export const NoteSettingsSchema = {
$id: 'NoteSettingsSchema',
type: 'object',
properties: {
id: {
type: 'number',
},
noteId: {
type: 'number',
},
customHostname: {
type: 'string',
},
Expand All @@ -32,15 +26,26 @@ export const NoteSettingsSchema = {
id: {
type: 'number',
},
noteId: {
type: 'number',
},
userId: {
type: 'number',
},
role: {
type: 'number',
},
user: {
type: 'object',
properties: {
id: {
type: 'number',
},
name: {
type: 'string',
},
email: {
type: 'string',
},
photo: {
type: 'string',
},
},
},
},
},
},
Expand Down

0 comments on commit 0dd5192

Please sign in to comment.