2018-03-24 21:39:15 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const Entity = require('./entity');
|
2018-04-02 20:46:46 -04:00
|
|
|
const dateUtils = require('../services/date_utils');
|
2018-04-03 22:15:28 -04:00
|
|
|
const sql = require('../services/sql');
|
2018-03-24 21:39:15 -04:00
|
|
|
|
2018-08-22 23:37:06 +02:00
|
|
|
/**
|
|
|
|
* Branch represents note's placement in the tree - it's essentially pair of noteId and parentNoteId.
|
|
|
|
* Each note can have multiple (at least one) branches, meaning it can be placed into multiple places in the tree.
|
|
|
|
*
|
2020-05-13 14:42:16 +02:00
|
|
|
* @property {string} branchId - primary key, immutable
|
|
|
|
* @property {string} noteId - immutable
|
|
|
|
* @property {string} parentNoteId - immutable
|
2019-11-09 09:36:08 +01:00
|
|
|
* @property {int} notePosition
|
|
|
|
* @property {string} prefix
|
|
|
|
* @property {boolean} isExpanded
|
|
|
|
* @property {boolean} isDeleted
|
2020-01-03 10:48:36 +01:00
|
|
|
* @property {string|null} deleteId - ID identifying delete transaction
|
2019-11-09 09:36:08 +01:00
|
|
|
* @property {string} utcDateModified
|
|
|
|
* @property {string} utcDateCreated
|
2018-08-22 23:37:06 +02:00
|
|
|
*
|
|
|
|
* @extends Entity
|
|
|
|
*/
|
2018-03-24 21:39:15 -04:00
|
|
|
class Branch extends Entity {
|
2018-08-16 23:00:04 +02:00
|
|
|
static get entityName() { return "branches"; }
|
2018-03-24 21:39:15 -04:00
|
|
|
static get primaryKeyName() { return "branchId"; }
|
2018-05-22 22:22:15 -04:00
|
|
|
// notePosition is not part of hash because it would produce a lot of updates in case of reordering
|
2020-01-03 10:48:36 +01:00
|
|
|
static get hashedProperties() { return ["branchId", "noteId", "parentNoteId", "isDeleted", "deleteId", "prefix"]; }
|
2018-03-31 22:15:06 -04:00
|
|
|
|
2020-07-25 23:24:48 +02:00
|
|
|
/** @returns {Note|null} */
|
2020-06-20 12:31:38 +02:00
|
|
|
getNote() {
|
|
|
|
return this.repository.getNote(this.noteId);
|
2020-04-06 20:59:04 +02:00
|
|
|
}
|
|
|
|
|
2020-07-25 23:24:48 +02:00
|
|
|
/** @returns {Note|null} */
|
2020-06-20 12:31:38 +02:00
|
|
|
getParentNote() {
|
|
|
|
return this.repository.getNote(this.parentNoteId);
|
2018-03-31 23:08:22 -04:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
beforeSaving() {
|
2018-04-03 22:15:28 -04:00
|
|
|
if (this.notePosition === undefined) {
|
2020-06-20 12:31:38 +02:00
|
|
|
const maxNotePos = sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [this.parentNoteId]);
|
2019-10-19 12:36:16 +02:00
|
|
|
this.notePosition = maxNotePos === null ? 0 : maxNotePos + 10;
|
2018-04-03 22:15:28 -04:00
|
|
|
}
|
|
|
|
|
2019-10-31 22:02:55 +01:00
|
|
|
if (!this.isExpanded) {
|
|
|
|
this.isExpanded = false;
|
|
|
|
}
|
|
|
|
|
2018-04-01 17:38:24 -04:00
|
|
|
if (!this.isDeleted) {
|
|
|
|
this.isDeleted = false;
|
|
|
|
}
|
|
|
|
|
2019-03-12 20:58:31 +01:00
|
|
|
if (!this.utcDateCreated) {
|
2019-03-13 22:43:59 +01:00
|
|
|
this.utcDateCreated = dateUtils.utcNowDateTime();
|
2018-05-26 12:38:25 -04:00
|
|
|
}
|
|
|
|
|
2018-08-06 08:59:26 +02:00
|
|
|
super.beforeSaving();
|
2018-08-12 20:04:48 +02:00
|
|
|
|
|
|
|
if (this.isChanged) {
|
2019-03-13 22:43:59 +01:00
|
|
|
this.utcDateModified = dateUtils.utcNowDateTime();
|
2018-08-12 20:04:48 +02:00
|
|
|
}
|
2018-03-31 22:15:06 -04:00
|
|
|
}
|
2018-11-30 10:20:03 +01:00
|
|
|
|
2020-01-28 22:37:06 +01:00
|
|
|
createClone(parentNoteId, notePosition) {
|
2020-01-28 22:15:33 +01:00
|
|
|
return new Branch({
|
|
|
|
noteId: this.noteId,
|
|
|
|
parentNoteId: parentNoteId,
|
|
|
|
notePosition: notePosition,
|
|
|
|
prefix: this.prefix,
|
|
|
|
isExpanded: this.isExpanded,
|
|
|
|
isDeleted: false,
|
|
|
|
utcDateCreated: this.utcDateCreated,
|
|
|
|
utcDateModified: this.utcDateModified
|
|
|
|
});
|
|
|
|
}
|
2018-03-24 21:39:15 -04:00
|
|
|
}
|
|
|
|
|
2020-05-13 14:42:16 +02:00
|
|
|
module.exports = Branch;
|