| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | const sql = require('./sql'); | 
					
						
							|  |  |  | const log = require('./log'); | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  | const entityChangesService = require('./entity_changes.js'); | 
					
						
							| 
									
										
										
										
											2019-01-03 23:27:10 +01:00
										 |  |  | const eventService = require('./events'); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function updateEntity(sync, entity, sourceId) { | 
					
						
							| 
									
										
										
										
											2020-03-08 21:59:19 +01:00
										 |  |  |     // can be undefined for options with isSynced=false
 | 
					
						
							|  |  |  |     if (!entity) { | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2020-03-08 21:59:19 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-12 18:31:29 -04:00
										 |  |  |     const {entityName} = sync; | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  |     let updated; | 
					
						
							| 
									
										
										
										
											2018-04-12 18:31:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     if (entityName === 'notes') { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         updated = updateNote(entity, sourceId); | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-15 00:15:09 +01:00
										 |  |  |     else if (entityName === 'note_contents') { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         updated = updateNoteContent(entity, sourceId); | 
					
						
							| 
									
										
										
										
											2019-02-15 00:15:09 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     else if (entityName === 'branches') { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         updated = updateBranch(entity, sourceId); | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (entityName === 'note_revisions') { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         updated = updateNoteRevision(entity, sourceId); | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  |     else if (entityName === 'note_revision_contents') { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         updated = updateNoteRevisionContent(entity, sourceId); | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     else if (entityName === 'note_reordering') { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         updated = updateNoteReordering(sync.entityId, entity, sourceId); | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (entityName === 'options') { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         updated = updateOptions(entity, sourceId); | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (entityName === 'recent_notes') { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         updated = updateRecentNotes(entity, sourceId); | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  |     else if (entityName === 'attributes') { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         updated = updateAttribute(entity, sourceId); | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     else if (entityName === 'api_tokens') { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         updated = updateApiToken(entity, sourceId); | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2018-07-29 20:33:42 +02:00
										 |  |  |         throw new Error(`Unrecognized entity type ${entityName}`); | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-01-03 23:27:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // currently making exception for protected notes and note revisions because here
 | 
					
						
							|  |  |  |     // the title and content are not available decrypted as listeners would expect
 | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  |     if (updated && | 
					
						
							|  |  |  |         (!['notes', 'note_contents', 'note_revisions', 'note_revision_contents'].includes(entityName) || !entity.isProtected)) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         eventService.emit(eventService.ENTITY_SYNCED, { | 
					
						
							| 
									
										
										
										
											2019-01-03 23:27:10 +01:00
										 |  |  |             entityName, | 
					
						
							|  |  |  |             entity | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return updated; | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 17:49:44 +01:00
										 |  |  | function shouldWeUpdateEntity(localEntity, remoteEntity) { | 
					
						
							|  |  |  |     if (!localEntity) { | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const localDate = localEntity.utcDateModified || localEntity.utcDateCreated; | 
					
						
							|  |  |  |     const remoteDate = remoteEntity.utcDateModified || remoteEntity.utcDateCreated; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (localDate < remoteDate) { | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // this can happen in case of sync error when hashes are different but dates are the same - we should still update
 | 
					
						
							|  |  |  |     if (localEntity.hash !== remoteEntity.hash && localDate === remoteDate) { | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function updateNote(remoteEntity, sourceId) { | 
					
						
							|  |  |  |     const localEntity = sql.getRow("SELECT * FROM notes WHERE noteId = ?", [remoteEntity.noteId]); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 17:49:44 +01:00
										 |  |  |     if (shouldWeUpdateEntity(localEntity, remoteEntity)) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         sql.transactional(() => { | 
					
						
							|  |  |  |             sql.replace("notes", remoteEntity); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |             entityChangesService.addNoteSync(remoteEntity.noteId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function updateNoteContent(remoteEntity, sourceId) { | 
					
						
							|  |  |  |     const localEntity = sql.getRow("SELECT * FROM note_contents WHERE noteId = ?", [remoteEntity.noteId]); | 
					
						
							| 
									
										
										
										
											2019-02-15 00:15:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 17:49:44 +01:00
										 |  |  |     if (shouldWeUpdateEntity(localEntity, remoteEntity)) { | 
					
						
							|  |  |  |         remoteEntity.content = remoteEntity.content === null ? null : Buffer.from(remoteEntity.content, 'base64'); | 
					
						
							| 
									
										
										
										
											2019-02-15 00:15:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         sql.transactional(() => { | 
					
						
							|  |  |  |             sql.replace("note_contents", remoteEntity); | 
					
						
							| 
									
										
										
										
											2019-02-15 00:15:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |             entityChangesService.addNoteContentSync(remoteEntity.noteId, sourceId); | 
					
						
							| 
									
										
										
										
											2019-02-15 00:15:09 +01:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2019-02-15 00:15:09 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2019-02-15 00:15:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function updateBranch(remoteEntity, sourceId) { | 
					
						
							|  |  |  |     const localEntity = sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [remoteEntity.branchId]); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  |     if (shouldWeUpdateEntity(localEntity, remoteEntity)) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         sql.transactional(() => { | 
					
						
							| 
									
										
										
										
											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.
 | 
					
						
							| 
									
										
										
										
											2019-12-24 17:49:44 +01:00
										 |  |  |             if (localEntity) { | 
					
						
							|  |  |  |                 delete remoteEntity.isExpanded; | 
					
						
							| 
									
										
										
										
											2018-07-24 22:03:36 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-11-09 21:11:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |             sql.replace('branches', remoteEntity); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |             entityChangesService.addBranchSync(remoteEntity.branchId, sourceId); | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function updateNoteRevision(remoteEntity, sourceId) { | 
					
						
							|  |  |  |     const localEntity = sql.getRowOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [remoteEntity.noteRevisionId]); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     sql.transactional(() => { | 
					
						
							| 
									
										
										
										
											2019-12-24 17:49:44 +01:00
										 |  |  |         if (shouldWeUpdateEntity(localEntity, remoteEntity)) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |             sql.replace('note_revisions', remoteEntity); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |             entityChangesService.addNoteRevisionSync(remoteEntity.noteRevisionId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 17:49:44 +01:00
										 |  |  |             log.info("Update/sync note revision " + remoteEntity.noteRevisionId); | 
					
						
							| 
									
										
										
										
											2017-11-28 17:04:47 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function updateNoteRevisionContent(remoteEntity, sourceId) { | 
					
						
							|  |  |  |     const localEntity = sql.getRowOrNull("SELECT * FROM note_revision_contents WHERE noteRevisionId = ?", [remoteEntity.noteRevisionId]); | 
					
						
							| 
									
										
										
										
											2019-11-01 20:00:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  |     if (shouldWeUpdateEntity(localEntity, remoteEntity)) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         sql.transactional(() => { | 
					
						
							| 
									
										
										
										
											2019-12-24 17:49:44 +01:00
										 |  |  |             remoteEntity.content = remoteEntity.content === null ? null : Buffer.from(remoteEntity.content, 'base64'); | 
					
						
							| 
									
										
										
										
											2019-11-01 20:00:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |             sql.replace('note_revision_contents', remoteEntity); | 
					
						
							| 
									
										
										
										
											2019-11-01 20:00:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |             entityChangesService.addNoteRevisionContentSync(remoteEntity.noteRevisionId, sourceId); | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2019-11-01 20:00:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2019-11-01 20:00:56 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function updateNoteReordering(entityId, remote, sourceId) { | 
					
						
							|  |  |  |     sql.transactional(() => { | 
					
						
							| 
									
										
										
										
											2019-12-24 17:49:44 +01:00
										 |  |  |         for (const key in remote) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |             sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [remote[key], key]); | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |         entityChangesService.addNoteReorderingSync(entityId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function updateOptions(remoteEntity, sourceId) { | 
					
						
							|  |  |  |     const localEntity = sql.getRowOrNull("SELECT * FROM options WHERE name = ?", [remoteEntity.name]); | 
					
						
							| 
									
										
										
										
											2018-01-11 22:45:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 17:49:44 +01:00
										 |  |  |     if (localEntity && !localEntity.isSynced) { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  |     if (shouldWeUpdateEntity(localEntity, remoteEntity)) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         sql.transactional(() => { | 
					
						
							|  |  |  |             sql.replace('options', remoteEntity); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |             entityChangesService.addOptionsSync(remoteEntity.name, sourceId, true); | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function updateRecentNotes(remoteEntity, sourceId) { | 
					
						
							|  |  |  |     const localEntity = sql.getRowOrNull("SELECT * FROM recent_notes WHERE noteId = ?", [remoteEntity.noteId]); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 17:49:44 +01:00
										 |  |  |     if (shouldWeUpdateEntity(localEntity, remoteEntity)) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         sql.transactional(() => { | 
					
						
							|  |  |  |             sql.replace('recent_notes', remoteEntity); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |             entityChangesService.addRecentNoteSync(remoteEntity.noteId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2018-11-08 10:11:00 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2018-11-08 10:11:00 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function updateAttribute(remoteEntity, sourceId) { | 
					
						
							|  |  |  |     const localEntity = sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [remoteEntity.attributeId]); | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 17:49:44 +01:00
										 |  |  |     if (shouldWeUpdateEntity(localEntity, remoteEntity)) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         sql.transactional(() => { | 
					
						
							|  |  |  |             sql.replace("attributes", remoteEntity); | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |             entityChangesService.addAttributeSync(remoteEntity.attributeId, sourceId); | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function updateApiToken(entity, sourceId) { | 
					
						
							|  |  |  |     const apiTokenId = sql.getRow("SELECT * FROM api_tokens WHERE apiTokenId = ?", [entity.apiTokenId]); | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!apiTokenId) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         sql.transactional(() => { | 
					
						
							|  |  |  |             sql.replace("api_tokens", entity); | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |             entityChangesService.addApiTokenSync(entity.apiTokenId, sourceId); | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-04 14:57:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2018-04-07 22:32:46 -04:00
										 |  |  |     updateEntity | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | }; |