Skip to content

Commit

Permalink
Add service for retrieve workspace members
Browse files Browse the repository at this point in the history
  • Loading branch information
devleejb committed Jan 18, 2024
1 parent f80af41 commit aec14b0
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 19 deletions.
18 changes: 9 additions & 9 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ datasource db {
}

model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
socialProvider String @map("social_provider")
socialUid String @unique @map("social_uid")
nickname String
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
UserWorkspace UserWorkspace[]
id String @id @default(auto()) @map("_id") @db.ObjectId
socialProvider String @map("social_provider")
socialUid String @unique @map("social_uid")
nickname String
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
userWorkspaceList UserWorkspace[]
@@map("users")
}
Expand Down
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 backend/src/workspace-users/types/workspace-user.domain.ts
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;
}
3 changes: 2 additions & 1 deletion backend/src/workspace-users/workspace-users.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +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],
providers: [WorkspaceUsersService, PrismaService],
})
export class WorkspaceUsersModule {}
55 changes: 53 additions & 2 deletions backend/src/workspace-users/workspace-users.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,55 @@
import { Injectable } from "@nestjs/common";
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 {}
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,
where: {
userWorkspaceList: {
some: {
workspaceId: {
equals: workspaceId,
},
},
},
},
orderBy: {
id: "desc",
},
...additionalOptions,
});

return {
workspaceUsers: workspaceUserList.slice(0, pageSize),
cursor: workspaceUserList.length > pageSize ? workspaceUserList[pageSize].id : null,
};
}
}

0 comments on commit aec14b0

Please sign in to comment.