-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(BE) Add API for Document Sharing (#65)
* Add JwtModule to workspace-documents module for sharing * Add service for sharing token * Add controller for creating sharing token * Add documents module base code * Fix formatting * Add JwtModule to document module * Add service for document sharing * Add controller for document sharing
- Loading branch information
Showing
19 changed files
with
313 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import { DocumentsController } from "./documents.controller"; | ||
|
||
describe("DocumentsController", () => { | ||
let controller: DocumentsController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [DocumentsController], | ||
}).compile(); | ||
|
||
controller = module.get<DocumentsController>(DocumentsController); | ||
}); | ||
|
||
it("should be defined", () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
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 { | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { DocumentsController } from "./documents.controller"; | ||
import { DocumentsService } from "./documents.service"; | ||
import { JwtModule } from "@nestjs/jwt"; | ||
import { ConfigService } from "@nestjs/config"; | ||
import { PrismaService } from "src/db/prisma.service"; | ||
|
||
@Module({ | ||
imports: [ | ||
JwtModule.registerAsync({ | ||
useFactory: async (configService: ConfigService) => { | ||
return { | ||
secret: configService.get<string>("JWT_SHARING_SECRET"), | ||
}; | ||
}, | ||
inject: [ConfigService], | ||
}), | ||
], | ||
controllers: [DocumentsController], | ||
providers: [DocumentsService, PrismaService], | ||
}) | ||
export class DocumentsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import { DocumentsService } from "./documents.service"; | ||
|
||
describe("DocumentsService", () => { | ||
let service: DocumentsService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [DocumentsService], | ||
}).compile(); | ||
|
||
service = module.get<DocumentsService>(DocumentsService); | ||
}); | ||
|
||
it("should be defined", () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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 { | ||
constructor( | ||
private prismaService: PrismaService, | ||
private jwtService: JwtService | ||
) {} | ||
|
||
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; | ||
|
||
try { | ||
document = await this.prismaService.document.findUniqueOrThrow({ | ||
where: { | ||
id: documentId, | ||
}, | ||
}); | ||
} catch (e) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
return { | ||
...document, | ||
role, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ApiProperty } from "@nestjs/swagger"; | ||
|
||
export class DocumentDomain { | ||
@ApiProperty({ type: String, description: "ID of the document" }) | ||
id: string; | ||
@ApiProperty({ type: String, description: "Yorkie Document ID of the document" }) | ||
yorkieDocumentId: string; | ||
@ApiProperty({ type: String, description: "Title of the document" }) | ||
title: string; | ||
@ApiProperty({ type: String, description: "Content of the document", required: false }) | ||
content?: string; | ||
@ApiProperty({ type: Date, description: "Created date of the document" }) | ||
createdAt: Date; | ||
@ApiProperty({ type: Date, description: "Updated date of the document" }) | ||
updatedAt: Date; | ||
@ApiProperty({ type: String, description: "ID of the workspace that includes the document" }) | ||
workspaceId: string; | ||
} |
8 changes: 8 additions & 0 deletions
8
backend/src/documents/types/find-document-from-sharing-token-response.type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ShareRoleEnum } from "src/utils/constants/share-role"; | ||
import { DocumentDomain } from "./document-domain.type"; | ||
import { ApiProperty } from "@nestjs/swagger"; | ||
|
||
export class FindDocumentFromSharingTokenResponse extends DocumentDomain { | ||
@ApiProperty({ enum: ShareRoleEnum, description: "Role of share token" }) | ||
role: ShareRoleEnum; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export const WorkspaceRoleConstants = { | ||
OWNER: "OWNER", | ||
MEMBER: "MEMBER", | ||
}; | ||
export enum WorkspaceRoleConstants { | ||
OWNER = "OWNER", | ||
MEMBER = "MEMBER", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum ShareRoleEnum { | ||
READ = "READ", | ||
EDIT = "EDIT", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type ShareRole = "READ" | "EDIT"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ShareRoleEnum } from "../constants/share-role"; | ||
|
||
export class SharingPayload { | ||
documentId: string; | ||
role: ShareRoleEnum; | ||
} |
14 changes: 14 additions & 0 deletions
14
backend/src/workspace-documents/dto/create-workspace-document-share-token.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ApiProperty } from "@nestjs/swagger"; | ||
import { Type } from "class-transformer"; | ||
import { IsDate } from "class-validator"; | ||
import { ShareRoleEnum } from "src/utils/constants/share-role"; | ||
|
||
export class CreateWorkspaceDocumentShareTokenDto { | ||
@ApiProperty({ enum: ShareRoleEnum, description: "Role to share" }) | ||
role: ShareRoleEnum; | ||
|
||
@ApiProperty({ type: Date, description: "Share link expiration date" }) | ||
@Type(() => Date) | ||
@IsDate() | ||
expirationDate: Date; | ||
} |
9 changes: 9 additions & 0 deletions
9
backend/src/workspace-documents/types/create-workspace-document-share-token-response.type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ApiProperty } from "@nestjs/swagger"; | ||
|
||
export class CreateWorkspaceDocumentShareTokenResponse { | ||
@ApiProperty({ | ||
type: String, | ||
description: "Sharing token", | ||
}) | ||
sharingToken: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.