chore: Improvements to document move behavior (#4689)

* chore: Improvements to document move behavior

* test
This commit is contained in:
Tom Moor
2023-01-12 21:48:09 -05:00
committed by GitHub
parent 17a8dbb3f0
commit e347404502
7 changed files with 112 additions and 69 deletions

View File

@@ -1,10 +1,6 @@
import { sequelize } from "@server/database/sequelize";
import Pin from "@server/models/Pin";
import {
buildDocument,
buildCollection,
buildUser,
} from "@server/test/factories";
import { buildDocument, buildCollection } from "@server/test/factories";
import { setupTestDatabase, seed } from "@server/test/support";
import documentMover from "./documentMover";
@@ -25,28 +21,31 @@ describe("documentMover", () => {
expect(response.documents.length).toEqual(1);
});
it("should error when not in source collection documentStructure", async () => {
const user = await buildUser();
const collection = await buildCollection({
teamId: user.teamId,
});
const document = await buildDocument({
it("should succeed when not in source collection documentStructure", async () => {
const { document, user, collection } = await seed();
const newDocument = await buildDocument({
parentDocumentId: document.id,
collectionId: collection.id,
teamId: collection.teamId,
userId: collection.createdById,
title: "Child document",
text: "content",
});
await document.archive(user.id);
let error;
try {
await documentMover({
user,
document,
collectionId: collection.id,
ip,
});
} catch (err) {
error = err;
}
expect(error).toBeTruthy();
const response = await documentMover({
user,
document,
collectionId: collection.id,
parentDocumentId: undefined,
index: 0,
ip,
});
expect(response.collections[0].documentStructure![0].children[0].id).toBe(
newDocument.id
);
expect(response.collections.length).toEqual(1);
expect(response.documents.length).toEqual(1);
expect(response.documents[0].collection?.id).toEqual(collection.id);
expect(response.documents[0].updatedBy.id).toEqual(user.id);
});
it("should move with children", async () => {

View File

@@ -1,6 +1,5 @@
import invariant from "invariant";
import { Transaction } from "sequelize";
import { ValidationError } from "@server/errors";
import { traceFunction } from "@server/logging/tracing";
import { User, Document, Collection, Pin, Event } from "@server/models";
import pinDestroyer from "./pinDestroyer";
@@ -74,11 +73,11 @@ async function documentMover({
save: collectionChanged,
});
const documentJson = response?.[0];
let documentJson = response?.[0];
const fromIndex = response?.[1] || 0;
if (!documentJson) {
throw ValidationError("The document was not found in the collection");
documentJson = await document.toNavigationNode({ transaction });
}
// if we're reordering from within the same parent