| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const repository = require('./repository'); | 
					
						
							| 
									
										
										
										
											2018-08-03 11:11:57 +02:00
										 |  |  | const sql = require('./sql'); | 
					
						
							|  |  |  | const utils = require('./utils'); | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | const Attribute = require('../entities/attribute'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 23:45:58 +01:00
										 |  |  | const ATTRIBUTE_TYPES = [ 'label', 'label-definition', 'relation', 'relation-definition' ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | const BUILTIN_ATTRIBUTES = [ | 
					
						
							| 
									
										
										
										
											2018-08-03 11:11:57 +02:00
										 |  |  |     // label names
 | 
					
						
							|  |  |  |     { type: 'label', name: 'disableVersioning' }, | 
					
						
							|  |  |  |     { type: 'label', name: 'calendarRoot' }, | 
					
						
							|  |  |  |     { type: 'label', name: 'archived' }, | 
					
						
							|  |  |  |     { type: 'label', name: 'excludeFromExport' }, | 
					
						
							|  |  |  |     { type: 'label', name: 'manualTransactionHandling' }, | 
					
						
							|  |  |  |     { type: 'label', name: 'disableInclusion' }, | 
					
						
							|  |  |  |     { type: 'label', name: 'appCss' }, | 
					
						
							| 
									
										
										
										
											2019-01-27 21:18:11 +01:00
										 |  |  |     { type: 'label', name: 'appTheme' }, | 
					
						
							| 
									
										
										
										
											2018-08-03 11:11:57 +02:00
										 |  |  |     { type: 'label', name: 'hideChildrenOverview' }, | 
					
						
							| 
									
										
										
										
											2018-11-13 22:14:41 +01:00
										 |  |  |     { type: 'label', name: 'hidePromotedAttributes' }, | 
					
						
							| 
									
										
										
										
											2019-01-27 13:10:03 +01:00
										 |  |  |     { type: 'label', name: 'readOnly' }, | 
					
						
							| 
									
										
										
										
											2019-02-11 23:45:58 +01:00
										 |  |  |     { type: 'label', name: 'run', isDangerous: true }, | 
					
						
							|  |  |  |     { type: 'label', name: 'customRequestHandler', isDangerous: true }, | 
					
						
							|  |  |  |     { type: 'label', name: 'customResourceProvider', isDangerous: true }, | 
					
						
							| 
									
										
										
										
											2018-08-03 11:11:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // relation names
 | 
					
						
							| 
									
										
										
										
											2019-02-11 23:45:58 +01:00
										 |  |  |     { type: 'relation', name: 'runOnNoteView', isDangerous: true }, | 
					
						
							|  |  |  |     { type: 'relation', name: 'runOnNoteCreation', isDangerous: true }, | 
					
						
							|  |  |  |     { type: 'relation', name: 'runOnNoteTitleChange', isDangerous: true }, | 
					
						
							|  |  |  |     { type: 'relation', name: 'runOnNoteChange', isDangerous: true }, | 
					
						
							|  |  |  |     { type: 'relation', name: 'runOnChildNoteCreation', isDangerous: true }, | 
					
						
							|  |  |  |     { type: 'relation', name: 'runOnAttributeCreation', isDangerous: true }, | 
					
						
							|  |  |  |     { type: 'relation', name: 'runOnAttributeChange', isDangerous: true }, | 
					
						
							| 
									
										
										
										
											2018-08-28 20:55:21 +02:00
										 |  |  |     { type: 'relation', name: 'template' }, | 
					
						
							| 
									
										
										
										
											2019-09-06 23:36:08 +02:00
										 |  |  |     { type: 'relation', name: 'widget', isDangerous: true }, | 
					
						
							| 
									
										
										
										
											2019-02-11 23:45:58 +01:00
										 |  |  |     { type: 'relation', name: 'renderNote', isDangerous: true } | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 12:48:11 +02:00
										 |  |  | async function getNotesWithLabel(name, value) { | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  |     let valueCondition = ""; | 
					
						
							|  |  |  |     let params = [name]; | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (value !== undefined) { | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  |         valueCondition = " AND attributes.value = ?"; | 
					
						
							|  |  |  |         params.push(value); | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  |     return await repository.getEntities(`SELECT notes.* FROM notes JOIN attributes USING(noteId) 
 | 
					
						
							|  |  |  |           WHERE notes.isDeleted = 0 AND attributes.isDeleted = 0 AND attributes.name = ? ${valueCondition} ORDER BY position`, params);
 | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 17:01:37 +01:00
										 |  |  | async function getNotesWithLabels(names) { | 
					
						
							|  |  |  |     const questionMarks = names.map(() => "?").join(", "); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return await repository.getEntities(`SELECT notes.* FROM notes JOIN attributes USING(noteId) 
 | 
					
						
							|  |  |  |           WHERE notes.isDeleted = 0 AND attributes.isDeleted = 0 AND attributes.name IN (${questionMarks}) ORDER BY position`, names);
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 12:48:11 +02:00
										 |  |  | async function getNoteWithLabel(name, value) { | 
					
						
							|  |  |  |     const notes = await getNotesWithLabel(name, value); | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return notes.length > 0 ? notes[0] : null; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 13:33:10 +02:00
										 |  |  | async function createLabel(noteId, name, value = "") { | 
					
						
							|  |  |  |     return await createAttribute({ | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  |         noteId: noteId, | 
					
						
							| 
									
										
										
										
											2018-08-07 13:33:10 +02:00
										 |  |  |         type: 'label', | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  |         name: name, | 
					
						
							|  |  |  |         value: value | 
					
						
							| 
									
										
										
										
											2018-08-07 13:33:10 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function createAttribute(attribute) { | 
					
						
							|  |  |  |     return await new Attribute(attribute).save(); | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-03 11:11:57 +02:00
										 |  |  | async function getAttributeNames(type, nameLike) { | 
					
						
							| 
									
										
										
										
											2018-08-15 22:06:49 +02:00
										 |  |  |     nameLike = nameLike.toLowerCase(); | 
					
						
							| 
									
										
										
										
											2018-08-15 18:22:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-15 22:06:49 +02:00
										 |  |  |     const names = await sql.getColumn( | 
					
						
							|  |  |  |         `SELECT DISTINCT name 
 | 
					
						
							| 
									
										
										
										
											2018-08-15 18:22:02 +02:00
										 |  |  |              FROM attributes  | 
					
						
							|  |  |  |              WHERE isDeleted = 0 | 
					
						
							|  |  |  |                AND type = ? | 
					
						
							|  |  |  |                AND name LIKE '%${utils.sanitizeSql(nameLike)}%'`, [type]);
 | 
					
						
							| 
									
										
										
										
											2018-08-15 22:06:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const attr of BUILTIN_ATTRIBUTES) { | 
					
						
							|  |  |  |         if (attr.type === type && attr.name.toLowerCase().includes(nameLike) && !names.includes(attr.name)) { | 
					
						
							|  |  |  |             names.push(attr.name); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-03 11:11:57 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     names.sort(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return names; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 23:45:58 +01:00
										 |  |  | function isAttributeType(type) { | 
					
						
							|  |  |  |     return ATTRIBUTE_TYPES.includes(type); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function isAttributeDangerous(type, name) { | 
					
						
							|  |  |  |     return BUILTIN_ATTRIBUTES.some(attr =>  | 
					
						
							|  |  |  |         attr.type === attr.type &&  | 
					
						
							|  |  |  |         attr.name.toLowerCase() === name.trim().toLowerCase() && | 
					
						
							|  |  |  |         attr.isDangerous | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2018-08-07 12:48:11 +02:00
										 |  |  |     getNotesWithLabel, | 
					
						
							| 
									
										
										
										
											2019-01-27 17:01:37 +01:00
										 |  |  |     getNotesWithLabels, | 
					
						
							| 
									
										
										
										
											2018-08-07 12:48:11 +02:00
										 |  |  |     getNoteWithLabel, | 
					
						
							| 
									
										
										
										
											2018-08-07 13:33:10 +02:00
										 |  |  |     createLabel, | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  |     createAttribute, | 
					
						
							| 
									
										
										
										
											2019-02-11 23:45:58 +01:00
										 |  |  |     getAttributeNames, | 
					
						
							|  |  |  |     isAttributeType, | 
					
						
							|  |  |  |     isAttributeDangerous | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | }; |