server-ts: Fix most type errors in becca

This commit is contained in:
Elian Doran 2024-02-17 11:39:29 +02:00
parent 26388ad3b6
commit 2f96dc2d9d
No known key found for this signature in database
6 changed files with 41 additions and 18 deletions

View File

@ -88,11 +88,17 @@ abstract class AbstractBeccaEntity<T extends AbstractBeccaEntity<T>> {
return this.getPojo();
}
abstract hasStringContent(): boolean;
hasStringContent(): boolean {
// FIXME: Not sure why some entities don't implement it.
return true;
}
abstract getPojo(): {};
abstract get isDeleted(): boolean;
get isDeleted(): boolean {
// FIXME: Not sure why some entities don't implement it.
return false;
}
/**
* Saves entity - executes SQL, but doesn't commit the transaction on its own

View File

@ -36,7 +36,7 @@ class BAttachment extends AbstractBeccaEntity<BAttachment> {
noteId?: number;
attachmentId?: string;
/** either noteId or revisionId to which this attachment belongs */
ownerId?: string;
ownerId: string;
role: string;
mime: string;
title: string;

View File

@ -1,5 +1,6 @@
import { BlobRow } from "./rows";
// FIXME: Why this does not extend the abstract becca?
class BBlob {
static get entityName() { return "blobs"; }
static get primaryKeyName() { return "blobId"; }

View File

@ -180,7 +180,9 @@ class BBranch extends AbstractBeccaEntity<BBranch> {
}
for (const childBranch of note.getChildBranches()) {
childBranch.deleteBranch(deleteId, taskContext);
if (childBranch) {
childBranch.deleteBranch(deleteId, taskContext);
}
}
// first delete children and then parent - this will show up better in recent changes
@ -220,11 +222,17 @@ class BBranch extends AbstractBeccaEntity<BBranch> {
if (this.notePosition === undefined || this.notePosition === null) {
let maxNotePos = 0;
for (const childBranch of this.parentNote.getChildBranches()) {
if (maxNotePos < childBranch.notePosition
&& childBranch.noteId !== '_hidden' // hidden has a very large notePosition to always stay last
) {
maxNotePos = childBranch.notePosition;
if (this.parentNote) {
for (const childBranch of this.parentNote.getChildBranches()) {
if (!childBranch) {
continue;
}
if (maxNotePos < childBranch.notePosition
&& childBranch.noteId !== '_hidden' // hidden has a very large notePosition to always stay last
) {
maxNotePos = childBranch.notePosition;
}
}
}

View File

@ -22,7 +22,7 @@ class BEtapiToken extends AbstractBeccaEntity<BEtapiToken> {
etapiTokenId!: string;
name!: string;
tokenHash!: string;
isDeleted!: boolean;
private _isDeleted!: boolean;
constructor(row: EtapiTokenRow) {
super();
@ -35,13 +35,17 @@ class BEtapiToken extends AbstractBeccaEntity<BEtapiToken> {
this.init();
}
get isDeleted() {
return this._isDeleted;
}
updateFromRow(row: EtapiTokenRow) {
this.etapiTokenId = row.etapiTokenId;
this.name = row.name;
this.tokenHash = row.tokenHash;
this.utcDateCreated = row.utcDateCreated || dateUtils.utcNowDateTime();
this.utcDateModified = row.utcDateModified || this.utcDateCreated;
this.isDeleted = !!row.isDeleted;
this._isDeleted = !!row.isDeleted;
if (this.etapiTokenId) {
this.becca.etapiTokens[this.etapiTokenId] = this;

View File

@ -203,8 +203,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
return this.children && this.children.length > 0;
}
/** @returns {BBranch[]} */
getChildBranches() {
getChildBranches(): (BBranch | null)[] {
return this.children.map(childNote => this.becca.getBranchFromChildAndParent(childNote.noteId, this.noteId));
}
@ -757,7 +756,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
this.parents = this.parentBranches
.map(branch => branch.parentNote)
.filter(note => !!note);
.filter(note => !!note) as BNote[];
}
sortChildren() {
@ -771,7 +770,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
const aBranch = becca.getBranchFromChildAndParent(a.noteId, this.noteId);
const bBranch = becca.getBranchFromChildAndParent(b.noteId, this.noteId);
return (aBranch?.notePosition - bBranch?.notePosition) || 0;
return ((aBranch?.notePosition || 0) - (bBranch?.notePosition || 0)) || 0;
});
}
@ -900,7 +899,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
const {searchResultNoteIds} = searchService.searchFromNote(this);
const becca = this.becca;
return searchResultNoteIds
return (searchResultNoteIds as string[]) // FIXME: remove cast once search is converted
.map(resultNoteId => becca.notes[resultNoteId])
.filter(note => !!note);
}
@ -1445,9 +1444,9 @@ class BNote extends AbstractBeccaEntity<BNote> {
cloneTo(parentNoteId: string) {
const cloningService = require('../../services/cloning');
const branch = this.becca.getNote(parentNoteId).getParentBranches()[0];
const branch = this.becca.getNote(parentNoteId)?.getParentBranches()[0];
return cloningService.cloneNoteToBranch(this.noteId, branch.branchId);
return cloningService.cloneNoteToBranch(this.noteId, branch?.branchId);
}
isEligibleForConversionToAttachment(opts: ConvertOpts = { autoConversion: false }) {
@ -1603,6 +1602,11 @@ class BNote extends AbstractBeccaEntity<BNote> {
for (const noteAttachment of this.getAttachments()) {
const revisionAttachment = noteAttachment.copy();
if (!revision.revisionId) {
throw new Error("Revision ID is missing.");
}
revisionAttachment.ownerId = revision.revisionId;
revisionAttachment.setContent(noteAttachment.getContent(), { forceSave: true });