| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const express = require('express'); | 
					
						
							|  |  |  | const router = express.Router(); | 
					
						
							|  |  |  | const sql = require('../../services/sql'); | 
					
						
							|  |  |  | const utils = require('../../services/utils'); | 
					
						
							|  |  |  | const sync_table = require('../../services/sync_table'); | 
					
						
							| 
									
										
										
										
											2017-12-23 09:57:20 -05:00
										 |  |  | const auth = require('../../services/auth'); | 
					
						
							| 
									
										
										
										
											2017-12-23 13:16:18 -05:00
										 |  |  | const log = require('../../services/log'); | 
					
						
							| 
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 |  |  | const wrap = require('express-promise-wrap').wrap; | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 |  |  | router.post('/cleanup-soft-deleted-items', auth.checkApiAuth, wrap(async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |         const noteIdsToDelete = await sql.getColumn("SELECT noteId FROM notes WHERE isDeleted = 1"); | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  |         const noteIdsSql = noteIdsToDelete | 
					
						
							|  |  |  |             .map(noteId => "'" + utils.sanitizeSql(noteId) + "'") | 
					
						
							|  |  |  |             .join(', '); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         await sql.execute(`DELETE FROM event_log WHERE noteId IN (${noteIdsSql})`); | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |         await sql.execute(`DELETE FROM note_revisions WHERE noteId IN (${noteIdsSql})`); | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |         await sql.execute(`DELETE FROM note_images WHERE noteId IN (${noteIdsSql})`); | 
					
						
							| 
									
										
										
										
											2018-01-09 22:09:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         await sql.execute(`DELETE FROM attributes WHERE noteId IN (${noteIdsSql})`); | 
					
						
							| 
									
										
										
										
											2018-01-09 22:09:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |         await sql.execute("DELETE FROM branches WHERE isDeleted = 1"); | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |         await sql.execute("DELETE FROM note_images WHERE isDeleted = 1"); | 
					
						
							| 
									
										
										
										
											2018-01-07 14:07:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         await sql.execute("DELETE FROM images WHERE isDeleted = 1"); | 
					
						
							| 
									
										
										
										
											2018-01-07 14:07:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         await sql.execute("DELETE FROM notes WHERE isDeleted = 1"); | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         await sql.execute("DELETE FROM recent_notes"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         await sync_table.cleanupSyncRowsForMissingEntities("notes", "noteId"); | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |         await sync_table.cleanupSyncRowsForMissingEntities("branches", "branchId"); | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |         await sync_table.cleanupSyncRowsForMissingEntities("note_revisions", "noteRevisionId"); | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |         await sync_table.cleanupSyncRowsForMissingEntities("recent_notes", "branchId"); | 
					
						
							| 
									
										
										
										
											2017-12-23 13:16:18 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         log.info("Following notes has been completely cleaned from database: " + noteIdsSql); | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							| 
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 |  |  | })); | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 14:07:59 -05:00
										 |  |  | router.post('/cleanup-unused-images', auth.checkApiAuth, wrap(async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2018-01-28 21:57:46 -05:00
										 |  |  |     const sourceId = req.headers.source_id; | 
					
						
							| 
									
										
										
										
											2018-01-07 14:07:59 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |         const unusedImageIds = await sql.getColumn(`
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |           SELECT images.imageId  | 
					
						
							| 
									
										
										
										
											2018-01-07 14:07:59 -05:00
										 |  |  |           FROM images  | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |             LEFT JOIN note_images ON note_images.imageId = images.imageId AND note_images.isDeleted = 0 | 
					
						
							| 
									
										
										
										
											2018-01-07 14:07:59 -05:00
										 |  |  |           WHERE | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             images.isDeleted = 0 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |             AND note_images.noteImageId IS NULL`);
 | 
					
						
							| 
									
										
										
										
											2018-01-07 14:07:59 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const now = utils.nowDate(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const imageId of unusedImageIds) { | 
					
						
							|  |  |  |             log.info(`Deleting unused image: ${imageId}`); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             await sql.execute("UPDATE images SET isDeleted = 1, data = null, dateModified = ? WHERE imageId = ?", | 
					
						
							| 
									
										
										
										
											2018-01-07 14:07:59 -05:00
										 |  |  |                 [now, imageId]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             await sync_table.addImageSync(imageId, sourceId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							|  |  |  | })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 |  |  | router.post('/vacuum-database', auth.checkApiAuth, wrap(async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  |     await sql.execute("VACUUM"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 13:16:18 -05:00
										 |  |  |     log.info("Database has been vacuumed."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  |     res.send({}); | 
					
						
							| 
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 |  |  | })); | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = router; |