Skip to content

Commit

Permalink
Add service for retrieving document list in the workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
devleejb committed Jan 18, 2024
1 parent 03caebd commit ed1c265
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<WorkspaceDocumentDomain>;

@ApiProperty({ type: String, description: "The ID of last document" })
cursor: string | null;
}
42 changes: 42 additions & 0 deletions backend/src/workspace-documents/workspace-documents.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -44,4 +46,44 @@ export class WorkspaceDocumentsService {
throw new NotFoundException();
}
}

async findMany(
userId: string,
workspaceId: string,
pageSize: number,
cursor?: string
): Promise<FindWorkspaceUsersResponse> {
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,
};
}
}

0 comments on commit ed1c265

Please sign in to comment.