diff --git a/src/domain/service/note.ts b/src/domain/service/note.ts index ca655464..fc8791a7 100644 --- a/src/domain/service/note.ts +++ b/src/domain/service/note.ts @@ -127,6 +127,11 @@ export default class NoteService { } } + /** + * Delete all note history records on note deletion + */ + await this.noteHistoryRepository.deleteNoteHistoryByNoteId(id); + const isNoteDeleted = await this.noteRepository.deleteNoteById(id); if (isNoteDeleted === false) { diff --git a/src/presentation/http/router/note.test.ts b/src/presentation/http/router/note.test.ts index 463f74bc..e8727463 100644 --- a/src/presentation/http/router/note.test.ts +++ b/src/presentation/http/router/note.test.ts @@ -2072,4 +2072,46 @@ describe('Note API', () => { } }); }); + + describe('DELETE /note/:noteId', () => { + test('Delete note history on note deletion', async () => { + /** + * Insert test user + */ + const user = await global.db.insertUser(); + + /** + * Authorization for user + */ + const accessToken = global.auth(user.id); + + /** + * Insert test note, note history record will be inserted automatically + */ + const note = await global.db.insertNote({ + creatorId: user.id, + }); + + /** + * Delete note + */ + await global.api?.fakeRequest({ + method: 'DELETE', + headers: { + authorization: `Bearer ${accessToken}`, + }, + url: `/note/${note.publicId}`, + }); + + const response = await global.api?.fakeRequest({ + method: 'GET', + headers: { + authorization: `Bearer ${accessToken}`, + }, + url: `/note/${note.publicId}/history`, + }); + + expect(response?.json().message).toBe('Note not found'); + }); + }); }); diff --git a/src/repository/noteHistory.repository.ts b/src/repository/noteHistory.repository.ts index e462ccaf..a20e5f84 100644 --- a/src/repository/noteHistory.repository.ts +++ b/src/repository/noteHistory.repository.ts @@ -51,4 +51,13 @@ export default class NoteHistoryRepository { public async getLastContentVersion(noteId: NoteHistoryRecord['noteId']): Promise { return await this.storage.getLastContentVersion(noteId); } + + /** + * Delete all note history records of the note + * @param noteId - internal id of the note + * @returns - true if history was deleted, false otherwise + */ + public async deleteNoteHistoryByNoteId(noteId: NoteHistoryRecord['id']): Promise { + return await this.storage.deleteNoteHistoryByNoteid(noteId); + } } diff --git a/src/repository/storage/postgres/orm/sequelize/noteHistory.ts b/src/repository/storage/postgres/orm/sequelize/noteHistory.ts index 1886ce66..85cb30a7 100644 --- a/src/repository/storage/postgres/orm/sequelize/noteHistory.ts +++ b/src/repository/storage/postgres/orm/sequelize/noteHistory.ts @@ -167,4 +167,19 @@ export default class NoteHistorySequelizeStorage { return latestHistory?.content; } + + /** + * Delete all note history records of the note + * @param noteId - internal id of the note + * @returns - true if history was deleted, false otherwise + */ + public async deleteNoteHistoryByNoteid(noteId: NoteHistoryRecord['id']): Promise { + const destroyedRows = await this.model.destroy({ + where: { + noteId, + }, + }); + + return destroyedRows !== 0; + } }