| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | const sql = require('./sql'); | 
					
						
							|  |  |  | const log = require('./log'); | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  | const eventLogService = require('./event_log'); | 
					
						
							|  |  |  | const syncTableService = require('./sync_table'); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-12 18:31:29 -04:00
										 |  |  | async function updateEntity(sync, entity, sourceId) { | 
					
						
							|  |  |  |     const {entityName} = sync; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     if (entityName === 'notes') { | 
					
						
							|  |  |  |         await updateNote(entity, sourceId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (entityName === 'branches') { | 
					
						
							|  |  |  |         await updateBranch(entity, sourceId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (entityName === 'note_revisions') { | 
					
						
							|  |  |  |         await updateNoteRevision(entity, sourceId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (entityName === 'note_reordering') { | 
					
						
							| 
									
										
										
										
											2018-04-12 18:31:29 -04:00
										 |  |  |         await updateNoteReordering(sync.entityId, entity, sourceId); | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (entityName === 'options') { | 
					
						
							|  |  |  |         await updateOptions(entity, sourceId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (entityName === 'recent_notes') { | 
					
						
							|  |  |  |         await updateRecentNotes(entity, sourceId); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-08 10:11:00 +01:00
										 |  |  |     else if (entityName === 'links') { | 
					
						
							|  |  |  |         await updateLink(entity, sourceId); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  |     else if (entityName === 'attributes') { | 
					
						
							|  |  |  |         await updateAttribute(entity, sourceId); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     else if (entityName === 'api_tokens') { | 
					
						
							|  |  |  |         await updateApiToken(entity, sourceId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2018-07-29 20:33:42 +02:00
										 |  |  |         throw new Error(`Unrecognized entity type ${entityName}`); | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-18 22:55:36 -05:00
										 |  |  | function deserializeNoteContentBuffer(note) { | 
					
						
							| 
									
										
										
										
											2018-12-22 09:54:09 +01:00
										 |  |  |     if (note.content !== null && (note.type === 'file' || note.type === 'image')) { | 
					
						
							| 
									
										
										
										
											2019-01-03 22:13:58 +01:00
										 |  |  |         note.content = Buffer.from(note.content, 'base64'); | 
					
						
							| 
									
										
										
										
											2018-02-18 22:55:36 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 20:47:01 -05:00
										 |  |  | async function updateNote(entity, sourceId) { | 
					
						
							| 
									
										
										
										
											2018-02-18 22:55:36 -05:00
										 |  |  |     deserializeNoteContentBuffer(entity); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const origNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [entity.noteId]); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     if (!origNote || origNote.dateModified <= entity.dateModified) { | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |         await sql.transactional(async () => { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |             await sql.replace("notes", entity); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |             await syncTableService.addNoteSync(entity.noteId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         log.info("Update/sync note " + entity.noteId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  | async function updateBranch(entity, sourceId) { | 
					
						
							|  |  |  |     const orig = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [entity.branchId]); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |     await sql.transactional(async () => { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         if (orig === null || orig.dateModified < entity.dateModified) { | 
					
						
							| 
									
										
										
										
											2018-07-24 22:03:36 +02:00
										 |  |  |             // isExpanded is not synced unless it's a new branch instance
 | 
					
						
							|  |  |  |             // otherwise in case of full new sync we'll get all branches (even root) collapsed.
 | 
					
						
							|  |  |  |             if (orig) { | 
					
						
							|  |  |  |                 delete entity.isExpanded; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-11-09 21:11:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |             await sql.replace('branches', entity); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |             await syncTableService.addBranchSync(entity.branchId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  |             log.info("Update/sync branch " + entity.branchId); | 
					
						
							| 
									
										
										
										
											2017-11-28 17:04:47 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  | async function updateNoteRevision(entity, sourceId) { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const orig = await sql.getRowOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [entity.noteRevisionId]); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |     await sql.transactional(async () => { | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  |         // we update note revision even if date modified to is the same because the only thing which might have changed
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         // is the protected status (and correnspondingly title and content) which doesn't affect the dateModifiedTo
 | 
					
						
							|  |  |  |         if (orig === null || orig.dateModifiedTo <= entity.dateModifiedTo) { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |             await sql.replace('note_revisions', entity); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |             await syncTableService.addNoteRevisionSync(entity.noteRevisionId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  |             log.info("Update/sync note revision " + entity.noteRevisionId); | 
					
						
							| 
									
										
										
										
											2017-11-28 17:04:47 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-12 18:31:29 -04:00
										 |  |  | async function updateNoteReordering(entityId, entity, sourceId) { | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |     await sql.transactional(async () => { | 
					
						
							| 
									
										
										
										
											2018-04-12 18:13:48 -04:00
										 |  |  |         Object.keys(entity).forEach(async key => { | 
					
						
							|  |  |  |             await sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [entity[key], key]); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-12 18:31:29 -04:00
										 |  |  |         await syncTableService.addNoteReorderingSync(entityId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function updateOptions(entity, sourceId) { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const orig = await sql.getRowOrNull("SELECT * FROM options WHERE name = ?", [entity.name]); | 
					
						
							| 
									
										
										
										
											2018-01-11 22:45:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-24 20:35:03 +02:00
										 |  |  |     if (orig && !orig.isSynced) { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |     await sql.transactional(async () => { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         if (orig === null || orig.dateModified < entity.dateModified) { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |             await sql.replace('options', entity); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |             await syncTableService.addOptionsSync(entity.name, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |             await eventLogService.addEvent("Synced option " + entity.name); | 
					
						
							| 
									
										
										
										
											2017-11-28 17:04:47 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function updateRecentNotes(entity, sourceId) { | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |     const orig = await sql.getRowOrNull("SELECT * FROM recent_notes WHERE branchId = ?", [entity.branchId]); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 12:38:25 -04:00
										 |  |  |     if (orig === null || orig.dateCreated < entity.dateCreated) { | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |         await sql.transactional(async () => { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |             await sql.replace('recent_notes', entity); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |             await syncTableService.addRecentNoteSync(entity.branchId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 10:11:00 +01:00
										 |  |  | async function updateLink(entity, sourceId) { | 
					
						
							|  |  |  |     const origLink = await sql.getRow("SELECT * FROM links WHERE linkId = ?", [entity.linkId]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!origLink || origLink.dateModified <= entity.dateModified) { | 
					
						
							|  |  |  |         await sql.transactional(async () => { | 
					
						
							|  |  |  |             await sql.replace("links", entity); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             await syncTableService.addLinkSync(entity.linkId, sourceId); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         log.info("Update/sync link " + entity.linkId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | async function updateAttribute(entity, sourceId) { | 
					
						
							|  |  |  |     const origAttribute = await sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [entity.attributeId]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!origAttribute || origAttribute.dateModified <= entity.dateModified) { | 
					
						
							|  |  |  |         await sql.transactional(async () => { | 
					
						
							|  |  |  |             await sql.replace("attributes", entity); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             await syncTableService.addAttributeSync(entity.attributeId, sourceId); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         log.info("Update/sync attribute " + entity.attributeId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  | async function updateApiToken(entity, sourceId) { | 
					
						
							|  |  |  |     const apiTokenId = await sql.getRow("SELECT * FROM api_tokens WHERE apiTokenId = ?", [entity.apiTokenId]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!apiTokenId) { | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |         await sql.transactional(async () => { | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  |             await sql.replace("api_tokens", entity); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |             await syncTableService.addApiTokenSync(entity.apiTokenId, sourceId); | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         log.info("Update/sync API token " + entity.apiTokenId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2018-04-07 22:32:46 -04:00
										 |  |  |     updateEntity | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | }; |