Skip to content

Commit

Permalink
Merge pull request #271 from codex-team/delete-note-history-on-note-d…
Browse files Browse the repository at this point in the history
…eletion
  • Loading branch information
e11sy authored Jul 24, 2024
2 parents a891800 + 6d979f1 commit 1fa4a8d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/domain/service/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
42 changes: 42 additions & 0 deletions src/presentation/http/router/note.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
9 changes: 9 additions & 0 deletions src/repository/noteHistory.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ export default class NoteHistoryRepository {
public async getLastContentVersion(noteId: NoteHistoryRecord['noteId']): Promise<NoteHistoryRecord['content'] | undefined> {
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<boolean> {
return await this.storage.deleteNoteHistoryByNoteid(noteId);
}
}
15 changes: 15 additions & 0 deletions src/repository/storage/postgres/orm/sequelize/noteHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
const destroyedRows = await this.model.destroy({
where: {
noteId,
},
});

return destroyedRows !== 0;
}
}

0 comments on commit 1fa4a8d

Please sign in to comment.