Skip to content

Commit

Permalink
Add controller for document sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
devleejb committed Jan 18, 2024
1 parent 9dcb8f8 commit 780bef5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
41 changes: 39 additions & 2 deletions backend/src/documents/documents.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
import { Controller } from "@nestjs/common";
import { Controller, Get, Query } from "@nestjs/common";
import { DocumentsService } from "./documents.service";
import { Public } from "src/utils/decorators/auth.decorator";
import {
ApiNotFoundResponse,
ApiOkResponse,
ApiOperation,
ApiQuery,
ApiTags,
ApiUnauthorizedResponse,
} from "@nestjs/swagger";
import { FindDocumentFromSharingTokenResponse } from "./types/find-document-from-sharing-token-response.type";
import { HttpExceptionResponse } from "src/utils/types/http-exception-response.type";

@ApiTags("Documents")
@Controller("documents")
export class DocumentsController {}
export class DocumentsController {
constructor(private documentsService: DocumentsService) {}

@Public()
@Get("share")
@ApiOperation({
summary: "Retrieve a Shared Document using Sharing Token",
description: "If the user has the access permissions, return a shared document.",
})
@ApiQuery({ type: String, name: "token", description: "Sharing Token" })
@ApiOkResponse({ type: FindDocumentFromSharingTokenResponse })
@ApiNotFoundResponse({
type: HttpExceptionResponse,
description: "The document does not exist.",
})
@ApiUnauthorizedResponse({
type: HttpExceptionResponse,
description: "The sharing token is expired or invalid.",
})
async findDocumentFromSharingToken(
@Query("token") token: string
): Promise<FindDocumentFromSharingTokenResponse> {
return this.documentsService.findDocumentFromSharingToken(token);
}
}
18 changes: 15 additions & 3 deletions backend/src/documents/documents.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Injectable, NotFoundException } from "@nestjs/common";
import { Injectable, NotFoundException, UnauthorizedException } from "@nestjs/common";
import { JwtService } from "@nestjs/jwt";
import { Document } from "@prisma/client";
import { PrismaService } from "src/db/prisma.service";
import { SharingPayload } from "src/utils/types/sharing.type";
import { FindDocumentFromSharingTokenResponse } from "./types/find-document-from-sharing-token-response.type";
import { ShareRoleEnum } from "src/utils/constants/share-role";

@Injectable()
export class DocumentsService {
Expand All @@ -11,8 +13,18 @@ export class DocumentsService {
private jwtService: JwtService
) {}

async findDocumentFromSharingToken(sharingToken: string) {
const { documentId, role } = this.jwtService.verify<SharingPayload>(sharingToken);
async findDocumentFromSharingToken(
sharingToken: string
): Promise<FindDocumentFromSharingTokenResponse> {
let documentId: string, role: ShareRoleEnum;

try {
const payload = this.jwtService.verify<SharingPayload>(sharingToken);
documentId = payload.documentId;
role = payload.role;
} catch (e) {
throw new UnauthorizedException("Invalid sharing token");
}

let document: Document;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class WorkspaceDocumentsService {
role,
},
{
expiresIn: Date.now() - expirationDate.getTime(),
expiresIn: expirationDate.getTime() - Date.now(),
}
);

Expand Down

0 comments on commit 780bef5

Please sign in to comment.