feat: Add documentId filter to events.list (#2287)

This commit is contained in:
Tom Moor
2021-07-08 10:12:06 -07:00
committed by GitHub
parent 241cb11493
commit 9a1c8f07d1
2 changed files with 56 additions and 5 deletions

View File

@ -16,6 +16,7 @@ router.post("events.list", auth(), pagination(), async (ctx) => {
let { let {
sort = "createdAt", sort = "createdAt",
actorId, actorId,
documentId,
collectionId, collectionId,
direction, direction,
name, name,
@ -31,10 +32,12 @@ router.post("events.list", auth(), pagination(), async (ctx) => {
if (actorId) { if (actorId) {
ctx.assertUuid(actorId, "actorId must be a UUID"); ctx.assertUuid(actorId, "actorId must be a UUID");
where = { where = { ...where, actorId };
...where, }
actorId,
}; if (documentId) {
ctx.assertUuid(documentId, "documentId must be a UUID");
where = { ...where, documentId };
} }
if (collectionId) { if (collectionId) {

View File

@ -1,7 +1,7 @@
/* eslint-disable flowtype/require-valid-file-annotation */ /* eslint-disable flowtype/require-valid-file-annotation */
import TestServer from "fetch-test-server"; import TestServer from "fetch-test-server";
import app from "../app"; import app from "../app";
import { buildEvent } from "../test/factories"; import { buildEvent, buildUser } from "../test/factories";
import { flushdb, seed } from "../test/support"; import { flushdb, seed } from "../test/support";
const server = new TestServer(app.callback()); const server = new TestServer(app.callback());
@ -101,6 +101,54 @@ describe("#events.list", () => {
expect(body.data[0].id).toEqual(auditEvent.id); expect(body.data[0].id).toEqual(auditEvent.id);
}); });
it("should allow filtering by documentId", async () => {
const { user, admin, document, collection } = await seed();
const event = await buildEvent({
name: "documents.publish",
collectionId: collection.id,
documentId: document.id,
teamId: user.teamId,
actorId: user.id,
});
const res = await server.post("/api/events.list", {
body: {
token: admin.getJwtToken(),
documentId: document.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(1);
expect(body.data[0].id).toEqual(event.id);
});
it("should not return events for documentId without authorization", async () => {
const { user, document, collection } = await seed();
const actor = await buildUser();
await buildEvent({
name: "documents.publish",
collectionId: collection.id,
documentId: document.id,
teamId: user.teamId,
actorId: user.id,
});
const res = await server.post("/api/events.list", {
body: {
token: actor.getJwtToken(),
documentId: document.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(0);
});
it("should allow filtering by event name", async () => { it("should allow filtering by event name", async () => {
const { user, admin, document, collection } = await seed(); const { user, admin, document, collection } = await seed();