Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(FE) Add retrieve a Document #83

Merged
merged 4 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion backend/src/documents/documents.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Controller, Get, Query } from "@nestjs/common";
import { Controller, Get, Param, Query, Req } from "@nestjs/common";
import { DocumentsService } from "./documents.service";
import { Public } from "src/utils/decorators/auth.decorator";
import {
ApiFoundResponse,
ApiNotFoundResponse,
ApiOkResponse,
ApiOperation,
Expand All @@ -11,6 +12,8 @@ import {
} from "@nestjs/swagger";
import { FindDocumentFromSharingTokenResponse } from "./types/find-document-from-sharing-token-response.type";
import { HttpExceptionResponse } from "src/utils/types/http-exception-response.type";
import { FindDocumentResponse } from "./types/find-document-response.type";
import { AuthroizedRequest } from "src/utils/types/req.type";

@ApiTags("Documents")
@Controller("documents")
Expand Down Expand Up @@ -38,4 +41,22 @@ export class DocumentsController {
): Promise<FindDocumentFromSharingTokenResponse> {
return this.documentsService.findDocumentFromSharingToken(token);
}

@Get(":document_slug")
@ApiOperation({
summary: "Retrieve a Document in the Workspace",
description: "If the user has the access permissions, return a document.",
})
@ApiFoundResponse({ type: FindDocumentResponse })
@ApiNotFoundResponse({
type: HttpExceptionResponse,
description:
"The workspace or document does not exist, or the user lacks the appropriate permissions.",
})
async findOne(
@Req() req: AuthroizedRequest,
@Param("document_slug") documentSlug: string
): Promise<FindDocumentResponse> {
return this.documentsService.findOneBySlug(req.user.id, documentSlug);
}
}
21 changes: 21 additions & 0 deletions backend/src/documents/documents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,25 @@ export class DocumentsService {
role,
};
}

async findOneBySlug(userId: string, documentSlug: string) {
try {
const document = await this.prismaService.document.findFirstOrThrow({
where: {
slug: documentSlug,
},
});

await this.prismaService.userWorkspace.findFirstOrThrow({
where: {
userId,
workspaceId: document.workspaceId,
},
});

return document;
} catch (e) {
throw new NotFoundException();
}
}
}
3 changes: 3 additions & 0 deletions backend/src/documents/types/find-document-response.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { WorkspaceDocumentDomain } from "../../workspace-documents/types/workspace-document-domain.type";

export class FindDocumentResponse extends WorkspaceDocumentDomain {}

This file was deleted.

20 changes: 0 additions & 20 deletions backend/src/workspace-documents/workspace-documents.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { AuthroizedRequest } from "src/utils/types/req.type";
import { CreateWorkspaceDocumentDto } from "./dto/create-workspace-document.dto";
import { CreateWorkspaceDocumentResponse } from "./types/create-workspace-document-response.type";
import { HttpExceptionResponse } from "src/utils/types/http-exception-response.type";
import { FindWorkspaceDocumentResponse } from "./types/find-workspace-document-response.type";
import { FindWorkspaceDocumentsResponse } from "./types/find-workspace-documents-response.type";
import { CreateWorkspaceDocumentShareTokenResponse } from "./types/create-workspace-document-share-token-response.type";
import { CreateWorkspaceDocumentShareTokenDto } from "./dto/create-workspace-document-share-token.dto";
Expand All @@ -36,25 +35,6 @@ import { CreateWorkspaceDocumentShareTokenDto } from "./dto/create-workspace-doc
export class WorkspaceDocumentsController {
constructor(private workspaceDocumentsService: WorkspaceDocumentsService) {}

@Get(":document_slug")
@ApiOperation({
summary: "Retrieve a Document in the Workspace",
description: "If the user has the access permissions, return a document.",
})
@ApiFoundResponse({ type: FindWorkspaceDocumentResponse })
@ApiNotFoundResponse({
type: HttpExceptionResponse,
description:
"The workspace or document does not exist, or the user lacks the appropriate permissions.",
})
async findOne(
@Req() req: AuthroizedRequest,
@Param("workspace_id") workspaceId: string,
@Param("document_slug") documentSlug: string
): Promise<FindWorkspaceDocumentResponse> {
return this.workspaceDocumentsService.findOneBySlug(req.user.id, workspaceId, documentSlug);
}

@Get("")
@ApiOperation({
summary: "Retrieve the Documents in Workspace",
Expand Down
19 changes: 0 additions & 19 deletions backend/src/workspace-documents/workspace-documents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,6 @@ export class WorkspaceDocumentsService {
});
}

async findOneBySlug(userId: string, workspaceId: string, documentSlug: string) {
try {
await this.prismaService.userWorkspace.findFirstOrThrow({
where: {
userId,
workspaceId,
},
});

return this.prismaService.document.findFirstOrThrow({
where: {
slug: documentSlug,
},
});
} catch (e) {
throw new NotFoundException();
}
}

async findMany(
userId: string,
workspaceId: string,
Expand Down
Loading
Loading