From eaf2fd102eb49e8f3657c01aefbcc8f9f87b07de Mon Sep 17 00:00:00 2001 From: "codegen-sh[bot]" <131295404+codegen-sh[bot]@users.noreply.github.com> Date: Tue, 20 May 2025 19:25:55 -0400 Subject: [PATCH] Add `attachments.list` API endpoint (#9259) * Add attachments.list endpoint to retrieve user's attachments * Fix TypeScript error in attachments.list endpoint * Address review comments: restore tests, properly type WhereOptions, remove comments * fix: Pagination, user filtering --------- Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com> Co-authored-by: Codegen Bot Co-authored-by: Tom Moor --- server/presenters/attachment.ts | 1 + .../api/attachments/attachments.test.ts | 114 ++++++++++++++++++ server/routes/api/attachments/attachments.ts | 53 +++++++- server/routes/api/attachments/schema.ts | 11 ++ 4 files changed, 178 insertions(+), 1 deletion(-) diff --git a/server/presenters/attachment.ts b/server/presenters/attachment.ts index 6c46c0fbee..f0794da806 100644 --- a/server/presenters/attachment.ts +++ b/server/presenters/attachment.ts @@ -2,6 +2,7 @@ import { Attachment } from "@server/models"; export default function presentAttachment(attachment: Attachment) { return { + userId: attachment.userId, documentId: attachment.documentId, contentType: attachment.contentType, name: attachment.name, diff --git a/server/routes/api/attachments/attachments.test.ts b/server/routes/api/attachments/attachments.test.ts index caeb12926d..4cee83831d 100644 --- a/server/routes/api/attachments/attachments.test.ts +++ b/server/routes/api/attachments/attachments.test.ts @@ -15,6 +15,120 @@ jest.mock("@server/storage/files"); const server = getTestServer(); +describe("#attachments.list", () => { + it("should return attachments for user", async () => { + const user = await buildUser(); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + }); + const attachment = await buildAttachment({ + teamId: user.teamId, + userId: user.id, + documentId: document.id, + }); + const attachment2 = await buildAttachment({ + teamId: user.teamId, + userId: user.id, + }); + + const res = await server.post("/api/attachments.list", { + body: { + token: user.getJwtToken(), + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.pagination.total).toEqual(2); + expect(body.data.length).toEqual(2); + expect(body.data[0].id).toEqual(attachment.id); + expect(body.data[1].id).toEqual(attachment2.id); + }); + + it("should allow filtering by userId when user is an admin", async () => { + const admin = await buildAdmin(); + const user = await buildUser({ teamId: admin.teamId }); + // Attachments for user + const attachment1 = await buildAttachment({ + teamId: admin.teamId, + userId: user.id, + }); + // Attachment for admin + await buildAttachment({ + teamId: admin.teamId, + userId: admin.id, + }); + + const res = await server.post("/api/attachments.list", { + body: { + userId: user.id, + token: admin.getJwtToken(), + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.length).toEqual(1); + expect(body.data[0].id).toEqual(attachment1.id); + }); + + it("should filter by documentId", async () => { + const user = await buildUser(); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + }); + const attachment = await buildAttachment({ + teamId: user.teamId, + userId: user.id, + documentId: document.id, + }); + await buildAttachment({ + teamId: user.teamId, + userId: user.id, + }); + + const res = await server.post("/api/attachments.list", { + body: { + documentId: document.id, + token: user.getJwtToken(), + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.length).toEqual(1); + expect(body.data[0].id).toEqual(attachment.id); + }); + + it("should not return attachments created by other users", async () => { + const user = await buildUser(); + const anotherUser = await buildUser({ + teamId: user.teamId, + }); + await buildAttachment({ + teamId: user.teamId, + userId: anotherUser.id, + }); + + const res = await server.post("/api/attachments.list", { + body: { + token: user.getJwtToken(), + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.length).toEqual(0); + }); + + it("should require authentication", async () => { + const res = await server.post("/api/attachments.list"); + expect(res.status).toEqual(401); + }); +}); + describe("#attachments.create", () => { it("should require authentication", async () => { const res = await server.post("/api/attachments.create"); diff --git a/server/routes/api/attachments/attachments.ts b/server/routes/api/attachments/attachments.ts index e2147daf0f..3da6498e79 100644 --- a/server/routes/api/attachments/attachments.ts +++ b/server/routes/api/attachments/attachments.ts @@ -1,4 +1,5 @@ import Router from "koa-router"; +import { WhereOptions } from "sequelize"; import { v4 as uuidv4 } from "uuid"; import { AttachmentPreset } from "@shared/types"; import { bytesToHumanReadable, getFileNameFromUrl } from "@shared/utils/files"; @@ -16,8 +17,9 @@ import validate from "@server/middlewares/validate"; import { Attachment, Document } from "@server/models"; import AttachmentHelper from "@server/models/helpers/AttachmentHelper"; import { authorize } from "@server/policies"; -import { presentAttachment } from "@server/presenters"; +import { presentAttachment, presentPolicies } from "@server/presenters"; import UploadAttachmentFromUrlTask from "@server/queues/tasks/UploadAttachmentFromUrlTask"; +import pagination from "@server/routes/api/middlewares/pagination"; import { sequelize } from "@server/storage/database"; import FileStorage from "@server/storage/files"; import BaseStorage from "@server/storage/files/BaseStorage"; @@ -28,6 +30,55 @@ import * as T from "./schema"; const router = new Router(); +router.post( + "attachments.list", + auth(), + pagination(), + validate(T.AttachmentsListSchema), + async (ctx: APIContext) => { + const { documentId, userId } = ctx.input.body; + const { user } = ctx.state.auth; + + const where: WhereOptions = { + teamId: user.teamId, + }; + + // If a specific user is passed then add to filters + if (userId && user.isAdmin) { + where.userId = userId; + } else { + where.userId = user.id; + } + + // If a specific document is passed then add to filters + if (documentId) { + const document = await Document.findByPk(documentId, { + userId: user.id, + }); + authorize(user, "read", document); + where.documentId = documentId; + } + + const [attachments, total] = await Promise.all([ + Attachment.findAll({ + where, + order: [["createdAt", "DESC"]], + offset: ctx.state.pagination.offset, + limit: ctx.state.pagination.limit, + }), + Attachment.count({ + where, + }), + ]); + + ctx.body = { + pagination: { ...ctx.state.pagination, total }, + data: attachments.map(presentAttachment), + policies: presentPolicies(user, attachments), + }; + } +); + router.post( "attachments.create", rateLimiter(RateLimiterStrategy.TenPerMinute), diff --git a/server/routes/api/attachments/schema.ts b/server/routes/api/attachments/schema.ts index 86a432a6ee..e4b4ae4d0b 100644 --- a/server/routes/api/attachments/schema.ts +++ b/server/routes/api/attachments/schema.ts @@ -3,6 +3,17 @@ import { z } from "zod"; import { AttachmentPreset } from "@shared/types"; import { BaseSchema } from "@server/routes/api/schema"; +export const AttachmentsListSchema = BaseSchema.extend({ + body: z.object({ + /** Id of the document to which the Attachment belongs */ + documentId: z.string().uuid().optional(), + /** Id of the user that uploaded the Attachment */ + userId: z.string().uuid().optional(), + }), +}); + +export type AttachmentsListReq = z.infer; + export const AttachmentsCreateSchema = BaseSchema.extend({ body: z.object({ /** Attachment name */