Skip to content

Commit 1fa4a8d

Browse files
authored
Merge pull request #271 from codex-team/delete-note-history-on-note-deletion
2 parents a891800 + 6d979f1 commit 1fa4a8d

4 files changed

Lines changed: 71 additions & 0 deletions

File tree

src/domain/service/note.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ export default class NoteService {
127127
}
128128
}
129129

130+
/**
131+
* Delete all note history records on note deletion
132+
*/
133+
await this.noteHistoryRepository.deleteNoteHistoryByNoteId(id);
134+
130135
const isNoteDeleted = await this.noteRepository.deleteNoteById(id);
131136

132137
if (isNoteDeleted === false) {

src/presentation/http/router/note.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,4 +2072,46 @@ describe('Note API', () => {
20722072
}
20732073
});
20742074
});
2075+
2076+
describe('DELETE /note/:noteId', () => {
2077+
test('Delete note history on note deletion', async () => {
2078+
/**
2079+
* Insert test user
2080+
*/
2081+
const user = await global.db.insertUser();
2082+
2083+
/**
2084+
* Authorization for user
2085+
*/
2086+
const accessToken = global.auth(user.id);
2087+
2088+
/**
2089+
* Insert test note, note history record will be inserted automatically
2090+
*/
2091+
const note = await global.db.insertNote({
2092+
creatorId: user.id,
2093+
});
2094+
2095+
/**
2096+
* Delete note
2097+
*/
2098+
await global.api?.fakeRequest({
2099+
method: 'DELETE',
2100+
headers: {
2101+
authorization: `Bearer ${accessToken}`,
2102+
},
2103+
url: `/note/${note.publicId}`,
2104+
});
2105+
2106+
const response = await global.api?.fakeRequest({
2107+
method: 'GET',
2108+
headers: {
2109+
authorization: `Bearer ${accessToken}`,
2110+
},
2111+
url: `/note/${note.publicId}/history`,
2112+
});
2113+
2114+
expect(response?.json().message).toBe('Note not found');
2115+
});
2116+
});
20752117
});

src/repository/noteHistory.repository.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,13 @@ export default class NoteHistoryRepository {
5151
public async getLastContentVersion(noteId: NoteHistoryRecord['noteId']): Promise<NoteHistoryRecord['content'] | undefined> {
5252
return await this.storage.getLastContentVersion(noteId);
5353
}
54+
55+
/**
56+
* Delete all note history records of the note
57+
* @param noteId - internal id of the note
58+
* @returns - true if history was deleted, false otherwise
59+
*/
60+
public async deleteNoteHistoryByNoteId(noteId: NoteHistoryRecord['id']): Promise<boolean> {
61+
return await this.storage.deleteNoteHistoryByNoteid(noteId);
62+
}
5463
}

src/repository/storage/postgres/orm/sequelize/noteHistory.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,19 @@ export default class NoteHistorySequelizeStorage {
167167

168168
return latestHistory?.content;
169169
}
170+
171+
/**
172+
* Delete all note history records of the note
173+
* @param noteId - internal id of the note
174+
* @returns - true if history was deleted, false otherwise
175+
*/
176+
public async deleteNoteHistoryByNoteid(noteId: NoteHistoryRecord['id']): Promise<boolean> {
177+
const destroyedRows = await this.model.destroy({
178+
where: {
179+
noteId,
180+
},
181+
});
182+
183+
return destroyedRows !== 0;
184+
}
170185
}

0 commit comments

Comments
 (0)