feat: Allow unpublishing documents (#1467)
* Allow unpublishing documents * add block unpublish files that has children * add api tests to new route
This commit is contained in:
@@ -5,20 +5,20 @@ import documentMover from "../commands/documentMover";
|
||||
import { InvalidRequestError } from "../errors";
|
||||
import auth from "../middlewares/authentication";
|
||||
import {
|
||||
Backlink,
|
||||
Collection,
|
||||
Document,
|
||||
Event,
|
||||
Revision,
|
||||
Share,
|
||||
Star,
|
||||
View,
|
||||
Revision,
|
||||
Backlink,
|
||||
User,
|
||||
View,
|
||||
} from "../models";
|
||||
import policy from "../policies";
|
||||
import {
|
||||
presentDocument,
|
||||
presentCollection,
|
||||
presentDocument,
|
||||
presentPolicies,
|
||||
} from "../presenters";
|
||||
import { sequelize } from "../sequelize";
|
||||
@@ -1018,4 +1018,31 @@ router.post("documents.delete", auth(), async (ctx) => {
|
||||
};
|
||||
});
|
||||
|
||||
router.post("documents.unpublish", auth(), async (ctx) => {
|
||||
const { id } = ctx.body;
|
||||
ctx.assertPresent(id, "id is required");
|
||||
|
||||
const user = ctx.state.user;
|
||||
const document = await Document.findByPk(id, { userId: user.id });
|
||||
|
||||
authorize(user, "unpublish", document);
|
||||
|
||||
await document.unpublish();
|
||||
|
||||
await Event.create({
|
||||
name: "documents.unpublish",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: user.id,
|
||||
data: { title: document.title },
|
||||
ip: ctx.request.ip,
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
data: await presentDocument(document),
|
||||
policies: presentPolicies(user, [document]),
|
||||
};
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -1736,3 +1736,59 @@ describe("#documents.delete", () => {
|
||||
expect(body).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
describe("#documents.unpublish", () => {
|
||||
it("should unpublish a document", async () => {
|
||||
const { user, document } = await seed();
|
||||
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 fail to unpublish a draft document", async () => {
|
||||
const { user, document } = await seed();
|
||||
document.publishedAt = null;
|
||||
await document.save();
|
||||
|
||||
const res = await server.post("/api/documents.unpublish", {
|
||||
body: { token: user.getJwtToken(), id: document.id },
|
||||
});
|
||||
|
||||
expect(res.status).toEqual(403);
|
||||
});
|
||||
|
||||
it("should fail to unpublish a deleted document", async () => {
|
||||
const { user, document } = await seed();
|
||||
await document.delete();
|
||||
|
||||
const res = await server.post("/api/documents.unpublish", {
|
||||
body: { token: user.getJwtToken(), id: document.id },
|
||||
});
|
||||
|
||||
expect(res.status).toEqual(403);
|
||||
});
|
||||
|
||||
it("should fail to unpublish a archived document", async () => {
|
||||
const { user, document } = await seed();
|
||||
await document.archive();
|
||||
|
||||
const res = await server.post("/api/documents.unpublish", {
|
||||
body: { token: user.getJwtToken(), id: document.id },
|
||||
});
|
||||
|
||||
expect(res.status).toEqual(403);
|
||||
});
|
||||
|
||||
it("should require authentication", async () => {
|
||||
const { document } = await seed();
|
||||
const res = await server.post("/api/documents.unpublish", {
|
||||
body: { id: document.id },
|
||||
});
|
||||
expect(res.status).toEqual(401);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user