-
Notifications
You must be signed in to change notification settings - Fork 20
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 Retrieving Workspace Members (#60)
* Add base codes for workspace user * Fix formatting * Add service for retrieve workspace members * Add controller for retrieving workspace members
- Loading branch information
Showing
10 changed files
with
193 additions
and
17 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
10 changes: 10 additions & 0 deletions
10
backend/src/workspace-users/types/find-workspace-users-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,10 @@ | ||
import { ApiProperty } from "@nestjs/swagger"; | ||
import { WorkspaceUserDomain } from "./workspace-user.domain"; | ||
|
||
export class FindWorkspaceUsersResponse { | ||
@ApiProperty({ type: [WorkspaceUserDomain], description: "List of found workspace users" }) | ||
workspaceUsers: Array<WorkspaceUserDomain>; | ||
|
||
@ApiProperty({ type: String, description: "The ID of last workspace user" }) | ||
cursor: string | null; | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/src/workspace-users/types/workspace-user.domain.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,12 @@ | ||
import { ApiProperty } from "@nestjs/swagger"; | ||
|
||
export class WorkspaceUserDomain { | ||
@ApiProperty({ type: String, description: "ID of the user" }) | ||
id: string; | ||
@ApiProperty({ type: String, description: "Nickname of the user" }) | ||
nickname: string; | ||
@ApiProperty({ type: Date, description: "Created date of the user" }) | ||
createdAt: Date; | ||
@ApiProperty({ type: Date, description: "Updated date of the user" }) | ||
updatedAt: Date; | ||
} |
18 changes: 18 additions & 0 deletions
18
backend/src/workspace-users/workspace-users.controller.spec.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,18 @@ | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import { WorkspaceUsersController } from "./workspace-users.controller"; | ||
|
||
describe("WorkspaceUsersController", () => { | ||
let controller: WorkspaceUsersController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [WorkspaceUsersController], | ||
}).compile(); | ||
|
||
controller = module.get<WorkspaceUsersController>(WorkspaceUsersController); | ||
}); | ||
|
||
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,40 @@ | ||
import { Controller, DefaultValuePipe, Get, Param, ParseIntPipe, Query, Req } from "@nestjs/common"; | ||
import { ApiBearerAuth, ApiFoundResponse, ApiOperation, ApiQuery, ApiTags } from "@nestjs/swagger"; | ||
import { FindWorkspaceUsersResponse } from "./types/find-workspace-users-response.type"; | ||
import { AuthroizedRequest } from "src/utils/types/req.type"; | ||
import { WorkspaceUsersService } from "./workspace-users.service"; | ||
|
||
@ApiTags("Workspace.Users") | ||
@ApiBearerAuth() | ||
@Controller("workspaces/:workspace_id/users") | ||
export class WorkspaceUsersController { | ||
constructor(private workspaceUsersService: WorkspaceUsersService) {} | ||
|
||
@Get("") | ||
@ApiOperation({ | ||
summary: "Retrieve the Users in Workspace", | ||
description: "Return the users in the workspace. This API supports KeySet pagination.", | ||
}) | ||
@ApiFoundResponse({ type: FindWorkspaceUsersResponse }) | ||
@ApiQuery({ | ||
name: "page_size", | ||
type: Number, | ||
description: "Page size to fetch (Default to 10)", | ||
required: false, | ||
}) | ||
@ApiQuery({ | ||
name: "cursor", | ||
type: String, | ||
description: | ||
"API returns a limited set of results after a given cursor. If no value is provided, it returns the first page.", | ||
required: false, | ||
}) | ||
async findMany( | ||
@Req() req: AuthroizedRequest, | ||
@Param("workspace_id") workspaceId: string, | ||
@Query("page_size", new DefaultValuePipe(10), ParseIntPipe) pageSize: number, | ||
@Query("cursor", new DefaultValuePipe(undefined)) cursor?: string | ||
): Promise<FindWorkspaceUsersResponse> { | ||
return this.workspaceUsersService.findMany(req.user.id, workspaceId, pageSize, cursor); | ||
} | ||
} |
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,10 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { WorkspaceUsersController } from "./workspace-users.controller"; | ||
import { WorkspaceUsersService } from "./workspace-users.service"; | ||
import { PrismaService } from "src/db/prisma.service"; | ||
|
||
@Module({ | ||
controllers: [WorkspaceUsersController], | ||
providers: [WorkspaceUsersService, PrismaService], | ||
}) | ||
export class WorkspaceUsersModule {} |
18 changes: 18 additions & 0 deletions
18
backend/src/workspace-users/workspace-users.service.spec.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,18 @@ | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import { WorkspaceUsersService } from "./workspace-users.service"; | ||
|
||
describe("WorkspaceUsersService", () => { | ||
let service: WorkspaceUsersService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [WorkspaceUsersService], | ||
}).compile(); | ||
|
||
service = module.get<WorkspaceUsersService>(WorkspaceUsersService); | ||
}); | ||
|
||
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,61 @@ | ||
import { Injectable, NotFoundException } from "@nestjs/common"; | ||
import { Prisma } from "@prisma/client"; | ||
import { PrismaService } from "src/db/prisma.service"; | ||
import { FindWorkspaceUsersResponse } from "./types/find-workspace-users-response.type"; | ||
|
||
@Injectable() | ||
export class WorkspaceUsersService { | ||
constructor(private prismaService: PrismaService) {} | ||
|
||
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.UserFindManyArgs = {}; | ||
|
||
if (cursor) { | ||
additionalOptions.cursor = { id: cursor }; | ||
} | ||
|
||
const workspaceUserList = await this.prismaService.user.findMany({ | ||
take: pageSize + 1, | ||
select: { | ||
id: true, | ||
nickname: true, | ||
updatedAt: true, | ||
createdAt: true, | ||
}, | ||
where: { | ||
userWorkspaceList: { | ||
some: { | ||
workspaceId: { | ||
equals: workspaceId, | ||
}, | ||
}, | ||
}, | ||
}, | ||
orderBy: { | ||
id: "desc", | ||
}, | ||
...additionalOptions, | ||
}); | ||
|
||
return { | ||
workspaceUsers: workspaceUserList.slice(0, pageSize), | ||
cursor: workspaceUserList.length > pageSize ? workspaceUserList[pageSize].id : null, | ||
}; | ||
} | ||
} |