feat: Nested document sharing (#2075)
* migration * frontend routing, api permissioning * feat: apiVersion=2 * feat: re-writing document links to point to share * poc nested documents on share links * fix: nested shareId permissions * ui and language tweaks, comments * breadcrumbs * Add icons to reference list items * refactor: Breadcrumb component * tweaks * Add shared parent note
This commit is contained in:
@@ -375,6 +375,77 @@ Collection.prototype.deleteDocument = async function (document) {
|
||||
await document.deleteWithChildren();
|
||||
};
|
||||
|
||||
Collection.prototype.isChildDocument = function (
|
||||
parentDocumentId,
|
||||
documentId
|
||||
): boolean {
|
||||
let result = false;
|
||||
|
||||
const loopChildren = (documents, input) => {
|
||||
return documents.map((document) => {
|
||||
let parents = [...input];
|
||||
if (document.id === documentId) {
|
||||
result = parents.includes(parentDocumentId);
|
||||
} else {
|
||||
parents.push(document.id);
|
||||
loopChildren(document.children, parents);
|
||||
}
|
||||
return document;
|
||||
});
|
||||
};
|
||||
|
||||
loopChildren(this.documentStructure, []);
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
Collection.prototype.getDocumentTree = function (documentId: string) {
|
||||
let result;
|
||||
|
||||
const loopChildren = (documents) => {
|
||||
if (result) {
|
||||
return;
|
||||
}
|
||||
|
||||
documents.forEach((document) => {
|
||||
if (result) {
|
||||
return;
|
||||
}
|
||||
if (document.id === documentId) {
|
||||
result = document;
|
||||
} else {
|
||||
loopChildren(document.children);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
loopChildren(this.documentStructure);
|
||||
return result;
|
||||
};
|
||||
|
||||
Collection.prototype.getDocumentParents = function (
|
||||
documentId: string
|
||||
): string[] | void {
|
||||
let result;
|
||||
|
||||
const loopChildren = (documents, path = []) => {
|
||||
if (result) {
|
||||
return;
|
||||
}
|
||||
|
||||
documents.forEach((document) => {
|
||||
if (document.id === documentId) {
|
||||
result = path;
|
||||
} else {
|
||||
loopChildren(document.children, [...path, document.id]);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
loopChildren(this.documentStructure);
|
||||
return result;
|
||||
};
|
||||
|
||||
Collection.prototype.removeDocumentInStructure = async function (
|
||||
document,
|
||||
options
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
buildGroup,
|
||||
buildCollection,
|
||||
buildTeam,
|
||||
buildDocument,
|
||||
} from "../test/factories";
|
||||
import { flushdb, seed } from "../test/support";
|
||||
|
||||
@@ -19,6 +20,134 @@ describe("#url", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getDocumentParents", () => {
|
||||
test("should return array of parent document ids", async () => {
|
||||
const parent = await buildDocument();
|
||||
const document = await buildDocument();
|
||||
const collection = await buildCollection({
|
||||
documentStructure: [
|
||||
{
|
||||
...parent.toJSON(),
|
||||
children: [document.toJSON()],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const result = collection.getDocumentParents(document.id);
|
||||
|
||||
expect(result.length).toBe(1);
|
||||
expect(result[0]).toBe(parent.id);
|
||||
});
|
||||
|
||||
test("should return array of parent document ids", async () => {
|
||||
const parent = await buildDocument();
|
||||
const document = await buildDocument();
|
||||
const collection = await buildCollection({
|
||||
documentStructure: [
|
||||
{
|
||||
...parent.toJSON(),
|
||||
children: [document.toJSON()],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const result = collection.getDocumentParents(parent.id);
|
||||
expect(result.length).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getDocumentTree", () => {
|
||||
test("should return document tree", async () => {
|
||||
const document = await buildDocument();
|
||||
const collection = await buildCollection({
|
||||
documentStructure: [document.toJSON()],
|
||||
});
|
||||
|
||||
expect(collection.getDocumentTree(document.id)).toEqual(document.toJSON());
|
||||
});
|
||||
|
||||
test("should return nested documents in tree", async () => {
|
||||
const parent = await buildDocument();
|
||||
const document = await buildDocument();
|
||||
const collection = await buildCollection({
|
||||
documentStructure: [
|
||||
{
|
||||
...parent.toJSON(),
|
||||
children: [document.toJSON()],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(collection.getDocumentTree(parent.id)).toEqual({
|
||||
...parent.toJSON(),
|
||||
children: [document.toJSON()],
|
||||
});
|
||||
expect(collection.getDocumentTree(document.id)).toEqual(document.toJSON());
|
||||
});
|
||||
});
|
||||
|
||||
describe("isChildDocument", () => {
|
||||
test("should return false with unexpected data", async () => {
|
||||
const document = await buildDocument();
|
||||
const collection = await buildCollection({
|
||||
documentStructure: [document.toJSON()],
|
||||
});
|
||||
|
||||
expect(collection.isChildDocument(document.id, document.id)).toEqual(false);
|
||||
expect(collection.isChildDocument(document.id, undefined)).toEqual(false);
|
||||
expect(collection.isChildDocument(undefined, document.id)).toEqual(false);
|
||||
});
|
||||
|
||||
test("should return false if sibling", async () => {
|
||||
const one = await buildDocument();
|
||||
const document = await buildDocument();
|
||||
const collection = await buildCollection({
|
||||
documentStructure: [one.toJSON(), document.toJSON()],
|
||||
});
|
||||
|
||||
expect(collection.isChildDocument(one.id, document.id)).toEqual(false);
|
||||
expect(collection.isChildDocument(document.id, one.id)).toEqual(false);
|
||||
});
|
||||
|
||||
test("should return true if direct child of parent", async () => {
|
||||
const parent = await buildDocument();
|
||||
const document = await buildDocument();
|
||||
const collection = await buildCollection({
|
||||
documentStructure: [
|
||||
{
|
||||
...parent.toJSON(),
|
||||
children: [document.toJSON()],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(collection.isChildDocument(parent.id, document.id)).toEqual(true);
|
||||
expect(collection.isChildDocument(document.id, parent.id)).toEqual(false);
|
||||
});
|
||||
|
||||
test("should return true if nested child of parent", async () => {
|
||||
const parent = await buildDocument();
|
||||
const nested = await buildDocument();
|
||||
const document = await buildDocument();
|
||||
const collection = await buildCollection({
|
||||
documentStructure: [
|
||||
{
|
||||
...parent.toJSON(),
|
||||
children: [
|
||||
{
|
||||
...nested.toJSON(),
|
||||
children: [document.toJSON()],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(collection.isChildDocument(parent.id, document.id)).toEqual(true);
|
||||
expect(collection.isChildDocument(document.id, parent.id)).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#addDocumentToStructure", () => {
|
||||
test("should add as last element without index", async () => {
|
||||
const { collection } = await seed();
|
||||
|
||||
@@ -10,6 +10,7 @@ const Share = sequelize.define(
|
||||
primaryKey: true,
|
||||
},
|
||||
published: DataTypes.BOOLEAN,
|
||||
includeChildDocuments: DataTypes.BOOLEAN,
|
||||
revokedAt: DataTypes.DATE,
|
||||
revokedById: DataTypes.UUID,
|
||||
lastAccessedAt: DataTypes.DATE,
|
||||
|
||||
Reference in New Issue
Block a user