Skip to content

Commit

Permalink
implemented noteHistoryPublic
Browse files Browse the repository at this point in the history
  • Loading branch information
e11sy committed Jul 22, 2024
1 parent e5c77ec commit b8fd56e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
11 changes: 10 additions & 1 deletion src/domain/entities/noteHistory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NoteInternalId, Note } from './note.js';
import type { NoteInternalId, Note, NotePublicId } from './note.js';
import type User from './user.js';

export interface NoteHistoryRecord {
Expand Down Expand Up @@ -38,4 +38,13 @@ export interface NoteHistoryRecord {
*/
export type NoteHistoryCreationAttributes = Omit<NoteHistoryRecord, 'id' | 'createdAt'>;

/**
* Meta data of the note history record
* Used for presentation of the note history recored in web
*/
export type NoteHistoryMeta = Omit<NoteHistoryRecord, 'content' | 'noteId' | 'tools'>;

/**
* Public note history record with note public id instead of note internal id
*/
export type NoteHistoryPublic = Omit<NoteHistoryRecord, 'noteId'> & { noteId: NotePublicId };
15 changes: 12 additions & 3 deletions src/domain/service/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type EditorToolsRepository from '@repository/editorTools.repository.js';
import type User from '@domain/entities/user.js';
import type { NoteList } from '@domain/entities/noteList.js';
import type NoteHistoryRepository from '@repository/noteHistory.repository.js';
import type { NoteHistoryMeta, NoteHistoryRecord } from '@domain/entities/noteHistory.js';
import type { NoteHistoryMeta, NoteHistoryRecord, NoteHistoryPublic } from '@domain/entities/noteHistory.js';

/**
* Note service
Expand Down Expand Up @@ -406,13 +406,22 @@ export default class NoteService {
* @param id - id of the note history record
* @returns full note history record or raises domain error if record not found
*/
public async getHistoryResordById(id: NoteHistoryRecord['id']): Promise<NoteHistoryRecord> {
public async getHistoryResordById(id: NoteHistoryRecord['id']): Promise<NoteHistoryPublic> {
const noteHistoryRecord = await this.noteHistoryRepository.getHistoryRecordById(id);

if (noteHistoryRecord === null) {
throw new DomainError('This version of the note not found');
}

return noteHistoryRecord;
const noteHistoryPublic = {
id: noteHistoryRecord.id,
noteId: await this.getNotePublicIdByInternal(noteHistoryRecord.noteId),
userId: noteHistoryRecord.userId,
content: noteHistoryRecord.content,
tools: noteHistoryRecord.tools,
createdAt: noteHistoryRecord.createdAt,
};

return noteHistoryPublic;
}
}
9 changes: 8 additions & 1 deletion src/presentation/http/router/note.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2067,7 +2067,14 @@ describe('Note API', () => {
expect(response?.json()).toStrictEqual({ message: expectedMessage });
} else if (expectedResponse !== undefined) {
expect(response?.json()).toStrictEqual({
noteHistoryRecord: history,
noteHistoryRecord: {
id: history.id,
userId: history.userId,
noteId: note.publicId,
createdAt: history.createdAt,
content: history.content,
tools: history.tools,
},
});
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/presentation/http/router/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { type NotePublic, definePublicNote } from '@domain/entities/notePublic.j
import type NoteVisitsService from '@domain/service/noteVisits.js';
import type EditorToolsService from '@domain/service/editorTools.js';
import type EditorTool from '@domain/entities/editorTools.js';
import type { NoteHistoryMeta, NoteHistoryRecord } from '@domain/entities/noteHistory.js';
import type { NoteHistoryMeta, NoteHistoryPublic, NoteHistoryRecord } from '@domain/entities/noteHistory.js';

/**
* Interface for the note router.
Expand Down Expand Up @@ -712,7 +712,7 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
historyId: NoteHistoryRecord['id'];
};
Reply: {
noteHistoryRecord: NoteHistoryRecord;
noteHistoryRecord: NoteHistoryPublic;
} | ErrorResponse;
}>('/:notePublicId/history/:historyId', {
config: {
Expand Down
6 changes: 1 addition & 5 deletions src/presentation/http/schema/History.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const HistotyRecordShema = {
},
noteId: {
description: 'unique note identifier',
type: 'number',
type: 'string',
},
userId: {
description: 'unique user identifier',
Expand Down Expand Up @@ -74,10 +74,6 @@ export const HistoryMetaSchema = {
description: 'unique note hisotry record identifier',
type: 'number',
},
noteId: {
description: 'unique note identifier',
type: 'number',
},
userId: {
description: 'unique user identifier',
type: 'number',
Expand Down

0 comments on commit b8fd56e

Please sign in to comment.