undelete becca conversion

This commit is contained in:
zadam 2021-05-08 22:01:14 +02:00
parent bcfe097dd6
commit 9441cb177f

View File

@ -4,7 +4,6 @@ const optionService = require('./options');
const dateUtils = require('./date_utils'); const dateUtils = require('./date_utils');
const entityChangesService = require('./entity_changes.js'); const entityChangesService = require('./entity_changes.js');
const eventService = require('./events'); const eventService = require('./events');
const repository = require('./repository');
const cls = require('../services/cls'); const cls = require('../services/cls');
const BeccaNote = require('../services/becca/entities/note.js'); const BeccaNote = require('../services/becca/entities/note.js');
const BeccaBranch = require('../services/becca/entities/branch.js'); const BeccaBranch = require('../services/becca/entities/branch.js');
@ -18,6 +17,9 @@ const request = require('./request');
const path = require('path'); const path = require('path');
const url = require('url'); const url = require('url');
const becca = require('../services/becca/becca'); const becca = require('../services/becca/becca');
const Branch = require('../services/becca/entities/branch');
const Note = require('../services/becca/entities/note');
const Attribute = require('../services/becca/entities/attribute');
function getNewNotePosition(parentNoteId) { function getNewNotePosition(parentNoteId) {
const note = becca.notes[parentNoteId]; const note = becca.notes[parentNoteId];
@ -569,73 +571,72 @@ function deleteBranch(branch, deleteId, taskContext) {
* @param {TaskContext} taskContext * @param {TaskContext} taskContext
*/ */
function undeleteNote(note, deleteId, taskContext) { function undeleteNote(note, deleteId, taskContext) {
const undeletedParentBranches = getUndeletedParentBranches(note.noteId, deleteId); const undeletedParentBranchIds = getUndeletedParentBranchIds(note.noteId, deleteId);
if (undeletedParentBranches.length === 0) { if (undeletedParentBranchIds.length === 0) {
// cannot undelete if there's no undeleted parent // cannot undelete if there's no undeleted parent
return; return;
} }
for (const parentBranch of undeletedParentBranches) { for (const parentBranchId of undeletedParentBranchIds) {
undeleteBranch(parentBranch, deleteId, taskContext); undeleteBranch(parentBranchId, deleteId, taskContext);
} }
} }
/** /**
* @param {Branch} branch * @param {string} branchId
* @param {string} deleteId * @param {string} deleteId
* @param {TaskContext} taskContext * @param {TaskContext} taskContext
*/ */
function undeleteBranch(branch, deleteId, taskContext) { function undeleteBranch(branchId, deleteId, taskContext) {
const branch = sql.getRow("SELECT * FROM branches WHERE branchId = ?", [branchId])
if (!branch.isDeleted) { if (!branch.isDeleted) {
return; return;
} }
const note = branch.getNote(); const note = sql.getRow("SELECT * FROM notes WHERE noteId = ?", [branch.noteId]);
if (note.isDeleted && note.deleteId !== deleteId) { if (note.isDeleted && note.deleteId !== deleteId) {
return; return;
} }
branch.isDeleted = false; new Branch(branch).save();
branch.save();
taskContext.increaseProgressCount(); taskContext.increaseProgressCount();
if (note.isDeleted && note.deleteId === deleteId) { if (note.isDeleted && note.deleteId === deleteId) {
note.isDeleted = false; new Note(note).save();
note.save();
const attributeIds = sql.getColumn(` const attributes = sql.getRows(`
SELECT attributeId FROM attributes SELECT * FROM attributes
WHERE isDeleted = 1 WHERE isDeleted = 1
AND deleteId = ? AND deleteId = ?
AND (noteId = ? AND (noteId = ?
OR (type = 'relation' AND value = ?))`, [deleteId, note.noteId, note.noteId]); OR (type = 'relation' AND value = ?))`, [deleteId, note.noteId, note.noteId]);
for (const attr of attributeIds) { for (const attribute of attributes) {
attr.isDeleted = false; new Attribute(attribute).save();
attr.save();
} }
const childBranches = repository.getEntities(` const childBranchIds = sql.getColumn(`
SELECT branches.* SELECT branches.id
FROM branches FROM branches
WHERE branches.isDeleted = 1 WHERE branches.isDeleted = 1
AND branches.deleteId = ? AND branches.deleteId = ?
AND branches.parentNoteId = ?`, [deleteId, note.noteId]); AND branches.parentNoteId = ?`, [deleteId, note.noteId]);
for (const childBranch of childBranches) { for (const childBranchId of childBranchIds) {
undeleteBranch(childBranch, deleteId, taskContext); undeleteBranch(childBranchId, deleteId, taskContext);
} }
} }
} }
/** /**
* @return return deleted branches of an undeleted parent note * @return return deleted branchIds of an undeleted parent note
*/ */
function getUndeletedParentBranches(noteId, deleteId) { function getUndeletedParentBranchIds(noteId, deleteId) {
const branchIds = sql.getColumn(` return sql.getColumn(`
SELECT branches.branchId SELECT branches.branchId
FROM branches FROM branches
JOIN notes AS parentNote ON parentNote.noteId = branches.parentNoteId JOIN notes AS parentNote ON parentNote.noteId = branches.parentNoteId
@ -643,8 +644,6 @@ function getUndeletedParentBranches(noteId, deleteId) {
AND branches.isDeleted = 1 AND branches.isDeleted = 1
AND branches.deleteId = ? AND branches.deleteId = ?
AND parentNote.isDeleted = 0`, [noteId, deleteId]); AND parentNote.isDeleted = 0`, [noteId, deleteId]);
return branchIds.map(branchId => becca.getBranch(branchId));
} }
function scanForLinks(note) { function scanForLinks(note) {
@ -875,7 +874,7 @@ module.exports = {
scanForLinks, scanForLinks,
duplicateSubtree, duplicateSubtree,
duplicateSubtreeWithoutRoot, duplicateSubtreeWithoutRoot,
getUndeletedParentBranches, getUndeletedParentBranches: getUndeletedParentBranchIds,
triggerNoteTitleChanged, triggerNoteTitleChanged,
eraseDeletedNotesNow eraseDeletedNotesNow
}; };