File tree Expand file tree Collapse file tree
storage/postgres/orm/sequelize Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ) {
Original file line number Diff line number Diff 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} ) ;
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments