| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const sql = require('../../services/sql'); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  | const sync_table = require('../../services/sync_table'); | 
					
						
							|  |  |  | const utils = require('../../services/utils'); | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  | const labels = require('../../services/labels'); | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 13:20:36 -04:00
										 |  |  | async function getNoteLabels(req) { | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 13:20:36 -04:00
										 |  |  |     return await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 13:20:36 -04:00
										 |  |  | async function updateNoteLabels(req, res, next) { | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     const labels = req.body; | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |     const now = utils.nowDate(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 09:59:44 -04:00
										 |  |  |     for (const label of labels) { | 
					
						
							|  |  |  |         if (label.labelId) { | 
					
						
							| 
									
										
										
										
											2018-03-30 13:20:36 -04:00
										 |  |  |             await sql.execute("UPDATE labels SET name = ?, value = ?, dateModified = ?, isDeleted = ?, position = ? WHERE labelId = ?", | 
					
						
							| 
									
										
										
										
											2018-04-01 09:59:44 -04:00
										 |  |  |                 [label.name, label.value, now, label.isDeleted, label.position, label.labelId]); | 
					
						
							| 
									
										
										
										
											2018-03-30 13:20:36 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             // if it was "created" and then immediatelly deleted, we just don't create it at all
 | 
					
						
							| 
									
										
										
										
											2018-04-01 09:59:44 -04:00
										 |  |  |             if (label.isDeleted) { | 
					
						
							| 
									
										
										
										
											2018-03-30 13:20:36 -04:00
										 |  |  |                 continue; | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 09:59:44 -04:00
										 |  |  |             label.labelId = utils.newLabelId(); | 
					
						
							| 
									
										
										
										
											2018-03-30 13:20:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             await sql.insert("labels", { | 
					
						
							| 
									
										
										
										
											2018-04-01 09:59:44 -04:00
										 |  |  |                 labelId: label.labelId, | 
					
						
							| 
									
										
										
										
											2018-03-30 13:20:36 -04:00
										 |  |  |                 noteId: noteId, | 
					
						
							| 
									
										
										
										
											2018-04-01 09:59:44 -04:00
										 |  |  |                 name: label.name, | 
					
						
							|  |  |  |                 value: label.value, | 
					
						
							|  |  |  |                 position: label.position, | 
					
						
							| 
									
										
										
										
											2018-03-30 13:20:36 -04:00
										 |  |  |                 dateCreated: now, | 
					
						
							|  |  |  |                 dateModified: now, | 
					
						
							|  |  |  |                 isDeleted: false | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 09:59:44 -04:00
										 |  |  |         await sync_table.addLabelSync(label.labelId); | 
					
						
							| 
									
										
										
										
											2018-03-30 13:20:36 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 13:20:36 -04:00
										 |  |  |     return await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getAllLabelNames(req) { | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     const names = await sql.getColumn("SELECT DISTINCT name FROM labels WHERE isDeleted = 0"); | 
					
						
							| 
									
										
										
										
											2018-02-04 19:27:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 09:59:44 -04:00
										 |  |  |     for (const label of labels.BUILTIN_LABELS) { | 
					
						
							|  |  |  |         if (!names.includes(label)) { | 
					
						
							|  |  |  |             names.push(label); | 
					
						
							| 
									
										
										
										
											2018-02-04 19:27:27 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     names.sort(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 13:20:36 -04:00
										 |  |  |     return names; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-04 19:27:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 13:20:36 -04:00
										 |  |  | async function getValuesForLabel(req) { | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     const labelName = req.params.labelName; | 
					
						
							| 
									
										
										
										
											2018-02-04 19:43:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 13:20:36 -04:00
										 |  |  |     return await sql.getColumn("SELECT DISTINCT value FROM labels WHERE isDeleted = 0 AND name = ? AND value != '' ORDER BY value", [labelName]); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-04 19:43:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 13:20:36 -04:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     getNoteLabels, | 
					
						
							|  |  |  |     updateNoteLabels, | 
					
						
							|  |  |  |     getAllLabelNames, | 
					
						
							|  |  |  |     getValuesForLabel | 
					
						
							|  |  |  | }; |