fix: Can't un-publish docs with archived children. closes #6408
This commit is contained in:
@@ -859,12 +859,9 @@ class Document extends ParanoidModel<
|
||||
const parent = await (this.constructor as typeof Document).findOne({
|
||||
where: {
|
||||
id: this.parentDocumentId,
|
||||
archivedAt: {
|
||||
[Op.is]: null,
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!parent) {
|
||||
if (parent?.isDraft || !parent?.isActive) {
|
||||
this.parentDocumentId = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3595,6 +3595,30 @@ describe("#documents.unpublish", () => {
|
||||
expect(reloaded!.createdById).toEqual(user.id);
|
||||
});
|
||||
|
||||
it("should unpublish a document with archived children", async () => {
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const child = await buildDocument({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
parentDocumentId: document.id,
|
||||
});
|
||||
await child.archive(user.id);
|
||||
const res = await server.post("/api/documents.unpublish", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
id: document.id,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.id).toEqual(document.id);
|
||||
expect(body.data.publishedAt).toBeNull();
|
||||
});
|
||||
|
||||
it("should unpublish another users document", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
|
||||
@@ -1253,7 +1253,11 @@ router.post(
|
||||
});
|
||||
authorize(user, "unpublish", document);
|
||||
|
||||
const childDocumentIds = await document.findAllChildDocumentIds();
|
||||
const childDocumentIds = await document.findAllChildDocumentIds({
|
||||
archivedAt: {
|
||||
[Op.eq]: null,
|
||||
},
|
||||
});
|
||||
if (childDocumentIds.length > 0) {
|
||||
throw InvalidRequestError(
|
||||
"Cannot unpublish document with child documents"
|
||||
|
||||
Reference in New Issue
Block a user