From 4ed11fd65526762ca8d156cfb72ad64d49d70ef4 Mon Sep 17 00:00:00 2001 From: Shiwon Park Date: Sat, 28 Dec 2024 01:52:03 +0900 Subject: [PATCH] =?UTF-8?q?[Feature]=20client=EA=B0=80=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D=ED=95=9C=20authorites=EB=A5=BC=20=ED=99=95=EC=9D=B8?= =?UTF-8?q?=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8A=94=20api=20=EB=A7=8C?= =?UTF-8?q?=EB=93=A4=EA=B8=B0=20Fixes=20#54?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/client.controller.ts | 18 ++++++++++++++++++ src/client/client.repository.ts | 13 +++++++++++++ src/client/client.service.ts | 16 ++++++++++++++++ src/client/dto/res/expandedClientRes.dto.ts | 16 ++++++++++++++++ src/client/types/ExpandedClient.type.ts | 7 +++++++ 5 files changed, 70 insertions(+) create mode 100644 src/client/dto/res/expandedClientRes.dto.ts create mode 100644 src/client/types/ExpandedClient.type.ts diff --git a/src/client/client.controller.ts b/src/client/client.controller.ts index 33611c4..8c56dce 100644 --- a/src/client/client.controller.ts +++ b/src/client/client.controller.ts @@ -2,6 +2,7 @@ import { Body, Controller, Delete, + Get, Param, Post, UseGuards, @@ -27,6 +28,7 @@ import { ClientGuard } from './guard/client.guard'; import { GetClient } from './decorator/getClient.decorator'; import { Client } from '@prisma/client'; import { AuthorityDto } from './dto/req/authority.dto'; +import { ExpandedClientResDto } from './dto/res/expandedClientRes.dto'; @ApiTags('client') @Controller('client') @@ -34,6 +36,22 @@ import { AuthorityDto } from './dto/req/authority.dto'; export class ClientController { constructor(private readonly clientService: ClientService) {} + @ApiOperation({ + summary: 'Get clients', + description: 'Get clients with name and uuid', + }) + @ApiOkResponse({ + description: 'The clients have been successfully fetched.', + type: [ExpandedClientResDto], + }) + @ApiInternalServerErrorResponse({ + description: 'Unknown errors', + }) + @Get(':uuid') + async getClient(@Param('uuid') uuid: string): Promise { + return this.clientService.getClient(uuid); + } + @ApiOperation({ summary: 'Register a new client', description: diff --git a/src/client/client.repository.ts b/src/client/client.repository.ts index d940b71..7909b2d 100644 --- a/src/client/client.repository.ts +++ b/src/client/client.repository.ts @@ -9,6 +9,7 @@ import { } from '@nestjs/common'; import { Client } from '@prisma/client'; import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'; +import { ExpandedClient } from './types/ExpandedClient.type'; @Injectable() @Loggable() @@ -54,6 +55,18 @@ export class ClientRepository { }); } + /** + * Find a client by uuid with authorities. if the client is not found, return null + * @param uuid the uuid of the client to find + * @returns Founded client or null + */ + async findByUuidWithAuthority(uuid: string): Promise { + return this.prismaService.client.findUnique({ + where: { uuid }, + include: { ExternalAuthority: true }, + }); + } + /** * Delete a client by uuid * @param uuid the uuid of the client to delete diff --git a/src/client/client.service.ts b/src/client/client.service.ts index bdc6526..663632b 100644 --- a/src/client/client.service.ts +++ b/src/client/client.service.ts @@ -8,6 +8,7 @@ import { ConfigService } from '@nestjs/config'; import { firstValueFrom } from 'rxjs'; import { HttpService } from '@nestjs/axios'; import { Loggable } from '@lib/logger/decorator/loggable'; +import { ExpandedClient } from './types/ExpandedClient.type'; @Injectable() @Loggable() @@ -23,6 +24,21 @@ export class ClientService { this.configService.getOrThrow('SLACK_WEBHOOK_URL'); } + /** + * get client + * @param uuid uuid of the client to get + * @returns client + */ + async getClient(uuid: string): Promise { + const client = await this.clientRepository.findByUuidWithAuthority(uuid); + if (!client) { + this.logger.debug(`client not found`); + throw new ForbiddenException('client not found'); + } + + return client; + } + /** * generate a password and create a new client and grant the request through slack * @param param0 dto object containing the client name diff --git a/src/client/dto/res/expandedClientRes.dto.ts b/src/client/dto/res/expandedClientRes.dto.ts new file mode 100644 index 0000000..c037304 --- /dev/null +++ b/src/client/dto/res/expandedClientRes.dto.ts @@ -0,0 +1,16 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { ClientResDto } from './clientRes.dto'; +import { ExternalAuthority } from '@prisma/client'; + +class AuthorityResDto implements ExternalAuthority { + @ApiProperty() + clientUuid: string; + + @ApiProperty() + authority: string; +} + +export class ExpandedClientResDto extends ClientResDto { + @ApiProperty() + ExternalAuthority: AuthorityResDto[]; +} diff --git a/src/client/types/ExpandedClient.type.ts b/src/client/types/ExpandedClient.type.ts new file mode 100644 index 0000000..2c00eaa --- /dev/null +++ b/src/client/types/ExpandedClient.type.ts @@ -0,0 +1,7 @@ +import { Prisma } from '@prisma/client'; + +export type ExpandedClient = Prisma.ClientGetPayload<{ + include: { + ExternalAuthority: true; + }; +}>;