| 
									
										
										
										
											2022-01-07 19:33:59 +01:00
										 |  |  | const becca = require("../becca/becca"); | 
					
						
							|  |  |  | const utils = require("../services/utils"); | 
					
						
							|  |  |  | const ru = require("./route_utils"); | 
					
						
							|  |  |  | const mappers = require("./mappers"); | 
					
						
							|  |  |  | const noteService = require("../services/notes"); | 
					
						
							|  |  |  | const TaskContext = require("../services/task_context"); | 
					
						
							|  |  |  | const validators = require("./validators"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function register(router) { | 
					
						
							|  |  |  |     ru.route(router, 'get', '/etapi/notes/:noteId', (req, res, next) => { | 
					
						
							|  |  |  |         const note = ru.getAndCheckNote(req.params.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res.json(mappers.mapNoteToPojo(note)); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ru.route(router, 'post' ,'/etapi/create-note', (req, res, next) => { | 
					
						
							|  |  |  |         const params = req.body; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ru.getAndCheckNote(params.parentNoteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             const resp = noteService.createNewNote(params); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             res.json({ | 
					
						
							|  |  |  |                 note: mappers.mapNoteToPojo(resp.note), | 
					
						
							|  |  |  |                 branch: mappers.mapBranchToPojo(resp.branch) | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         catch (e) { | 
					
						
							|  |  |  |             return ru.sendError(res, 400, ru.GENERIC_CODE, e.message); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const ALLOWED_PROPERTIES_FOR_PATCH = { | 
					
						
							|  |  |  |         'title': validators.isString, | 
					
						
							|  |  |  |         'type': validators.isString, | 
					
						
							|  |  |  |         'mime': validators.isString | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ru.route(router, 'patch' ,'/etapi/notes/:noteId', (req, res, next) => { | 
					
						
							|  |  |  |         const note = ru.getAndCheckNote(req.params.noteId) | 
					
						
							| 
									
										
										
										
											2022-01-07 23:06:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-07 19:33:59 +01:00
										 |  |  |         if (note.isProtected) { | 
					
						
							| 
									
										
										
										
											2022-01-07 23:06:04 +01:00
										 |  |  |             throw new ru.EtapiError(400, "NOTE_IS_PROTECTED", `Note '${req.params.noteId}' is protected and cannot be modified through ETAPI`); | 
					
						
							| 
									
										
										
										
											2022-01-07 19:33:59 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-07 23:06:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-07 19:33:59 +01:00
										 |  |  |         ru.validateAndPatch(note, req.body, ALLOWED_PROPERTIES_FOR_PATCH); | 
					
						
							| 
									
										
										
										
											2022-01-07 23:06:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-07 19:33:59 +01:00
										 |  |  |         res.json(mappers.mapNoteToPojo(note)); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ru.route(router, 'delete' ,'/etapi/notes/:noteId', (req, res, next) => { | 
					
						
							|  |  |  |         const {noteId} = req.params; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const note = becca.getNote(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-08 12:01:54 +01:00
										 |  |  |         if (!note || note.isDeleted) { | 
					
						
							| 
									
										
										
										
											2022-01-07 19:33:59 +01:00
										 |  |  |             return res.sendStatus(204); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         noteService.deleteNote(note, null, new TaskContext('no-progress-reporting')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res.sendStatus(204); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2022-01-08 12:01:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ru.route(router, 'get', '/etapi/notes/:noteId/content', (req, res, next) => { | 
					
						
							|  |  |  |         const note = ru.getAndCheckNote(req.params.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const filename = utils.formatDownloadTitle(note.title, note.type, note.mime); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res.setHeader('Content-Disposition', utils.getContentDisposition(filename)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); | 
					
						
							|  |  |  |         res.setHeader('Content-Type', note.mime); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res.send(note.getContent()); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ru.route(router, 'put', '/etapi/notes/:noteId/content', (req, res, next) => { | 
					
						
							|  |  |  |         const note = ru.getAndCheckNote(req.params.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         note.setContent(req.body); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return res.sendStatus(204); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2022-01-07 19:33:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     register | 
					
						
							| 
									
										
										
										
											2022-01-07 23:06:04 +01:00
										 |  |  | }; |