feat: Allow moving draft documents (#4652)
* feat: Allow moving draft documents * Allow drag-n-drop move of draft documents * fix: Allow moving draft without a collection * fix: Allow moving draft without a collection
This commit is contained in:
@@ -67,43 +67,45 @@ async function documentMover({
|
||||
|
||||
invariant(newCollection, "collection should exist");
|
||||
|
||||
// Remove the document from the current collection
|
||||
const response = await collection?.removeDocumentInStructure(document, {
|
||||
transaction,
|
||||
});
|
||||
if (document.publishedAt) {
|
||||
// Remove the document from the current collection
|
||||
const response = await collection?.removeDocumentInStructure(document, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
const documentJson = response?.[0];
|
||||
const fromIndex = response?.[1] || 0;
|
||||
const documentJson = response?.[0];
|
||||
const fromIndex = response?.[1] || 0;
|
||||
|
||||
if (!documentJson) {
|
||||
throw ValidationError("The document was not found in the collection");
|
||||
if (!documentJson) {
|
||||
throw ValidationError("The document was not found in the collection");
|
||||
}
|
||||
|
||||
// if we're reordering from within the same parent
|
||||
// the original and destination collection are the same,
|
||||
// so when the initial item is removed above, the list will reduce by 1.
|
||||
// We need to compensate for this when reordering
|
||||
const toIndex =
|
||||
index !== undefined &&
|
||||
document.parentDocumentId === parentDocumentId &&
|
||||
document.collectionId === collectionId &&
|
||||
fromIndex < index
|
||||
? index - 1
|
||||
: index;
|
||||
|
||||
// Add the document and it's tree to the new collection
|
||||
await newCollection.addDocumentToStructure(document, toIndex, {
|
||||
documentJson,
|
||||
transaction,
|
||||
});
|
||||
}
|
||||
|
||||
// if we're reordering from within the same parent
|
||||
// the original and destination collection are the same,
|
||||
// so when the initial item is removed above, the list will reduce by 1.
|
||||
// We need to compensate for this when reordering
|
||||
const toIndex =
|
||||
index !== undefined &&
|
||||
document.parentDocumentId === parentDocumentId &&
|
||||
document.collectionId === collectionId &&
|
||||
fromIndex < index
|
||||
? index - 1
|
||||
: index;
|
||||
|
||||
// Update the properties on the document record
|
||||
document.collectionId = collectionId;
|
||||
document.parentDocumentId = parentDocumentId;
|
||||
document.lastModifiedById = user.id;
|
||||
document.updatedBy = user;
|
||||
|
||||
// Add the document and it's tree to the new collection
|
||||
await newCollection.addDocumentToStructure(document, toIndex, {
|
||||
documentJson,
|
||||
transaction,
|
||||
});
|
||||
|
||||
if (collection) {
|
||||
if (collection && document.publishedAt) {
|
||||
result.collections.push(collection);
|
||||
}
|
||||
|
||||
|
||||
@@ -128,7 +128,7 @@ describe("no collection", () => {
|
||||
expect(abilities.createChildDocument).toEqual(false);
|
||||
expect(abilities.delete).toEqual(true);
|
||||
expect(abilities.download).toEqual(true);
|
||||
expect(abilities.move).toEqual(false);
|
||||
expect(abilities.move).toEqual(true);
|
||||
expect(abilities.permanentDelete).toEqual(false);
|
||||
expect(abilities.pin).toEqual(false);
|
||||
expect(abilities.pinToHome).toEqual(false);
|
||||
|
||||
@@ -174,14 +174,7 @@ allow(User, "move", Document, (user, document) => {
|
||||
if (document.deletedAt) {
|
||||
return false;
|
||||
}
|
||||
if (!document.publishedAt) {
|
||||
return false;
|
||||
}
|
||||
invariant(
|
||||
document.collection,
|
||||
"collection is missing, did you forget to include in the query scope?"
|
||||
);
|
||||
if (cannot(user, "update", document.collection)) {
|
||||
if (document.collection && cannot(user, "update", document.collection)) {
|
||||
return false;
|
||||
}
|
||||
return user.teamId === document.teamId;
|
||||
|
||||
Reference in New Issue
Block a user