From ed1c2659e1cdc70f1031fb1efb69e03a5f2203cd Mon Sep 17 00:00:00 2001 From: devleejb Date: Thu, 18 Jan 2024 15:05:46 +0900 Subject: [PATCH] Add service for retrieving document list in the workspace --- .../find-workspace-documents-response.type.ts | 13 ++++++ .../workspace-documents.service.ts | 42 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 backend/src/workspace-documents/types/find-workspace-documents-response.type.ts diff --git a/backend/src/workspace-documents/types/find-workspace-documents-response.type.ts b/backend/src/workspace-documents/types/find-workspace-documents-response.type.ts new file mode 100644 index 00000000..4910dbb5 --- /dev/null +++ b/backend/src/workspace-documents/types/find-workspace-documents-response.type.ts @@ -0,0 +1,13 @@ +import { ApiProperty } from "@nestjs/swagger"; +import { WorkspaceDocumentDomain } from "./workspace-document-domain.type"; + +export class FindWorkspaceUsersResponse { + @ApiProperty({ + type: [WorkspaceDocumentDomain], + description: "List of found workspace documents", + }) + documents: Array; + + @ApiProperty({ type: String, description: "The ID of last document" }) + cursor: string | null; +} diff --git a/backend/src/workspace-documents/workspace-documents.service.ts b/backend/src/workspace-documents/workspace-documents.service.ts index e2f522b2..01a41ff2 100644 --- a/backend/src/workspace-documents/workspace-documents.service.ts +++ b/backend/src/workspace-documents/workspace-documents.service.ts @@ -1,5 +1,7 @@ import { Injectable, NotFoundException } from "@nestjs/common"; +import { Prisma } from "@prisma/client"; import { PrismaService } from "src/db/prisma.service"; +import { FindWorkspaceUsersResponse } from "./types/find-workspace-documents-response.type"; @Injectable() export class WorkspaceDocumentsService { @@ -44,4 +46,44 @@ export class WorkspaceDocumentsService { throw new NotFoundException(); } } + + async findMany( + userId: string, + workspaceId: string, + pageSize: number, + cursor?: string + ): Promise { + try { + await this.prismaService.userWorkspace.findFirstOrThrow({ + where: { + userId, + workspaceId, + }, + }); + } catch (e) { + throw new NotFoundException(); + } + + const additionalOptions: Prisma.DocumentFindManyArgs = {}; + + if (cursor) { + additionalOptions.cursor = { id: cursor }; + } + + const documentList = await this.prismaService.document.findMany({ + take: pageSize + 1, + where: { + workspaceId, + }, + orderBy: { + id: "desc", + }, + ...additionalOptions, + }); + + return { + documents: documentList.slice(0, pageSize), + cursor: documentList.length > pageSize ? documentList[pageSize].id : null, + }; + } }