Skip to content

Commit

Permalink
2227 disable deletion of an anchored entity (#2327)
Browse files Browse the repository at this point in the history
* add entityIds field to document, not only for response

* restrict removal of anchored entity
  • Loading branch information
jancimertel authored Sep 15, 2024
1 parent 6eb2178 commit ecfc5ac
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/server/src/models/document/document.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IDbModel } from "@models/common";
import { r as rethink, Connection, WriteResult } from "rethinkdb-ts";
import { r as rethink, Connection, WriteResult, RDatum } from "rethinkdb-ts";
import { IDocument } from "@shared/types";
import { UserEnums } from "@shared/enums";
import { InternalServerError, ModelNotValidError } from "@shared/types/errors";
Expand Down Expand Up @@ -191,6 +191,20 @@ export default class Document implements IDocument, IDbModel {
return entries && entries.length ? entries.map((d) => new Document(d)) : [];
}

static async findByEntityId(
db: Connection,
entityId: string
): Promise<Document[]> {
const entries = await rethink
.table(Document.table)
.filter(function (row: RDatum) {
return row("entityIds").contains(entityId);
})
.run(db);

return entries && entries.length ? entries.map((d) => new Document(d)) : [];
}

/**
* Retrieves all documents
* @param db Connection database connection
Expand Down
18 changes: 18 additions & 0 deletions packages/server/src/modules/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { IRequest } from "src/custom_typings/request";
import Relation from "@models/relation/relation";
import { RelationEnums } from "@shared/enums";
import { Relation as RelationType } from "@shared/types";
import Document from "@models/document/document";
import { copyRelations } from "@models/relation/functions";
import Entity from "@models/entity/entity";
import { EventType } from "@shared/types/stats";
Expand Down Expand Up @@ -521,6 +522,23 @@ export default Router()
continue;
}

const docsIds = await Document.findByEntityId(
req.db.connection,
entity.id
);
if (docsIds.length) {
out.result = false;
out.data[entity.id] = new InvalidDeleteError(
`Cannot be deleted while anchored to documents (${
docsIds[0].id +
(docsIds.length > 1
? " + " + (docsIds.length - 1) + " others"
: "")
})`
).withData(docsIds.map((d) => d.id));
continue;
}

const model = getEntityClass(entity);
if (!model.canBeDeletedByUser(req.getUserOrFail())) {
out.result = false;
Expand Down

0 comments on commit ecfc5ac

Please sign in to comment.